-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
282 lines (245 loc) · 8.02 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ViewPatterns #-}
module Main where
import Control.Applicative
import Data.Char
import Numeric
import System.Exit
data Input = Input
{ inputLoc :: Int
, inputStr :: String
} deriving (Show, Eq)
-- | Pull the first character of the input if there is one
inputUncons :: Input -> Maybe (Char, Input)
inputUncons (Input _ []) = Nothing
inputUncons (Input loc (x:xs)) = Just (x, Input (loc + 1) xs)
data TomlValue
= TomlString String
| TomlInt Integer
| TomlFloat Double
| TomlBool Bool
| TomlArray [TomlValue]
| TomlTable [(String, TomlValue)]
deriving (Show, Eq)
data ParserError = ParserError Int String deriving (Show)
newtype Parser a = Parser
{ runParser :: Input -> Either ParserError (Input, a)
}
instance Functor Parser where
fmap f (Parser p) =
Parser $ \input -> do
(input', x) <- p input
return (input', f x)
instance Applicative Parser where
pure x = Parser $ \input -> Right (input, x)
(Parser p1) <*> (Parser p2) =
Parser $ \input -> do
(input', f) <- p1 input
(input'', a) <- p2 input'
return (input'', f a)
instance Alternative (Either ParserError) where
empty = Left $ ParserError 0 "empty"
Left _ <|> e2 = e2
e1 <|> _ = e1
instance Alternative Parser where
empty = Parser $ const empty
(Parser p1) <|> (Parser p2) =
Parser $ \input -> p1 input <|> p2 input
charP :: Char -> Parser Char
charP x = Parser f
where
f input@(inputUncons -> Just (y, ys))
| y == x = Right (ys, x)
| otherwise =
Left $
ParserError
(inputLoc input)
("Expected '" ++ [x] ++ "', but found '" ++ [y] ++ "'")
f input =
Left $
ParserError
(inputLoc input)
("Expected '" ++ [x] ++ "', but reached end of string")
stringP :: String -> Parser String
stringP str =
Parser $ \input ->
case runParser (traverse charP str) input of
Left _ ->
Left $
ParserError
(inputLoc input)
("Expected \"" ++ str ++ "\", but found \"" ++ inputStr input ++ "\"")
result -> result
parseIf :: String -> (Char -> Bool) -> Parser Char
parseIf desc f =
Parser $ \input ->
case input of
(inputUncons -> Just (y, ys))
| f y -> Right (ys, y)
| otherwise ->
Left $
ParserError
(inputLoc input)
("Expected " ++ desc ++ ", but found '" ++ [y] ++ "'")
_ ->
Left $
ParserError
(inputLoc input)
("Expected " ++ desc ++ ", but reached end of string")
spanP :: String -> (Char -> Bool) -> Parser String
spanP desc = many . parseIf desc
ws :: Parser String
ws = spanP "whitespace" isSpace
comment :: Parser ()
comment = do
charP '#'
_ <- spanP "any character" (/= '\n')
return ()
-- | Consume whitespace and comments
wsAndComments :: Parser ()
wsAndComments = do
_ <- many (ws *> optional comment)
_ <- ws
return ()
-- | Basic TOML values
tomlBool :: Parser TomlValue
tomlBool = (TomlBool True <$ stringP "true") <|> (TomlBool False <$ stringP "false")
tomlString :: Parser TomlValue
tomlString = TomlString <$> (charP '"' *> many normalChar <* charP '"')
where
normalChar = parseIf "non-quote char" (/= '"')
digits :: Parser String
digits = some (parseIf "digit" isDigit)
tomlNumber :: Parser TomlValue
tomlNumber = tryFloat <|> tryInt
where
tryFloat = do
-- Float: integral part, dot, fractional part, optional exponent
integral <- digits
charP '.'
frac <- digits
expo <- optional exponentPart
let val = read (integral ++ "." ++ frac ++ maybe "" id expo) :: Double
return (TomlFloat val)
tryInt = do
numStr <- digits
return (TomlInt (read numStr))
exponentPart = do
e <- charP 'e' <|> charP 'E'
sign <- optional (charP '+' <|> charP '-')
expoDigits <- digits
return (e : maybe "" (:[]) sign ++ expoDigits)
-- | Arrays: key = [ value, value, ... ]
tomlArray :: Parser TomlValue
tomlArray = TomlArray <$> (charP '[' *> wsAndComments *> sepBy (charP ',' *> wsAndComments) tomlValue <* wsAndComments <* charP ']')
-- | Tables: [table], [table.subtable]
-- We'll store tables as TomlTable. For simplicity, parse them into a flat structure.
tableHeader :: Parser [String]
tableHeader = charP '[' *> wsAndComments *> sepBy (charP '.' *> wsAndComments) keyName <* wsAndComments <* charP ']'
keyName :: Parser String
keyName = bareKey <|> quotedKey
where
bareKey = some (parseIf "bare key char" (\c -> isAlphaNum c || c == '_' || c == '-'))
quotedKey = charP '"' *> many (parseIf "non-quote char" (/= '"')) <* charP '"'
-- | Key-Value pair: key = value
keyValuePair :: Parser (String, TomlValue)
keyValuePair = do
k <- keyName
wsAndComments
charP '='
wsAndComments
v <- tomlValue
return (k, v)
-- | Parse a list of key-value pairs for a table
tableBody :: Parser [(String, TomlValue)]
tableBody = many (wsAndComments *> keyValuePair <* wsAndComments)
-- | A top-level TOML file has zero or more key-values and tables
tomlFile :: Parser TomlValue
tomlFile = do
globalKVs <- tableBody
tables <- many parseTable
return (TomlTable (globalKVs ++ concat tables))
where
parseTable = do
wsAndComments
header <- tableHeader
kvs <- tableBody
-- For simplicity, just store them directly. A full TOML parser would nest these.
let prefix = concatMap (++ ".") (init header) ++ last header
return (map (\(k,v) -> (prefix ++ "." ++ k, v)) kvs)
-- | Try parsing a given parser, but if it fails, restore input (used in number parsing)
tryP :: Parser a -> Parser a
tryP (Parser p) = Parser $ \input ->
case p input of
Left _ -> Left (ParserError (inputLoc input) "tryP failed")
Right r -> Right r
tryFloat :: Parser TomlValue
tryFloat = tryP (do
integral <- digits
charP '.'
frac <- digits
expo <- optional exponentPart
return (TomlFloat (read (integral ++ "." ++ frac ++ maybe "" id expo))))
exponentPart :: Parser String
exponentPart = do
e <- charP 'e' <|> charP 'E'
sign <- optional (charP '+' <|> charP '-')
expoDigits <- digits
return (e : maybe "" (:[]) sign ++ expoDigits)
-- | Combine all value parsers
tomlValue :: Parser TomlValue
tomlValue =
wsAndComments *>
(tomlBool <|> tomlString <|> tomlArray <|> tomlNumber)
<* wsAndComments
-- | sepBy combinator
sepBy :: Parser a -> Parser b -> Parser [b]
sepBy sep element = (:) <$> element <*> many (sep *> element) <|> pure []
-- | Main function: parse a sample TOML input
main :: IO ()
main = do
putStrLn "[INFO] TOML:"
putStrLn testTomlText
case runParser tomlFile $ Input 0 testTomlText of
Right (input, actualTomlAst) -> do
putStrLn ("[INFO] Parsed as: " ++ show actualTomlAst)
putStrLn ("[INFO] Remaining input (codes): " ++ show (map ord $ inputStr input))
if actualTomlAst == expectedTomlAst
then putStrLn "[SUCCESS] Parser produced expected result."
else do
putStrLn
("[ERROR] Parser produced unexpected result. Expected result was: " ++
show expectedTomlAst)
exitFailure
Left (ParserError loc msg) -> do
putStrLn $
"[ERROR] Parser failed at character " ++ show loc ++ ": " ++ msg
exitFailure
where
testTomlText =
unlines
[ "# A TOML file"
, "title = \"TOML Example\""
, "number = 42"
, "float_val = 3.1415"
, "array = [1, 2, 3, true, \"hello\"]"
, ""
, "[owner]"
, "name = \"Alice\""
, "age = 30"
, ""
, "[database]"
, "type = \"sql\""
, "enabled = true"
]
expectedTomlAst =
TomlTable
[ ("title", TomlString "TOML Example")
, ("number", TomlInt 42)
, ("float_val", TomlFloat 3.1415)
, ("array", TomlArray [TomlInt 1, TomlInt 2, TomlInt 3, TomlBool True, TomlString "hello"])
, ("owner.name", TomlString "Alice")
, ("owner.age", TomlInt 30)
, ("database.type", TomlString "sql")
, ("database.enabled", TomlBool True)
]