clarity-pattern-parser 7.0.0 → 7.0.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/package.json
CHANGED
|
@@ -266,6 +266,24 @@ describe("Grammar", () => {
|
|
|
266
266
|
expect(result.ast?.value).toEqual("[1, []]");
|
|
267
267
|
});
|
|
268
268
|
|
|
269
|
+
test("Alias", () => {
|
|
270
|
+
const expression = `
|
|
271
|
+
name = /regex/
|
|
272
|
+
alias = name
|
|
273
|
+
`;
|
|
274
|
+
|
|
275
|
+
const patterns = Grammar.parse(expression);
|
|
276
|
+
|
|
277
|
+
const name = patterns.get("name");
|
|
278
|
+
const expectedName = new Regex("name", "regex");
|
|
279
|
+
|
|
280
|
+
const alias = patterns.get("alias");
|
|
281
|
+
const expectedAlias = new Regex("alias", "regex");
|
|
282
|
+
|
|
283
|
+
expect(name).toEqual(expectedName);
|
|
284
|
+
expect(alias).toEqual(expectedAlias);
|
|
285
|
+
});
|
|
286
|
+
|
|
269
287
|
test("Bad Grammar At Beginning", () => {
|
|
270
288
|
|
|
271
289
|
expect(() => {
|
package/src/grammar/Grammar.ts
CHANGED
|
@@ -95,6 +95,10 @@ export class Grammar {
|
|
|
95
95
|
this._buildRepeat(n)
|
|
96
96
|
break;
|
|
97
97
|
}
|
|
98
|
+
case "alias-literal": {
|
|
99
|
+
this._buildAlias(n)
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
98
102
|
}
|
|
99
103
|
});
|
|
100
104
|
}
|
|
@@ -226,6 +230,17 @@ export class Grammar {
|
|
|
226
230
|
this._parseContext.patternsByName.set(name, repeat);
|
|
227
231
|
}
|
|
228
232
|
|
|
233
|
+
private _buildAlias(statementNode: Node) {
|
|
234
|
+
const nameNode = statementNode.find(n => n.name === "name") as Node;
|
|
235
|
+
const aliasNode = statementNode.find(n => n.name === "alias-literal") as Node;
|
|
236
|
+
const aliasName = aliasNode.value;
|
|
237
|
+
const name = nameNode.value;
|
|
238
|
+
const pattern = this._getPattern(aliasName);
|
|
239
|
+
const alias = pattern.clone(name);
|
|
240
|
+
|
|
241
|
+
this._parseContext.patternsByName.set(name, alias)
|
|
242
|
+
}
|
|
243
|
+
|
|
229
244
|
static parse(expression: string) {
|
|
230
245
|
const grammar = new Grammar();
|
|
231
246
|
return grammar.parse(expression);
|