Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating errors in FParsec's OperatorPrecedenceParser

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?

like image 435
Francesco Bertolaccini Avatar asked May 29 '26 08:05

Francesco Bertolaccini


1 Answers

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.

like image 149
Stephan Tolksdorf Avatar answered May 31 '26 15:05

Stephan Tolksdorf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!