grammar-well 1.1.0

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.
Files changed (117) hide show
  1. package/.eslintrc.cjs +14 -0
  2. package/README.md +288 -0
  3. package/bootstrap.ts +35 -0
  4. package/build/compiler/compiler.d.ts +48 -0
  5. package/build/compiler/compiler.js +227 -0
  6. package/build/compiler/compiler.js.map +1 -0
  7. package/build/compiler/generator.d.ts +23 -0
  8. package/build/compiler/generator.js +213 -0
  9. package/build/compiler/generator.js.map +1 -0
  10. package/build/compiler/import-resolver.d.ts +15 -0
  11. package/build/compiler/import-resolver.js +37 -0
  12. package/build/compiler/import-resolver.js.map +1 -0
  13. package/build/compiler/outputs/javascript.d.ts +3 -0
  14. package/build/compiler/outputs/javascript.js +29 -0
  15. package/build/compiler/outputs/javascript.js.map +1 -0
  16. package/build/compiler/outputs/json.d.ts +2 -0
  17. package/build/compiler/outputs/json.js +8 -0
  18. package/build/compiler/outputs/json.js.map +1 -0
  19. package/build/compiler/outputs/typescript.d.ts +2 -0
  20. package/build/compiler/outputs/typescript.js +108 -0
  21. package/build/compiler/outputs/typescript.js.map +1 -0
  22. package/build/grammars/gwell.d.ts +997 -0
  23. package/build/grammars/gwell.js +537 -0
  24. package/build/grammars/gwell.js.map +1 -0
  25. package/build/grammars/json.d.ts +151 -0
  26. package/build/grammars/json.js +112 -0
  27. package/build/grammars/json.js.map +1 -0
  28. package/build/grammars/number.d.ts +239 -0
  29. package/build/grammars/number.js +115 -0
  30. package/build/grammars/number.js.map +1 -0
  31. package/build/grammars/number.json +1 -0
  32. package/build/grammars/string.d.ts +116 -0
  33. package/build/grammars/string.js +50 -0
  34. package/build/grammars/string.js.map +1 -0
  35. package/build/grammars/string.json +1 -0
  36. package/build/grammars/whitespace.d.ts +51 -0
  37. package/build/grammars/whitespace.js +30 -0
  38. package/build/grammars/whitespace.js.map +1 -0
  39. package/build/grammars/whitespace.json +1 -0
  40. package/build/index.d.ts +4 -0
  41. package/build/index.js +21 -0
  42. package/build/index.js.map +1 -0
  43. package/build/lexers/character-lexer.d.ts +27 -0
  44. package/build/lexers/character-lexer.js +71 -0
  45. package/build/lexers/character-lexer.js.map +1 -0
  46. package/build/lexers/stateful-lexer.d.ts +48 -0
  47. package/build/lexers/stateful-lexer.js +309 -0
  48. package/build/lexers/stateful-lexer.js.map +1 -0
  49. package/build/lexers/token-buffer.d.ts +32 -0
  50. package/build/lexers/token-buffer.js +92 -0
  51. package/build/lexers/token-buffer.js.map +1 -0
  52. package/build/parser/algorithms/cyk.d.ts +16 -0
  53. package/build/parser/algorithms/cyk.js +58 -0
  54. package/build/parser/algorithms/cyk.js.map +1 -0
  55. package/build/parser/algorithms/earley.d.ts +48 -0
  56. package/build/parser/algorithms/earley.js +158 -0
  57. package/build/parser/algorithms/earley.js.map +1 -0
  58. package/build/parser/algorithms/lr.d.ts +10 -0
  59. package/build/parser/algorithms/lr.js +34 -0
  60. package/build/parser/algorithms/lr.js.map +1 -0
  61. package/build/parser/parser.d.ts +26 -0
  62. package/build/parser/parser.js +74 -0
  63. package/build/parser/parser.js.map +1 -0
  64. package/build/typings.d.ts +198 -0
  65. package/build/typings.js +3 -0
  66. package/build/typings.js.map +1 -0
  67. package/build/utility/general.d.ts +46 -0
  68. package/build/utility/general.js +112 -0
  69. package/build/utility/general.js.map +1 -0
  70. package/build/utility/lint.d.ts +2 -0
  71. package/build/utility/lint.js +28 -0
  72. package/build/utility/lint.js.map +1 -0
  73. package/build/utility/lr.d.ts +56 -0
  74. package/build/utility/lr.js +131 -0
  75. package/build/utility/lr.js.map +1 -0
  76. package/build/utility/text-format.d.ts +11 -0
  77. package/build/utility/text-format.js +84 -0
  78. package/build/utility/text-format.js.map +1 -0
  79. package/licenses/LICENSE.txt +165 -0
  80. package/licenses/moo.license +29 -0
  81. package/licenses/nearley.license +21 -0
  82. package/package.json +52 -0
  83. package/src/compiler/compiler.ts +239 -0
  84. package/src/compiler/generator.ts +229 -0
  85. package/src/compiler/import-resolver.ts +36 -0
  86. package/src/compiler/outputs/javascript.ts +27 -0
  87. package/src/compiler/outputs/json.ts +5 -0
  88. package/src/compiler/outputs/typescript.ts +105 -0
  89. package/src/grammars/gwell.gwell +278 -0
  90. package/src/grammars/gwell.js +539 -0
  91. package/src/grammars/gwell.json +1 -0
  92. package/src/grammars/json.gwell +75 -0
  93. package/src/grammars/json.js +121 -0
  94. package/src/grammars/json.json +1 -0
  95. package/src/grammars/number.gwell +20 -0
  96. package/src/grammars/number.js +117 -0
  97. package/src/grammars/number.json +1 -0
  98. package/src/grammars/string.gwell +15 -0
  99. package/src/grammars/string.js +52 -0
  100. package/src/grammars/string.json +1 -0
  101. package/src/grammars/whitespace.gwell +6 -0
  102. package/src/grammars/whitespace.js +32 -0
  103. package/src/grammars/whitespace.json +1 -0
  104. package/src/index.ts +4 -0
  105. package/src/lexers/character-lexer.ts +73 -0
  106. package/src/lexers/stateful-lexer.ts +335 -0
  107. package/src/lexers/token-buffer.ts +102 -0
  108. package/src/parser/algorithms/cyk.ts +74 -0
  109. package/src/parser/algorithms/earley.ts +193 -0
  110. package/src/parser/algorithms/lr.ts +37 -0
  111. package/src/parser/parser.ts +77 -0
  112. package/src/typings.ts +221 -0
  113. package/src/utility/general.ts +120 -0
  114. package/src/utility/lint.ts +26 -0
  115. package/src/utility/lr.ts +153 -0
  116. package/src/utility/text-format.ts +84 -0
  117. package/testing.ts +18 -0
@@ -0,0 +1,84 @@
1
+ import { TokenBuffer } from "../lexers/token-buffer";
2
+ import { GrammarRule, GrammarRuleSymbol, LexerToken } from "../typings";
3
+
4
+ export class TextFormatter {
5
+
6
+ static UnexpectedToken(queue: TokenBuffer, expected: (GrammarRule & { index?: number })[]) {
7
+ const token = queue.active;
8
+ const tokenDisplay = TextFormatter.LexerTokenShort(token);
9
+ const lines = [];
10
+ lines.push('Unexpected token: ' + tokenDisplay + ' at line: ' + token.line + ' column: ' + token.column);
11
+ if (expected.length === 0) {
12
+ lines.push('End of input was expected.');
13
+ } else {
14
+ lines.push('Instead, I was expecting to see one of the following:\n');
15
+ for (const ex of expected) {
16
+ const nextSymbol = ex.symbols[ex.index];
17
+ const symbolDisplay = TextFormatter.GrammarRuleSymbol(nextSymbol, false, true);
18
+ lines.push('A ' + symbolDisplay + ' based on:');
19
+ lines.push(TextFormatter.GrammarRule(ex, ex.index));
20
+ }
21
+ lines.push("");
22
+ return lines.join("\n");
23
+ }
24
+ }
25
+
26
+ static LexerTokenShort(token: LexerToken) {
27
+ if (token.type)
28
+ return `[${token.type}] ${JSON.stringify(token.value)}`;
29
+ return `${JSON.stringify(token.value)}`;
30
+ }
31
+
32
+ static LexerTokenError(lexer: TokenBuffer) {
33
+ let i = 0;
34
+ let token;
35
+ let string = lexer.peek(i).value;
36
+ let lines = 0;
37
+ const { line, column, offset } = lexer;
38
+
39
+ // eslint-disable-next-line no-cond-assign
40
+ while (token = lexer.peek(--i)) {
41
+ if (token.value == '\n') {
42
+ lines++;
43
+ string = `${(line + 2 - lines)} ${string}`;
44
+ }
45
+ string = token.value + string;
46
+ if (lines >= 2)
47
+ break;
48
+ }
49
+ string = `${line + 2 - (lines + 1)} ${string}`;
50
+
51
+ const n = string.lastIndexOf('\n');
52
+ const pad = (string.length - n);
53
+ string += '\n' + '^'.padStart(pad - 1);
54
+ if (typeof column != 'undefined' && typeof line != 'undefined')
55
+ return `Syntax error at line ${line + 1} col ${column + 1}:\n\n${string}\n`;
56
+ return `Syntax error at index ${offset}:\n\n${string}\n`;
57
+ }
58
+
59
+ static GrammarRuleSymbol(symbol: GrammarRuleSymbol, short?: boolean, error?: boolean) {
60
+ if (typeof symbol === 'string') {
61
+ return symbol;
62
+ } else if (typeof symbol === 'function') {
63
+ return short ? `<${symbol.toString()}>` : `token matching ${symbol.toString()}`;
64
+ } else {
65
+ if ("literal" in symbol) {
66
+ return JSON.stringify(symbol.literal);
67
+ } else if (symbol instanceof RegExp) {
68
+ return short ? symbol.toString() : `character matching ${symbol.toString()}`;
69
+ } else if ("token" in symbol) {
70
+ return short ? `%${symbol.token}` : `${symbol.token} token`;
71
+ } else if (error) {
72
+ return 'Unknown symbol type: ' + JSON.stringify(symbol);
73
+ }
74
+ }
75
+ }
76
+
77
+ static GrammarRule(rule: GrammarRule, withCursorAt?: number) {
78
+ let symbolSequence = rule.symbols.slice(0, withCursorAt).map(v => TextFormatter.GrammarRuleSymbol(v, true, true)).join(' ');
79
+ if (typeof withCursorAt !== "undefined") {
80
+ symbolSequence += " ● " + rule.symbols.slice(withCursorAt).map(v => TextFormatter.GrammarRuleSymbol(v, true, true)).join(' ');
81
+ }
82
+ return rule.name + " → " + symbolSequence;
83
+ }
84
+ }
package/testing.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { readFileSync, writeFileSync } from 'fs';
2
+ import { AsyncRun, BuildTest } from './tests/system/testbed';
3
+ (async () => {
4
+ const options: any = {};
5
+ const grammar = Read('./tests/samples/grammars/lr.gwell');
6
+ const input = "aabb";
7
+ options.algorithm = 'lr0';
8
+ const execution = await AsyncRun(() => BuildTest(grammar, input, options));
9
+ console.log(execution);
10
+
11
+ })()
12
+
13
+ function Read(path) {
14
+ return readFileSync(path, 'utf-8');
15
+ }
16
+ function Write(path, body) {
17
+ return writeFileSync(path, typeof body == 'string' ? body : JSON.stringify(body, null, 2), 'utf-8');
18
+ }