JavaScript Expression Parser
JavaScript CSS HTML
Clone or download
duncanwerner and LeaVerou Throw on missing function arguments (#94), resolves #93
In #93 I noted that JSEP ignored missing function arguments. This code will throw in that case (see test for examples).

What I did not realize at the time was that arrays with missing elements were also broken, in the same code path. This PR fixes that as well, so 

`[1,,2]` returns `[1, null, 2]` (`null` is what esprima inserts in this case; I would prefer `undefined`, but I'm trying to keep it consistent). 

Finally I noticed that esprima is actually incorrect in the case of a trailing, missing array entry, so 
`[1,,]` in esprima returns `[1,null]` (a single trailing null) instead of the expected `[1,null,null]`. This could be fixed but I opted here to just replicate the esprima behavior. (Also FWIW those two arrays are functionally equivalent).
Latest commit 57bee62 Jul 3, 2018

README.md

jsep: A Tiny JavaScript Expression Parser

jsep is a simple expression parser written in JavaScript. It can parse JavaScript expressions but not operations. The difference between expressions and operations is akin to the difference between a cell in an Excel spreadsheet vs. a proper JavaScript program.

Why jsep?

I wanted a lightweight, tiny parser to be included in one of my other libraries. esprima and other parsers are great, but had more power than I need and were way too large to be included in a library that I wanted to keep relatively small.

jsep's output is almost identical to esprima's, which is in turn based on SpiderMonkey's.

Custom Build

First, install Grunt. While in the jsep project directory, run:

npm install .
grunt

The jsep built files will be in the build/ directory.

Usage

Client-side

<script src="/PATH/TO/jsep.min.js" type="text/javascript"></script>
...
var parse_tree = jsep("1 + 1");

Node.JS

First, run npm install jsep. Then, in your source file:

var jsep = require("jsep");
var parse_tree = jsep("1 + 1");

Custom Operators

// Add a custom ^ binary operator with precedence 10
jsep.addBinaryOp("^", 10);

// Add a custom @ unary operator
jsep.addUnaryOp('@');

// Remove a binary operator
jsep.removeBinaryOp(">>>");

// Remove a unary operator
jsep.removeUnaryOp("~");

License

jsep is under the MIT license. See LICENSE file.

Thanks

Some parts of the latest version of jsep were adapted from the esprima parser.