Skip to content

Commit

Permalink
feat(parser): integrate jsep directly
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Apr 21, 2024
1 parent a1f36f9 commit 5147e14
Show file tree
Hide file tree
Showing 12 changed files with 1,563 additions and 467 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ Nimma consists of 3 major components. These are:
- codegen (iterator/feedback + baseline),
- runtime (scope + sandbox + traverse).

Parser takes a JSON Path expression and generates an AST that's consumed by the codegen in the next step.
Parser takes a JSON Path expression and generates an AST that's consumed by the codegen in the next step. Nimma has its own JSON Path expression parser that's different from all remaining ones, thus some there might be instances where Nimma will parse a given expression differently than JSONPath-plus or jsonpath.

Codegen is a two-step process:

Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
"import": "./src/parser/index.mjs",
"require": "./cjs/parser/index.cjs"
},
"./parser/jsep": {
"import": "./src/parser/jsep.mjs",
"require": "./cjs/parser/jsep.cjs"
},
"./runtime": {
"import": "./src/runtime/index.mjs",
"require": "./cjs/runtime/index.cjs"
Expand Down Expand Up @@ -91,7 +87,6 @@
"dependencies": {
"@jsep-plugin/regex": "^1.0.3",
"@jsep-plugin/ternary": "^1.1.3",
"astring": "^1.8.6",
"jsep": "^1.3.8"
"astring": "^1.8.6"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function print(expr) {
return astring(branch[0].test);
}

describe('parseFilterExpression', () => {
describe('generateFilterScriptExpression', () => {
it('at member expression', () => {
expect(print(`?(@.schema || @.ex)`)).to.eq(
`!(scope.sandbox.value.schema || scope.sandbox.value.ex)`,
Expand Down Expand Up @@ -50,7 +50,14 @@ describe('parseFilterExpression', () => {
);
});

it('basic binary expressions', () => {
it('unary expressions', () => {
expect(print(`?([email protected])`)).to.eq(`!!scope.sandbox.value.amount`);
expect(print(`?([email protected] == [email protected])`)).to.eq(
'!(!scope.sandbox.value.line == -scope.sandbox.value.char)',
);
});

it('binary expressions', () => {
expect(print(`?(@.amount + 2 === 4)`)).to.eq(
`!(scope.sandbox.value.amount + 2 === 4)`,
);
Expand Down
4 changes: 1 addition & 3 deletions src/codegen/baseline/generators.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import jsep from '../../parser/jsep.mjs';
import * as b from '../ast/builders.mjs';
import { isNegativeSliceExpression } from '../guards.mjs';
import internalScope from '../templates/internal-scope.mjs';
Expand Down Expand Up @@ -258,10 +257,9 @@ export function generateWildcardExpression(branch, iterator) {
export function generateFilterScriptExpression(
branch,
iterator,
{ value },
{ value: esTree },
tree,
) {
const esTree = jsep(value);
assertDefinedIdentifier(esTree);
const node = rewriteESTree(tree, esTree);

Expand Down
2 changes: 1 addition & 1 deletion src/codegen/tree/tree.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default class ESTree {
b.identifier(name),
statefulFnParams,
b.blockStatement([
b.returnStatement(jsep(this.#availableShorthands[name])),
b.returnStatement(jsep.parse(this.#availableShorthands[name])),
]),
),
);
Expand Down
Loading

0 comments on commit 5147e14

Please sign in to comment.