The character |
may be in a set of reserved characters. Test with other characters, like ^
, and I assume it will fail just as well. The only way around this would probably be to change the set of reserved characters, or the structure of your interpreter.
Haskell Parser Fails on "|" Read
-
03-06-2022 - |
سؤال
I am working on a parser in Haskell using Parsec. The issue lies in reading in the string "| "
. When I attempt to read in the following,
parseExpr = parseAtom
-- | ...
<|> do string "{|"
args <- try parseList <|> parseDottedList
string "| "
body <- try parseExpr
string " }"
return $ List [Atom "lambda", args, body]
I get a parse error, the following.
Lampas >> {|a b| "a" }
Parse error at "lisp" (line 1, column 12):
unexpected "}"
expecting letter, "\"", digit, "'", "(", "[", "{|" or "."
Another failing case is ^
which bears the following.
Lampas >> {|a b^ "a" }
Parse error at "lisp" (line 1, column 12):
unexpected "}"
expecting letter, "\"", digit, "'", "(", "[", "{|" or "."
However, it works as expected when the string "| "
is replaced with "} "
.
parseExpr = parseAtom
-- | ...
<|> do string "{|"
args <- try parseList <|> parseDottedList
string "} "
body <- try parseExpr
string " }"
return $ List [Atom "lambda", args, body]
The following is the REPL behavior with the above modification.
Lampas >> {|a b} "a" }
(lambda ("a" "b") ...)
So the question is (a) does pipe have a special behavior in Haskell strings, perhaps only in <|>
chains?, and (b) how is this behavior averted?.
المحلول
لا تنتمي إلى StackOverflow