File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11module JsonParser (
22 JsonValue (.. ),
3+ json ,
34 jsonValue ,
45 jsonNull ,
56 jsonBool ,
@@ -22,6 +23,8 @@ data JsonValue = JsonNull
2223 | JsonObject [(String , JsonValue )]
2324 deriving (Show , Eq )
2425
26+ json = ws *> jsonValue <* ws
27+
2528jsonValue :: Parser JsonValue
2629jsonValue = jsonNull
2730 <|> jsonBool
@@ -45,8 +48,18 @@ jsonNumber = lstToInt <$> number
4548 where
4649 lstToInt nms = JsonNumber $ read $ map intToDigit nms
4750
51+ simpleChar_ :: Parser Char
52+ simpleChar_ = satisfy ((&&) <$> (/= ' "' ) <*> (/= ' \\ ' ))
53+
54+ escapedChar_ :: Parser Char
55+ escapedChar_ = (' "' <$ string " \\\" " )
56+ <|> (' \\ ' <$ string " \\\\ " )
57+ <|> (' \n ' <$ string " \\ n" )
58+ <|> (' \r ' <$ string " \\ r" )
59+ <|> (' \t ' <$ string " \\ t" )
60+
4861stringLiteral :: Parser [Char ]
49- stringLiteral = char ' "' *> ( many $ satisfy ( /= ' " ' ) ) <* char ' "'
62+ stringLiteral = char ' "' *> many (simpleChar_ <|> escapedChar_ ) <* char ' "'
5063
5164jsonString :: Parser JsonValue
5265jsonString = JsonString <$> stringLiteral
Original file line number Diff line number Diff line change @@ -4,6 +4,38 @@ module Lib (
44
55import Parser
66import JsonParser
7+ import System.IO
78
89mainLoop :: IO ()
9- mainLoop = putStrLn " mainLoop"
10+ mainLoop = do
11+ inp <- readStdin
12+ putStrLn inp
13+ case (runParser json inp) of
14+ (Right (ast, rest)) -> putStrLn $ show ast
15+ (Left err) -> putStrLn err
16+ hFlush stdout
17+
18+ myLoop = do
19+ done <- isEOF
20+ if done
21+ then do
22+ putStrLn " Bye!"
23+ hFlush stdout
24+ else do
25+ inp <- getLine
26+ putStrLn inp
27+ hFlush stdout
28+ myLoop
29+
30+ readStdin :: IO String
31+ readStdin = readStdin_ " "
32+
33+ readStdin_ :: String -> IO String
34+ readStdin_ acc = do
35+ done <- isEOF
36+ if done
37+ then do
38+ return acc
39+ else do
40+ inp <- getLine
41+ readStdin_ $ acc ++ " \n " ++ inp
Original file line number Diff line number Diff line change @@ -53,14 +53,13 @@ char :: Char -> Parser Char
5353char c = Parser $ \ inp ->
5454 case (runParser (char_ c) inp) of
5555 (Left err) -> Left (err ++ " , expected '" ++ [c] ++ " '" ++ context inp )
56- result -> result
57-
56+ result -> result
5857
5958digit :: Parser Int
6059digit = digitToInt <$> satisfy isDigit
6160
6261ws :: Parser String
63- ws = many (satisfy isSpace)
62+ ws = many (satisfy ( \ c -> isSpace c || c == ' \xfeff ' ) )
6463
6564string_ :: String -> Parser String
6665string_ str = sequenceA $ map char str
@@ -78,4 +77,4 @@ number :: Parser [Int]
7877number = some digit
7978
8079context :: String -> String
81- context inp = " in " ++ take 10 inp
80+ context inp = " : " ++ take 20 inp
You can’t perform that action at this time.
0 commit comments