diff --git a/lab09/complex.json b/lab09/complex.json new file mode 100644 index 0000000..72ffafe --- /dev/null +++ b/lab09/complex.json @@ -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 +} + diff --git a/lab09/csvParser.hs b/lab09/csvParser.hs new file mode 100644 index 0000000..4ab0ca4 --- /dev/null +++ b/lab09/csvParser.hs @@ -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 + diff --git a/lab09/csvParserImproved.hs b/lab09/csvParserImproved.hs new file mode 100644 index 0000000..1897152 --- /dev/null +++ b/lab09/csvParserImproved.hs @@ -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 + diff --git a/lab09/jsonParser.hs b/lab09/jsonParser.hs new file mode 100644 index 0000000..97e1a20 --- /dev/null +++ b/lab09/jsonParser.hs @@ -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 + + diff --git a/lab09/numList.json b/lab09/numList.json new file mode 100644 index 0000000..723731d --- /dev/null +++ b/lab09/numList.json @@ -0,0 +1,5 @@ +[ + 42, + 33, + 71 +] diff --git a/lab09/strList.json b/lab09/strList.json new file mode 100644 index 0000000..f736bcf --- /dev/null +++ b/lab09/strList.json @@ -0,0 +1 @@ +['hi',"how",'are','u',null] diff --git a/lab09/test.csv b/lab09/test.csv new file mode 100644 index 0000000..37a99fb --- /dev/null +++ b/lab09/test.csv @@ -0,0 +1,4 @@ +Year,Make,Model,Length +1997,Ford,E350,2.34 +2000,Mercury,Cougar,2.38 +