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
@@ -0,0 +1,401 @@
1
+ import { ASTConfig, ASTDirectives, ASTGrammar, ASTGrammarProduction, ASTGrammarProductionRule, ASTGrammarSymbol, ASTGrammarSymbolGroup, ASTGrammarSymbolLiteral, ASTGrammarSymbolRepeat, ASTImport, ASTLexer, ASTLexerConfig, ASTLexerState, ASTLexerStateImportRule, ASTLexerStateMatchRule, ASTLexerStateSpan, GeneratorContext, GeneratorGrammarProductionRule, GeneratorGrammarSymbol, GeneratorOptions, GeneratorExportFormat, ImportResolver, GeneratorOutputOptions, GenerateOptions, ASTJavascriptLifecycleLiteral } from "../typings/index.js";
2
+
3
+ import { Parse } from "../parser/parse.js";
4
+ import GrammarV1 from './grammars/v1.js';
5
+ import GrammarV2 from './grammars/v2.js';
6
+
7
+ import BuiltInRegistry from "./builtin/registry.json" with {type: 'json'};
8
+ import { GeneratorState } from "./state.js";
9
+ import { ExportsRegistry } from "./stringify/exports/registry.js";
10
+ import { JavaScriptGenerator } from "./stringify/javascript.js";
11
+ import { BrowserImportResolver } from "./import-resolvers/browser.js";
12
+
13
+
14
+ export async function Generate(source: string, config?: GenerateOptions): Promise<ReturnType<Generator['output']>>;
15
+ export async function Generate(directive: ASTDirectives, config?: GenerateOptions): Promise<ReturnType<Generator['output']>>;
16
+ export async function Generate(directives: ASTDirectives[], config?: GenerateOptions): Promise<ReturnType<Generator['output']>>;
17
+ export async function Generate(source: string | ASTDirectives | (ASTDirectives[]), config: GenerateOptions = {}) {
18
+ const builder = new Generator(config);
19
+ await builder.import(source);
20
+ return builder.output(config.output);
21
+ }
22
+
23
+ export const NAME_DELIMITER = '.';
24
+ export class Generator {
25
+ private state = new GeneratorState();
26
+
27
+ constructor(
28
+ private config: GeneratorOptions = {},
29
+ private context: GeneratorContext = {
30
+ imported: new Set(),
31
+ resolver: undefined as ImportResolver,
32
+ state: undefined as GeneratorState
33
+ },
34
+ private aliasPrefix: string = ''
35
+ ) {
36
+
37
+ if (typeof config.resolver == 'function') {
38
+ this.context.resolver = new config.resolver(config.basedir);
39
+ } else if (config.resolver && typeof config.resolver.path == 'function' && typeof config.resolver.body == 'function') {
40
+ this.context.resolver = config.resolver;
41
+ } else {
42
+ this.context.resolver == new BrowserImportResolver(config.basedir);
43
+ }
44
+
45
+ if (this.context.state?.grammar) {
46
+ this.state.initializeGrammar();
47
+ this.state.grammar.uuids = context.state.grammar.uuids;
48
+ }
49
+ }
50
+
51
+
52
+ async import(source: string): Promise<void>
53
+ async import(directive: ASTDirectives): Promise<void>
54
+ async import(directives: ASTDirectives[]): Promise<void>
55
+ async import(directives: string | ASTDirectives | (ASTDirectives[])): Promise<void>
56
+ async import(directives: string | ASTDirectives | (ASTDirectives[])): Promise<void> {
57
+ if (typeof directives == 'string') {
58
+ await this.mergeGrammar(directives);
59
+ return;
60
+ }
61
+ directives = Array.isArray(directives) ? directives : [directives];
62
+ for (const directive of directives) {
63
+ if ("lifecycle" in directive) {
64
+ await this.processLifecycleDirective(directive);
65
+ } else if ("import" in directive) {
66
+ await this.processImportDirective(directive);
67
+ } else if ("config" in directive) {
68
+ this.processConfigDirective(directive);
69
+ } else if ("grammar" in directive) {
70
+ this.processGrammarDirective(directive);
71
+ } else if ("lexer" in directive) {
72
+ this.processLexerDirective(directive);
73
+ }
74
+ }
75
+ }
76
+
77
+ output(options: GeneratorOutputOptions) {
78
+ const format = options?.format || 'esm';
79
+ if (!ExportsRegistry[format]) {
80
+ throw new Error("No such output format: " + format)
81
+ }
82
+ const generator = new JavaScriptGenerator(this.state, options);
83
+ return ExportsRegistry[format](generator);
84
+ }
85
+
86
+ private async processImportDirective(directive: ASTImport) {
87
+ if (directive.path) {
88
+ await this.importGrammar(directive.import, this.aliasPrefix + (directive.alias || ''));
89
+ } else {
90
+ await this.importBuiltIn(directive.import, this.aliasPrefix + (directive.alias || ''));
91
+ }
92
+ }
93
+
94
+ private async processLifecycleDirective(directive: ASTJavascriptLifecycleLiteral) {
95
+ this.state.addLifecycle(directive.lifecycle, directive.js.js);
96
+ }
97
+
98
+ private processConfigDirective(directive: ASTConfig) {
99
+ Object.assign(this.state.config, directive.config);
100
+ }
101
+
102
+ private processLexerDirective(directive: ASTLexer) {
103
+ this.state.initializeLexer();
104
+ if (directive.lexer.start) {
105
+ this.state.lexer.start = this.aliasPrefix + directive.lexer.start;
106
+ }
107
+
108
+ if (!this.state.lexer.start && directive.lexer.states.length) {
109
+ this.state.lexer.start = this.aliasPrefix + directive.lexer.states[0].name
110
+ }
111
+ this.importLexerStates(directive.lexer.states);
112
+ }
113
+
114
+ private importLexerStates(states: ASTLexerConfig['states']) {
115
+ for (let state of states) {
116
+ this.importLexerState(state.name, state.state);
117
+ }
118
+ }
119
+
120
+ private importLexerState(name: string, state: ASTLexerState | ASTLexerStateSpan) {
121
+ if ('span' in state) {
122
+ this.state.addLexerState(this.aliasPrefix + name, { rules: [{ import: [`${name}${NAME_DELIMITER}start`] }] });
123
+ const states = this.buildLexerSpanStates(name, state);
124
+ this.importLexerStates(states);
125
+ } else {
126
+ if (state.default && state.unmatched) {
127
+ state.unmatched.type = typeof state.unmatched.type != 'undefined' ? state.unmatched.type : state.default?.type;
128
+ state.unmatched.tag = typeof state.unmatched.tag != 'undefined' ? state.unmatched.tag : state.default?.tag;
129
+ state.unmatched.open = typeof state.unmatched.open != 'undefined' ? state.unmatched.open : state.default?.open;
130
+ state.unmatched.close = typeof state.unmatched.close != 'undefined' ? state.unmatched.close : state.default?.close;
131
+ state.unmatched.highlight = typeof state.unmatched.highlight != 'undefined' ? state.unmatched.highlight : state.default?.highlight;
132
+ }
133
+ const rules = [];
134
+ for (const rule of state.rules) {
135
+ if ('span' in rule) {
136
+ let i = 1;
137
+ while (`${this.aliasPrefix}${name}${NAME_DELIMITER}${i}` in this.state.lexer.states)
138
+ ++i;
139
+ const states = this.buildLexerSpanStates(`${name}${NAME_DELIMITER}${i}`, rule);
140
+ this.importLexerStates(states);
141
+ rules.push({ import: `${name}.${i}${NAME_DELIMITER}start` });
142
+ continue;
143
+ } else {
144
+ if (this.aliasPrefix) {
145
+ if ('import' in rule) {
146
+ rule.import = rule.import.map(v2 => this.aliasPrefix + v2);
147
+ }
148
+ if ('set' in rule) {
149
+ rule.set = this.aliasPrefix + rule.set;
150
+ }
151
+ if ('goto' in rule) {
152
+ rule.goto = this.aliasPrefix + rule.goto;
153
+ }
154
+ }
155
+ if ('when' in rule && state.default) {
156
+ rule.type = typeof rule.type != 'undefined' ? rule.type : state.default?.type;
157
+ rule.tag = typeof rule.tag != 'undefined' ? rule.tag : state.default?.tag;
158
+ rule.open = typeof rule.open != 'undefined' ? rule.open : state.default?.open;
159
+ rule.close = typeof rule.close != 'undefined' ? rule.close : state.default?.close;
160
+ rule.highlight = typeof rule.highlight != 'undefined' ? rule.highlight : state.default?.highlight;
161
+ }
162
+ rules.push(rule);
163
+ }
164
+ }
165
+ this.state.addLexerState(this.aliasPrefix + name, { ...state, rules });
166
+ }
167
+ }
168
+
169
+ private buildLexerSpanStates(name: string, rule: ASTLexerStateSpan): ASTLexerConfig['states'] {
170
+ const transition = rule.config?.transition == 'set' ? 'set' : 'goto';
171
+ const startRules: (ASTLexerStateMatchRule | ASTLexerStateImportRule)[] = [];
172
+ const spanRules: (ASTLexerStateMatchRule | ASTLexerStateImportRule | ASTLexerStateSpan)[] = [];
173
+ const stopRules: (ASTLexerStateMatchRule | ASTLexerStateImportRule)[] = [];
174
+ const start = rule.span.find(v => v.name == 'start');
175
+ const span = rule.span.find(v => v.name == 'span');
176
+ const stop = rule.span.find(v => v.name == 'stop');
177
+
178
+ if (span?.state?.rules)
179
+ for (const r of span?.state?.rules) {
180
+ spanRules.push(r);
181
+ }
182
+
183
+ if (stop?.state?.rules)
184
+ for (const r of stop?.state?.rules) {
185
+ if ('when' in r) {
186
+ stopRules.push({
187
+ when: r.when,
188
+ type: r.type,
189
+ tag: r.tag,
190
+ before: r.before,
191
+ skip: r.skip,
192
+ highlight: r.highlight,
193
+ open: r.open,
194
+ close: r.close,
195
+ embed: r.embed,
196
+ unembed: r.unembed,
197
+ set: r.skip ? undefined : r.set,
198
+ pop: r.skip || r.set ? undefined : 1
199
+ });
200
+ }
201
+ if ('import' in r) {
202
+ stopRules.push({
203
+ import: r.import,
204
+ set: r.set,
205
+ pop: r.set ? undefined : 1
206
+ })
207
+ }
208
+ }
209
+
210
+ if (stopRules.length && spanRules.length)
211
+ spanRules.push({ import: [`${name}${NAME_DELIMITER}stop`] })
212
+
213
+ const target = spanRules.length ? 'span' : 'stop';
214
+
215
+ for (const r of start?.state?.rules) {
216
+ if ('when' in r) {
217
+ startRules.push({
218
+ when: r.when,
219
+ skip: r.skip,
220
+ type: r.type,
221
+ tag: r.tag,
222
+ before: r.before,
223
+ highlight: r.highlight,
224
+ open: r.open,
225
+ close: r.close,
226
+ embed: r.embed,
227
+ unembed: r.unembed,
228
+ [transition]: r.stay ? undefined : `${name}${NAME_DELIMITER}${target}`
229
+ });
230
+ }
231
+
232
+ if ('import' in r) {
233
+ startRules.push({
234
+ import: r.import,
235
+ [transition]: r.stay ? undefined : `${name}${NAME_DELIMITER}${target}`
236
+ })
237
+ }
238
+ }
239
+
240
+ return [
241
+ {
242
+ name: `${name}${NAME_DELIMITER}start`,
243
+ state: {
244
+ default: start.state.default,
245
+ rules: startRules
246
+ }
247
+ },
248
+ {
249
+ name: `${name}${NAME_DELIMITER}span`,
250
+ state: {
251
+ default: span?.state?.default,
252
+ unmatched: span?.state?.unmatched,
253
+ rules: spanRules
254
+ }
255
+ },
256
+ {
257
+ name: `${name}${NAME_DELIMITER}stop`,
258
+ state: {
259
+ default: stop?.state?.default,
260
+ rules: stopRules
261
+ }
262
+ }
263
+ ]
264
+ }
265
+
266
+ private processGrammarDirective(directive: ASTGrammar) {
267
+ this.state.initializeGrammar();
268
+ if (directive.grammar.config) {
269
+ if (directive.grammar.config.start) {
270
+ this.state.grammar.start = this.aliasPrefix + directive.grammar.config.start;
271
+ }
272
+
273
+ Object.assign(this.state.grammar.config, directive.grammar.config);
274
+ }
275
+
276
+ if (!this.state.grammar.start && directive.grammar.rules.length) {
277
+ this.state.grammar.start = this.aliasPrefix + directive.grammar.rules[0].name;
278
+ }
279
+
280
+ for (const rule of directive.grammar.rules) {
281
+ rule.name = this.aliasPrefix + rule.name;
282
+ this.buildRules(rule.name, rule.expressions, rule);
283
+ }
284
+ }
285
+
286
+ private async importBuiltIn(name: string, alias?: string) {
287
+ name = name.toLowerCase();
288
+ if (!this.context.imported.has(name)) {
289
+ this.context.imported.add(name);
290
+ if (!BuiltInRegistry[name])
291
+ return;
292
+ await this.mergeGrammar(BuiltInRegistry[name], alias);
293
+ }
294
+ }
295
+
296
+ private async importGrammar(path: string, alias?: string) {
297
+ const resolver = this.context.resolver as ImportResolver;
298
+ const fullPath = resolver.path(path);
299
+ if (!this.context.imported.has(fullPath)) {
300
+ this.context.imported.add(fullPath);
301
+ await this.mergeGrammar(await resolver.body(fullPath), alias);
302
+ }
303
+ }
304
+
305
+ private async mergeGrammar(body: string, alias: string = '') {
306
+ const grammar = body.indexOf('// Grammar Well Version 1') == 0 ? GrammarV1 : GrammarV2;
307
+ const generator = new Generator(this.config, { ...this.context, state: this.state }, alias);
308
+ await generator.import(Parse(new grammar() as any, body));
309
+ this.state.merge(generator.state);
310
+ return;
311
+ }
312
+
313
+ private buildRules(name: string, expressions: ASTGrammarProductionRule[], rule?: ASTGrammarProduction) {
314
+ for (const expression of expressions) {
315
+ this.state.addGrammarRule(this.buildRule(name, expression, rule));
316
+ }
317
+ }
318
+
319
+ private buildRule(name: string, expression: ASTGrammarProductionRule, rule?: ASTGrammarProduction): GeneratorGrammarProductionRule {
320
+ const symbols: GeneratorGrammarSymbol[] = [];
321
+ for (let i = 0; i < expression.symbols.length; i++) {
322
+ const symbol = this.buildSymbol(name, expression.symbols[i]);
323
+ if (symbol)
324
+ symbols.push(symbol);
325
+ }
326
+ return { name, symbols, postprocess: expression.postprocess || rule?.postprocess };
327
+ }
328
+
329
+ private buildSymbol(name: string, symbol: ASTGrammarSymbol): GeneratorGrammarSymbol {
330
+ if ('repeat' in symbol) {
331
+ return this.buildRepeatRules(name, symbol);
332
+ }
333
+ if ('rule' in symbol) {
334
+ return { ...symbol, rule: this.aliasPrefix + symbol.rule };
335
+ }
336
+ if ('regex' in symbol) {
337
+ return symbol;
338
+ }
339
+ if ('token' in symbol) {
340
+ return symbol;
341
+ }
342
+ if ('literal' in symbol) {
343
+ if (!symbol.literal.length) {
344
+ return null;
345
+ }
346
+ if (symbol.literal.length === 1 || this.state.lexer) {
347
+ return symbol;
348
+ }
349
+ return this.buildCharacterRules(name, symbol);
350
+ }
351
+ if ('subexpression' in symbol) {
352
+ return this.buildSubExpressionRules(name, symbol);
353
+ }
354
+ }
355
+
356
+ private buildCharacterRules(name: string, symbol: ASTGrammarSymbolLiteral) {
357
+ const id = this.state.grammarUUID(name + NAME_DELIMITER + "STR");
358
+ this.buildRules(id, [
359
+ {
360
+ symbols: symbol.literal
361
+ .split("")
362
+ .map((literal) => {
363
+ if (symbol.insensitive && literal.toLowerCase() != literal.toUpperCase())
364
+ return { regex: literal, flags: 'i' }
365
+ return { literal }
366
+ }),
367
+ postprocess: { builtin: "join" }
368
+ }
369
+ ]);
370
+ return { rule: id };
371
+ }
372
+
373
+ private buildSubExpressionRules(name: string, symbol: ASTGrammarSymbolGroup) {
374
+ const id = this.state.grammarUUID(name + NAME_DELIMITER + "SUB");
375
+ this.buildRules(id, symbol.subexpression);
376
+ return { rule: id };
377
+ }
378
+
379
+ private buildRepeatRules(name: string, symbol: ASTGrammarSymbolRepeat) {
380
+ let id: string;
381
+ const expr1: ASTGrammarProductionRule = { symbols: [] };
382
+ const expr2: ASTGrammarProductionRule = { symbols: [] };
383
+ if (symbol.repeat == '+') {
384
+ id = this.state.grammarUUID(name + NAME_DELIMITER + "RPT1N");
385
+ expr1.symbols = [symbol.expression];
386
+ expr2.symbols = [{ rule: id }, symbol.expression];
387
+ expr2.postprocess = { builtin: "concat" };
388
+ } else if (symbol.repeat == '*') {
389
+ id = this.state.grammarUUID(name + NAME_DELIMITER + "RPT0N");
390
+ expr2.symbols = [{ rule: id }, symbol.expression];
391
+ expr2.postprocess = { builtin: "concat" };
392
+ } else if (symbol.repeat == '?') {
393
+ id = this.state.grammarUUID(name + NAME_DELIMITER + "RPT01");
394
+ expr1.symbols = [symbol.expression];
395
+ expr1.postprocess = { builtin: "first" };
396
+ expr2.postprocess = { builtin: "null" };
397
+ }
398
+ this.buildRules(id, [expr1, expr2]);
399
+ return { rule: id };
400
+ }
401
+ }
@@ -0,0 +1,2 @@
1
+ export { default as V1Grammar } from './v1.js';
2
+ export { default as V2Grammar } from './v2.js';