0

I'm writing a grammar parser for my config evaluation library using pest parser. The grammar supports and, or, matches, == operators to create complex queries. For e.g.

Input: "foo: when bar == 'apple' and baz matches 'foobaz'"

Precedence of "==", "matches" is greater han "and"

I want to create an AST representation of the input so that later I can evaluate the expression based on values of 'bar' and 'baz' variables

{
        "foo" : {
            {
                "and": [
                {
                    "ident": "bar",
                    "op": "==",
                    "value": "apple"
                },
                {
                    "ident": "baz",
                    "op": "matches",
                    "value": "foobaz"
                }],
            }
        }
}

I'm having trouble using specifying the precedence relations. How should I specify operator precedence in pest. Can someone provide me an example. Thanks.

Here is my pest file that doesn't work if I use "and", "or" operators

input = _{ monadicExpr }

monadicExpr = { ident ~ startingverb ~ expr }

expr = {
    | terms
    | dyadicExpr
    | ident
}

terms = { dyadicExpr ~ ((logicalAnd | logicalOr) ~ dyadicExpr)* }

dyadicExpr = { ident ~ verb ~ value }

value = _{ string | array }

startingverb = _{
 ":" ~ WHITESPACE* ~ "when"
}

verb = {
    "matches" | "==" | logicalAnd | logicalOr
}

logicalAnd = { "and" }
logicalOr = { "or" }

ident = @{ ASCII_ALPHA ~ (ASCII_ALPHANUMERIC | "_")* }
h1990
  • 125
  • 3
  • 11

0 Answers0