grammar-well 1.3.3 → 2.0.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 (261) hide show
  1. package/README.md +76 -560
  2. package/build/generator/artifacts/basic.d.ts +7 -0
  3. package/build/generator/artifacts/basic.js +24 -0
  4. package/build/generator/artifacts/basic.js.map +1 -0
  5. package/build/generator/artifacts/lexer.d.ts +13 -0
  6. package/build/generator/artifacts/lexer.js +204 -0
  7. package/build/generator/artifacts/lexer.js.map +1 -0
  8. package/build/{compiler/generator → generator}/artifacts/lr.d.ts +14 -14
  9. package/build/{compiler/generator → generator}/artifacts/lr.js +18 -22
  10. package/build/generator/artifacts/lr.js.map +1 -0
  11. package/build/generator/builtin/json.d.ts +183 -0
  12. package/build/generator/builtin/json.js +124 -0
  13. package/build/generator/builtin/json.js.map +1 -0
  14. package/build/generator/builtin/registry.json +1 -0
  15. package/build/generator/builtin/whitespace.d.ts +61 -0
  16. package/build/generator/builtin/whitespace.js +37 -0
  17. package/build/generator/builtin/whitespace.js.map +1 -0
  18. package/build/generator/generator.d.ts +38 -0
  19. package/build/generator/generator.js +368 -0
  20. package/build/generator/generator.js.map +1 -0
  21. package/build/generator/grammars/index.d.ts +2 -0
  22. package/build/generator/grammars/index.js +3 -0
  23. package/build/generator/grammars/index.js.map +1 -0
  24. package/build/generator/grammars/v1.d.ts +1190 -0
  25. package/build/generator/grammars/v1.js +614 -0
  26. package/build/generator/grammars/v1.js.map +1 -0
  27. package/build/generator/grammars/v2.d.ts +1367 -0
  28. package/build/generator/grammars/v2.js +695 -0
  29. package/build/generator/grammars/v2.js.map +1 -0
  30. package/build/generator/import-resolvers/auto.d.ts +2 -0
  31. package/build/generator/import-resolvers/auto.js +11 -0
  32. package/build/generator/import-resolvers/auto.js.map +1 -0
  33. package/build/generator/import-resolvers/browser.d.ts +7 -0
  34. package/build/generator/import-resolvers/browser.js +13 -0
  35. package/build/generator/import-resolvers/browser.js.map +1 -0
  36. package/build/generator/import-resolvers/default.d.ts +2 -0
  37. package/build/generator/import-resolvers/default.js +11 -0
  38. package/build/generator/import-resolvers/default.js.map +1 -0
  39. package/build/generator/import-resolvers/filesystem.d.ts +7 -0
  40. package/build/generator/import-resolvers/filesystem.js +15 -0
  41. package/build/generator/import-resolvers/filesystem.js.map +1 -0
  42. package/build/generator/index.d.ts +3 -0
  43. package/build/generator/index.js +4 -0
  44. package/build/generator/index.js.map +1 -0
  45. package/build/generator/state.d.ts +28 -0
  46. package/build/generator/state.js +73 -0
  47. package/build/generator/state.js.map +1 -0
  48. package/build/generator/stringify/common.d.ts +22 -0
  49. package/build/generator/stringify/common.js +83 -0
  50. package/build/generator/stringify/common.js.map +1 -0
  51. package/build/generator/stringify/exports/javascript.d.ts +3 -0
  52. package/build/generator/stringify/exports/javascript.js +26 -0
  53. package/build/generator/stringify/exports/javascript.js.map +1 -0
  54. package/build/generator/stringify/exports/json.d.ts +2 -0
  55. package/build/generator/stringify/exports/json.js +4 -0
  56. package/build/generator/stringify/exports/json.js.map +1 -0
  57. package/build/generator/stringify/exports/registry.d.ts +20 -0
  58. package/build/generator/stringify/exports/registry.js +17 -0
  59. package/build/generator/stringify/exports/registry.js.map +1 -0
  60. package/build/generator/stringify/exports/typescript.d.ts +2 -0
  61. package/build/generator/stringify/exports/typescript.js +16 -0
  62. package/build/generator/stringify/exports/typescript.js.map +1 -0
  63. package/build/generator/stringify/grammar/v2.d.ts +20 -0
  64. package/build/generator/stringify/grammar/v2.js +211 -0
  65. package/build/generator/stringify/grammar/v2.js.map +1 -0
  66. package/build/generator/stringify/javascript.d.ts +13 -0
  67. package/build/generator/stringify/javascript.js +83 -0
  68. package/build/generator/stringify/javascript.js.map +1 -0
  69. package/build/index.d.ts +7 -6
  70. package/build/index.js +7 -24
  71. package/build/index.js.map +1 -1
  72. package/build/lexers/character-lexer.d.ts +2 -2
  73. package/build/lexers/character-lexer.js +1 -5
  74. package/build/lexers/character-lexer.js.map +1 -1
  75. package/build/lexers/stateful-lexer.d.ts +12 -17
  76. package/build/lexers/stateful-lexer.js +38 -186
  77. package/build/lexers/stateful-lexer.js.map +1 -1
  78. package/build/lexers/token-buffer.d.ts +8 -8
  79. package/build/lexers/token-buffer.js +1 -5
  80. package/build/lexers/token-buffer.js.map +1 -1
  81. package/build/parser/algorithms/cyk.d.ts +6 -6
  82. package/build/parser/algorithms/cyk.js +10 -13
  83. package/build/parser/algorithms/cyk.js.map +1 -1
  84. package/build/parser/algorithms/earley.d.ts +7 -7
  85. package/build/parser/algorithms/earley.js +7 -11
  86. package/build/parser/algorithms/earley.js.map +1 -1
  87. package/build/parser/algorithms/lrk/algorithm.d.ts +3 -3
  88. package/build/parser/algorithms/lrk/algorithm.js +10 -13
  89. package/build/parser/algorithms/lrk/algorithm.js.map +1 -1
  90. package/build/parser/algorithms/lrk/bimap.js +1 -5
  91. package/build/parser/algorithms/lrk/bimap.js.map +1 -1
  92. package/build/parser/algorithms/lrk/canonical-collection.d.ts +7 -7
  93. package/build/parser/algorithms/lrk/canonical-collection.js +11 -15
  94. package/build/parser/algorithms/lrk/canonical-collection.js.map +1 -1
  95. package/build/parser/algorithms/lrk/closure.d.ts +3 -3
  96. package/build/parser/algorithms/lrk/closure.js +3 -7
  97. package/build/parser/algorithms/lrk/closure.js.map +1 -1
  98. package/build/parser/algorithms/lrk/stack.d.ts +6 -6
  99. package/build/parser/algorithms/lrk/stack.js +1 -5
  100. package/build/parser/algorithms/lrk/stack.js.map +1 -1
  101. package/build/parser/algorithms/lrk/state.d.ts +5 -5
  102. package/build/parser/algorithms/lrk/state.js +1 -2
  103. package/build/parser/parse.d.ts +10 -0
  104. package/build/parser/parse.js +34 -0
  105. package/build/parser/parse.js.map +1 -0
  106. package/build/parser/parser.d.ts +6 -17
  107. package/build/parser/parser.js +26 -42
  108. package/build/parser/parser.js.map +1 -1
  109. package/build/tsconfig.tsbuildinfo +1 -0
  110. package/build/typings/ast.d.ts +134 -0
  111. package/build/typings/ast.js +3 -0
  112. package/build/typings/ast.js.map +1 -0
  113. package/build/typings/common.d.ts +4 -0
  114. package/build/typings/common.js +2 -0
  115. package/build/typings/common.js.map +1 -0
  116. package/build/typings/generator.d.ts +58 -0
  117. package/build/typings/generator.js +3 -0
  118. package/build/typings/generator.js.map +1 -0
  119. package/build/typings/index.d.ts +43 -0
  120. package/build/typings/index.js +5 -0
  121. package/build/typings/index.js.map +1 -0
  122. package/build/typings/runtime.d.ts +70 -0
  123. package/build/typings/runtime.js +2 -0
  124. package/build/typings/runtime.js.map +1 -0
  125. package/build/utility/format.d.ts +1 -0
  126. package/build/utility/format.js +12 -0
  127. package/build/utility/format.js.map +1 -0
  128. package/build/utility/formatter.d.ts +1 -0
  129. package/build/utility/formatter.js +12 -0
  130. package/build/utility/formatter.js.map +1 -0
  131. package/build/utility/general.d.ts +1 -1
  132. package/build/utility/general.js +5 -13
  133. package/build/utility/general.js.map +1 -1
  134. package/build/utility/index.d.ts +4 -1
  135. package/build/utility/index.js +4 -17
  136. package/build/utility/index.js.map +1 -1
  137. package/build/utility/lint.d.ts +2 -2
  138. package/build/utility/lint.js +2 -6
  139. package/build/utility/lint.js.map +1 -1
  140. package/build/utility/migrate.d.ts +1 -0
  141. package/build/utility/migrate.js +10 -0
  142. package/build/utility/migrate.js.map +1 -0
  143. package/build/utility/monarch.d.ts +2 -2
  144. package/build/utility/monarch.js +33 -38
  145. package/build/utility/monarch.js.map +1 -1
  146. package/build/utility/parsing.d.ts +6 -0
  147. package/build/utility/parsing.js +26 -0
  148. package/build/utility/parsing.js.map +1 -0
  149. package/build/utility/text-format.d.ts +6 -6
  150. package/build/utility/text-format.js +2 -6
  151. package/build/utility/text-format.js.map +1 -1
  152. package/package.json +17 -24
  153. package/src/generator/artifacts/basic.ts +26 -0
  154. package/src/generator/artifacts/lexer.ts +228 -0
  155. package/src/{compiler/generator → generator}/artifacts/lr.ts +25 -24
  156. package/src/generator/builtin/character.well +7 -0
  157. package/src/generator/builtin/json.well +85 -0
  158. package/src/generator/builtin/number.well +21 -0
  159. package/src/generator/builtin/registry.json +1 -0
  160. package/src/generator/builtin/string.well +54 -0
  161. package/src/generator/builtin/whitespace.well +16 -0
  162. package/src/generator/generator.ts +401 -0
  163. package/src/generator/grammars/index.ts +2 -0
  164. package/src/generator/grammars/v1.ts +620 -0
  165. package/src/generator/grammars/v1.well +422 -0
  166. package/src/generator/grammars/v2.ts +701 -0
  167. package/src/generator/grammars/v2.well +413 -0
  168. package/src/generator/import-resolvers/auto.ts +12 -0
  169. package/src/generator/import-resolvers/browser.ts +13 -0
  170. package/src/generator/import-resolvers/filesystem.ts +18 -0
  171. package/src/generator/index.ts +3 -0
  172. package/src/generator/state.ts +89 -0
  173. package/src/generator/stringify/common.ts +90 -0
  174. package/src/generator/stringify/exports/javascript.ts +29 -0
  175. package/src/generator/stringify/exports/json.ts +5 -0
  176. package/src/generator/stringify/exports/registry.ts +20 -0
  177. package/src/generator/stringify/exports/typescript.ts +17 -0
  178. package/src/generator/stringify/grammar/v2.ts +223 -0
  179. package/src/generator/stringify/javascript.ts +94 -0
  180. package/src/index.ts +7 -6
  181. package/src/lexers/character-lexer.ts +2 -2
  182. package/src/lexers/stateful-lexer.ts +46 -203
  183. package/src/lexers/token-buffer.ts +3 -3
  184. package/src/parser/algorithms/cyk.ts +13 -12
  185. package/src/parser/algorithms/earley.ts +10 -10
  186. package/src/parser/algorithms/lrk/algorithm.ts +8 -7
  187. package/src/parser/algorithms/lrk/canonical-collection.ts +14 -14
  188. package/src/parser/algorithms/lrk/closure.ts +6 -6
  189. package/src/parser/algorithms/lrk/stack.ts +6 -6
  190. package/src/parser/algorithms/lrk/state.ts +5 -5
  191. package/src/parser/parse.ts +45 -0
  192. package/src/typings/ast.ts +148 -0
  193. package/src/typings/common.ts +2 -0
  194. package/src/typings/generator.ts +62 -0
  195. package/src/typings/index.ts +38 -0
  196. package/src/typings/runtime.ts +82 -0
  197. package/src/utility/format.ts +12 -0
  198. package/src/utility/general.ts +1 -2
  199. package/src/utility/index.ts +4 -1
  200. package/src/utility/lint.ts +5 -5
  201. package/src/utility/monarch.ts +34 -34
  202. package/src/utility/parsing.ts +30 -0
  203. package/src/utility/text-format.ts +7 -7
  204. package/.eslintrc.cjs +0 -14
  205. package/bootstrap.ts +0 -45
  206. package/build/compiler/builtin.json +0 -1
  207. package/build/compiler/compiler.d.ts +0 -50
  208. package/build/compiler/compiler.js +0 -249
  209. package/build/compiler/compiler.js.map +0 -1
  210. package/build/compiler/generator/artifacts/lr.js.map +0 -1
  211. package/build/compiler/generator/artifacts/standard.d.ts +0 -7
  212. package/build/compiler/generator/artifacts/standard.js +0 -28
  213. package/build/compiler/generator/artifacts/standard.js.map +0 -1
  214. package/build/compiler/generator/generator.d.ts +0 -24
  215. package/build/compiler/generator/generator.js +0 -217
  216. package/build/compiler/generator/generator.js.map +0 -1
  217. package/build/compiler/gwell.d.ts +0 -1112
  218. package/build/compiler/gwell.js +0 -576
  219. package/build/compiler/gwell.js.map +0 -1
  220. package/build/compiler/import-resolver.d.ts +0 -15
  221. package/build/compiler/import-resolver.js +0 -37
  222. package/build/compiler/import-resolver.js.map +0 -1
  223. package/build/compiler/index.d.ts +0 -2
  224. package/build/compiler/index.js +0 -19
  225. package/build/compiler/index.js.map +0 -1
  226. package/build/compiler/outputs/javascript.d.ts +0 -3
  227. package/build/compiler/outputs/javascript.js +0 -29
  228. package/build/compiler/outputs/javascript.js.map +0 -1
  229. package/build/compiler/outputs/json.d.ts +0 -2
  230. package/build/compiler/outputs/json.js +0 -8
  231. package/build/compiler/outputs/json.js.map +0 -1
  232. package/build/compiler/outputs/typescript.d.ts +0 -2
  233. package/build/compiler/outputs/typescript.js +0 -21
  234. package/build/compiler/outputs/typescript.js.map +0 -1
  235. package/build/parser/algorithms/lr.d.ts +0 -7
  236. package/build/parser/algorithms/lr.js +0 -108
  237. package/build/parser/algorithms/lr.js.map +0 -5
  238. package/build/parser/algorithms/lr0.d.ts +0 -7
  239. package/build/parser/algorithms/lr0.js +0 -156
  240. package/build/parser/algorithms/lr0.js.map +0 -1
  241. package/build/typings.d.ts +0 -226
  242. package/build/typings.js +0 -3
  243. package/build/typings.js.map +0 -1
  244. package/src/compiler/builtin/json.gwell +0 -74
  245. package/src/compiler/builtin/number.gwell +0 -20
  246. package/src/compiler/builtin/string.gwell +0 -48
  247. package/src/compiler/builtin/whitespace.gwell +0 -10
  248. package/src/compiler/builtin.json +0 -1
  249. package/src/compiler/compiler.ts +0 -265
  250. package/src/compiler/generator/artifacts/standard.ts +0 -26
  251. package/src/compiler/generator/generator.ts +0 -237
  252. package/src/compiler/gwell.gwell +0 -294
  253. package/src/compiler/gwell.js +0 -578
  254. package/src/compiler/import-resolver.ts +0 -36
  255. package/src/compiler/index.ts +0 -2
  256. package/src/compiler/outputs/javascript.ts +0 -27
  257. package/src/compiler/outputs/json.ts +0 -5
  258. package/src/compiler/outputs/typescript.ts +0 -18
  259. package/src/parser/parser.ts +0 -77
  260. package/src/typings.ts +0 -248
  261. package/testing.ts +0 -18
@@ -1,7 +1,7 @@
1
- import { Lexer, TQRestorePoint, LexerToken } from '../typings';
1
+ import { RuntimeLexer, RuntimeLexerToken, TQRestorePoint } from '../typings/index.js';
2
2
 
3
3
  export class TokenBuffer {
4
- private history: LexerToken[] = [];
4
+ private history: RuntimeLexerToken[] = [];
5
5
  private queued: string = '';
6
6
 
7
7
  private $historyIndex = -1;
@@ -15,7 +15,7 @@ export class TokenBuffer {
15
15
  return { historyIndex: this.$historyIndex, offset: this.offset };
16
16
  }
17
17
 
18
- constructor(private lexer: Lexer) { }
18
+ constructor(public lexer: RuntimeLexer) { }
19
19
 
20
20
  reset(buffer: string) {
21
21
  this.lexer.feed(buffer);
@@ -1,13 +1,14 @@
1
- import { TokenBuffer } from "../../lexers/token-buffer";
2
- import { GrammarRule, GrammarRuleSymbol, LanguageDefinition, LexerToken } from "../../typings";
3
- import { Matrix } from "../../utility/general";
4
- import { ParserUtility } from "../parser";
1
+ import { RuntimeGrammarProductionRule, RuntimeGrammarRuleSymbol, RuntimeParserClass, RuntimeLexerToken } from "../../typings/index.js";
2
+ import { TokenBuffer } from "../../lexers/token-buffer.js";
3
+ import { Matrix } from "../../utility/general.js";
4
+ import { ParserUtility } from "../../utility/parsing.js";
5
5
 
6
- export function CYK(language: LanguageDefinition & { tokens: TokenBuffer }, _options = {}) {
7
- const { grammar, tokens } = language;
6
+ export function CYK(language: RuntimeParserClass & { tokens: TokenBuffer }, _options = {}) {
7
+ const { grammar } = language.artifacts;
8
+ const { tokens } = language;
8
9
 
9
- const terminals: GrammarRule[] = [];
10
- const nonTerminals: GrammarRule[] = [];
10
+ const terminals: RuntimeGrammarProductionRule[] = [];
11
+ const nonTerminals: RuntimeGrammarProductionRule[] = [];
11
12
 
12
13
  for (const name in grammar.rules) {
13
14
  for (const rule of grammar.rules[name]) {
@@ -21,7 +22,7 @@ export function CYK(language: LanguageDefinition & { tokens: TokenBuffer }, _opt
21
22
  }
22
23
 
23
24
  let currentTokenIndex = -1;
24
- const chart = new Matrix(0, 0, () => new Map<GrammarRuleSymbol, Terminal | NonTerminal>());
25
+ const chart = new Matrix(0, 0, () => new Map<RuntimeGrammarRuleSymbol, Terminal | NonTerminal>());
25
26
  for (const token of tokens) {
26
27
  currentTokenIndex++;
27
28
  chart.resize(currentTokenIndex + 2, currentTokenIndex + 2);
@@ -63,12 +64,12 @@ function GetValue(ref: Terminal | NonTerminal) {
63
64
  }
64
65
 
65
66
  export interface NonTerminal {
66
- rule: GrammarRule;
67
+ rule: RuntimeGrammarProductionRule;
67
68
  left: NonTerminal | Terminal;
68
69
  right: NonTerminal | Terminal;
69
70
  }
70
71
 
71
72
  export interface Terminal {
72
- rule: GrammarRule;
73
- token: LexerToken;
73
+ rule: RuntimeGrammarProductionRule;
74
+ token: RuntimeLexerToken;
74
75
  }
@@ -1,15 +1,15 @@
1
- import { Dictionary, GrammarRule, LanguageDefinition } from "../../typings";
2
- import { TokenBuffer } from "../../lexers/token-buffer";
3
- import { TextFormatter } from "../../utility/text-format";
4
- import { ParserUtility } from "../parser";
1
+ import { Dictionary, RuntimeGrammarProductionRule, RuntimeParserClass } from "../../typings/index.js";
2
+ import { TokenBuffer } from "../../lexers/token-buffer.js";
3
+ import { TextFormatter } from "../../utility/text-format.js";
4
+ import { ParserUtility } from "../../utility/parsing.js";
5
5
 
6
6
  export interface EarleyParserOptions {
7
7
  keepHistory?: boolean;
8
8
  }
9
9
 
10
- export function Earley(language: LanguageDefinition & { tokens: TokenBuffer }, options: EarleyParserOptions = {}) {
10
+ export function Earley(language: RuntimeParserClass & { tokens: TokenBuffer }, options: EarleyParserOptions = {}) {
11
11
  const { tokens } = language;
12
- const { rules, start } = language.grammar;
12
+ const { rules, start } = language.artifacts.grammar;
13
13
  const column = new Column(rules, 0);
14
14
  const table: Column[] = [column];
15
15
  column.wants[start] = [];
@@ -70,7 +70,7 @@ class Column {
70
70
  completed: Dictionary<State[]> = Object.create(null); // states that are nullable
71
71
 
72
72
  constructor(
73
- private rules: Dictionary<GrammarRule[]>,
73
+ private rules: Dictionary<RuntimeGrammarProductionRule[]>,
74
74
  public index: number
75
75
  ) { }
76
76
 
@@ -127,8 +127,8 @@ class Column {
127
127
  }
128
128
  }
129
129
 
130
- expects(): GrammarRule[] {
131
- const result: GrammarRule[] = [];
130
+ expects(): RuntimeGrammarProductionRule[] {
131
+ const result: RuntimeGrammarProductionRule[] = [];
132
132
  for (const state of this.states) {
133
133
  if (state.rule.symbols[state.dot] && typeof state.rule.symbols[state.dot] !== 'string') {
134
134
  result.push({ ...state.rule, index: state.dot } as any)
@@ -149,7 +149,7 @@ class State {
149
149
  left: State;
150
150
  right: State | StateToken;
151
151
  constructor(
152
- public rule: GrammarRule,
152
+ public rule: RuntimeGrammarProductionRule,
153
153
  public dot: number,
154
154
  public reference: number,
155
155
  public wantedBy: State[]
@@ -1,11 +1,12 @@
1
- import { TokenBuffer } from "../../../lexers/token-buffer";
2
- import { LanguageDefinition } from "../../../typings";
3
- import { ParserUtility } from "../../parser";
4
- import { CanonicalCollection } from "./canonical-collection";
5
- import { LRStack } from "./stack";
1
+ import { RuntimeParserClass } from "../../../typings/index.js";
2
+ import { TokenBuffer } from "../../../lexers/token-buffer.js";
3
+ import { ParserUtility } from "../../../utility/parsing.js";
4
+ import { CanonicalCollection } from "./canonical-collection.js";
5
+ import { LRStack } from "./stack.js";
6
6
 
7
- export function LRK(language: LanguageDefinition & { tokens: TokenBuffer }, options = {}) {
8
- const { grammar, tokens } = language;
7
+ export function LRK(language: RuntimeParserClass & { tokens: TokenBuffer }, options = {}) {
8
+ const { grammar } = language.artifacts;
9
+ const { tokens } = language;
9
10
  const { states, rules: rules } = new CanonicalCollection(grammar);
10
11
  const stack = new LRStack();
11
12
  const s = states.get('0.0');
@@ -1,30 +1,30 @@
1
- import { GrammarRule, GrammarRuleSymbol, LanguageDefinition } from "../../../typings";
2
- import { ParserUtility } from "../../parser";
3
- import { BiMap } from "./bimap";
4
- import { ClosureBuilder } from "./closure";
5
- import { State } from "./state";
1
+ import { RuntimeGrammarProductionRule, RuntimeGrammarRuleSymbol, RuntimeParserClass } from "../../../typings/index.js";
2
+ import { ParserUtility } from "../../../utility/parsing.js";
3
+ import { BiMap } from "./bimap.js";
4
+ import { ClosureBuilder } from "./closure.js";
5
+ import { State } from "./state.js";
6
6
 
7
7
  export class CanonicalCollection {
8
8
  states: Map<string, State> = new Map();
9
- rules: BiMap<GrammarRule> = new BiMap();
10
- terminals: BiMap<GrammarRuleSymbol> = new BiMap();
9
+ rules: BiMap<RuntimeGrammarProductionRule> = new BiMap();
10
+ terminals: BiMap<RuntimeGrammarRuleSymbol> = new BiMap();
11
11
 
12
12
  private closure: ClosureBuilder;
13
13
  constructor(
14
- public grammar: LanguageDefinition['grammar']
14
+ public grammar: RuntimeParserClass['artifacts']['grammar']
15
15
  ) {
16
16
  const augmented = {
17
17
  name: Symbol() as unknown as string,
18
- symbols: [grammar.start]
18
+ symbols: [this.grammar.start]
19
19
  }
20
- grammar['rules'][augmented.name] = [augmented];
21
- this.closure = new ClosureBuilder(grammar);
20
+ this.grammar['rules'][augmented.name] = [augmented];
21
+ this.closure = new ClosureBuilder(this.grammar);
22
22
  this.rules.id(augmented);
23
- this.addState(grammar['rules'][augmented.name][0], 0);
23
+ this.addState(this.grammar['rules'][augmented.name][0], 0);
24
24
  this.linkStates('0.0');
25
25
  }
26
26
 
27
- private addState(rule: GrammarRule, dot: number) {
27
+ private addState(rule: RuntimeGrammarProductionRule, dot: number) {
28
28
  const id = this.getStateId(rule, dot);
29
29
  if (this.states.has(id))
30
30
  return;
@@ -73,7 +73,7 @@ export class CanonicalCollection {
73
73
  }
74
74
  }
75
75
 
76
- private getStateId(rule: GrammarRule, dot: number) {
76
+ private getStateId(rule: RuntimeGrammarProductionRule, dot: number) {
77
77
  return this.rules.id(rule) + '.' + dot;
78
78
  }
79
79
  }
@@ -1,9 +1,9 @@
1
- import { GrammarRule, GrammarRuleSymbol, LanguageDefinition } from "../../../typings"
2
- import { ParserUtility } from "../../parser"
1
+ import { RuntimeGrammarProductionRule, RuntimeGrammarRuleSymbol, RuntimeParserClass } from "../../../typings/index.js";
2
+ import { ParserUtility } from "../../../utility/parsing.js";
3
3
 
4
4
  export class ClosureBuilder {
5
5
  constructor(
6
- private grammar: LanguageDefinition['grammar'],
6
+ private grammar: RuntimeParserClass['artifacts']['grammar'],
7
7
  ) { }
8
8
 
9
9
  get(rule: string) {
@@ -12,7 +12,7 @@ export class ClosureBuilder {
12
12
  return closure.items;
13
13
  }
14
14
 
15
- private addClosure(closure: RuleClosure, symbol: GrammarRuleSymbol) {
15
+ private addClosure(closure: RuleClosure, symbol: RuntimeGrammarRuleSymbol) {
16
16
  if (!ParserUtility.SymbolIsTerminal(symbol)) {
17
17
  const key = symbol as string;
18
18
  if (!(closure.visited.has(key))) {
@@ -30,8 +30,8 @@ export class ClosureBuilder {
30
30
 
31
31
  interface RuleClosure {
32
32
  items: {
33
- rule: GrammarRule,
33
+ rule: RuntimeGrammarProductionRule,
34
34
  dot: number,
35
35
  }[]
36
- visited: Set<GrammarRuleSymbol>;
36
+ visited: Set<RuntimeGrammarRuleSymbol>;
37
37
  }
@@ -1,5 +1,5 @@
1
- import { GrammarRule, GrammarRuleSymbol } from "../../../typings";
2
- import { State } from "./state";
1
+ import { RuntimeGrammarProductionRule, RuntimeGrammarRuleSymbol } from "../../../typings/index.js";
2
+ import { State } from "./state.js";
3
3
 
4
4
  export class LRStack {
5
5
 
@@ -18,7 +18,7 @@ export class LRStack {
18
18
  this.current.state = state;
19
19
  }
20
20
 
21
- reduce(rule: GrammarRule) {
21
+ reduce(rule: RuntimeGrammarProductionRule) {
22
22
  const n = LRStack.NewItem();
23
23
  const l = rule.symbols.length;
24
24
  n.children = this.stack.splice(l * -1, l);
@@ -28,7 +28,7 @@ export class LRStack {
28
28
  this.stack.push(n);
29
29
  }
30
30
 
31
- append(symbol: GrammarRuleSymbol) {
31
+ append(symbol: RuntimeGrammarRuleSymbol) {
32
32
  this.stack.push(LRStack.NewItem())
33
33
  this.current.symbol = symbol;
34
34
  }
@@ -47,7 +47,7 @@ export class LRStack {
47
47
  interface LRStackItem {
48
48
  children: LRStackItem[];
49
49
  state: State;
50
- symbol: GrammarRuleSymbol;
51
- rule: GrammarRule;
50
+ symbol: RuntimeGrammarRuleSymbol;
51
+ rule: RuntimeGrammarProductionRule;
52
52
  value: any;
53
53
  }
@@ -1,10 +1,10 @@
1
- import { GrammarRule, GrammarRuleSymbol } from "../../../typings";
1
+ import { RuntimeGrammarProductionRule, RuntimeGrammarRuleSymbol } from "../../../typings/index.js";
2
2
 
3
3
  export interface State {
4
- items: { rule: GrammarRule, dot: number }[];
4
+ items: { rule: RuntimeGrammarProductionRule, dot: number }[];
5
5
  isFinal: boolean;
6
- actions: Map<GrammarRuleSymbol, string>;
7
- goto: Map<GrammarRuleSymbol, string>;
6
+ actions: Map<RuntimeGrammarRuleSymbol, string>;
7
+ goto: Map<RuntimeGrammarRuleSymbol, string>;
8
8
  reduce: number;
9
- rule: GrammarRule
9
+ rule: RuntimeGrammarProductionRule
10
10
  }
@@ -0,0 +1,45 @@
1
+ import { ParserAlgorithm, RuntimeParserClass } from "../typings/index.js";
2
+ import { CharacterLexer } from "../lexers/character-lexer.js";
3
+ import { StatefulLexer } from "../lexers/stateful-lexer.js";
4
+ import { TokenBuffer } from "../lexers/token-buffer.js";
5
+ import { CYK } from "./algorithms/cyk.js";
6
+ import { Earley } from "./algorithms/earley.js";
7
+ import { LRK } from "./algorithms/lrk/algorithm.js";
8
+ import { ParserUtility } from "../utility/parsing.js";
9
+
10
+ const ParserRegistry: { [key: string]: ParserAlgorithm } = {
11
+ earley: Earley,
12
+ cyk: CYK,
13
+ lr0: LRK
14
+ }
15
+
16
+ export function Parse(
17
+ language: InstanceType<RuntimeParserClass>,
18
+ input: string,
19
+ options: ParserOptions = {
20
+ algorithm: 'earley',
21
+ parserOptions: {}
22
+ },
23
+ results: 'full' | 'first' = 'first'
24
+ ) {
25
+ const tokenizer = GetTokenizer(language.artifacts);
26
+ tokenizer.feed(input);
27
+ const algorithm = typeof options.algorithm == 'function' ? options.algorithm : ParserRegistry[options.algorithm];
28
+ const result = algorithm({ ...language, tokens: tokenizer, utility: ParserUtility }, options.parserOptions);
29
+ return results == 'full' ? result : result.results[0];
30
+ }
31
+
32
+ function GetTokenizer({ lexer }: RuntimeParserClass['artifacts']) {
33
+ if (!lexer) {
34
+ return new TokenBuffer(new CharacterLexer());
35
+ } else if ("feed" in lexer && typeof lexer.feed == 'function') {
36
+ return new TokenBuffer(lexer);
37
+ } else if ('states' in lexer) {
38
+ return new TokenBuffer(new StatefulLexer(lexer));
39
+ }
40
+ }
41
+
42
+ interface ParserOptions {
43
+ algorithm: (keyof typeof ParserRegistry) | ParserAlgorithm,
44
+ parserOptions?: any;
45
+ }
@@ -0,0 +1,148 @@
1
+ import { Dictionary } from "./common.js";
2
+
3
+ export type AST = ASTDirectives[];
4
+
5
+ export type ASTJavascriptLifecycleLiteral = {
6
+ lifecycle: string;
7
+ path?: boolean;
8
+ js: ASTJavaScriptLiteral;
9
+ }
10
+ export type ASTJavaScriptLiteral = { js: string }
11
+ export type ASTJavaScriptTemplate = { template: string }
12
+
13
+ /**
14
+ * Only available during the generator phase
15
+ *
16
+ * @export
17
+ * @interface ASTJavaScriptBuiltin
18
+ */
19
+ export interface ASTJavaScriptBuiltin { builtin: string }
20
+
21
+ export interface ASTImport {
22
+ import: string;
23
+ path?: boolean;
24
+ alias?: string;
25
+ }
26
+
27
+ export interface ASTConfig {
28
+ config: Dictionary<any>;
29
+ }
30
+
31
+ export interface ASTGrammar {
32
+ grammar: {
33
+ config?: Dictionary<any>;
34
+ rules: ASTGrammarProduction[];
35
+ }
36
+ }
37
+
38
+ export interface ASTLexer {
39
+ lexer: ASTLexerConfig;
40
+ }
41
+ export interface ASTLexerConfig {
42
+ start?: string;
43
+ states: { name: string, state: (ASTLexerState | ASTLexerStateSpan) }[];
44
+ };
45
+ export interface ASTGrammarProduction {
46
+ name: string;
47
+ expressions: ASTGrammarProductionRule[];
48
+ postprocess?: ASTJavaScriptLiteral | ASTJavaScriptTemplate | ASTJavaScriptBuiltin;
49
+ }
50
+
51
+ export interface ASTGrammarProductionRule {
52
+ symbols: ASTGrammarSymbol[];
53
+ postprocess?: ASTJavaScriptLiteral | ASTJavaScriptTemplate | ASTJavaScriptBuiltin;
54
+ }
55
+
56
+ export type ASTGrammarSymbol = ASTGrammarSymbolAlias & (ASTGrammarSymbolNonTerminal | ASTGrammarSymbolRegex | ASTGrammarSymbolToken | ASTGrammarSymbolLiteral | ASTGrammarSymbolRepeat | ASTGrammarSymbolGroup);
57
+ export type ASTGrammarSymbolAlias = { alias?: string };
58
+
59
+ export interface ASTGrammarSymbolGroup {
60
+ subexpression: ASTGrammarProductionRule[];
61
+ }
62
+
63
+ export interface ASTGrammarSymbolRepeat {
64
+ expression: ASTGrammarSymbol;
65
+ repeat: "+" | "*" | "?";
66
+ }
67
+
68
+ export interface ASTGrammarSymbolNonTerminal {
69
+ rule: string;
70
+ }
71
+
72
+ export interface ASTGrammarSymbolRegex {
73
+ regex: string;
74
+ flags?: string;
75
+ quote?: string;
76
+ }
77
+
78
+ export interface ASTGrammarSymbolToken {
79
+ token: string;
80
+ }
81
+
82
+ export interface ASTGrammarSymbolLiteral {
83
+ literal: string;
84
+ insensitive?: boolean;
85
+ }
86
+
87
+ export type ASTDirectives = (ASTJavascriptLifecycleLiteral | ASTImport | ASTConfig | ASTGrammar | ASTLexer);
88
+
89
+ export interface ASTLexerState {
90
+ unmatched?: ASTLexerStateNonMatchRule;
91
+ default?: ASTLexerStateMatchRule;
92
+ rules: (ASTLexerStateImportRule | ASTLexerStateMatchRule | ASTLexerStateSpan)[];
93
+ }
94
+
95
+ export interface ASTLexerStateSpan {
96
+ config?: {
97
+ transition?: "goto" | "set"
98
+ }
99
+ span: { name: string, state: (ASTLexerState) }[];
100
+ }
101
+
102
+ export interface ASTLexerStateImportRule {
103
+ import: string[];
104
+
105
+ pop?: number | 'all';
106
+ inset?: number;
107
+ goto?: string;
108
+ set?: string;
109
+ stay?: boolean;
110
+ }
111
+
112
+ export interface ASTLexerStateMatchRule {
113
+ when: string | ASTGrammarSymbolRegex;
114
+ type?: string;
115
+ tag?: string[];
116
+ before?: boolean;
117
+ skip?: boolean;
118
+ highlight?: string;
119
+ open?: string;
120
+ close?: string;
121
+ embed?: string;
122
+ unembed?: boolean;
123
+
124
+ pop?: number | 'all';
125
+ stay?: true;
126
+ inset?: number;
127
+ goto?: string;
128
+ set?: string;
129
+ }
130
+
131
+ export interface ASTLexerStateNonMatchRule {
132
+ type?: string;
133
+ tag?: string[];
134
+ before?: undefined;
135
+ skip?: undefined;
136
+ highlight?: string;
137
+ open?: string;
138
+ close?: string;
139
+ embed?: string;
140
+ unembed?: boolean;
141
+
142
+ pop?: number | 'all';
143
+ inset?: number;
144
+ goto?: string;
145
+ set?: string;
146
+ }
147
+
148
+ export type StateList = { name: string, state: (ASTLexerState | ASTLexerStateSpan) }[];
@@ -0,0 +1,2 @@
1
+ export type Dictionary<T> = { [key: string]: T }
2
+ export type Override<T, R> = Omit<T, keyof R> & R;
@@ -0,0 +1,62 @@
1
+ import { GeneratorState } from "../generator/state.js";
2
+ import { ASTGrammarSymbolLiteral, ASTGrammarSymbolNonTerminal, ASTGrammarSymbolRegex, ASTGrammarSymbolToken, ASTJavaScriptBuiltin, ASTJavaScriptLiteral, ASTJavaScriptTemplate, ASTLexerStateImportRule, ASTLexerStateMatchRule, ASTLexerStateNonMatchRule } from "./ast.js";
3
+ import { Dictionary } from "./common.js";
4
+ import { ImportResolver, ImportResolverConstructor } from "./index.js";
5
+
6
+
7
+ export type GenerateOptions = GeneratorOptions & { output?: GeneratorOutputOptions };
8
+
9
+ export interface GeneratorOptions {
10
+ version?: string;
11
+ basedir?: string;
12
+ resolver?: ImportResolverConstructor | ImportResolver;
13
+ overrides?: Dictionary<string>;
14
+ }
15
+
16
+ export interface GeneratorOutputOptions {
17
+ artifacts?: {
18
+ lexer?: boolean;
19
+ grammar?: boolean;
20
+ [key: string]: boolean;
21
+ }
22
+ parser?: boolean;
23
+ format?: GeneratorExportFormat;
24
+ noscript?: boolean;
25
+ name?: string;
26
+ }
27
+
28
+ export type GeneratorExportFormat = 'object' | 'json' | 'cjs' | 'commonjs' | 'js' | 'javascript' | 'module' | 'esmodule' | 'esm' | 'ts' | 'typescript';
29
+
30
+ export interface GeneratorContext {
31
+ imported: Set<string>;
32
+ resolver: ImportResolver;
33
+ state: GeneratorState;
34
+ }
35
+ export interface GeneratorStateGrammar {
36
+ start: string;
37
+ config: {
38
+ algorithm?: 'earley' | 'lr0' | 'cyk';
39
+ postprocessorDefault?: ASTJavaScriptLiteral | ASTJavaScriptBuiltin;
40
+ postprocessorOverride?: ASTJavaScriptLiteral | ASTJavaScriptBuiltin;
41
+ }
42
+ rules: Dictionary<GeneratorGrammarProductionRule[]>,
43
+ uuids: { [key: string]: number }
44
+ };
45
+
46
+ export interface GeneratorLexerConfig {
47
+ start?: string
48
+ states: Dictionary<GeneratorLexerState>;
49
+ }
50
+
51
+ export interface GeneratorLexerState {
52
+ unmatched?: ASTLexerStateNonMatchRule;
53
+ rules: (ASTLexerStateImportRule | ASTLexerStateMatchRule)[];
54
+ }
55
+
56
+ export interface GeneratorGrammarProductionRule {
57
+ name: string;
58
+ symbols: GeneratorGrammarSymbol[];
59
+ postprocess?: ASTJavaScriptTemplate | ASTJavaScriptLiteral | ASTJavaScriptBuiltin;
60
+ }
61
+
62
+ export type GeneratorGrammarSymbol = { alias?: string } & (ASTGrammarSymbolNonTerminal | ASTGrammarSymbolRegex | ASTGrammarSymbolLiteral | ASTGrammarSymbolToken);
@@ -0,0 +1,38 @@
1
+ import { TokenBuffer } from "../lexers/token-buffer.js";
2
+ import { ParserUtility } from "../utility/parsing.js";
3
+ import { ASTLexerStateNonMatchRule } from "./ast.js";
4
+ import { RuntimeGrammarProductionRule, RuntimeGrammarRuleSymbol, RuntimeParserClass, RuntimeLexerStateMatchRule } from "./runtime.js";
5
+ export * from './ast.js';
6
+ export * from './common.js';
7
+ export * from './generator.js';
8
+ export * from './runtime.js';
9
+
10
+ export interface ImportResolver {
11
+ path(path: string): string;
12
+ body(path: string): Promise<string>;
13
+ }
14
+
15
+ export interface ImportResolverConstructor {
16
+ new(basePath: string): ImportResolver;
17
+ }
18
+
19
+ export type ParserAlgorithm = ((language: RuntimeParserClass & { tokens: TokenBuffer; utility: ParserUtility }, options?: any) => { results: any[], info?: any });
20
+
21
+ export interface LRState {
22
+ actions: Next[];
23
+ goto: { [key: string]: string };
24
+ reduce?: RuntimeGrammarProductionRule;
25
+ isFinal: boolean;
26
+ }
27
+
28
+ type Next = { symbol: RuntimeGrammarRuleSymbol, next: string };
29
+
30
+ export interface TQRestorePoint {
31
+ historyIndex: number;
32
+ offset: number;
33
+ }
34
+ export interface StatefulLexerStateDefinition {
35
+ regex: RegExp;
36
+ unmatched?: ASTLexerStateNonMatchRule;
37
+ rules: RuntimeLexerStateMatchRule[];
38
+ }
@@ -0,0 +1,82 @@
1
+ import { LRState } from "./index.js";
2
+ import { ASTGrammarSymbolLiteral, ASTGrammarSymbolToken, ASTLexerStateMatchRule, ASTLexerStateNonMatchRule } from "./ast.js";
3
+ import { Dictionary } from "./common.js";
4
+
5
+ export interface RuntimeParserClass {
6
+ artifacts: {
7
+ lexer?: RuntimeLexer | RuntimeLexerConfig;
8
+ grammar: {
9
+ start: string;
10
+ rules: Dictionary<RuntimeGrammarProductionRule[]>;
11
+ }
12
+ lr?: {
13
+ k: number;
14
+ table: Dictionary<LRState>;
15
+ }
16
+ }
17
+ new();
18
+ }
19
+
20
+ export interface RuntimeGrammarProductionRule {
21
+ name: string;
22
+ symbols: RuntimeGrammarRuleSymbol[];
23
+ postprocess?: RuntimePostProcessor;
24
+ }
25
+
26
+ export type RuntimeGrammarRuleSymbol = string | RegExp | ASTGrammarSymbolLiteral | ASTGrammarSymbolToken | RuntimeGrammarRuleSymbolFunction;
27
+
28
+ type RuntimeGrammarRuleSymbolFunction = (data: RuntimeLexerToken) => boolean;
29
+
30
+ export type RuntimePostProcessor = (payload: RuntimePostProcessorPayload) => any;
31
+
32
+ interface RuntimePostProcessorPayload {
33
+ data: any[];
34
+ rule: RuntimeGrammarProductionRule;
35
+ meta: any;
36
+ }
37
+
38
+ export interface RuntimeLexerConfig {
39
+ start?: string
40
+ states: Dictionary<RuntimeLexerState>;
41
+ }
42
+
43
+ export interface RuntimeLexerState {
44
+ unmatched?: ASTLexerStateNonMatchRule;
45
+ rules: RuntimeLexerStateMatchRule[];
46
+ regex: RegExp;
47
+ }
48
+
49
+ export interface RuntimeLexer {
50
+ next(): RuntimeLexerToken | undefined;
51
+ feed(chunk?: string, state?: ReturnType<RuntimeLexer['state']>): void;
52
+ state(): any;
53
+ flush?(): void;
54
+ }
55
+
56
+ export interface RuntimeLexerToken {
57
+ type?: string | undefined;
58
+ tag?: Set<string>;
59
+ value: string;
60
+ offset: number;
61
+ line: number;
62
+ column: number;
63
+ }
64
+
65
+
66
+ export interface RuntimeLexerStateMatchRule {
67
+ when: string | RegExp;
68
+ type?: string;
69
+ tag?: string[];
70
+ before?: boolean;
71
+ skip?: boolean;
72
+ highlight?: string;
73
+ open?: string;
74
+ close?: string;
75
+ embed?: string;
76
+ unembed?: boolean;
77
+
78
+ pop?: number | 'all';
79
+ inset?: number;
80
+ goto?: string;
81
+ set?: string;
82
+ }
@@ -0,0 +1,12 @@
1
+ import V1Grammar from '../generator/grammars/v1.js';
2
+ import V2Grammar from '../generator/grammars/v2.js';
3
+ import { V2GrammarString } from "../generator/stringify/grammar/v2.js";
4
+ import { Parse } from '../parser/parse.js';
5
+
6
+ export function Format(source: string, sourceVersion: '1' | '2' = '2') {
7
+ const grammar = sourceVersion.toString() == '1' ? V1Grammar : V2Grammar;
8
+ const result = Parse(new grammar() as any, source);
9
+ const stringer = new V2GrammarString();
10
+ stringer.append(result);
11
+ return stringer.source;
12
+ }