From: Samir Benmendil Date: Sun, 14 Jan 2018 00:03:01 +0000 (+0000) Subject: Parse Strings surrounded by `"` X-Git-Url: https://git.rmz.io/my-scheme.git/commitdiff_plain/10ec228b216032b4d61216414aef8b1e36cf151f?ds=inline Parse Strings surrounded by `"` We need to use a `do-block` because we would like to keep the return value of a parse in the middle. So we parse a single `char '"'`, parse `many (noneOf "\"")` and assign the return value to `x` and parse another `"`. We apply the `String` constructor to the value of `x` to turn it into a `LispVal` with `String x`. Finally we inject the `LispVal` into the `Parser` monad. We need to do this because each line of a `do-block` needs to be of the same type but the result of `String x` is a `LispVal`, not a `Parser`. `return` wraps the `LispVal` up in a `Parser` action that consumes no input. Consider `a $ b c` as syntactic sugar for `a (b c)`. --- diff --git a/app/Main.hs b/app/Main.hs index c64fd18..22de748 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -15,6 +15,13 @@ symbol = oneOf "!#$%&|*+-/:<=>?@^_~" spaces :: Parser () spaces = skipMany space +parseString :: Parser LispVal +parseString = do + char '"' + x <- many (noneOf "\"") + char '"' + return $ String x + readExpr :: String -> String readExpr input = case parse (spaces >> symbol) "lisp" input of Left err -> "No match: " ++ show err