简体   繁体   English

Haskell Parser未能通过“ |”读取

[英]Haskell Parser Fails on “|” Read

I am working on a parser in Haskell using Parsec. 我正在使用Parsec在Haskell中分析器。 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. 以下是具有上述修改的REPL行为。

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? 所以问题是(a) 管道在Haskell字符串中是否有特殊行为,也许仅在<|>链中? , and (b) how is this behavior averted? ,以及(b) 如何避免这种行为? .

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. 解决此问题的唯一方法可能是更改保留字符集或解释器的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM