a tiny JavaScript expression parser

Download v0.3.5 1kb minified & gzipped

jsep is a JavaScript parser for JavaScript expressions. jsep is able to parse expressions like x * (1 + 2) or foo(bar.baz[0]) and convert them into an AST. jsep works in both server-side and client-side JavaScript.

jsep does not parse JavaScript operations (e.g. var a = b;) nor blocks of code (e.g. if (...) {...}.) Think of jsep as a tool to parse the kinds of expressions that would be written in individual Excel cells.

jsep was originally written as part of ConstraintJS and was extracted as a useful module for cases where full parsers like Esprima are too heavyweight.

The easiest way to use jsep is to use the download link above. To build the latest version from code, you'll first need node.js, npm, git, and grunt. Then, run the following command in your terminal:
git clone git@github.com:soney/jsep.git;
cd jsep;
npm install .;
The output will be in the build/ directory.

Including jsep:

<script src="PATH/TO/jsep.min.js"></script>
// After running npm install jsep
var jsep = require("jsep");


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

Ninja Mode:

var ninja_jsep = jsep.noConflict();
// The previous value of window.jsep restored

Custom Operators:

jsep also allows user-defined operators to be created.

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

// Add a custom @ unary operator with precedence 10

// Remove a binary operator

// Remove a unary operator