clarity-pattern-parser 8.4.9 → 8.4.11

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 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,12 @@ new Block(startPattern,contentPattern,endPattern);
5
14
  ```
6
15
 
7
16
  ```
8
- block = [start-pattern content-pattern end-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(pattern, segmentPattern);
23
+ new Segments(segmentPattern);
24
+
15
25
  ```
@@ -1,17 +1,16 @@
1
1
  import { Pattern } from "../patterns/Pattern";
2
- export interface GrammarMeta {
3
- originPath?: string;
4
- }
5
2
  export interface GrammarFile {
6
- path: string;
3
+ resource: string;
7
4
  expression: string;
8
5
  }
9
6
  export interface GrammarOptions {
10
- resolveImport?: (path: string, originPath: string | null) => Promise<GrammarFile>;
11
- meta?: GrammarMeta | null;
7
+ resolveImport?: (resource: string, originResource: string | null) => Promise<GrammarFile>;
8
+ originResource?: string | null;
9
+ params?: Pattern[];
12
10
  }
13
11
  export declare class Grammar {
14
- private _meta;
12
+ private _params;
13
+ private _originResource?;
15
14
  private _resolveImport;
16
15
  private _parseContext;
17
16
  private _autoComplete;
@@ -23,6 +22,7 @@ export declare class Grammar {
23
22
  private _hasImports;
24
23
  private _buildPatterns;
25
24
  private _resolveImports;
25
+ private _getParams;
26
26
  private _buildLiteral;
27
27
  private _buildRegex;
28
28
  private _buildOr;
@@ -32,5 +32,5 @@ export declare class Grammar {
32
32
  private _buildAlias;
33
33
  static parse(expression: string, options?: GrammarOptions): Promise<Map<string, Pattern>>;
34
34
  static import(path: string, options?: GrammarOptions): Promise<Map<string, Pattern>>;
35
- static parseString(expression: string): Map<string, Pattern>;
35
+ static parseString(expression: string, options?: GrammarOptions): Map<string, Pattern>;
36
36
  }
@@ -0,0 +1,2 @@
1
+ import { Repeat } from "../../patterns/Repeat";
2
+ export declare const body: Repeat;
@@ -1,2 +1,2 @@
1
- import { Repeat } from "../../patterns/Repeat";
2
- export declare const grammar: Repeat;
1
+ import { And } from "../../patterns/And";
2
+ export declare const grammar: And;
@@ -1,2 +1,2 @@
1
- import { And } from "../../patterns/And";
2
- export declare const importStatement: And;
1
+ import { Or } from "../../patterns/Or";
2
+ export declare const importStatement: Or;
@@ -1,2 +1,7 @@
1
1
  import { Regex } from "../../patterns/Regex";
2
+ import { Repeat } from "../../patterns/Repeat";
3
+ export declare const tabs: Regex;
2
4
  export declare const spaces: Regex;
5
+ export declare const newLine: Regex;
6
+ export declare const lineSpaces: Repeat;
7
+ export declare const allSpaces: Regex;
@@ -1,2 +1,2 @@
1
- import { And } from "../../patterns/And";
2
- export declare const statement: And;
1
+ import { Or } from "../../patterns/Or";
2
+ export declare const statement: Or;
@@ -1690,6 +1690,17 @@
1690
1690
  }
1691
1691
  }
1692
1692
 
1693
+ const literal = new Regex("literal", "\"(?:\\\\[\"\\\\]|[^\n\"\\\\])*\"");
1694
+
1695
+ const tabs$1 = new Regex("tabs", "\\t+");
1696
+ const spaces$1 = new Regex("spaces", "[ ]+");
1697
+ const newLine$1 = new Regex("new-line", "(\\r?\\n)+");
1698
+ spaces$1.setTokens([" "]);
1699
+ tabs$1.setTokens(["\t"]);
1700
+ newLine$1.setTokens(["\n"]);
1701
+ const lineSpaces$1 = new Repeat("line-spaces", new Or("line-space", [tabs$1, spaces$1]));
1702
+ const allSpaces = new Regex("all-spaces", "\\s+", true);
1703
+
1693
1704
  const name$1 = new Regex("name", "[a-zA-Z_-]+[a-zA-Z0-9_-]*");
1694
1705
 
1695
1706
  const optionalNot = new Literal("not", "!", true);
@@ -1711,9 +1722,6 @@
1711
1722
 
1712
1723
  const regexLiteral = new Regex("regex-literal", "/(\\\\/|[^/\\n\\r])*/");
1713
1724
 
1714
- const spaces$1 = new Regex("spaces", "[ \\t]+");
1715
- spaces$1.setTokens([" "]);
1716
-
1717
1725
  const patternName = name$1.clone("pattern-name");
1718
1726
  const optionalSpaces$2 = spaces$1.clone("optional-spaces", true);
1719
1727
  const dividerPattern = name$1.clone("divider-pattern");
@@ -1766,11 +1774,8 @@
1766
1774
  new And("optional-trim-divider-section", [spaces$1, trimDivider], true)
1767
1775
  ]);
1768
1776
 
1769
- const literal = new Regex("literal", "\"(?:\\\\[\"\\\\]|[^\n\"\\\\])*\"");
1770
-
1771
1777
  const optionalSpaces$1 = spaces$1.clone("optional-spaces", true);
1772
1778
  const assignOperator = new Literal("assign-operator", "=");
1773
- const optionalComment = comment.clone("inline-comment", true);
1774
1779
  const statements = new Or("statements", [
1775
1780
  literal,
1776
1781
  regexLiteral,
@@ -1779,53 +1784,106 @@
1779
1784
  repeatLiteral,
1780
1785
  name$1.clone("alias-literal"),
1781
1786
  ]);
1782
- const statement = new And("statement", [
1787
+ const assignStatement = new And("assign-statement", [
1783
1788
  optionalSpaces$1,
1784
1789
  name$1,
1785
1790
  optionalSpaces$1,
1786
1791
  assignOperator,
1787
1792
  optionalSpaces$1,
1788
- statements,
1789
- optionalSpaces$1,
1790
- optionalComment,
1791
- optionalSpaces$1,
1793
+ statements
1792
1794
  ]);
1795
+ const statement = new Or("statement", [assignStatement, name$1.clone("export-name")]);
1793
1796
 
1794
- const spaces = new Regex("spaces", "\\s+", true);
1797
+ const bodyLineContent = new Or("body-line-content", [
1798
+ comment,
1799
+ statement
1800
+ ]);
1801
+ const bodyLine = new And("body-line", [
1802
+ lineSpaces$1.clone("line-spaces", true),
1803
+ bodyLineContent,
1804
+ lineSpaces$1.clone("line-spaces", true),
1805
+ ]);
1806
+ const body = new Repeat("body", bodyLine, { divider: newLine$1, min: 0 });
1807
+
1808
+ const optionalSpaces = allSpaces.clone("optional-spaces", true);
1809
+ const optionalLineSpaces = lineSpaces$1.clone("options-line-spaces", true);
1795
1810
  const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
1796
1811
  const importKeyword = new Literal("import", "import");
1812
+ const useParamsKeyword = new Literal("use-params", "use params");
1813
+ const asKeyword = new Literal("as", "as");
1797
1814
  const fromKeyword = new Literal("from", "from");
1798
1815
  const openBracket = new Literal("open-bracket", "{");
1799
1816
  const closeBracket = new Literal("close-bracket", "}");
1800
1817
  const name = new Regex("import-name", "[^}\\s,]+");
1801
- const importedNames = new Repeat("imported-names", name, { divider: importNameDivider });
1802
- const optionalSpaces = spaces.clone("optional-spaces", true);
1803
- const importStatement = new And("import-statement", [
1804
- importKeyword,
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 });
1821
+ const paramName = name.clone("param-name");
1822
+ const paramNames = new Repeat("param-names", paramName, { divider: importNameDivider });
1823
+ const resource = literal.clone("resource");
1824
+ const useParams = new And("import-params", [
1825
+ useParamsKeyword,
1826
+ optionalLineSpaces,
1827
+ openBracket,
1828
+ optionalSpaces,
1829
+ paramNames,
1805
1830
  optionalSpaces,
1831
+ closeBracket
1832
+ ]);
1833
+ const withParamsKeyword = new Literal("with-params", "with params");
1834
+ const withParamsStatement = new And("with-params-statement", [
1835
+ withParamsKeyword,
1836
+ optionalLineSpaces,
1837
+ openBracket,
1838
+ optionalSpaces,
1839
+ body.clone("with-params-body"),
1840
+ optionalSpaces,
1841
+ closeBracket
1842
+ ], true);
1843
+ const importFromStatement = new And("import-from", [
1844
+ importKeyword,
1845
+ optionalLineSpaces,
1806
1846
  openBracket,
1807
1847
  optionalSpaces,
1808
1848
  importedNames,
1809
1849
  optionalSpaces,
1810
1850
  closeBracket,
1811
- optionalSpaces,
1851
+ optionalLineSpaces,
1812
1852
  fromKeyword,
1813
- spaces,
1814
- literal.clone("url"),
1853
+ optionalLineSpaces,
1854
+ resource,
1855
+ optionalLineSpaces,
1856
+ withParamsStatement
1857
+ ]);
1858
+ const importStatement = new Or("import-statement", [
1859
+ useParams,
1860
+ importFromStatement
1815
1861
  ]);
1816
1862
 
1817
- const whitespace = new Regex("whitespace", "[ \\t]+((\\r?\\n)+)?");
1863
+ const tabs = new Regex("tabs", "\\t+");
1864
+ const spaces = new Regex("spaces", "[ ]+");
1818
1865
  const newLine = new Regex("new-line", "(\\r?\\n)+");
1819
- whitespace.setTokens([" "]);
1866
+ spaces.setTokens([" "]);
1867
+ tabs.setTokens(["\t"]);
1820
1868
  newLine.setTokens(["\n"]);
1821
- const line = new Or("line", [
1822
- newLine,
1823
- whitespace,
1869
+ const lineSpaces = new Repeat("line-spaces", new Or("line-space", [tabs, spaces]));
1870
+ const headLineContent = new Or("head-line-content", [
1824
1871
  comment,
1825
- importStatement,
1826
- statement
1872
+ importStatement
1873
+ ]);
1874
+ const headLine = new And("head-line-content", [
1875
+ lineSpaces.clone("line-spaces", true),
1876
+ headLineContent,
1877
+ lineSpaces.clone("line-spaces", true),
1878
+ ]);
1879
+ const head = new Repeat("head", headLine, { divider: newLine, min: 0 });
1880
+ const grammar = new And("grammar", [
1881
+ allSpaces,
1882
+ head,
1883
+ allSpaces,
1884
+ body,
1885
+ allSpaces
1827
1886
  ]);
1828
- const grammar = new Repeat("grammer", line);
1829
1887
 
1830
1888
  class Not {
1831
1889
  get type() {
@@ -2128,9 +2186,11 @@
2128
2186
  }
2129
2187
 
2130
2188
  class ParseContext {
2131
- constructor() {
2189
+ constructor(params) {
2132
2190
  this.patternsByName = new Map();
2133
2191
  this.importedPatternsByName = new Map();
2192
+ this.paramsByName = new Map();
2193
+ params.forEach(p => this.paramsByName.set(p.name, p));
2134
2194
  }
2135
2195
  }
2136
2196
  function defaultImportResolver(_path, _basePath) {
@@ -2138,9 +2198,10 @@
2138
2198
  }
2139
2199
  class Grammar {
2140
2200
  constructor(options = {}) {
2141
- this._meta = options.meta == null ? null : options.meta;
2201
+ this._params = (options === null || options === void 0 ? void 0 : options.params) == null ? [] : options.params;
2202
+ this._originResource = (options === null || options === void 0 ? void 0 : options.originResource) == null ? null : options.originResource;
2142
2203
  this._resolveImport = options.resolveImport == null ? defaultImportResolver : options.resolveImport;
2143
- this._parseContext = new ParseContext();
2204
+ this._parseContext = new ParseContext(this._params);
2144
2205
  this._autoComplete = new AutoComplete(grammar, {
2145
2206
  greedyPatternNames: ["spaces", "optional-spaces", "whitespace", "new-line"],
2146
2207
  customTokens: {
@@ -2154,13 +2215,17 @@
2154
2215
  import(path) {
2155
2216
  return __awaiter(this, void 0, void 0, function* () {
2156
2217
  const grammarFile = yield this._resolveImport(path, null);
2157
- const grammar = new Grammar({ resolveImport: this._resolveImport, meta: { originPath: grammarFile.path } });
2218
+ const grammar = new Grammar({
2219
+ resolveImport: this._resolveImport,
2220
+ originResource: grammarFile.resource,
2221
+ params: this._params
2222
+ });
2158
2223
  return grammar.parse(grammarFile.expression);
2159
2224
  });
2160
2225
  }
2161
2226
  parse(expression) {
2162
2227
  return __awaiter(this, void 0, void 0, function* () {
2163
- this._parseContext = new ParseContext();
2228
+ this._parseContext = new ParseContext(this._params);
2164
2229
  const ast = this._tryToParse(expression);
2165
2230
  yield this._resolveImports(ast);
2166
2231
  this._buildPatterns(ast);
@@ -2168,7 +2233,7 @@
2168
2233
  });
2169
2234
  }
2170
2235
  parseString(expression) {
2171
- this._parseContext = new ParseContext();
2236
+ this._parseContext = new ParseContext(this._params);
2172
2237
  const ast = this._tryToParse(expression);
2173
2238
  if (this._hasImports(ast)) {
2174
2239
  throw new Error("Cannot use imports on parseString, use parse instead.");
@@ -2200,9 +2265,13 @@
2200
2265
  return importBlock && importBlock.children.length > 0;
2201
2266
  }
2202
2267
  _buildPatterns(ast) {
2203
- ast.children.forEach((n) => {
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) => {
2204
2273
  const typeNode = n.find(n => n.name.includes("literal"));
2205
- 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";
2206
2275
  switch (type) {
2207
2276
  case "literal": {
2208
2277
  this._buildLiteral(n);
@@ -2228,39 +2297,87 @@
2228
2297
  this._buildAlias(n);
2229
2298
  break;
2230
2299
  }
2300
+ case "export-name": {
2301
+ const pattern = this._getPattern(n.value);
2302
+ this._parseContext.patternsByName.set(n.value, pattern);
2303
+ break;
2304
+ }
2231
2305
  }
2232
2306
  });
2233
2307
  }
2234
2308
  _resolveImports(ast) {
2235
- var _a;
2236
2309
  return __awaiter(this, void 0, void 0, function* () {
2237
2310
  const parseContext = this._parseContext;
2238
- const importStatements = ast.findAll(n => n.name === "import-statement");
2311
+ const importStatements = ast.findAll(n => n.name === "import-from");
2239
2312
  for (const importStatement of importStatements) {
2240
- const urlNode = importStatement.find(n => n.name === "url");
2241
- const url = urlNode.value.slice(1, -1);
2242
- const grammarFile = yield this._resolveImport(url, ((_a = this._meta) === null || _a === void 0 ? void 0 : _a.originPath) || null);
2243
- const grammar = new Grammar({ resolveImport: this._resolveImport, meta: { originPath: grammarFile.path } });
2313
+ const resourceNode = importStatement.find(n => n.name === "resource");
2314
+ const params = this._getParams(importStatement);
2315
+ const resource = resourceNode.value.slice(1, -1);
2316
+ const grammarFile = yield this._resolveImport(resource, this._originResource || null);
2317
+ const grammar = new Grammar({
2318
+ resolveImport: this._resolveImport,
2319
+ originResource: grammarFile.resource,
2320
+ params
2321
+ });
2244
2322
  try {
2245
2323
  const patterns = yield grammar.parse(grammarFile.expression);
2246
- const importNames = importStatement.findAll(n => n.name === "import-name").map(n => n.value);
2247
- importNames.forEach((importName) => {
2248
- if (parseContext.importedPatternsByName.has(importName)) {
2249
- throw new Error(`'${importName}' was already used within another import.`);
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);
2250
2336
  }
2251
- const pattern = patterns.get(importName);
2252
- if (pattern == null) {
2253
- throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${url}.`);
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);
2254
2350
  }
2255
- parseContext.importedPatternsByName.set(importName, pattern);
2256
2351
  });
2257
2352
  }
2258
2353
  catch (e) {
2259
- throw new Error(`Failed loading expression from: "${url}". Error details: "${e.message}"`);
2354
+ throw new Error(`Failed loading expression from: "${resource}". Error details: "${e.message}"`);
2260
2355
  }
2261
2356
  }
2262
2357
  });
2263
2358
  }
2359
+ _getParams(importStatement) {
2360
+ let params = [];
2361
+ const paramsStatement = importStatement.find(n => n.name === "with-params-statement");
2362
+ if (paramsStatement != null) {
2363
+ const statements = paramsStatement.find(n => n.name === "with-params-body");
2364
+ if (statements != null) {
2365
+ const expression = statements.toString();
2366
+ const importedValues = Array.from(this
2367
+ ._parseContext
2368
+ .importedPatternsByName
2369
+ .values());
2370
+ const grammar = new Grammar({
2371
+ params: importedValues,
2372
+ originResource: this._originResource,
2373
+ resolveImport: this._resolveImport
2374
+ });
2375
+ const patterns = grammar.parseString(expression);
2376
+ params = Array.from(patterns.values());
2377
+ }
2378
+ }
2379
+ return params;
2380
+ }
2264
2381
  _buildLiteral(statementNode) {
2265
2382
  const nameNode = statementNode.find(n => n.name === "name");
2266
2383
  const literalNode = statementNode.find(n => n.name === "literal");
@@ -2291,6 +2408,9 @@
2291
2408
  if (pattern == null) {
2292
2409
  pattern = this._parseContext.importedPatternsByName.get(name);
2293
2410
  }
2411
+ if (pattern == null) {
2412
+ pattern = this._parseContext.paramsByName.get(name);
2413
+ }
2294
2414
  if (pattern == null) {
2295
2415
  return new Reference(name);
2296
2416
  }
@@ -2382,8 +2502,8 @@
2382
2502
  const grammar = new Grammar(options);
2383
2503
  return grammar.import(path);
2384
2504
  }
2385
- static parseString(expression) {
2386
- const grammar = new Grammar();
2505
+ static parseString(expression, options) {
2506
+ const grammar = new Grammar(options);
2387
2507
  return grammar.parseString(expression);
2388
2508
  }
2389
2509
  }