lab09: init

This commit is contained in:
2026-03-08 21:48:10 -07:00
parent aff81eb6da
commit 9662155716
7 changed files with 183 additions and 0 deletions

11
lab09/complex.json Normal file
View File

@@ -0,0 +1,11 @@
{
name: "Complex number list example",
nums: [
{ real: 42, imaginary: 1 },
{ real: 30, imaginary: 0 },
{ real: 15, imaginary: 7 }
],
knownIssues: null,
verified: false
}

46
lab09/csvParser.hs Normal file
View File

@@ -0,0 +1,46 @@
import Text.ParserCombinators.Parsec
import System.Environment
-- Each parser takes in a sequence of Chars and returns either
-- * an array of Strings ([String])
-- * an array of arrays of Strings ([String])
csvFile :: GenParser Char st [[String]]
-- A csv file contains 0 or more lines and is terminated by the end of the file
csvFile = do
result <- many line -- The result is an array of an array of Strings, i.e. [[String]]
eof -- match end of file
return result
line :: GenParser Char st [String]
-- A line is a comma separated list of values, ending with a newline character
line = do
result <- cells
char '\n' -- end of line, only works in Linux or OSX formatted files
return result
cells :: GenParser Char st [String]
cells = do
first <- cellContent
next <- remainingCells
return (first : next)
remainingCells :: GenParser Char st [String]
remainingCells = do
(char ',' >> cells) -- found a comma, so more cells
<|> (return []) -- No comma, so we wrap up an empty array
cellContent :: GenParser Char st String
cellContent =
many (noneOf ",\n") -- Matches any character that is not a comma or newline
parseCSV :: String -> Either ParseError [[String]]
parseCSV input = parse csvFile "(unknown)" input
main = do
args <- getArgs
p <- parseFromFile csvFile (head args)
case p of
Left err -> print err
Right csv -> print csv

View File

@@ -0,0 +1,40 @@
import Text.ParserCombinators.Parsec
import System.Environment
-- Each parser takes in a sequence of Chars and returns either
-- * an array of Strings ([String])
-- * an array of arrays of Strings ([String])
csvFile :: GenParser Char st [[String]]
csvFile = line `endBy` eol -- a series of lines ended by the end of the file
line :: GenParser Char st [String]
line = cell `sepBy` (char ',') -- a series of cells separated by commas
cell :: GenParser Char st String
cell = many (noneOf ",\n")
eol :: GenParser Char st String
{-
eol = char '\n' -- does not work on Windows
eol = string "\n" <|> string "\n\r" -- always matches first pattern
eol = string "\n\r" <|> string "\n" -- fails on non-windows, since the left hand tries and consumes the \n
eol = do -- works, but ugly, and changes the types
char '\n'
char '\r' <|> return '\n'
-}
eol = try (string "\n\r")
<|> string "\n"
<?> "end of line"
parseCSV :: String -> Either ParseError [[String]]
parseCSV input = parse csvFile "(unknown)" input
main = do
args <- getArgs
p <- parseFromFile csvFile (head args)
case p of
Left err -> print err
Right csv -> print csv

76
lab09/jsonParser.hs Normal file
View File

@@ -0,0 +1,76 @@
import Text.ParserCombinators.Parsec
import System.Environment
data JValue = JString String
| JNumber Double
| JBool Bool
| JNull
| JObject [(String, JValue)]
| JArray [JValue]
deriving (Eq, Ord, Show)
jsonFile :: GenParser Char st JValue
jsonFile = do
result <- jsonArr
spaces
eof
return result
jsonElem :: GenParser Char st JValue
jsonElem = do
spaces
result <- jsonElem'
spaces
return result
jsonElem' = jsonArr
<|> jsonString
<|> jsonBool
<|> jsonNull
<?> "json element"
jsonString :: GenParser Char st JValue
jsonString = jsonStringDQ <|> jsonStringSQ
jsonStringDQ = do
char '"'
s <- many $ noneOf "\"" -- crude. does not allow double quotes within strings
char '"'
return $ JString s
jsonStringSQ = do
char '\''
s <- many $ noneOf "'" -- crude, same as above
char '\''
return $ JString s
jsonBool = do
bStr <- string "true" <|> string "false"
return $ case bStr of
"true" -> JBool True
"false" -> JBool False
jsonNull = do
string "null"
return JNull
jsonArr = do
char '['
arr <- jsonElem `sepBy` (char ',')
char ']'
return $ JArray arr
parseJSON :: String -> Either ParseError JValue
parseJSON input = parse jsonFile "(unknown)" input
main = do
args <- getArgs
p <- parseFromFile jsonFile (head args)
case p of
Left err -> print err
Right json -> print json

5
lab09/numList.json Normal file
View File

@@ -0,0 +1,5 @@
[
42,
33,
71
]

1
lab09/strList.json Normal file
View File

@@ -0,0 +1 @@
['hi',"how",'are','u',null]

4
lab09/test.csv Normal file
View File

@@ -0,0 +1,4 @@
Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38
1 Year Make Model Length
2 1997 Ford E350 2.34
3 2000 Mercury Cougar 2.38