简体   繁体   中英

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

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?

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 = [*/];
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM