fparsec icon indicating copy to clipboard operation
fparsec copied to clipboard

OperatorPrecedenceParser Reply Status is OK when parser with InfixOperator parses a single term

Open JCKortlang opened this issue 10 months ago • 1 comments

//Given
let sanityExpr, sanityExprImpl = createParserForwardedToRef()
let sanityParser = OperatorPrecedenceParser<String, unit, unit>()
sanityParser.AddOperator(InfixOperator("+", spaces, 100, Associativity.Left, fun s1 s2 -> s1 + s2))
let sanityTerm = (choice [
    attempt (pWordCI "hello");
    attempt (pWordCI "world")
])
sanityParser.TermParser <- sanityTerm
sanityExprImpl.Value <- sanityParser

let runParser p s =
    match run p s with
    | Failure(errorString, _, _) ->
        InvalidOperationException(errorString) |> raise
    | Success(result, _, _) -> result
[<Test>]
let ``Infix operator precedence parser should fail when operator and second term is absent`` () =
    //Expect parser to fail.
    //Operator = "+"
    //Term = ["hello";"world"] i.e. "hello+world" -> "helloworld"
    //Expect parser to throw
    let result = runParser sanityParser "hello"
    //Should not pass
    result |> should equal "hello"

Example reply

Reply(Ok, "hello", [Expected("infix operator")])

JCKortlang avatar Apr 01 '24 22:04 JCKortlang