Pegjs: How can I create a PEG.js pattern for my DB query (X “AND” Y “AND” …)

Created on 27 Nov 2018  ·  3Comments  ·  Source: pegjs/pegjs

Issue type

QUESTION

Prerequisites

  • Can you reproduce the issue?: YES
  • Did you search the repository issues?: YES
  • Did you check the forums?: YES
  • Did you perform a web search (google, yahoo, etc)?: YES

Description / Example code

So far my parser looks like this.

const parser = peg.generate(`

eq
  = left:attribute "=" right:value { return left == right; }

and
  = left:eq "AND" right:eq { return left && right; }

It is able to read queries like id = 2 AND createdOn = 193242. I want to be able to read id = 2 AND secondId = 444 AND createdOn = 193242 and so on... (Any number of "AND"s). How can I achieve this via PEG.js?

question

All 3 comments

This should do it:

and
  = left:eq right:and_eq+ {
      if ( right.length === 1 ) return left && right;
      // handle more then 1 'AND eq' here
    }

and_eq
  = "AND" e:eq { return e; }

Generic solution for any number of operators with any precedence (creates AST):

{
function leftAssotiative(left, tail) {
  let r = left;
  for (let i = 0; i < tail.length; ++i) {
    r = { kind: tail[i][0], left: r, right: tail[i][1] };
  }
  return r;
}
function rightAssotiative(left, tail) {
  if (tail.length > 1) {
    let r = tail[tail.length-1][1];
    for (let i = tail.length-2; i >= 0; --i) {
      r = { kind: tail[i+1][0], left: tail[i][1], right: r };
    }
    return { kind: tail[0][0], left: left, right: r };
  }
  return left;
}
}

// Expressions
Expr// minimal priority
  = l:Expr0 t:(p0 Expr0)* { return rightAssotiative(l, t); };
Expr0
  = l:Expr1 t:(p1 Expr1)* { return leftAssotiative(l, t); };
...
ExprN// maximal priority
  = '(' @Expr ')';

// Operators
p0 = [+-];
p1 = [*/];
...

@futagoza sexy brain. thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dmajda picture dmajda  ·  7Comments

dmajda picture dmajda  ·  20Comments

Coffee2CodeNL picture Coffee2CodeNL  ·  13Comments

audinue picture audinue  ·  13Comments

doersino picture doersino  ·  15Comments