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.
git clone https://github.com/EricSmekens/jsep.git cd jsep npm install npm run buildThe output will be in the
dist/ directory.
The simplest way to use jsep is to import it in your JS code:
import jsep from "PATH/TO/dist/jsep.min.js";
// Use jsep:
jsep("1 + 1");
Please note that for this to work, your the <script>
element with your code needs to have type="module"
Note that you don't need to name the import jsep, you can name it anything you want.
E.g. this works just as well:
import parse from "PATH/TO/dist/jsep.min.js";
// Use jsep:
parse("1 + 1");
If you want to use jsep as a global (legacy style):
<script src="PATH/TO/dist/jsep.iife.min.js"></script>
// After running npm install jsep
let jsep = require("jsep");
let parse_tree = jsep("1 + 1");
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
jsep.addUnaryOp('@');
// Remove a binary operator
jsep.removeBinaryOp(">>>");
// Remove a unary operator
jsep.removeUnaryOp("~");