Add parsers

This commit is contained in:
Chris Done 2019-11-25 15:39:37 +00:00
parent c8d85590c9
commit f6c19caf0c
2 changed files with 17 additions and 4 deletions

View File

@ -1,16 +1,29 @@
data Tuple a b = Tuple a b
data Result a = OK a String | Error String
data Parser a = Parser (String -> Result a)
parseBool =
Parser
(\string ->
case take 4 string of
"True" -> OK True (drop 4 string)
"True" ->
case drop 4 string of
!rest -> OK True rest
_ ->
case take 5 string of
"False" -> OK False (drop 5 string)
"False" ->
case drop 5 string of
!rest -> OK False rest
_ -> Error (append "Expected a bool, but got: " string))
runParser =
\p s ->
case p of
Parser f -> f s
main = runParser parseBool "TrueFalse"
bind =
\m f ->
Parser
(\s ->
case runParser m s of
OK a rest -> runParser (f a) rest
Error err -> Error err)
pure = \a -> Parser (OK a)
main = runParser (bind parseBool (\x -> bind parseBool (\y -> pure (Tuple x y)))) "TrueFalse"

View File

@ -101,7 +101,7 @@ displayRenamerException specialTypes =
IdentifierNotInVarScope scope name label ->
"Not in variable scope " ++
curlyQuotes (printit defaultPrint name) ++
" (AST tree label: "++show label ++")"++
-- " (AST tree label: "++show label ++")"++
"\n" ++
"Nearest names in scope:\n\n" ++
intercalate