clarity-pattern-parser 8.4.9 → 8.4.10
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 +8 -8
- package/dist/grammar/patterns/body.d.ts +2 -0
- package/dist/grammar/patterns/grammar.d.ts +2 -2
- package/dist/grammar/patterns/import.d.ts +2 -2
- package/dist/grammar/patterns/spaces.d.ts +5 -0
- package/dist/index.browser.js +132 -42
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +132 -42
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +132 -42
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/grammar/Grammar.test.ts +38 -5
- package/src/grammar/Grammar.ts +69 -25
- package/src/grammar/patterns/body.ts +19 -0
- package/src/grammar/patterns/grammar.ts +26 -9
- package/src/grammar/patterns/import.ts +44 -7
- package/src/grammar/patterns/spaces.ts +12 -2
- package/src/grammar/patterns/statement.ts +1 -6
- package/src/grammar/spec.md +24 -0
package/dist/index.esm.js
CHANGED
|
@@ -1684,6 +1684,17 @@ class And {
|
|
|
1684
1684
|
}
|
|
1685
1685
|
}
|
|
1686
1686
|
|
|
1687
|
+
const literal = new Regex("literal", "\"(?:\\\\[\"\\\\]|[^\n\"\\\\])*\"");
|
|
1688
|
+
|
|
1689
|
+
const tabs$1 = new Regex("tabs", "\\t+");
|
|
1690
|
+
const spaces$1 = new Regex("spaces", "[ ]+");
|
|
1691
|
+
const newLine$1 = new Regex("new-line", "(\\r?\\n)+");
|
|
1692
|
+
spaces$1.setTokens([" "]);
|
|
1693
|
+
tabs$1.setTokens(["\t"]);
|
|
1694
|
+
newLine$1.setTokens(["\n"]);
|
|
1695
|
+
const lineSpaces$1 = new Repeat("line-spaces", new Or("line-space", [tabs$1, spaces$1]));
|
|
1696
|
+
const allSpaces = new Regex("all-spaces", "\\s+", true);
|
|
1697
|
+
|
|
1687
1698
|
const name$1 = new Regex("name", "[a-zA-Z_-]+[a-zA-Z0-9_-]*");
|
|
1688
1699
|
|
|
1689
1700
|
const optionalNot = new Literal("not", "!", true);
|
|
@@ -1705,9 +1716,6 @@ const orLiteral = new Repeat("or-literal", name$1.clone("pattern-name"), { divid
|
|
|
1705
1716
|
|
|
1706
1717
|
const regexLiteral = new Regex("regex-literal", "/(\\\\/|[^/\\n\\r])*/");
|
|
1707
1718
|
|
|
1708
|
-
const spaces$1 = new Regex("spaces", "[ \\t]+");
|
|
1709
|
-
spaces$1.setTokens([" "]);
|
|
1710
|
-
|
|
1711
1719
|
const patternName = name$1.clone("pattern-name");
|
|
1712
1720
|
const optionalSpaces$2 = spaces$1.clone("optional-spaces", true);
|
|
1713
1721
|
const dividerPattern = name$1.clone("divider-pattern");
|
|
@@ -1760,11 +1768,8 @@ const repeatLiteral = new And("repeat-literal", [
|
|
|
1760
1768
|
new And("optional-trim-divider-section", [spaces$1, trimDivider], true)
|
|
1761
1769
|
]);
|
|
1762
1770
|
|
|
1763
|
-
const literal = new Regex("literal", "\"(?:\\\\[\"\\\\]|[^\n\"\\\\])*\"");
|
|
1764
|
-
|
|
1765
1771
|
const optionalSpaces$1 = spaces$1.clone("optional-spaces", true);
|
|
1766
1772
|
const assignOperator = new Literal("assign-operator", "=");
|
|
1767
|
-
const optionalComment = comment.clone("inline-comment", true);
|
|
1768
1773
|
const statements = new Or("statements", [
|
|
1769
1774
|
literal,
|
|
1770
1775
|
regexLiteral,
|
|
@@ -1779,47 +1784,96 @@ const statement = new And("statement", [
|
|
|
1779
1784
|
optionalSpaces$1,
|
|
1780
1785
|
assignOperator,
|
|
1781
1786
|
optionalSpaces$1,
|
|
1782
|
-
statements
|
|
1783
|
-
optionalSpaces$1,
|
|
1784
|
-
optionalComment,
|
|
1785
|
-
optionalSpaces$1,
|
|
1787
|
+
statements
|
|
1786
1788
|
]);
|
|
1787
1789
|
|
|
1788
|
-
const
|
|
1790
|
+
const bodyLineContent = new Or("body-line-content", [
|
|
1791
|
+
comment,
|
|
1792
|
+
statement
|
|
1793
|
+
]);
|
|
1794
|
+
const bodyLine = new And("body-line", [
|
|
1795
|
+
lineSpaces$1.clone("line-spaces", true),
|
|
1796
|
+
bodyLineContent,
|
|
1797
|
+
lineSpaces$1.clone("line-spaces", true),
|
|
1798
|
+
]);
|
|
1799
|
+
const body = new Repeat("body", bodyLine, { divider: newLine$1, min: 0 });
|
|
1800
|
+
|
|
1789
1801
|
const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
|
|
1790
1802
|
const importKeyword = new Literal("import", "import");
|
|
1803
|
+
const useParamsKeyword = new Literal("use-params", "use params");
|
|
1791
1804
|
const fromKeyword = new Literal("from", "from");
|
|
1792
1805
|
const openBracket = new Literal("open-bracket", "{");
|
|
1793
1806
|
const closeBracket = new Literal("close-bracket", "}");
|
|
1794
1807
|
const name = new Regex("import-name", "[^}\\s,]+");
|
|
1795
1808
|
const importedNames = new Repeat("imported-names", name, { divider: importNameDivider });
|
|
1796
|
-
const
|
|
1797
|
-
const
|
|
1798
|
-
|
|
1809
|
+
const paramName = name.clone("param-name");
|
|
1810
|
+
const paramNames = new Repeat("param-names", paramName, { divider: importNameDivider });
|
|
1811
|
+
const optionalSpaces = allSpaces.clone("optional-spaces", true);
|
|
1812
|
+
const optionalLineSpaces = lineSpaces$1.clone("options-line-spaces", true);
|
|
1813
|
+
const resource = literal.clone("resource");
|
|
1814
|
+
const useParams = new And("import-params", [
|
|
1815
|
+
useParamsKeyword,
|
|
1816
|
+
optionalLineSpaces,
|
|
1817
|
+
openBracket,
|
|
1818
|
+
optionalSpaces,
|
|
1819
|
+
paramNames,
|
|
1799
1820
|
optionalSpaces,
|
|
1821
|
+
closeBracket
|
|
1822
|
+
]);
|
|
1823
|
+
const withParamsKeyword = new Literal("with-params", "with params");
|
|
1824
|
+
const withParamsStatement = new And("with-params-statement", [
|
|
1825
|
+
withParamsKeyword,
|
|
1826
|
+
optionalLineSpaces,
|
|
1827
|
+
openBracket,
|
|
1828
|
+
optionalSpaces,
|
|
1829
|
+
body,
|
|
1830
|
+
optionalSpaces,
|
|
1831
|
+
closeBracket
|
|
1832
|
+
], true);
|
|
1833
|
+
const importFromStatement = new And("import-from", [
|
|
1834
|
+
importKeyword,
|
|
1835
|
+
optionalLineSpaces,
|
|
1800
1836
|
openBracket,
|
|
1801
1837
|
optionalSpaces,
|
|
1802
1838
|
importedNames,
|
|
1803
1839
|
optionalSpaces,
|
|
1804
1840
|
closeBracket,
|
|
1805
|
-
|
|
1841
|
+
optionalLineSpaces,
|
|
1806
1842
|
fromKeyword,
|
|
1807
|
-
|
|
1808
|
-
|
|
1843
|
+
optionalLineSpaces,
|
|
1844
|
+
resource,
|
|
1845
|
+
optionalLineSpaces,
|
|
1846
|
+
withParamsStatement
|
|
1847
|
+
]);
|
|
1848
|
+
const importStatement = new Or("import-statement", [
|
|
1849
|
+
useParams,
|
|
1850
|
+
importFromStatement
|
|
1809
1851
|
]);
|
|
1810
1852
|
|
|
1811
|
-
const
|
|
1853
|
+
const tabs = new Regex("tabs", "\\t+");
|
|
1854
|
+
const spaces = new Regex("spaces", "[ ]+");
|
|
1812
1855
|
const newLine = new Regex("new-line", "(\\r?\\n)+");
|
|
1813
|
-
|
|
1856
|
+
spaces.setTokens([" "]);
|
|
1857
|
+
tabs.setTokens(["\t"]);
|
|
1814
1858
|
newLine.setTokens(["\n"]);
|
|
1815
|
-
const
|
|
1816
|
-
|
|
1817
|
-
whitespace,
|
|
1859
|
+
const lineSpaces = new Repeat("line-spaces", new Or("line-space", [tabs, spaces]));
|
|
1860
|
+
const headLineContent = new Or("head-line-content", [
|
|
1818
1861
|
comment,
|
|
1819
|
-
importStatement
|
|
1820
|
-
|
|
1862
|
+
importStatement
|
|
1863
|
+
]);
|
|
1864
|
+
const headLine = new And("head-line-content", [
|
|
1865
|
+
lineSpaces.clone("line-spaces", true),
|
|
1866
|
+
headLineContent,
|
|
1867
|
+
lineSpaces.clone("line-spaces", true),
|
|
1868
|
+
]);
|
|
1869
|
+
const head = new Repeat("head", headLine, { divider: newLine, min: 0 });
|
|
1870
|
+
const grammar = new And("grammar", [
|
|
1871
|
+
allSpaces,
|
|
1872
|
+
head,
|
|
1873
|
+
allSpaces,
|
|
1874
|
+
body,
|
|
1875
|
+
allSpaces
|
|
1821
1876
|
]);
|
|
1822
|
-
const grammar = new Repeat("grammer", line);
|
|
1823
1877
|
|
|
1824
1878
|
class Not {
|
|
1825
1879
|
get type() {
|
|
@@ -2122,9 +2176,11 @@ function getFurthestOptions(options) {
|
|
|
2122
2176
|
}
|
|
2123
2177
|
|
|
2124
2178
|
class ParseContext {
|
|
2125
|
-
constructor() {
|
|
2179
|
+
constructor(params) {
|
|
2126
2180
|
this.patternsByName = new Map();
|
|
2127
2181
|
this.importedPatternsByName = new Map();
|
|
2182
|
+
this.paramsByName = new Map();
|
|
2183
|
+
params.forEach(p => this.paramsByName.set(p.name, p));
|
|
2128
2184
|
}
|
|
2129
2185
|
}
|
|
2130
2186
|
function defaultImportResolver(_path, _basePath) {
|
|
@@ -2132,9 +2188,10 @@ function defaultImportResolver(_path, _basePath) {
|
|
|
2132
2188
|
}
|
|
2133
2189
|
class Grammar {
|
|
2134
2190
|
constructor(options = {}) {
|
|
2135
|
-
this.
|
|
2191
|
+
this._params = (options === null || options === void 0 ? void 0 : options.params) == null ? [] : options.params;
|
|
2192
|
+
this._originResource = (options === null || options === void 0 ? void 0 : options.originResource) == null ? null : options.originResource;
|
|
2136
2193
|
this._resolveImport = options.resolveImport == null ? defaultImportResolver : options.resolveImport;
|
|
2137
|
-
this._parseContext = new ParseContext();
|
|
2194
|
+
this._parseContext = new ParseContext(this._params);
|
|
2138
2195
|
this._autoComplete = new AutoComplete(grammar, {
|
|
2139
2196
|
greedyPatternNames: ["spaces", "optional-spaces", "whitespace", "new-line"],
|
|
2140
2197
|
customTokens: {
|
|
@@ -2148,13 +2205,17 @@ class Grammar {
|
|
|
2148
2205
|
import(path) {
|
|
2149
2206
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2150
2207
|
const grammarFile = yield this._resolveImport(path, null);
|
|
2151
|
-
const grammar = new Grammar({
|
|
2208
|
+
const grammar = new Grammar({
|
|
2209
|
+
resolveImport: this._resolveImport,
|
|
2210
|
+
originResource: grammarFile.resource,
|
|
2211
|
+
params: this._params
|
|
2212
|
+
});
|
|
2152
2213
|
return grammar.parse(grammarFile.expression);
|
|
2153
2214
|
});
|
|
2154
2215
|
}
|
|
2155
2216
|
parse(expression) {
|
|
2156
2217
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2157
|
-
this._parseContext = new ParseContext();
|
|
2218
|
+
this._parseContext = new ParseContext(this._params);
|
|
2158
2219
|
const ast = this._tryToParse(expression);
|
|
2159
2220
|
yield this._resolveImports(ast);
|
|
2160
2221
|
this._buildPatterns(ast);
|
|
@@ -2162,7 +2223,7 @@ class Grammar {
|
|
|
2162
2223
|
});
|
|
2163
2224
|
}
|
|
2164
2225
|
parseString(expression) {
|
|
2165
|
-
this._parseContext = new ParseContext();
|
|
2226
|
+
this._parseContext = new ParseContext(this._params);
|
|
2166
2227
|
const ast = this._tryToParse(expression);
|
|
2167
2228
|
if (this._hasImports(ast)) {
|
|
2168
2229
|
throw new Error("Cannot use imports on parseString, use parse instead.");
|
|
@@ -2194,7 +2255,7 @@ class Grammar {
|
|
|
2194
2255
|
return importBlock && importBlock.children.length > 0;
|
|
2195
2256
|
}
|
|
2196
2257
|
_buildPatterns(ast) {
|
|
2197
|
-
ast.
|
|
2258
|
+
ast.findAll(n => n.name === "statement").forEach((n) => {
|
|
2198
2259
|
const typeNode = n.find(n => n.name.includes("literal"));
|
|
2199
2260
|
const type = (typeNode === null || typeNode === void 0 ? void 0 : typeNode.name) || "unknown";
|
|
2200
2261
|
switch (type) {
|
|
@@ -2226,15 +2287,19 @@ class Grammar {
|
|
|
2226
2287
|
});
|
|
2227
2288
|
}
|
|
2228
2289
|
_resolveImports(ast) {
|
|
2229
|
-
var _a;
|
|
2230
2290
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2231
2291
|
const parseContext = this._parseContext;
|
|
2232
|
-
const importStatements = ast.findAll(n => n.name === "import-
|
|
2292
|
+
const importStatements = ast.findAll(n => n.name === "import-from");
|
|
2233
2293
|
for (const importStatement of importStatements) {
|
|
2234
|
-
const
|
|
2235
|
-
const
|
|
2236
|
-
const
|
|
2237
|
-
const
|
|
2294
|
+
const resourceNode = importStatement.find(n => n.name === "resource");
|
|
2295
|
+
const params = this._getParams(importStatement);
|
|
2296
|
+
const resource = resourceNode.value.slice(1, -1);
|
|
2297
|
+
const grammarFile = yield this._resolveImport(resource, this._originResource || null);
|
|
2298
|
+
const grammar = new Grammar({
|
|
2299
|
+
resolveImport: this._resolveImport,
|
|
2300
|
+
originResource: grammarFile.resource,
|
|
2301
|
+
params
|
|
2302
|
+
});
|
|
2238
2303
|
try {
|
|
2239
2304
|
const patterns = yield grammar.parse(grammarFile.expression);
|
|
2240
2305
|
const importNames = importStatement.findAll(n => n.name === "import-name").map(n => n.value);
|
|
@@ -2244,17 +2309,39 @@ class Grammar {
|
|
|
2244
2309
|
}
|
|
2245
2310
|
const pattern = patterns.get(importName);
|
|
2246
2311
|
if (pattern == null) {
|
|
2247
|
-
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${
|
|
2312
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
2248
2313
|
}
|
|
2249
2314
|
parseContext.importedPatternsByName.set(importName, pattern);
|
|
2250
2315
|
});
|
|
2251
2316
|
}
|
|
2252
2317
|
catch (e) {
|
|
2253
|
-
throw new Error(`Failed loading expression from: "${
|
|
2318
|
+
throw new Error(`Failed loading expression from: "${resource}". Error details: "${e.message}"`);
|
|
2254
2319
|
}
|
|
2255
2320
|
}
|
|
2256
2321
|
});
|
|
2257
2322
|
}
|
|
2323
|
+
_getParams(importStatement) {
|
|
2324
|
+
let params = [];
|
|
2325
|
+
const paramsStatement = importStatement.find(n => n.name === "with-params-statement");
|
|
2326
|
+
if (paramsStatement != null) {
|
|
2327
|
+
const statements = paramsStatement.find(n => n.name === "body");
|
|
2328
|
+
if (statements != null) {
|
|
2329
|
+
const expression = statements.toString();
|
|
2330
|
+
const importedValues = Array.from(this
|
|
2331
|
+
._parseContext
|
|
2332
|
+
.importedPatternsByName
|
|
2333
|
+
.values());
|
|
2334
|
+
const grammar = new Grammar({
|
|
2335
|
+
params: importedValues,
|
|
2336
|
+
originResource: this._originResource,
|
|
2337
|
+
resolveImport: this._resolveImport
|
|
2338
|
+
});
|
|
2339
|
+
const patterns = grammar.parseString(expression);
|
|
2340
|
+
params = Array.from(patterns.values());
|
|
2341
|
+
}
|
|
2342
|
+
}
|
|
2343
|
+
return params;
|
|
2344
|
+
}
|
|
2258
2345
|
_buildLiteral(statementNode) {
|
|
2259
2346
|
const nameNode = statementNode.find(n => n.name === "name");
|
|
2260
2347
|
const literalNode = statementNode.find(n => n.name === "literal");
|
|
@@ -2285,6 +2372,9 @@ class Grammar {
|
|
|
2285
2372
|
if (pattern == null) {
|
|
2286
2373
|
pattern = this._parseContext.importedPatternsByName.get(name);
|
|
2287
2374
|
}
|
|
2375
|
+
if (pattern == null) {
|
|
2376
|
+
pattern = this._parseContext.paramsByName.get(name);
|
|
2377
|
+
}
|
|
2288
2378
|
if (pattern == null) {
|
|
2289
2379
|
return new Reference(name);
|
|
2290
2380
|
}
|
|
@@ -2376,8 +2466,8 @@ class Grammar {
|
|
|
2376
2466
|
const grammar = new Grammar(options);
|
|
2377
2467
|
return grammar.import(path);
|
|
2378
2468
|
}
|
|
2379
|
-
static parseString(expression) {
|
|
2380
|
-
const grammar = new Grammar();
|
|
2469
|
+
static parseString(expression, options) {
|
|
2470
|
+
const grammar = new Grammar(options);
|
|
2381
2471
|
return grammar.parseString(expression);
|
|
2382
2472
|
}
|
|
2383
2473
|
}
|