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.js
CHANGED
|
@@ -1688,6 +1688,17 @@ class And {
|
|
|
1688
1688
|
}
|
|
1689
1689
|
}
|
|
1690
1690
|
|
|
1691
|
+
const literal = new Regex("literal", "\"(?:\\\\[\"\\\\]|[^\n\"\\\\])*\"");
|
|
1692
|
+
|
|
1693
|
+
const tabs$1 = new Regex("tabs", "\\t+");
|
|
1694
|
+
const spaces$1 = new Regex("spaces", "[ ]+");
|
|
1695
|
+
const newLine$1 = new Regex("new-line", "(\\r?\\n)+");
|
|
1696
|
+
spaces$1.setTokens([" "]);
|
|
1697
|
+
tabs$1.setTokens(["\t"]);
|
|
1698
|
+
newLine$1.setTokens(["\n"]);
|
|
1699
|
+
const lineSpaces$1 = new Repeat("line-spaces", new Or("line-space", [tabs$1, spaces$1]));
|
|
1700
|
+
const allSpaces = new Regex("all-spaces", "\\s+", true);
|
|
1701
|
+
|
|
1691
1702
|
const name$1 = new Regex("name", "[a-zA-Z_-]+[a-zA-Z0-9_-]*");
|
|
1692
1703
|
|
|
1693
1704
|
const optionalNot = new Literal("not", "!", true);
|
|
@@ -1709,9 +1720,6 @@ const orLiteral = new Repeat("or-literal", name$1.clone("pattern-name"), { divid
|
|
|
1709
1720
|
|
|
1710
1721
|
const regexLiteral = new Regex("regex-literal", "/(\\\\/|[^/\\n\\r])*/");
|
|
1711
1722
|
|
|
1712
|
-
const spaces$1 = new Regex("spaces", "[ \\t]+");
|
|
1713
|
-
spaces$1.setTokens([" "]);
|
|
1714
|
-
|
|
1715
1723
|
const patternName = name$1.clone("pattern-name");
|
|
1716
1724
|
const optionalSpaces$2 = spaces$1.clone("optional-spaces", true);
|
|
1717
1725
|
const dividerPattern = name$1.clone("divider-pattern");
|
|
@@ -1764,11 +1772,8 @@ const repeatLiteral = new And("repeat-literal", [
|
|
|
1764
1772
|
new And("optional-trim-divider-section", [spaces$1, trimDivider], true)
|
|
1765
1773
|
]);
|
|
1766
1774
|
|
|
1767
|
-
const literal = new Regex("literal", "\"(?:\\\\[\"\\\\]|[^\n\"\\\\])*\"");
|
|
1768
|
-
|
|
1769
1775
|
const optionalSpaces$1 = spaces$1.clone("optional-spaces", true);
|
|
1770
1776
|
const assignOperator = new Literal("assign-operator", "=");
|
|
1771
|
-
const optionalComment = comment.clone("inline-comment", true);
|
|
1772
1777
|
const statements = new Or("statements", [
|
|
1773
1778
|
literal,
|
|
1774
1779
|
regexLiteral,
|
|
@@ -1783,47 +1788,96 @@ const statement = new And("statement", [
|
|
|
1783
1788
|
optionalSpaces$1,
|
|
1784
1789
|
assignOperator,
|
|
1785
1790
|
optionalSpaces$1,
|
|
1786
|
-
statements
|
|
1787
|
-
optionalSpaces$1,
|
|
1788
|
-
optionalComment,
|
|
1789
|
-
optionalSpaces$1,
|
|
1791
|
+
statements
|
|
1790
1792
|
]);
|
|
1791
1793
|
|
|
1792
|
-
const
|
|
1794
|
+
const bodyLineContent = new Or("body-line-content", [
|
|
1795
|
+
comment,
|
|
1796
|
+
statement
|
|
1797
|
+
]);
|
|
1798
|
+
const bodyLine = new And("body-line", [
|
|
1799
|
+
lineSpaces$1.clone("line-spaces", true),
|
|
1800
|
+
bodyLineContent,
|
|
1801
|
+
lineSpaces$1.clone("line-spaces", true),
|
|
1802
|
+
]);
|
|
1803
|
+
const body = new Repeat("body", bodyLine, { divider: newLine$1, min: 0 });
|
|
1804
|
+
|
|
1793
1805
|
const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
|
|
1794
1806
|
const importKeyword = new Literal("import", "import");
|
|
1807
|
+
const useParamsKeyword = new Literal("use-params", "use params");
|
|
1795
1808
|
const fromKeyword = new Literal("from", "from");
|
|
1796
1809
|
const openBracket = new Literal("open-bracket", "{");
|
|
1797
1810
|
const closeBracket = new Literal("close-bracket", "}");
|
|
1798
1811
|
const name = new Regex("import-name", "[^}\\s,]+");
|
|
1799
1812
|
const importedNames = new Repeat("imported-names", name, { divider: importNameDivider });
|
|
1800
|
-
const
|
|
1801
|
-
const
|
|
1802
|
-
|
|
1813
|
+
const paramName = name.clone("param-name");
|
|
1814
|
+
const paramNames = new Repeat("param-names", paramName, { divider: importNameDivider });
|
|
1815
|
+
const optionalSpaces = allSpaces.clone("optional-spaces", true);
|
|
1816
|
+
const optionalLineSpaces = lineSpaces$1.clone("options-line-spaces", true);
|
|
1817
|
+
const resource = literal.clone("resource");
|
|
1818
|
+
const useParams = new And("import-params", [
|
|
1819
|
+
useParamsKeyword,
|
|
1820
|
+
optionalLineSpaces,
|
|
1821
|
+
openBracket,
|
|
1822
|
+
optionalSpaces,
|
|
1823
|
+
paramNames,
|
|
1803
1824
|
optionalSpaces,
|
|
1825
|
+
closeBracket
|
|
1826
|
+
]);
|
|
1827
|
+
const withParamsKeyword = new Literal("with-params", "with params");
|
|
1828
|
+
const withParamsStatement = new And("with-params-statement", [
|
|
1829
|
+
withParamsKeyword,
|
|
1830
|
+
optionalLineSpaces,
|
|
1831
|
+
openBracket,
|
|
1832
|
+
optionalSpaces,
|
|
1833
|
+
body,
|
|
1834
|
+
optionalSpaces,
|
|
1835
|
+
closeBracket
|
|
1836
|
+
], true);
|
|
1837
|
+
const importFromStatement = new And("import-from", [
|
|
1838
|
+
importKeyword,
|
|
1839
|
+
optionalLineSpaces,
|
|
1804
1840
|
openBracket,
|
|
1805
1841
|
optionalSpaces,
|
|
1806
1842
|
importedNames,
|
|
1807
1843
|
optionalSpaces,
|
|
1808
1844
|
closeBracket,
|
|
1809
|
-
|
|
1845
|
+
optionalLineSpaces,
|
|
1810
1846
|
fromKeyword,
|
|
1811
|
-
|
|
1812
|
-
|
|
1847
|
+
optionalLineSpaces,
|
|
1848
|
+
resource,
|
|
1849
|
+
optionalLineSpaces,
|
|
1850
|
+
withParamsStatement
|
|
1851
|
+
]);
|
|
1852
|
+
const importStatement = new Or("import-statement", [
|
|
1853
|
+
useParams,
|
|
1854
|
+
importFromStatement
|
|
1813
1855
|
]);
|
|
1814
1856
|
|
|
1815
|
-
const
|
|
1857
|
+
const tabs = new Regex("tabs", "\\t+");
|
|
1858
|
+
const spaces = new Regex("spaces", "[ ]+");
|
|
1816
1859
|
const newLine = new Regex("new-line", "(\\r?\\n)+");
|
|
1817
|
-
|
|
1860
|
+
spaces.setTokens([" "]);
|
|
1861
|
+
tabs.setTokens(["\t"]);
|
|
1818
1862
|
newLine.setTokens(["\n"]);
|
|
1819
|
-
const
|
|
1820
|
-
|
|
1821
|
-
whitespace,
|
|
1863
|
+
const lineSpaces = new Repeat("line-spaces", new Or("line-space", [tabs, spaces]));
|
|
1864
|
+
const headLineContent = new Or("head-line-content", [
|
|
1822
1865
|
comment,
|
|
1823
|
-
importStatement
|
|
1824
|
-
|
|
1866
|
+
importStatement
|
|
1867
|
+
]);
|
|
1868
|
+
const headLine = new And("head-line-content", [
|
|
1869
|
+
lineSpaces.clone("line-spaces", true),
|
|
1870
|
+
headLineContent,
|
|
1871
|
+
lineSpaces.clone("line-spaces", true),
|
|
1872
|
+
]);
|
|
1873
|
+
const head = new Repeat("head", headLine, { divider: newLine, min: 0 });
|
|
1874
|
+
const grammar = new And("grammar", [
|
|
1875
|
+
allSpaces,
|
|
1876
|
+
head,
|
|
1877
|
+
allSpaces,
|
|
1878
|
+
body,
|
|
1879
|
+
allSpaces
|
|
1825
1880
|
]);
|
|
1826
|
-
const grammar = new Repeat("grammer", line);
|
|
1827
1881
|
|
|
1828
1882
|
class Not {
|
|
1829
1883
|
get type() {
|
|
@@ -2126,9 +2180,11 @@ function getFurthestOptions(options) {
|
|
|
2126
2180
|
}
|
|
2127
2181
|
|
|
2128
2182
|
class ParseContext {
|
|
2129
|
-
constructor() {
|
|
2183
|
+
constructor(params) {
|
|
2130
2184
|
this.patternsByName = new Map();
|
|
2131
2185
|
this.importedPatternsByName = new Map();
|
|
2186
|
+
this.paramsByName = new Map();
|
|
2187
|
+
params.forEach(p => this.paramsByName.set(p.name, p));
|
|
2132
2188
|
}
|
|
2133
2189
|
}
|
|
2134
2190
|
function defaultImportResolver(_path, _basePath) {
|
|
@@ -2136,9 +2192,10 @@ function defaultImportResolver(_path, _basePath) {
|
|
|
2136
2192
|
}
|
|
2137
2193
|
class Grammar {
|
|
2138
2194
|
constructor(options = {}) {
|
|
2139
|
-
this.
|
|
2195
|
+
this._params = (options === null || options === void 0 ? void 0 : options.params) == null ? [] : options.params;
|
|
2196
|
+
this._originResource = (options === null || options === void 0 ? void 0 : options.originResource) == null ? null : options.originResource;
|
|
2140
2197
|
this._resolveImport = options.resolveImport == null ? defaultImportResolver : options.resolveImport;
|
|
2141
|
-
this._parseContext = new ParseContext();
|
|
2198
|
+
this._parseContext = new ParseContext(this._params);
|
|
2142
2199
|
this._autoComplete = new AutoComplete(grammar, {
|
|
2143
2200
|
greedyPatternNames: ["spaces", "optional-spaces", "whitespace", "new-line"],
|
|
2144
2201
|
customTokens: {
|
|
@@ -2152,13 +2209,17 @@ class Grammar {
|
|
|
2152
2209
|
import(path) {
|
|
2153
2210
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2154
2211
|
const grammarFile = yield this._resolveImport(path, null);
|
|
2155
|
-
const grammar = new Grammar({
|
|
2212
|
+
const grammar = new Grammar({
|
|
2213
|
+
resolveImport: this._resolveImport,
|
|
2214
|
+
originResource: grammarFile.resource,
|
|
2215
|
+
params: this._params
|
|
2216
|
+
});
|
|
2156
2217
|
return grammar.parse(grammarFile.expression);
|
|
2157
2218
|
});
|
|
2158
2219
|
}
|
|
2159
2220
|
parse(expression) {
|
|
2160
2221
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2161
|
-
this._parseContext = new ParseContext();
|
|
2222
|
+
this._parseContext = new ParseContext(this._params);
|
|
2162
2223
|
const ast = this._tryToParse(expression);
|
|
2163
2224
|
yield this._resolveImports(ast);
|
|
2164
2225
|
this._buildPatterns(ast);
|
|
@@ -2166,7 +2227,7 @@ class Grammar {
|
|
|
2166
2227
|
});
|
|
2167
2228
|
}
|
|
2168
2229
|
parseString(expression) {
|
|
2169
|
-
this._parseContext = new ParseContext();
|
|
2230
|
+
this._parseContext = new ParseContext(this._params);
|
|
2170
2231
|
const ast = this._tryToParse(expression);
|
|
2171
2232
|
if (this._hasImports(ast)) {
|
|
2172
2233
|
throw new Error("Cannot use imports on parseString, use parse instead.");
|
|
@@ -2198,7 +2259,7 @@ class Grammar {
|
|
|
2198
2259
|
return importBlock && importBlock.children.length > 0;
|
|
2199
2260
|
}
|
|
2200
2261
|
_buildPatterns(ast) {
|
|
2201
|
-
ast.
|
|
2262
|
+
ast.findAll(n => n.name === "statement").forEach((n) => {
|
|
2202
2263
|
const typeNode = n.find(n => n.name.includes("literal"));
|
|
2203
2264
|
const type = (typeNode === null || typeNode === void 0 ? void 0 : typeNode.name) || "unknown";
|
|
2204
2265
|
switch (type) {
|
|
@@ -2230,15 +2291,19 @@ class Grammar {
|
|
|
2230
2291
|
});
|
|
2231
2292
|
}
|
|
2232
2293
|
_resolveImports(ast) {
|
|
2233
|
-
var _a;
|
|
2234
2294
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2235
2295
|
const parseContext = this._parseContext;
|
|
2236
|
-
const importStatements = ast.findAll(n => n.name === "import-
|
|
2296
|
+
const importStatements = ast.findAll(n => n.name === "import-from");
|
|
2237
2297
|
for (const importStatement of importStatements) {
|
|
2238
|
-
const
|
|
2239
|
-
const
|
|
2240
|
-
const
|
|
2241
|
-
const
|
|
2298
|
+
const resourceNode = importStatement.find(n => n.name === "resource");
|
|
2299
|
+
const params = this._getParams(importStatement);
|
|
2300
|
+
const resource = resourceNode.value.slice(1, -1);
|
|
2301
|
+
const grammarFile = yield this._resolveImport(resource, this._originResource || null);
|
|
2302
|
+
const grammar = new Grammar({
|
|
2303
|
+
resolveImport: this._resolveImport,
|
|
2304
|
+
originResource: grammarFile.resource,
|
|
2305
|
+
params
|
|
2306
|
+
});
|
|
2242
2307
|
try {
|
|
2243
2308
|
const patterns = yield grammar.parse(grammarFile.expression);
|
|
2244
2309
|
const importNames = importStatement.findAll(n => n.name === "import-name").map(n => n.value);
|
|
@@ -2248,17 +2313,39 @@ class Grammar {
|
|
|
2248
2313
|
}
|
|
2249
2314
|
const pattern = patterns.get(importName);
|
|
2250
2315
|
if (pattern == null) {
|
|
2251
|
-
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${
|
|
2316
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
2252
2317
|
}
|
|
2253
2318
|
parseContext.importedPatternsByName.set(importName, pattern);
|
|
2254
2319
|
});
|
|
2255
2320
|
}
|
|
2256
2321
|
catch (e) {
|
|
2257
|
-
throw new Error(`Failed loading expression from: "${
|
|
2322
|
+
throw new Error(`Failed loading expression from: "${resource}". Error details: "${e.message}"`);
|
|
2258
2323
|
}
|
|
2259
2324
|
}
|
|
2260
2325
|
});
|
|
2261
2326
|
}
|
|
2327
|
+
_getParams(importStatement) {
|
|
2328
|
+
let params = [];
|
|
2329
|
+
const paramsStatement = importStatement.find(n => n.name === "with-params-statement");
|
|
2330
|
+
if (paramsStatement != null) {
|
|
2331
|
+
const statements = paramsStatement.find(n => n.name === "body");
|
|
2332
|
+
if (statements != null) {
|
|
2333
|
+
const expression = statements.toString();
|
|
2334
|
+
const importedValues = Array.from(this
|
|
2335
|
+
._parseContext
|
|
2336
|
+
.importedPatternsByName
|
|
2337
|
+
.values());
|
|
2338
|
+
const grammar = new Grammar({
|
|
2339
|
+
params: importedValues,
|
|
2340
|
+
originResource: this._originResource,
|
|
2341
|
+
resolveImport: this._resolveImport
|
|
2342
|
+
});
|
|
2343
|
+
const patterns = grammar.parseString(expression);
|
|
2344
|
+
params = Array.from(patterns.values());
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
return params;
|
|
2348
|
+
}
|
|
2262
2349
|
_buildLiteral(statementNode) {
|
|
2263
2350
|
const nameNode = statementNode.find(n => n.name === "name");
|
|
2264
2351
|
const literalNode = statementNode.find(n => n.name === "literal");
|
|
@@ -2289,6 +2376,9 @@ class Grammar {
|
|
|
2289
2376
|
if (pattern == null) {
|
|
2290
2377
|
pattern = this._parseContext.importedPatternsByName.get(name);
|
|
2291
2378
|
}
|
|
2379
|
+
if (pattern == null) {
|
|
2380
|
+
pattern = this._parseContext.paramsByName.get(name);
|
|
2381
|
+
}
|
|
2292
2382
|
if (pattern == null) {
|
|
2293
2383
|
return new Reference(name);
|
|
2294
2384
|
}
|
|
@@ -2380,8 +2470,8 @@ class Grammar {
|
|
|
2380
2470
|
const grammar = new Grammar(options);
|
|
2381
2471
|
return grammar.import(path);
|
|
2382
2472
|
}
|
|
2383
|
-
static parseString(expression) {
|
|
2384
|
-
const grammar = new Grammar();
|
|
2473
|
+
static parseString(expression, options) {
|
|
2474
|
+
const grammar = new Grammar(options);
|
|
2385
2475
|
return grammar.parseString(expression);
|
|
2386
2476
|
}
|
|
2387
2477
|
}
|