What is jsep?
JSEP (JavaScript Expression Parser) is a lightweight JavaScript library that parses JavaScript expressions into an abstract syntax tree (AST). It is useful for evaluating expressions, building interpreters, or creating domain-specific languages.
What are jsep's main functionalities?
Parsing Expressions
JSEP can parse a string expression into an abstract syntax tree (AST). This is useful for analyzing or transforming expressions.
const jsep = require('jsep');
const ast = jsep('a + b * (c - d)');
console.log(JSON.stringify(ast, null, 2));
Custom Operators
JSEP allows you to add custom operators to the parser. This is useful for extending the language to support new operations.
const jsep = require('jsep');
jsep.addBinaryOp('**', 10);
const ast = jsep('a ** b');
console.log(JSON.stringify(ast, null, 2));
Custom Functions
JSEP allows you to add custom functions to the parser. This is useful for extending the language to support new functions.
const jsep = require('jsep');
jsep.addUnaryOp('sqrt');
const ast = jsep('sqrt(a)');
console.log(JSON.stringify(ast, null, 2));
Other packages similar to jsep
mathjs
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser that can evaluate mathematical expressions. Compared to JSEP, Math.js offers a broader range of mathematical functions and utilities, but it is also larger in size.
expr-eval
Expr-eval is a small, fast JavaScript expression parser and evaluator. It supports basic arithmetic, logical operations, and custom functions. Compared to JSEP, expr-eval includes built-in evaluation capabilities, making it more suitable for direct expression evaluation.
jison
Jison is a parser generator that converts a grammar specification into a JavaScript parser. It is more powerful and flexible than JSEP, allowing for the creation of complex parsers for custom languages. However, it requires more setup and configuration.
##jsep: A 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
...
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");
###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.