I have written a parser using megaparsec for a very simple language consisting of integer literals and two unary operators "a" and "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)
However, it seems that this doesn't allow me to "chain" unary operators, i.e. when trying to parse "ab1", I'm met with "unexpected 'b'". Why is that?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…