jit-parser 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +632 -0
  2. package/lib/codegen/CodegenAstFactory.d.ts +14 -0
  3. package/lib/codegen/CodegenAstFactory.js +119 -0
  4. package/lib/codegen/CodegenAstFactory.js.map +1 -0
  5. package/lib/codegen/CodegenGrammar.d.ts +21 -0
  6. package/lib/codegen/CodegenGrammar.js +145 -0
  7. package/lib/codegen/CodegenGrammar.js.map +1 -0
  8. package/lib/codegen/CodegenList.d.ts +15 -0
  9. package/lib/codegen/CodegenList.js +63 -0
  10. package/lib/codegen/CodegenList.js.map +1 -0
  11. package/lib/codegen/CodegenProduction.d.ts +15 -0
  12. package/lib/codegen/CodegenProduction.js +72 -0
  13. package/lib/codegen/CodegenProduction.js.map +1 -0
  14. package/lib/codegen/CodegenTerminal.d.ts +14 -0
  15. package/lib/codegen/CodegenTerminal.js +133 -0
  16. package/lib/codegen/CodegenTerminal.js.map +1 -0
  17. package/lib/codegen/CodegenUnion.d.ts +15 -0
  18. package/lib/codegen/CodegenUnion.js +71 -0
  19. package/lib/codegen/CodegenUnion.js.map +1 -0
  20. package/lib/codegen/Pattern.d.ts +8 -0
  21. package/lib/codegen/Pattern.js +41 -0
  22. package/lib/codegen/Pattern.js.map +1 -0
  23. package/lib/context.d.ts +13 -0
  24. package/lib/context.js +20 -0
  25. package/lib/context.js.map +1 -0
  26. package/lib/generator.d.ts +20 -0
  27. package/lib/generator.js +100 -0
  28. package/lib/generator.js.map +1 -0
  29. package/lib/grammars/calculator.d.ts +2 -0
  30. package/lib/grammars/calculator.js +76 -0
  31. package/lib/grammars/calculator.js.map +1 -0
  32. package/lib/grammars/esql.d.ts +2 -0
  33. package/lib/grammars/esql.js +221 -0
  34. package/lib/grammars/esql.js.map +1 -0
  35. package/lib/grammars/javascript.d.ts +2 -0
  36. package/lib/grammars/javascript.js +251 -0
  37. package/lib/grammars/javascript.js.map +1 -0
  38. package/lib/grammars/json-expression.d.ts +2 -0
  39. package/lib/grammars/json-expression.js +101 -0
  40. package/lib/grammars/json-expression.js.map +1 -0
  41. package/lib/grammars/json.d.ts +2 -0
  42. package/lib/grammars/json.js +79 -0
  43. package/lib/grammars/json.js.map +1 -0
  44. package/lib/index.d.ts +0 -0
  45. package/lib/index.js +2 -0
  46. package/lib/index.js.map +1 -0
  47. package/lib/matches.d.ts +15 -0
  48. package/lib/matches.js +21 -0
  49. package/lib/matches.js.map +1 -0
  50. package/lib/print.d.ts +11 -0
  51. package/lib/print.js +97 -0
  52. package/lib/print.js.map +1 -0
  53. package/lib/testing/cli.d.ts +1 -0
  54. package/lib/testing/cli.js +84 -0
  55. package/lib/testing/cli.js.map +1 -0
  56. package/lib/testing/driver.d.ts +21 -0
  57. package/lib/testing/driver.js +73 -0
  58. package/lib/testing/driver.js.map +1 -0
  59. package/lib/testing/index.d.ts +5 -0
  60. package/lib/testing/index.js +9 -0
  61. package/lib/testing/index.js.map +1 -0
  62. package/lib/testing/jest.d.ts +4 -0
  63. package/lib/testing/jest.js +31 -0
  64. package/lib/testing/jest.js.map +1 -0
  65. package/lib/testing/report.d.ts +3 -0
  66. package/lib/testing/report.js +64 -0
  67. package/lib/testing/report.js.map +1 -0
  68. package/lib/testing/runner.d.ts +29 -0
  69. package/lib/testing/runner.js +93 -0
  70. package/lib/testing/runner.js.map +1 -0
  71. package/lib/testing/types.d.ts +34 -0
  72. package/lib/testing/types.js +3 -0
  73. package/lib/testing/types.js.map +1 -0
  74. package/lib/types.d.ts +70 -0
  75. package/lib/types.js +3 -0
  76. package/lib/types.js.map +1 -0
  77. package/lib/util.d.ts +10 -0
  78. package/lib/util.js +39 -0
  79. package/lib/util.js.map +1 -0
  80. package/package.json +103 -0
package/lib/types.d.ts ADDED
@@ -0,0 +1,70 @@
1
+ import type { Expr } from '@jsonjoy.com/json-expression';
2
+ import type { ParseContext } from './context';
3
+ import type { Pattern } from './codegen/Pattern';
4
+ export interface Grammar {
5
+ start: string;
6
+ cst: Record<string, GrammarNode>;
7
+ ast?: Record<string, AstNodeExpression>;
8
+ }
9
+ export type GrammarNode = RefNode | TerminalNodeShorthand | TerminalNode | ProductionNodeShorthand | ProductionNode | UnionNode | ListNode;
10
+ export type ResolvedGrammarNode = TerminalNode | ProductionNode | UnionNode | ListNode;
11
+ export type RefNode<Name extends string = string> = {
12
+ r: Name;
13
+ };
14
+ export type TerminalNodeShorthand = RegExp | string | '';
15
+ export interface TerminalNode extends GrammarNodeBase {
16
+ type?: string;
17
+ t: TerminalNodeShorthand | string[];
18
+ repeat?: '*' | '+';
19
+ }
20
+ export type ProductionNodeShorthand = GrammarNode[];
21
+ export interface ProductionNode extends GrammarNodeBase {
22
+ p: ProductionNodeShorthand;
23
+ type?: string;
24
+ }
25
+ export interface UnionNode extends GrammarNodeBase {
26
+ u: GrammarNode[];
27
+ type?: string;
28
+ }
29
+ export interface ListNode extends GrammarNodeBase {
30
+ l: GrammarNode;
31
+ type?: string;
32
+ }
33
+ export interface GrammarNodeBase extends AstCodegenOpts {
34
+ sample?: string;
35
+ }
36
+ export interface AstCodegenOpts {
37
+ ast?: AstNodeExpression;
38
+ children?: Record<number, string>;
39
+ leaf?: boolean;
40
+ }
41
+ export interface CstNode {
42
+ ptr: Pattern;
43
+ pos: number;
44
+ end: number;
45
+ children?: CstNode[];
46
+ }
47
+ export interface CanonicalAstNode {
48
+ type: string;
49
+ pos: number;
50
+ end: number;
51
+ raw?: string;
52
+ children?: (CanonicalAstNode | unknown)[];
53
+ }
54
+ export type Parser = (ctx: ParseContext, pos: number) => CstNode | undefined;
55
+ export type AstNodeExpression = undefined | null | unknown | Expr;
56
+ export interface AstExpressionData {
57
+ cst: CstNode;
58
+ ast: CanonicalAstNode | unknown;
59
+ }
60
+ export type AstNodeFactory = (cst: CstNode, src: string) => unknown;
61
+ export interface ParseTraceNode {
62
+ pos: number;
63
+ ptr: ResolvedGrammarNode;
64
+ match?: CstNode;
65
+ children?: ParseTraceNode[];
66
+ }
67
+ export interface RootTraceNode {
68
+ pos: number;
69
+ children: ParseTraceNode[];
70
+ }
package/lib/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/lib/util.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { Grammar, GrammarNode, ListNode, ProductionNode, ProductionNodeShorthand, RefNode, ResolvedGrammarNode, TerminalNode, TerminalNodeShorthand, UnionNode } from './types';
2
+ export declare const scrub: (str: string) => any;
3
+ export declare const isTerminalShorthandNode: (item: any) => item is TerminalNodeShorthand;
4
+ export declare const isTerminalNode: (item: any) => item is TerminalNode;
5
+ export declare const isProductionShorthandNode: (item: any) => item is ProductionNodeShorthand;
6
+ export declare const isProductionNode: (item: any) => item is ProductionNode;
7
+ export declare const isUnionNode: (item: any) => item is UnionNode;
8
+ export declare const isListNode: (item: any) => item is ListNode;
9
+ export declare const isRefNode: (item: any) => item is RefNode;
10
+ export declare const delimitedList: (name: string, delim: GrammarNode, elem: ResolvedGrammarNode) => Grammar["cst"];
package/lib/util.js ADDED
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.delimitedList = exports.isRefNode = exports.isListNode = exports.isUnionNode = exports.isProductionNode = exports.isProductionShorthandNode = exports.isTerminalNode = exports.isTerminalShorthandNode = exports.scrub = void 0;
4
+ const scrub = (str) => JSON.parse(JSON.stringify(str));
5
+ exports.scrub = scrub;
6
+ const isTerminalShorthandNode = (item) => typeof item === 'string' || item instanceof RegExp;
7
+ exports.isTerminalShorthandNode = isTerminalShorthandNode;
8
+ const isTerminalNode = (item) => typeof item === 'object' && item && typeof item.t !== 'undefined' && !(0, exports.isTerminalShorthandNode)(item);
9
+ exports.isTerminalNode = isTerminalNode;
10
+ const isProductionShorthandNode = (item) => item instanceof Array;
11
+ exports.isProductionShorthandNode = isProductionShorthandNode;
12
+ const isProductionNode = (item) => typeof item === 'object' && item && (0, exports.isProductionShorthandNode)(item.p);
13
+ exports.isProductionNode = isProductionNode;
14
+ const isUnionNode = (item) => typeof item === 'object' && item && item.u instanceof Array;
15
+ exports.isUnionNode = isUnionNode;
16
+ const isListNode = (item) => typeof item === 'object' && item && typeof item.l !== 'undefined';
17
+ exports.isListNode = isListNode;
18
+ const isRefNode = (item) => typeof item === 'object' && item && typeof item.r === 'string';
19
+ exports.isRefNode = isRefNode;
20
+ const delimitedList = (name, delim, elem) => {
21
+ const itemName = elem.type ?? `${name}Item`;
22
+ return {
23
+ [name]: {
24
+ p: [
25
+ { r: itemName },
26
+ {
27
+ l: {
28
+ p: [delim, { r: itemName }],
29
+ ast: ['$', '/children/0'],
30
+ },
31
+ },
32
+ ],
33
+ ast: ['concat', ['push', [[]], ['$', '/children/0']], ['$', '/children/1/children', [[]]]],
34
+ },
35
+ [itemName]: elem,
36
+ };
37
+ };
38
+ exports.delimitedList = delimitedList;
39
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;;AAaO,MAAM,KAAK,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC;AAAzD,QAAA,KAAK,SAAoD;AAE/D,MAAM,uBAAuB,GAAG,CAAC,IAAS,EAAiC,EAAE,CAClF,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,YAAY,MAAM,CAAC;AADxC,QAAA,uBAAuB,2BACiB;AAE9C,MAAM,cAAc,GAAG,CAAC,IAAS,EAAwB,EAAE,CAChE,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,IAAI,OAAO,IAAI,CAAC,CAAC,KAAK,WA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EAAE,CAAC,GAAG,EAAE,aAAa,CAAC;qBAC1B;iBACF;aACF;YACD,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC,EAAE,CAAC,GAAG,EAAE,sBAAsB,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;SAC3F;QACD,CAAC,QAAQ,CAAC,EAAE,IAAI;KACjB,CAAC;AACJ,CAAC,CAAC;AAjBW,QAAA,aAAa,iBAiBxB"}
package/package.json ADDED
@@ -0,0 +1,103 @@
1
+ {
2
+ "name": "jit-parser",
3
+ "private": false,
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "version": "1.0.0",
8
+ "description": "High-performance JIT parser",
9
+ "author": {
10
+ "name": "streamich",
11
+ "url": "https://github.com/streamich"
12
+ },
13
+ "homepage": "https://github.com/streamich/jit-parser",
14
+ "repository": "streamich/jit-parser",
15
+ "funding": {
16
+ "type": "github",
17
+ "url": "https://github.com/sponsors/streamich"
18
+ },
19
+ "keywords": [
20
+ "parser",
21
+ "jit",
22
+ "tree",
23
+ "parse-tree",
24
+ "ast",
25
+ "tokenizer",
26
+ "radix"
27
+ ],
28
+ "engines": {
29
+ "node": ">=10.0"
30
+ },
31
+ "main": "lib/index.js",
32
+ "types": "lib/index.d.ts",
33
+ "typings": "lib/index.d.ts",
34
+ "files": [
35
+ "LICENSE",
36
+ "lib/"
37
+ ],
38
+ "license": "Apache-2.0",
39
+ "scripts": {
40
+ "prettier": "prettier --ignore-path .gitignore --write \"src/**/*.{ts,tsx,js,jsx}\"",
41
+ "prettier:check": "prettier --ignore-path .gitignore --list-different 'src/**/*.{ts,tsx,js,jsx}'",
42
+ "lint": "yarn tslint",
43
+ "tslint": "tslint 'src/**/*.{js,jsx,ts,tsx}' -t verbose --project .",
44
+ "clean": "npx rimraf@5.0.5 lib es6 es2019 es2020 esm typedocs coverage gh-pages yarn-error.log",
45
+ "build": "tsc --project tsconfig.build.json --module commonjs --target es2020 --outDir lib",
46
+ "jest": "node -r ts-node/register ./node_modules/.bin/jest",
47
+ "grammar-test": "node -r ts-node/register src/testing/cli.ts",
48
+ "test": "jest --maxWorkers 7",
49
+ "test:ci": "yarn jest --maxWorkers 3 --no-cache",
50
+ "coverage": "yarn test --collectCoverage",
51
+ "typedoc": "npx typedoc@0.28.5 --tsconfig tsconfig.build.json",
52
+ "build:pages": "rimraf gh-pages && mkdir -p gh-pages && cp -r typedocs/* gh-pages && cp -r coverage gh-pages/coverage",
53
+ "deploy:pages": "gh-pages -d gh-pages",
54
+ "publish-coverage-and-typedocs": "yarn typedoc && yarn coverage && yarn build:pages && yarn deploy:pages"
55
+ },
56
+ "peerDependencies": {
57
+ "tslib": "2"
58
+ },
59
+ "peerDependenciesMeta": {},
60
+ "dependencies": {
61
+ "@jsonjoy.com/json-expression": "^1.1.0",
62
+ "@jsonjoy.com/util": "^1.8.0",
63
+ "json-joy": "^17.49.1",
64
+ "reregexp": "^1.6.1",
65
+ "tree-dump": "^1.0.3"
66
+ },
67
+ "devDependencies": {
68
+ "@types/jest": "^29.5.12",
69
+ "jest": "^29.7.0",
70
+ "prettier": "^3.3.2",
71
+ "ts-jest": "^29.1.4",
72
+ "ts-node": "^10.9.2",
73
+ "tslib": "^2.6.3",
74
+ "tslint": "^6.1.3",
75
+ "tslint-config-common": "^1.6.2",
76
+ "typescript": "^5.4.5"
77
+ },
78
+ "jest": {
79
+ "verbose": true,
80
+ "testEnvironmentOptions": {
81
+ "url": "http://localhost/"
82
+ },
83
+ "moduleFileExtensions": [
84
+ "ts",
85
+ "js"
86
+ ],
87
+ "transform": {
88
+ "^.+\\.ts$": "ts-jest"
89
+ },
90
+ "transformIgnorePatterns": [],
91
+ "testRegex": ".*/(__tests__|__jest__|demo)/.*\\.(test|spec)\\.ts$"
92
+ },
93
+ "prettier": {
94
+ "arrowParens": "always",
95
+ "printWidth": 120,
96
+ "tabWidth": 2,
97
+ "useTabs": false,
98
+ "semi": true,
99
+ "singleQuote": true,
100
+ "trailingComma": "all",
101
+ "bracketSpacing": false
102
+ }
103
+ }