Skip to content

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

@JCKortlang

Description

@JCKortlang
//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")])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions