Вопрос

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?.

Это было полезно?

Решение

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 с атрибуция
Не связан с StackOverflow
scroll top