clarity-pattern-parser 11.3.13 → 11.4.1
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.
- package/dist/grammar/Grammar.d.ts +6 -2
- package/dist/grammar_v2/patterns/Grammar.d.ts +23 -0
- package/dist/grammar_v2/patterns/patterns/grammar.d.ts +87 -0
- package/dist/grammar_v2/patterns/patterns/grammar.test.d.ts +1 -0
- package/dist/index.browser.js +82 -13
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +82 -13
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +82 -13
- package/dist/index.js.map +1 -1
- package/dist/patterns/Cursor.d.ts +1 -1
- package/package.json +1 -1
- package/src/grammar/Grammar.test.ts +38 -0
- package/src/grammar/Grammar.ts +87 -8
- package/src/grammar_v2/patterns/Grammar.ts +170 -0
- package/src/grammar_v2/patterns/patterns/cpat.cpat +91 -0
- package/src/grammar_v2/patterns/patterns/grammar.test.ts +218 -0
- package/src/grammar_v2/patterns/patterns/grammar.ts +103 -0
- package/src/patterns/Cursor.ts +6 -6
- package/src/patterns/Regex.ts +4 -2
- package/src/patterns/Sequence.test.ts +11 -0
|
@@ -6,6 +6,7 @@ export interface GrammarFile {
|
|
|
6
6
|
}
|
|
7
7
|
export interface GrammarOptions {
|
|
8
8
|
resolveImport?: (resource: string, originResource: string | null) => Promise<GrammarFile>;
|
|
9
|
+
resolveImportSync?: (resource: string, originResource: string | null) => GrammarFile;
|
|
9
10
|
originResource?: string | null;
|
|
10
11
|
params?: Pattern[];
|
|
11
12
|
decorators?: Record<string, Decorator>;
|
|
@@ -14,6 +15,7 @@ export declare class Grammar {
|
|
|
14
15
|
private _params;
|
|
15
16
|
private _originResource?;
|
|
16
17
|
private _resolveImport;
|
|
18
|
+
private _resolveImportSync;
|
|
17
19
|
private _parseContext;
|
|
18
20
|
constructor(options?: GrammarOptions);
|
|
19
21
|
import(path: string): Promise<Record<string, Pattern>>;
|
|
@@ -42,8 +44,10 @@ export declare class Grammar {
|
|
|
42
44
|
private _saveConfigurableAnonymous;
|
|
43
45
|
private _buildComplexAnonymousPattern;
|
|
44
46
|
private _resolveImports;
|
|
45
|
-
private
|
|
46
|
-
private
|
|
47
|
+
private _resolveImportsSync;
|
|
48
|
+
private _processImportSync;
|
|
49
|
+
private _processImport;
|
|
50
|
+
private _processUseParams;
|
|
47
51
|
private _applyDecorators;
|
|
48
52
|
private _getParams;
|
|
49
53
|
private _getPattern;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { GrammarFile } from "../../grammar/Grammar";
|
|
2
|
+
import { Pattern } from "../../patterns/Pattern";
|
|
3
|
+
export type Decorator = (pattern: Pattern, arg?: string | boolean | number | null | Record<string, any> | any[]) => void;
|
|
4
|
+
export interface GrammarOptions {
|
|
5
|
+
resolveImport?: (resource: string, originResource: string | null) => Promise<GrammarFile>;
|
|
6
|
+
resolveImportSync?: (resource: string, originResource: string | null) => GrammarFile;
|
|
7
|
+
originResource?: string | null;
|
|
8
|
+
params?: Pattern[];
|
|
9
|
+
decorators?: Record<string, Decorator>;
|
|
10
|
+
}
|
|
11
|
+
export declare class Grammar {
|
|
12
|
+
private _options;
|
|
13
|
+
private _parseContext;
|
|
14
|
+
private _resolveImportSync;
|
|
15
|
+
constructor(options: GrammarOptions);
|
|
16
|
+
private _resolveImportsSync;
|
|
17
|
+
private _processImportSync;
|
|
18
|
+
private _getWithParams;
|
|
19
|
+
private _processUseParams;
|
|
20
|
+
private _tryToParse;
|
|
21
|
+
private _flattenExpressions;
|
|
22
|
+
private _unwrapNode;
|
|
23
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Expression } from "../../../patterns/Expression";
|
|
2
|
+
import { Literal } from "../../../patterns/Literal";
|
|
3
|
+
import { Optional } from "../../../patterns/Optional";
|
|
4
|
+
import { Options } from "../../../patterns/Options";
|
|
5
|
+
import { Regex } from "../../../patterns/Regex";
|
|
6
|
+
import { Repeat } from "../../../patterns/Repeat";
|
|
7
|
+
import { Sequence } from "../../../patterns/Sequence";
|
|
8
|
+
export declare const syntax: Literal;
|
|
9
|
+
export declare const imprt: Literal;
|
|
10
|
+
export declare const useParams: Literal;
|
|
11
|
+
export declare const withParams: Literal;
|
|
12
|
+
export declare const from: Literal;
|
|
13
|
+
export declare const right: Literal;
|
|
14
|
+
export declare const ws: Regex;
|
|
15
|
+
export declare const ls: Regex;
|
|
16
|
+
export declare const assign: Literal;
|
|
17
|
+
export declare const bar: Literal;
|
|
18
|
+
export declare const greedyBar: Literal;
|
|
19
|
+
export declare const concat: Literal;
|
|
20
|
+
export declare const colon: Literal;
|
|
21
|
+
export declare const openParen: Literal;
|
|
22
|
+
export declare const closeParen: Literal;
|
|
23
|
+
export declare const openSquareBracket: Literal;
|
|
24
|
+
export declare const closeSquareBracket: Literal;
|
|
25
|
+
export declare const openBracket: Literal;
|
|
26
|
+
export declare const closeBracket: Literal;
|
|
27
|
+
export declare const comma: Regex;
|
|
28
|
+
export declare const trim: Literal;
|
|
29
|
+
export declare const not: Literal;
|
|
30
|
+
export declare const optional: Literal;
|
|
31
|
+
export declare const newLine: Regex;
|
|
32
|
+
export declare const syntaxVersion: Regex;
|
|
33
|
+
export declare const at: Literal;
|
|
34
|
+
export declare const optionalWS: Optional;
|
|
35
|
+
export declare const optionalLS: Optional;
|
|
36
|
+
export declare const literal: Regex;
|
|
37
|
+
export declare const regex: Regex;
|
|
38
|
+
export declare const integer: Regex;
|
|
39
|
+
export declare const name: Regex;
|
|
40
|
+
export declare const patternName: Regex;
|
|
41
|
+
export declare const patternIdentifier: Regex;
|
|
42
|
+
export declare const resource: Regex;
|
|
43
|
+
export declare const comment: Regex;
|
|
44
|
+
export declare const jsonString: Regex;
|
|
45
|
+
export declare const jsonNumber: Regex;
|
|
46
|
+
export declare const trueLiteral: Literal;
|
|
47
|
+
export declare const falseLiteral: Literal;
|
|
48
|
+
export declare const jsonBoolean: Options;
|
|
49
|
+
export declare const jsonNull: Literal;
|
|
50
|
+
export declare const jsonArrayItems: Repeat;
|
|
51
|
+
export declare const jsonArray: Sequence;
|
|
52
|
+
export declare const jsonObjectPropertyName: Regex;
|
|
53
|
+
export declare const jsonObjectProperty: Sequence;
|
|
54
|
+
export declare const jsonObjectProperties: Repeat;
|
|
55
|
+
export declare const jsonObject: Sequence;
|
|
56
|
+
export declare const jsonValue: Options;
|
|
57
|
+
export declare const syntaxStatement: Sequence;
|
|
58
|
+
export declare const decorationName: Regex;
|
|
59
|
+
export declare const decorationStatement: Sequence;
|
|
60
|
+
export declare const useParamPatterns: Repeat;
|
|
61
|
+
export declare const useParamsStatement: Sequence;
|
|
62
|
+
export declare const withParamStatements: Repeat;
|
|
63
|
+
export declare const withParamsExpr: Sequence;
|
|
64
|
+
export declare const patternNames: Repeat;
|
|
65
|
+
export declare const importedPatterns: Sequence;
|
|
66
|
+
export declare const importStatement: Sequence;
|
|
67
|
+
export declare const notExpr: Sequence;
|
|
68
|
+
export declare const optionalExpr: Sequence;
|
|
69
|
+
export declare const rightAssociationExpr: Sequence;
|
|
70
|
+
export declare const unaryPatternExpr: Expression;
|
|
71
|
+
export declare const repeatBounds: Sequence;
|
|
72
|
+
export declare const oneOrMore: Literal;
|
|
73
|
+
export declare const zeroOrMore: Literal;
|
|
74
|
+
export declare const repeatOptions: Options;
|
|
75
|
+
export declare const delimiter: Sequence;
|
|
76
|
+
export declare const repeatExpr: Sequence;
|
|
77
|
+
export declare const sequenceExpr: Sequence;
|
|
78
|
+
export declare const optionsExpr: Sequence;
|
|
79
|
+
export declare const greedyOptionsExpr: Sequence;
|
|
80
|
+
export declare const patternGroupExpr: Sequence;
|
|
81
|
+
export declare const exportPattern: Regex;
|
|
82
|
+
export declare const patternExpr: Expression;
|
|
83
|
+
export declare const patternAssignment: Sequence;
|
|
84
|
+
export declare const statement: Options;
|
|
85
|
+
export declare const statements: Repeat;
|
|
86
|
+
export declare const cpat: Sequence;
|
|
87
|
+
export declare const grammar: Sequence;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.browser.js
CHANGED
|
@@ -435,7 +435,7 @@
|
|
|
435
435
|
|
|
436
436
|
class Cursor {
|
|
437
437
|
get text() {
|
|
438
|
-
return this.
|
|
438
|
+
return this._chars.join("");
|
|
439
439
|
}
|
|
440
440
|
get isOnFirst() {
|
|
441
441
|
return this._index === 0;
|
|
@@ -483,12 +483,12 @@
|
|
|
483
483
|
return this._history.error != null;
|
|
484
484
|
}
|
|
485
485
|
get currentChar() {
|
|
486
|
-
return this.
|
|
486
|
+
return this._chars[this._index];
|
|
487
487
|
}
|
|
488
488
|
constructor(text) {
|
|
489
|
-
this.
|
|
489
|
+
this._chars = [...text];
|
|
490
490
|
this._index = 0;
|
|
491
|
-
this._length =
|
|
491
|
+
this._length = this._chars.length;
|
|
492
492
|
this._history = new CursorHistory();
|
|
493
493
|
}
|
|
494
494
|
hasNext() {
|
|
@@ -522,7 +522,7 @@
|
|
|
522
522
|
return this._length - 1;
|
|
523
523
|
}
|
|
524
524
|
getChars(first, last) {
|
|
525
|
-
return this.
|
|
525
|
+
return this._chars.slice(first, last + 1).join("");
|
|
526
526
|
}
|
|
527
527
|
recordMatch(pattern, node) {
|
|
528
528
|
this._history.recordMatch(pattern, node);
|
|
@@ -771,7 +771,9 @@
|
|
|
771
771
|
}
|
|
772
772
|
processResult(cursor, result) {
|
|
773
773
|
const currentIndex = cursor.index;
|
|
774
|
-
const
|
|
774
|
+
const match = result[0];
|
|
775
|
+
const matchLength = [...match].length;
|
|
776
|
+
const newIndex = currentIndex + matchLength - 1;
|
|
775
777
|
this._node = new Node("regex", this._name, currentIndex, newIndex, undefined, result[0]);
|
|
776
778
|
cursor.moveTo(newIndex);
|
|
777
779
|
cursor.recordMatch(this, this._node);
|
|
@@ -3503,11 +3505,15 @@
|
|
|
3503
3505
|
function defaultImportResolver(_path, _basePath) {
|
|
3504
3506
|
throw new Error("No import resolver supplied.");
|
|
3505
3507
|
}
|
|
3508
|
+
function defaultImportResolverSync(_path, _basePath) {
|
|
3509
|
+
throw new Error("No import resolver supplied.");
|
|
3510
|
+
}
|
|
3506
3511
|
class Grammar {
|
|
3507
3512
|
constructor(options = {}) {
|
|
3508
3513
|
this._params = (options === null || options === void 0 ? void 0 : options.params) == null ? [] : options.params;
|
|
3509
3514
|
this._originResource = (options === null || options === void 0 ? void 0 : options.originResource) == null ? null : options.originResource;
|
|
3510
3515
|
this._resolveImport = options.resolveImport == null ? defaultImportResolver : options.resolveImport;
|
|
3516
|
+
this._resolveImportSync = options.resolveImportSync == null ? defaultImportResolverSync : options.resolveImportSync;
|
|
3511
3517
|
this._parseContext = new ParseContext(this._params, options.decorators || {});
|
|
3512
3518
|
}
|
|
3513
3519
|
import(path) {
|
|
@@ -3534,9 +3540,7 @@
|
|
|
3534
3540
|
parseString(expression) {
|
|
3535
3541
|
this._parseContext = new ParseContext(this._params, this._parseContext.decorators);
|
|
3536
3542
|
const ast = this._tryToParse(expression);
|
|
3537
|
-
|
|
3538
|
-
throw new Error("Cannot use imports on parseString, use parse instead.");
|
|
3539
|
-
}
|
|
3543
|
+
this._resolveImportsSync(ast);
|
|
3540
3544
|
this._buildPatterns(ast);
|
|
3541
3545
|
return this._buildPatternRecord();
|
|
3542
3546
|
}
|
|
@@ -3843,15 +3847,80 @@
|
|
|
3843
3847
|
});
|
|
3844
3848
|
for (const statement of importStatements) {
|
|
3845
3849
|
if (statement.name === "import-from") {
|
|
3846
|
-
yield this.
|
|
3850
|
+
yield this._processImport(statement);
|
|
3847
3851
|
}
|
|
3848
3852
|
else {
|
|
3849
|
-
this.
|
|
3853
|
+
this._processUseParams(statement);
|
|
3850
3854
|
}
|
|
3851
3855
|
}
|
|
3852
3856
|
});
|
|
3853
3857
|
}
|
|
3854
|
-
|
|
3858
|
+
_resolveImportsSync(ast) {
|
|
3859
|
+
const importStatements = ast.findAll(n => {
|
|
3860
|
+
return n.name === "import-from" || n.name === "param-name-with-default-value";
|
|
3861
|
+
});
|
|
3862
|
+
for (const statement of importStatements) {
|
|
3863
|
+
if (statement.name === "import-from") {
|
|
3864
|
+
this._processImportSync(statement);
|
|
3865
|
+
}
|
|
3866
|
+
else {
|
|
3867
|
+
this._processUseParams(statement);
|
|
3868
|
+
}
|
|
3869
|
+
}
|
|
3870
|
+
}
|
|
3871
|
+
_processImportSync(importStatement) {
|
|
3872
|
+
const parseContext = this._parseContext;
|
|
3873
|
+
const resourceNode = importStatement.find(n => n.name === "resource");
|
|
3874
|
+
const params = this._getParams(importStatement);
|
|
3875
|
+
const resource = resourceNode.value.slice(1, -1);
|
|
3876
|
+
const grammarFile = this._resolveImportSync(resource, this._originResource || null);
|
|
3877
|
+
const grammar = new Grammar({
|
|
3878
|
+
resolveImport: this._resolveImport,
|
|
3879
|
+
resolveImportSync: this._resolveImportSync,
|
|
3880
|
+
originResource: grammarFile.resource,
|
|
3881
|
+
params,
|
|
3882
|
+
decorators: this._parseContext.decorators
|
|
3883
|
+
});
|
|
3884
|
+
try {
|
|
3885
|
+
const patterns = grammar.parseString(grammarFile.expression);
|
|
3886
|
+
const importStatements = importStatement.findAll(n => n.name === "import-name" || n.name === "import-alias");
|
|
3887
|
+
importStatements.forEach((node) => {
|
|
3888
|
+
var _a, _b;
|
|
3889
|
+
if (node.name === "import-name" && ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.name) === "import-alias") {
|
|
3890
|
+
return;
|
|
3891
|
+
}
|
|
3892
|
+
if (node.name === "import-name" && ((_b = node.parent) === null || _b === void 0 ? void 0 : _b.name) !== "import-alias") {
|
|
3893
|
+
const importName = node.value;
|
|
3894
|
+
if (parseContext.importedPatternsByName.has(importName)) {
|
|
3895
|
+
throw new Error(`'${importName}' was already used within another import.`);
|
|
3896
|
+
}
|
|
3897
|
+
const pattern = patterns[importName];
|
|
3898
|
+
if (pattern == null) {
|
|
3899
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
3900
|
+
}
|
|
3901
|
+
parseContext.importedPatternsByName.set(importName, pattern);
|
|
3902
|
+
}
|
|
3903
|
+
else {
|
|
3904
|
+
const importNameNode = node.find(n => n.name === "import-name");
|
|
3905
|
+
const importName = importNameNode.value;
|
|
3906
|
+
const aliasNode = node.find(n => n.name === "import-name-alias");
|
|
3907
|
+
const alias = aliasNode.value;
|
|
3908
|
+
if (parseContext.importedPatternsByName.has(alias)) {
|
|
3909
|
+
throw new Error(`'${alias}' was already used within another import.`);
|
|
3910
|
+
}
|
|
3911
|
+
const pattern = patterns[importName];
|
|
3912
|
+
if (pattern == null) {
|
|
3913
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
3914
|
+
}
|
|
3915
|
+
parseContext.importedPatternsByName.set(alias, pattern.clone(alias));
|
|
3916
|
+
}
|
|
3917
|
+
});
|
|
3918
|
+
}
|
|
3919
|
+
catch (e) {
|
|
3920
|
+
throw new Error(`Failed loading expression from: "${resource}". Error details: "${e.message}"`);
|
|
3921
|
+
}
|
|
3922
|
+
}
|
|
3923
|
+
_processImport(importStatement) {
|
|
3855
3924
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3856
3925
|
const parseContext = this._parseContext;
|
|
3857
3926
|
const resourceNode = importStatement.find(n => n.name === "resource");
|
|
@@ -3904,7 +3973,7 @@
|
|
|
3904
3973
|
}
|
|
3905
3974
|
});
|
|
3906
3975
|
}
|
|
3907
|
-
|
|
3976
|
+
_processUseParams(paramName) {
|
|
3908
3977
|
const defaultValueNode = paramName.find(n => n.name === "param-default");
|
|
3909
3978
|
if (defaultValueNode === null) {
|
|
3910
3979
|
return;
|