clarity-pattern-parser 11.0.29 → 11.1.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.
package/dist/index.js CHANGED
@@ -1943,8 +1943,8 @@ class Sequence {
1943
1943
  }
1944
1944
  }
1945
1945
 
1946
- const literal = new Regex("literal", '"(?:\\\\.|[^"\\\\])*"');
1947
- literal.setTokens(["[LITERAL]"]);
1946
+ const literal$1 = new Regex("literal", '"(?:\\\\.|[^"\\\\])*"');
1947
+ literal$1.setTokens(["[LITERAL]"]);
1948
1948
 
1949
1949
  const tabs$1 = new Regex("tabs", "\\t+");
1950
1950
  tabs$1.setTokens(["\t"]);
@@ -1963,7 +1963,7 @@ regexLiteral.setTokens(["[REGEX_EXPRESSION]"]);
1963
1963
 
1964
1964
  const patternName$3 = name$1.clone("pattern-name");
1965
1965
  const anonymousLiterals = new Options("anonymous-literals", [
1966
- literal,
1966
+ literal$1,
1967
1967
  regexLiteral,
1968
1968
  patternName$3,
1969
1969
  new Reference("repeat-literal"),
@@ -2084,9 +2084,9 @@ const anonymousPattern = new Options("anonymous-pattern", [
2084
2084
  ]);
2085
2085
 
2086
2086
  const optionalSpaces$3 = new Optional("optional-spaces", spaces$1);
2087
- const openBracket$1 = new Literal("repeat-open-bracket", "{");
2088
- const closeBracket$1 = new Literal("repeat-close-bracket", "}");
2089
- const comma = new Literal("comma", ",");
2087
+ const openBracket$2 = new Literal("repeat-open-bracket", "{");
2088
+ const closeBracket$2 = new Literal("repeat-close-bracket", "}");
2089
+ const comma$1 = new Literal("comma", ",");
2090
2090
  const integer = new Regex("integer", "([1-9][0-9]*)|0");
2091
2091
  integer.setTokens(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
2092
2092
  const min = new Optional("optional-min", integer.clone("min"));
@@ -2094,21 +2094,21 @@ const max = new Optional("optional-max", integer.clone("max"));
2094
2094
  const trimKeyword = new Literal("trim-keyword", "trim");
2095
2095
  const trimFlag = new Optional("optional-trim-flag", new Sequence("trim-flag", [lineSpaces$1, trimKeyword]));
2096
2096
  const bounds = new Sequence("bounds", [
2097
- openBracket$1,
2097
+ openBracket$2,
2098
2098
  optionalSpaces$3,
2099
2099
  min,
2100
2100
  optionalSpaces$3,
2101
- comma,
2101
+ comma$1,
2102
2102
  optionalSpaces$3,
2103
2103
  max,
2104
- closeBracket$1
2104
+ closeBracket$2
2105
2105
  ]);
2106
2106
  const exactCount = new Sequence("exact-count", [
2107
- openBracket$1,
2107
+ openBracket$2,
2108
2108
  optionalSpaces$3,
2109
2109
  integer,
2110
2110
  optionalSpaces$3,
2111
- closeBracket$1,
2111
+ closeBracket$2,
2112
2112
  ]);
2113
2113
  const quantifierShorthand = new Regex("quantifier-shorthand", "\\*|\\+");
2114
2114
  quantifierShorthand.setTokens(["*", "+"]);
@@ -2117,8 +2117,8 @@ const quantifier = new Options("quantifier", [
2117
2117
  exactCount,
2118
2118
  bounds
2119
2119
  ]);
2120
- const openParen = new Literal("repeat-open-paren", "(");
2121
- const closeParen = new Literal("repeat-close-paren", ")");
2120
+ const openParen$1 = new Literal("repeat-open-paren", "(");
2121
+ const closeParen$1 = new Literal("repeat-close-paren", ")");
2122
2122
  const dividerComma = new Regex("divider-comma", "\\s*,\\s*");
2123
2123
  dividerComma.setTokens([", "]);
2124
2124
  const patternName$2 = name$1.clone("pattern-name");
@@ -2127,12 +2127,12 @@ const repeatDividerPattern = repeatPattern.clone("repeat-divider-pattern");
2127
2127
  const repeatDividerSection = new Sequence("repeat-divider-section", [dividerComma, repeatDividerPattern, trimFlag]);
2128
2128
  const repeatOptionalDividerSection = new Optional("repeat-optional-divider-section", repeatDividerSection);
2129
2129
  const repeatLiteral = new Sequence("repeat-literal", [
2130
- openParen,
2130
+ openParen$1,
2131
2131
  optionalSpaces$3,
2132
2132
  repeatPattern,
2133
2133
  repeatOptionalDividerSection,
2134
2134
  optionalSpaces$3,
2135
- closeParen,
2135
+ closeParen$1,
2136
2136
  new Sequence("quantifier-section", [quantifier]),
2137
2137
  ]);
2138
2138
 
@@ -2167,7 +2167,7 @@ aliasLiteral.setTokens(["[ALIAS_LITERAL]"]);
2167
2167
  const optionalIsOptional = new Optional("optional-flag", new Literal("is-optional", "?"));
2168
2168
  const configurableAnonymousPattern = new Sequence("configurable-anonymous-pattern", [anonymousPattern, optionalIsOptional]);
2169
2169
  const pattern = new Options("pattern", [
2170
- literal,
2170
+ literal$1,
2171
2171
  regexLiteral,
2172
2172
  repeatLiteral,
2173
2173
  aliasLiteral,
@@ -2176,6 +2176,74 @@ const pattern = new Options("pattern", [
2176
2176
  configurableAnonymousPattern,
2177
2177
  ], true);
2178
2178
 
2179
+ const colon = new Literal("colon", ":");
2180
+ const comma = new Regex("comma", "\\s*,\\s*");
2181
+ const openBracket$1 = new Literal("open-bracket", "{");
2182
+ const closeBracket$1 = new Literal("close-bracket", "}");
2183
+ const openSquareBracket = new Literal("open-square-bracket", "[");
2184
+ const closeSquareBracket = new Literal("close-square-bracket", "]");
2185
+ const optionalAllSpaces = new Optional("optional-all-spaces", allSpaces);
2186
+ const stringLiteral = new Regex("string-literal", '"(?:\\\\.|[^"\\\\])*"');
2187
+ const numberLiteral = new Regex("number-literal", '[+-]?\\d+(\\\\.\\d+)?([eE][+-]?\\d+)?');
2188
+ const nullLiteral = new Literal("null-literal", "null");
2189
+ const trueLiteral = new Literal("true-literal", "true");
2190
+ const falseLiteral = new Literal("false-literal", "false");
2191
+ const booleanLiteral = new Options("", [trueLiteral, falseLiteral]);
2192
+ const objectKey = name$1.clone("object-key");
2193
+ const objectProperty = new Sequence("object-property", [
2194
+ objectKey,
2195
+ optionalAllSpaces,
2196
+ colon,
2197
+ optionalAllSpaces,
2198
+ new Reference("literal"),
2199
+ ]);
2200
+ const objectProperies = new Repeat("object-properties", objectProperty, { divider: comma });
2201
+ const objectLiteral = new Sequence("object-literal", [
2202
+ openBracket$1,
2203
+ optionalAllSpaces,
2204
+ new Optional("optional-object-properties", objectProperies),
2205
+ optionalAllSpaces,
2206
+ closeBracket$1
2207
+ ]);
2208
+ const arrayItems = new Repeat("array-items", new Reference("literal"), { divider: comma });
2209
+ const arrayLiteral = new Sequence("array-literal", [
2210
+ openSquareBracket,
2211
+ optionalAllSpaces,
2212
+ arrayItems,
2213
+ optionalAllSpaces,
2214
+ closeSquareBracket,
2215
+ ]);
2216
+ const literal = new Options("literal", [
2217
+ objectLiteral,
2218
+ arrayLiteral,
2219
+ stringLiteral,
2220
+ booleanLiteral,
2221
+ nullLiteral,
2222
+ numberLiteral,
2223
+ ]);
2224
+ const decoratorPrefix = new Literal("decorator-prefix", "@");
2225
+ const decoratorName = name$1.clone("decorator-name");
2226
+ const openParen = new Literal("open-paren", "(");
2227
+ const closeParen = new Literal("close-paren", ")");
2228
+ const methodDecoration = new Sequence("method-decorator", [
2229
+ decoratorPrefix,
2230
+ decoratorName,
2231
+ optionalAllSpaces,
2232
+ openParen,
2233
+ optionalAllSpaces,
2234
+ new Optional("optional-args", literal),
2235
+ optionalAllSpaces,
2236
+ closeParen
2237
+ ]);
2238
+ const nameDecoration = new Sequence("name-decorator", [
2239
+ decoratorPrefix,
2240
+ decoratorName,
2241
+ ]);
2242
+ const decoratorStatement = new Options("decorator-statement", [
2243
+ methodDecoration,
2244
+ nameDecoration,
2245
+ ]);
2246
+
2179
2247
  const optionalSpaces$2 = new Optional("optional-spaces", spaces$1);
2180
2248
  const assignOperator = new Literal("assign-operator", "=");
2181
2249
  const assignStatement = new Sequence("assign-statement", [
@@ -2186,7 +2254,7 @@ const assignStatement = new Sequence("assign-statement", [
2186
2254
  optionalSpaces$2,
2187
2255
  pattern,
2188
2256
  ]);
2189
- const statement = new Options("statement", [assignStatement, name$1.clone("export-name")]);
2257
+ const statement = new Options("statement", [decoratorStatement, assignStatement, name$1.clone("export-name")]);
2190
2258
 
2191
2259
  const bodyLineContent = new Options("body-line-content", [
2192
2260
  comment,
@@ -2217,7 +2285,7 @@ const importAlias = new Sequence("import-alias", [name, lineSpaces$1, asKeyword,
2217
2285
  const importedNames = new Repeat("imported-names", new Options("import-names", [importAlias, name]), { divider: importNameDivider });
2218
2286
  const paramName = name.clone("param-name");
2219
2287
  const paramNames = new Repeat("param-names", paramName, { divider: importNameDivider });
2220
- const resource = literal.clone("resource");
2288
+ const resource = literal$1.clone("resource");
2221
2289
  const useParams = new Sequence("import-params", [
2222
2290
  useParamsKeyword,
2223
2291
  optionalLineSpaces$1,
@@ -2435,7 +2503,7 @@ class Context {
2435
2503
  return this._pattern.test(text, record);
2436
2504
  }
2437
2505
  clone(name = this._name) {
2438
- const clone = new Context(name, this._pattern, Object.values(this._patterns));
2506
+ const clone = new Context(name, this._pattern.clone(name), Object.values(this._patterns));
2439
2507
  clone._id = this._id;
2440
2508
  return clone;
2441
2509
  }
@@ -3176,7 +3244,23 @@ function cleanSuggestions(suggestions) {
3176
3244
  return suggestions.map(s => s.trim()).filter(s => s.length > 0);
3177
3245
  }
3178
3246
 
3247
+ const tokens = (pattern, arg) => {
3248
+ if (pattern.type === "regex" && Array.isArray(arg)) {
3249
+ const regex = pattern;
3250
+ const tokens = [];
3251
+ arg.forEach(token => {
3252
+ if (typeof token === "string") {
3253
+ tokens.push(token);
3254
+ }
3255
+ });
3256
+ regex.setTokens(tokens);
3257
+ }
3258
+ };
3259
+
3179
3260
  let anonymousIndexId = 0;
3261
+ const defaultDecorators = {
3262
+ tokens: tokens
3263
+ };
3180
3264
  const patternNodes = {
3181
3265
  "literal": true,
3182
3266
  "regex-literal": true,
@@ -3187,11 +3271,12 @@ const patternNodes = {
3187
3271
  "configurable-anonymous-pattern": true
3188
3272
  };
3189
3273
  class ParseContext {
3190
- constructor(params) {
3274
+ constructor(params, decorators = {}) {
3191
3275
  this.patternsByName = new Map();
3192
3276
  this.importedPatternsByName = new Map();
3193
3277
  this.paramsByName = new Map();
3194
3278
  params.forEach(p => this.paramsByName.set(p.name, p));
3279
+ this.decorators = Object.assign(Object.assign({}, decorators), defaultDecorators);
3195
3280
  }
3196
3281
  }
3197
3282
  function defaultImportResolver(_path, _basePath) {
@@ -3202,7 +3287,7 @@ class Grammar {
3202
3287
  this._params = (options === null || options === void 0 ? void 0 : options.params) == null ? [] : options.params;
3203
3288
  this._originResource = (options === null || options === void 0 ? void 0 : options.originResource) == null ? null : options.originResource;
3204
3289
  this._resolveImport = options.resolveImport == null ? defaultImportResolver : options.resolveImport;
3205
- this._parseContext = new ParseContext(this._params);
3290
+ this._parseContext = new ParseContext(this._params, options.decorators || {});
3206
3291
  }
3207
3292
  import(path) {
3208
3293
  return __awaiter(this, void 0, void 0, function* () {
@@ -3210,14 +3295,15 @@ class Grammar {
3210
3295
  const grammar = new Grammar({
3211
3296
  resolveImport: this._resolveImport,
3212
3297
  originResource: grammarFile.resource,
3213
- params: this._params
3298
+ params: this._params,
3299
+ decorators: this._parseContext.decorators
3214
3300
  });
3215
3301
  return grammar.parse(grammarFile.expression);
3216
3302
  });
3217
3303
  }
3218
3304
  parse(expression) {
3219
3305
  return __awaiter(this, void 0, void 0, function* () {
3220
- this._parseContext = new ParseContext(this._params);
3306
+ this._parseContext = new ParseContext(this._params, this._parseContext.decorators);
3221
3307
  const ast = this._tryToParse(expression);
3222
3308
  yield this._resolveImports(ast);
3223
3309
  this._buildPatterns(ast);
@@ -3225,7 +3311,7 @@ class Grammar {
3225
3311
  });
3226
3312
  }
3227
3313
  parseString(expression) {
3228
- this._parseContext = new ParseContext(this._params);
3314
+ this._parseContext = new ParseContext(this._params, this._parseContext.decorators);
3229
3315
  const ast = this._tryToParse(expression);
3230
3316
  if (this._hasImports(ast)) {
3231
3317
  throw new Error("Cannot use imports on parseString, use parse instead.");
@@ -3308,6 +3394,7 @@ class Grammar {
3308
3394
  const literalNode = statementNode.find(n => n.name === "literal");
3309
3395
  const name = nameNode.value;
3310
3396
  const literal = this._buildLiteral(name, literalNode);
3397
+ this._applyDecorators(statementNode, literal);
3311
3398
  this._parseContext.patternsByName.set(name, literal);
3312
3399
  }
3313
3400
  _buildLiteral(name, node) {
@@ -3330,6 +3417,7 @@ class Grammar {
3330
3417
  const regexNode = statementNode.find(n => n.name === "regex-literal");
3331
3418
  const name = nameNode.value;
3332
3419
  const regex = this._buildRegex(name, regexNode);
3420
+ this._applyDecorators(statementNode, regex);
3333
3421
  this._parseContext.patternsByName.set(name, regex);
3334
3422
  }
3335
3423
  _buildRegex(name, node) {
@@ -3341,6 +3429,7 @@ class Grammar {
3341
3429
  const name = nameNode.value;
3342
3430
  const optionsNode = statementNode.find(n => n.name === "options-literal");
3343
3431
  const options = this._buildOptions(name, optionsNode);
3432
+ this._applyDecorators(statementNode, options);
3344
3433
  this._parseContext.patternsByName.set(name, options);
3345
3434
  }
3346
3435
  _buildOptions(name, node) {
@@ -3419,6 +3508,7 @@ class Grammar {
3419
3508
  const name = nameNode.value;
3420
3509
  const sequenceNode = statementNode.find(n => n.name === "sequence-literal");
3421
3510
  const sequence = this._buildSequence(name, sequenceNode);
3511
+ this._applyDecorators(statementNode, sequence);
3422
3512
  this._parseContext.patternsByName.set(name, sequence);
3423
3513
  }
3424
3514
  _buildSequence(name, node) {
@@ -3441,6 +3531,7 @@ class Grammar {
3441
3531
  const name = nameNode.value;
3442
3532
  const repeatNode = statementNode.find(n => n.name === "repeat-literal");
3443
3533
  const repeat = this._buildRepeat(name, repeatNode);
3534
+ this._applyDecorators(statementNode, repeat);
3444
3535
  this._parseContext.patternsByName.set(name, repeat);
3445
3536
  }
3446
3537
  _buildRepeat(name, repeatNode) {
@@ -3495,6 +3586,7 @@ class Grammar {
3495
3586
  const anonymousNode = node.find(n => n.name === "configurable-anonymous-pattern");
3496
3587
  const isOptional = node.children[1] != null;
3497
3588
  const anonymous = isOptional ? new Optional(name, this._buildPattern(anonymousNode.children[0])) : this._buildPattern(anonymousNode.children[0]);
3589
+ this._applyDecorators(node, anonymous);
3498
3590
  this._parseContext.patternsByName.set(name, anonymous);
3499
3591
  }
3500
3592
  _buildComplexAnonymousPattern(node) {
@@ -3513,7 +3605,8 @@ class Grammar {
3513
3605
  const grammar = new Grammar({
3514
3606
  resolveImport: this._resolveImport,
3515
3607
  originResource: grammarFile.resource,
3516
- params
3608
+ params,
3609
+ decorators: this._parseContext.decorators
3517
3610
  });
3518
3611
  try {
3519
3612
  const patterns = yield grammar.parse(grammarFile.expression);
@@ -3556,6 +3649,46 @@ class Grammar {
3556
3649
  }
3557
3650
  });
3558
3651
  }
3652
+ _applyDecorators(statementNode, pattern) {
3653
+ const decorators = this._parseContext.decorators;
3654
+ const bodyLine = statementNode.parent;
3655
+ if (bodyLine == null) {
3656
+ return;
3657
+ }
3658
+ let prevSibling = bodyLine.previousSibling();
3659
+ let decoratorNodes = [];
3660
+ while (prevSibling != null) {
3661
+ if (prevSibling.find(n => n.name === "assign-statement")) {
3662
+ break;
3663
+ }
3664
+ decoratorNodes.push(prevSibling);
3665
+ prevSibling = prevSibling.previousSibling();
3666
+ }
3667
+ decoratorNodes = decoratorNodes.filter(n => n.find(n => n.name.includes("decorator")) != null);
3668
+ decoratorNodes.forEach((d) => {
3669
+ const nameNode = d.find(n => n.name === "decorator-name");
3670
+ if (nameNode == null || decorators[nameNode.value] == null) {
3671
+ return;
3672
+ }
3673
+ const nameDocorator = d.find(n => n.name === "name-decorator");
3674
+ if (nameDocorator != null) {
3675
+ decorators[nameNode.value](pattern);
3676
+ return;
3677
+ }
3678
+ const methodDecorator = d.find(n => n.name === "method-decorator");
3679
+ if (methodDecorator == null) {
3680
+ return;
3681
+ }
3682
+ methodDecorator.findAll(n => n.name.includes("space")).forEach(n => n.remove());
3683
+ const argsNode = methodDecorator.children[3];
3684
+ if (argsNode == null || argsNode.name === "close-paren") {
3685
+ decorators[nameNode.value](pattern);
3686
+ }
3687
+ else {
3688
+ decorators[nameNode.value](pattern, JSON.parse(argsNode.value));
3689
+ }
3690
+ });
3691
+ }
3559
3692
  _getParams(importStatement) {
3560
3693
  let params = [];
3561
3694
  const paramsStatement = importStatement.find(n => n.name === "with-params-statement");
@@ -3570,7 +3703,8 @@ class Grammar {
3570
3703
  const grammar = new Grammar({
3571
3704
  params: [...importedValues, ...this._parseContext.paramsByName.values()],
3572
3705
  originResource: this._originResource,
3573
- resolveImport: this._resolveImport
3706
+ resolveImport: this._resolveImport,
3707
+ decorators: this._parseContext.decorators
3574
3708
  });
3575
3709
  const patterns = grammar.parseString(expression);
3576
3710
  params = Array.from(Object.values(patterns));
@@ -3600,10 +3734,12 @@ class Grammar {
3600
3734
  // This solves the problem for an alias pointing to a reference.
3601
3735
  if (aliasPattern.type === "reference") {
3602
3736
  const reference = new Reference(name, aliasName);
3737
+ this._applyDecorators(statementNode, reference);
3603
3738
  this._parseContext.patternsByName.set(name, reference);
3604
3739
  }
3605
3740
  else {
3606
3741
  const alias = aliasPattern.clone(name);
3742
+ this._applyDecorators(statementNode, alias);
3607
3743
  this._parseContext.patternsByName.set(name, alias);
3608
3744
  }
3609
3745
  }