clarity-pattern-parser 8.4.10 → 8.4.12
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/TODO.md +17 -3
- package/dist/grammar/patterns/statement.d.ts +2 -2
- package/dist/index.browser.js +46 -16
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +46 -16
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +46 -16
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/grammar/Grammar.test.ts +76 -10
- package/src/grammar/Grammar.ts +49 -12
- package/src/grammar/patterns/import.ts +8 -4
- package/src/grammar/patterns/statement.ts +4 -2
package/TODO.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
* Anonymous patterns within cpat files.
|
|
2
|
+
```
|
|
3
|
+
pattern = ("Name" | "Name") & "Blah" & /foo/
|
|
4
|
+
```
|
|
5
|
+
* Aliasing imports.
|
|
6
|
+
* Send through params
|
|
7
|
+
|
|
8
|
+
* Generate typescript files from cpat
|
|
9
|
+
|
|
1
10
|
* We should make a Block, Segments Pattern. These will be breadth first patterns. It look something like this.
|
|
2
11
|
|
|
3
12
|
```ts
|
|
@@ -5,11 +14,16 @@ new Block(startPattern,contentPattern,endPattern);
|
|
|
5
14
|
```
|
|
6
15
|
|
|
7
16
|
```
|
|
8
|
-
block = [start-pattern
|
|
17
|
+
block = [start-pattern, end-pattern];
|
|
9
18
|
```
|
|
10
19
|
|
|
11
20
|
It will count the startPattern and only be done when its ends equal to end Pattern. It will store the internal pattern needed for its content. This is what is lazily done at some point in the future.
|
|
12
21
|
|
|
13
22
|
```ts
|
|
14
|
-
new Segments(
|
|
15
|
-
|
|
23
|
+
new Segments(segmentPattern);
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
NEVER BE TEMPTED TO PUT LOGIC IN SYNTAX. THIS IS MEANT TO BE DECLARATIVE AND COMPOSABLE. If you want a different behavior from a parse, then compose it.
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const statement:
|
|
1
|
+
import { Or } from "../../patterns/Or";
|
|
2
|
+
export declare const statement: Or;
|
package/dist/index.browser.js
CHANGED
|
@@ -1784,7 +1784,7 @@
|
|
|
1784
1784
|
repeatLiteral,
|
|
1785
1785
|
name$1.clone("alias-literal"),
|
|
1786
1786
|
]);
|
|
1787
|
-
const
|
|
1787
|
+
const assignStatement = new And("assign-statement", [
|
|
1788
1788
|
optionalSpaces$1,
|
|
1789
1789
|
name$1,
|
|
1790
1790
|
optionalSpaces$1,
|
|
@@ -1792,6 +1792,7 @@
|
|
|
1792
1792
|
optionalSpaces$1,
|
|
1793
1793
|
statements
|
|
1794
1794
|
]);
|
|
1795
|
+
const statement = new Or("statement", [assignStatement, name$1.clone("export-name")]);
|
|
1795
1796
|
|
|
1796
1797
|
const bodyLineContent = new Or("body-line-content", [
|
|
1797
1798
|
comment,
|
|
@@ -1804,18 +1805,21 @@
|
|
|
1804
1805
|
]);
|
|
1805
1806
|
const body = new Repeat("body", bodyLine, { divider: newLine$1, min: 0 });
|
|
1806
1807
|
|
|
1808
|
+
const optionalSpaces = allSpaces.clone("optional-spaces", true);
|
|
1809
|
+
const optionalLineSpaces = lineSpaces$1.clone("options-line-spaces", true);
|
|
1807
1810
|
const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
|
|
1808
1811
|
const importKeyword = new Literal("import", "import");
|
|
1809
1812
|
const useParamsKeyword = new Literal("use-params", "use params");
|
|
1813
|
+
const asKeyword = new Literal("as", "as");
|
|
1810
1814
|
const fromKeyword = new Literal("from", "from");
|
|
1811
1815
|
const openBracket = new Literal("open-bracket", "{");
|
|
1812
1816
|
const closeBracket = new Literal("close-bracket", "}");
|
|
1813
1817
|
const name = new Regex("import-name", "[^}\\s,]+");
|
|
1814
|
-
const
|
|
1818
|
+
const importNameAlias = name.clone("import-name-alias");
|
|
1819
|
+
const importAlias = new And("import-alias", [name, lineSpaces$1, asKeyword, lineSpaces$1, importNameAlias]);
|
|
1820
|
+
const importedNames = new Repeat("imported-names", new Or("import-names", [importAlias, name]), { divider: importNameDivider });
|
|
1815
1821
|
const paramName = name.clone("param-name");
|
|
1816
1822
|
const paramNames = new Repeat("param-names", paramName, { divider: importNameDivider });
|
|
1817
|
-
const optionalSpaces = allSpaces.clone("optional-spaces", true);
|
|
1818
|
-
const optionalLineSpaces = lineSpaces$1.clone("options-line-spaces", true);
|
|
1819
1823
|
const resource = literal.clone("resource");
|
|
1820
1824
|
const useParams = new And("import-params", [
|
|
1821
1825
|
useParamsKeyword,
|
|
@@ -1832,7 +1836,7 @@
|
|
|
1832
1836
|
optionalLineSpaces,
|
|
1833
1837
|
openBracket,
|
|
1834
1838
|
optionalSpaces,
|
|
1835
|
-
body,
|
|
1839
|
+
body.clone("with-params-body"),
|
|
1836
1840
|
optionalSpaces,
|
|
1837
1841
|
closeBracket
|
|
1838
1842
|
], true);
|
|
@@ -2261,9 +2265,13 @@
|
|
|
2261
2265
|
return importBlock && importBlock.children.length > 0;
|
|
2262
2266
|
}
|
|
2263
2267
|
_buildPatterns(ast) {
|
|
2264
|
-
ast.
|
|
2268
|
+
const body = ast.find(n => n.name === "body");
|
|
2269
|
+
if (body == null) {
|
|
2270
|
+
return;
|
|
2271
|
+
}
|
|
2272
|
+
body.findAll(n => n.name === "assign-statement" || n.name === "export-name").forEach((n) => {
|
|
2265
2273
|
const typeNode = n.find(n => n.name.includes("literal"));
|
|
2266
|
-
const type = (typeNode === null || typeNode === void 0 ? void 0 : typeNode.name) || "unknown";
|
|
2274
|
+
const type = n.name === "export-name" ? "export-name" : (typeNode === null || typeNode === void 0 ? void 0 : typeNode.name) || "unknown";
|
|
2267
2275
|
switch (type) {
|
|
2268
2276
|
case "literal": {
|
|
2269
2277
|
this._buildLiteral(n);
|
|
@@ -2289,6 +2297,11 @@
|
|
|
2289
2297
|
this._buildAlias(n);
|
|
2290
2298
|
break;
|
|
2291
2299
|
}
|
|
2300
|
+
case "export-name": {
|
|
2301
|
+
const pattern = this._getPattern(n.value);
|
|
2302
|
+
this._parseContext.patternsByName.set(n.value, pattern);
|
|
2303
|
+
break;
|
|
2304
|
+
}
|
|
2292
2305
|
}
|
|
2293
2306
|
});
|
|
2294
2307
|
}
|
|
@@ -2308,16 +2321,33 @@
|
|
|
2308
2321
|
});
|
|
2309
2322
|
try {
|
|
2310
2323
|
const patterns = yield grammar.parse(grammarFile.expression);
|
|
2311
|
-
const
|
|
2312
|
-
|
|
2313
|
-
if (
|
|
2314
|
-
|
|
2324
|
+
const importStatements = importStatement.findAll(n => n.name === "import-name" || n.name === "import-alias");
|
|
2325
|
+
importStatements.forEach((node) => {
|
|
2326
|
+
if (node.name === "import-name") {
|
|
2327
|
+
const importName = node.value;
|
|
2328
|
+
if (parseContext.importedPatternsByName.has(importName)) {
|
|
2329
|
+
throw new Error(`'${importName}' was already used within another import.`);
|
|
2330
|
+
}
|
|
2331
|
+
const pattern = patterns.get(importName);
|
|
2332
|
+
if (pattern == null) {
|
|
2333
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
2334
|
+
}
|
|
2335
|
+
parseContext.importedPatternsByName.set(importName, pattern);
|
|
2315
2336
|
}
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2337
|
+
else {
|
|
2338
|
+
const importNameNode = node.find(n => n.name === "import-name");
|
|
2339
|
+
const importName = importNameNode.value;
|
|
2340
|
+
const aliasNode = node.find(n => n.name === "import-name-alias");
|
|
2341
|
+
const alias = aliasNode.value;
|
|
2342
|
+
if (parseContext.importedPatternsByName.has(alias)) {
|
|
2343
|
+
throw new Error(`'${alias}' was already used within another import.`);
|
|
2344
|
+
}
|
|
2345
|
+
const pattern = patterns.get(importName);
|
|
2346
|
+
if (pattern == null) {
|
|
2347
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
2348
|
+
}
|
|
2349
|
+
parseContext.importedPatternsByName.set(alias, pattern);
|
|
2319
2350
|
}
|
|
2320
|
-
parseContext.importedPatternsByName.set(importName, pattern);
|
|
2321
2351
|
});
|
|
2322
2352
|
}
|
|
2323
2353
|
catch (e) {
|
|
@@ -2330,7 +2360,7 @@
|
|
|
2330
2360
|
let params = [];
|
|
2331
2361
|
const paramsStatement = importStatement.find(n => n.name === "with-params-statement");
|
|
2332
2362
|
if (paramsStatement != null) {
|
|
2333
|
-
const statements = paramsStatement.find(n => n.name === "body");
|
|
2363
|
+
const statements = paramsStatement.find(n => n.name === "with-params-body");
|
|
2334
2364
|
if (statements != null) {
|
|
2335
2365
|
const expression = statements.toString();
|
|
2336
2366
|
const importedValues = Array.from(this
|