Generating errors in FParsec's OperatorPrecedenceParser

113 Views Asked by At

I have the need to generate errors while parsing operators using FParsec's OperatorPrecedenceParsers, specifically during the mapping phase. Suppose I have the following code:

let pOperatorExpr : ExpressionParser =
    let opp = new OperatorPrecedenceParser<MyType, unit, unit>()
    let arithmeticOperator a b ->
        if someOperation a b then
            // Fatal error! Abort!
        else foobar a b

    opp.AddOperator(InfixOperator("+", spaces, 1, Associativity.Left, arithmeticOperator)
    opp.ExpressionParser

What should I do to generate an error in that particular position?

1

There are 1 best solutions below

3
On BEST ANSWER

There is no direct support for triggering an error in the mapping function of the operator.

In the "More uses of the after‐string‐parser" section of the OPP reference you can find an example for how to get hold of the precise text location of the binary operator. You could also have your term parser include the text position in its result value. Once you have the locations, you could construct an "error node" in your AST and then manually generate an error later.