用 megaparsec 链接一元运算符

Chaining unary operators with megaparsec

我使用 megaparsec 为一种非常简单的语言编写了一个解析器,该语言由整数文字和两个一元运算符“a”和“b”组成:

data ValueExpr = OpA ValueExpr  
               | OpB ValueExpr
               | Integer Integer

valueExpr :: Parser ValueExpr
valueExpr = makeExprParser valueTerm valueOperatorTable

valueTerm :: Parser ValueExpr
valueTerm = parenthesised valueExpr
          <|> Integer <$> integerLiteral

integerLiteral :: Parser Integer
integerLiteral = -- omitted

valueOperatorTable :: [[Operator Parser ValueExpr]]
valueOperatorTable = [[unaryOp "a" AOp,
                       unaryOp "b" BOp]]

parenthesised :: Parser a -> Parser a
parenthesised = between (char '(') (char ')')

unaryOp :: Text -> (a -> a) -> Operator Parser a
unaryOp name f = Prefix (f <$ symbol name)

binaryOp :: Text -> (a -> a -> a) -> Operator Parser a
binaryOp name f = InfixL (f <$ symbol name)

但是,这似乎不允许我“链接”一元运算符,即在尝试解析“ab1”时,我遇到了“意外 'b'”。这是为什么?

这在 documentation 中简要提到 makeExprParser:

Unary operators of the same precedence can only occur once (i.e., --2 is not allowed if - is prefix negate). If you need to parse several prefix or postfix operators in a row, ... you can use this approach:

manyUnaryOp = foldr1 (.) <$> some singleUnaryOp

This is not done by default because in some cases allowing repeating prefix or postfix operators is not desirable.

在您的具体示例中,类似以下内容应该有效:

valueOperatorTable :: [[Operator Parser ValueExpr]]
valueOperatorTable = [[Prefix unaryOps]]

unaryOps :: Parser (ValueExpr -> ValueExpr)
unaryOps = foldr1 (.) <$> some (OpA <$ symbol "a" <|> OpB <$ symbol "b")