rd-parse-jsexpr

ES6 expression grammar for use with rd-parse parser generator

Usage no npm install needed!

<script type="module">
  import rdParseJsexpr from 'https://cdn.skypack.dev/rd-parse-jsexpr';
</script>

README

jsexpr

Javascript (ES2015) expression grammar, defined in Javascript, and parsed by Javascript ! Designed for rd-parse.

It is an "immutable" pure functional reduction of the ECMAScript grammar: loosely based on descriptions from here and here. Matches (almost) anything you can put on the right hand side of an assignment operator in ES2015.

The mutating constructs such as increment / decrement / assignment operators, as well as higher level language constructs (loops, classes etc) are excluded.

Usage:

    import Parser from 'rd-parse';
    import Grammar from 'rd-parse-jsexpr';

    const parser = Parser(Grammar);

    const ast = parser('[1,2,3].map(a => a*a).reduce((a,b) => a + b)');

    console.log(JSON.stringify(ast, null, 2));      // Pretty print your AST

I tried to keep the AST as close as possible to the format produced by another popular JS expression parser jsep.

This grammar is slightly richer than jsep though. Unlike the latter, I properly parse ES6 arrow functions, object literals, and support spread elements, and shorthand notation. The code is also readable ;)