Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multi-config JSEP (mostly backward compatible) #272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,47 @@ const { Jsep } = require('jsep');
const parse_tree = Jsep.parse('1 + 1');
```

### Configuration

By default, JSEP is configured for basic javascript expression parsing:
- binary operators (
`||`, `??`, `&&`, `|`, `^`, `&`,
`==`, `!=`, `===`, `!==`, `<`, `>`, `<=`, `>=`,
`<<`, `>>`, `>>>`, `+`, `-`, `*`, `/`, `%`, `**`)
- unary operators (`-`, `!`, `~`, `+`)
- identifier chars (`&`, `_`)
- literals (`true`, `false`, `null`)
- (NOT `undefined`, `Infinity` or `NaN`)
- `this`
- and the ternary plugin (`condition ? true : false`)

The configuration can be modified by calling any of the `addBinaryOp`, `addUnaryOp`, `addLiteral`, or `addIdentifierChar`
(or their related `remove` and `removeAll` methods).

In addition, the JSEP configuration can be completely cleared by calling `clearConfig`,
and reset back to defaults by calling `defaultConfig`.

By default, jsep exports an instance of the Jsep class with its default configuration.
In order to have a separate instance for another configuration, you can use the `instance` method
to obtain a new instance, then configure that instance as needed.

```javascript
import jsep from 'jsep'; // function & Jsep methods

const jsep2 = jsep.instance(); // empty config

jsep('1 + 1') // BinaryExpression

jsep2('1 + 1') // Error - unexpected + (no binary operators)
jsep2.defaultConfig();
jsep2.parse('1 + 1') // BinaryExpression

jsep2.addBinaryOp('@', 10);
jsep2.parse('1 @ 1'); // BinaryExpression
jsep.parse('1 @ 1'); // Error - unexpected @
```


#### Custom Operators

```javascript
Expand Down Expand Up @@ -182,6 +223,9 @@ export interface HookScope {
readonly expr: string;
readonly char: string; // current character of the expression
readonly code: number; // current character code of the expression
isDecimalDigit: (ch: number) => boolean;
isIdentifierStart: (ch: number) => boolean;
isIdentifierPart: (ch: number) => boolean;
gobbleSpaces: () => void;
gobbleExpressions: (untilICode?: number) => Expression[];
gobbleExpression: () => Expression;
Expand Down
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default [
versionPlugin,
replace({
'export class Jsep': 'class Jsep', // single default export
'export const Jsep = jsep': '// export const Jsep = jsep', // single default export
preventAssignment: false,
}),
],
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import jsep from './jsep.js';
import ternary from '../packages/ternary/src/index.js';

jsep.plugins.register(ternary);
// NOTE: jsep has historically included ternary support, which was later moved into a plugin
// to include ternary in the "defaultConfig()" method, it needs to be specially added to Jsep
jsep.registerTernary(ternary);

export * from './jsep.js';
export const Jsep = jsep;
export default jsep;
Loading