lab09: impl parsing
This commit is contained in:
@@ -12,7 +12,7 @@ data JValue = JString String
|
||||
|
||||
jsonFile :: GenParser Char st JValue
|
||||
jsonFile = do
|
||||
result <- jsonArr
|
||||
result <- jsonArr <|> jsonObj
|
||||
spaces
|
||||
eof
|
||||
return result
|
||||
@@ -28,6 +28,8 @@ jsonElem' = jsonArr
|
||||
<|> jsonString
|
||||
<|> jsonBool
|
||||
<|> jsonNull
|
||||
<|> jsonInt
|
||||
<|> jsonObj
|
||||
<?> "json element"
|
||||
|
||||
jsonString :: GenParser Char st JValue
|
||||
@@ -61,7 +63,22 @@ jsonArr = do
|
||||
char ']'
|
||||
return $ JArray arr
|
||||
|
||||
jsonInt = do
|
||||
i <- many1 digit
|
||||
return $ JNumber (read i :: Double)
|
||||
|
||||
jsonObj = do
|
||||
char '{'
|
||||
members <- jsonMember `sepBy` (char ',')
|
||||
char '}'
|
||||
return $ JObject members
|
||||
|
||||
jsonMember = do
|
||||
spaces
|
||||
key <- many $ noneOf ":"
|
||||
char ':'
|
||||
value <- jsonElem
|
||||
return (key, value)
|
||||
|
||||
parseJSON :: String -> Either ParseError JValue
|
||||
parseJSON input = parse jsonFile "(unknown)" input
|
||||
|
||||
Reference in New Issue
Block a user