Skip to content

How to enforce use of parentheses? (no operator precedence) #1784

Answered by NaridaL
hasanayan asked this question in Q&A
Discussion options

You must be logged in to vote

Something like this would probably work:

public query = this.RULE("query", () => {

  this.SUBRULE2(this.expression, { LABEL: "lhs" })

  // save the first operator so we can check that later operators match it.
  let operator = undefined
  this.MANY1({
    DEF: () => {
      operator = this.OR([
        {
          GATE: () => operator === undefined || operator === AND,
          ALT: () => this.CONSUME(TOKEN.AND)
        },
        {
          GATE: () => operator === undefined || operator === OR,
          ALT: () => this.CONSUME(TOKEN.OR)
        }
      ])
      this.SUBRULE3(this.expression, { LABEL: "rhs" })
    },
  })

You can also probably solve it without the OR having a branch …

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@hasanayan
Comment options

Answer selected by hasanayan
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants