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
package/.eslintrc.cjs ADDED
@@ -0,0 +1,14 @@
1
+ module.exports = {
2
+ extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
3
+ parser: '@typescript-eslint/parser',
4
+ plugins: ['@typescript-eslint'],
5
+ root: true,
6
+ rules: {
7
+ "@typescript-eslint/no-inferrable-types": "off",
8
+ "@typescript-eslint/no-explicit-any": "off",
9
+ "@typescript-eslint/no-var-requires": "off",
10
+ "@typescript-eslint/no-unused-vars": ["error", {argsIgnorePattern: "^_", destructuredArrayIgnorePattern: "^_", varsIgnorePattern: "^_"}],
11
+ "no-unused-vars": "off"
12
+ },
13
+ ignorePatterns: ["**/grammars/*.js"],
14
+ };
package/README.md ADDED
@@ -0,0 +1,288 @@
1
+ # Grammar Well
2
+ A cross-platform grammar compiler and interpreter. That aims to facilitate a simple way to create and evaluate custom grammars on the front-end and back-end. Formerly a TypeScript port of [Nearley](https://github.com/kach/nearley).
3
+
4
+ # Warning
5
+ A lot has changed and documentation needs to be written. For now here's the Grammar Well grammar file that parses Garmmar Well's syntax.
6
+
7
+
8
+ ```
9
+ lexer: {{
10
+ start: "start"
11
+
12
+ start ->
13
+ - import: string, js, ws, comment, l_scolon, l_star
14
+ - when: /lexer(?![a-zA-Z\d_])/ tag: "T_WORD" goto: lexer
15
+ - when: /grammar(?![a-zA-Z\d_])/ tag: "T_WORD" goto: grammar
16
+ - when: /config(?![a-zA-Z\d_])/ tag: "T_WORD" goto: config
17
+ - import: kv
18
+ config ->
19
+ - import: ws, l_colon
20
+ - when: "{{" tag: "L_TEMPLATEL" set: config_inner
21
+ config_inner ->
22
+ - import: comment, kv
23
+ - when: "}}" tag: "L_TEMPLATER" pop: 1
24
+ grammar ->
25
+ - import: ws, l_colon
26
+ - when: "{{" tag: "L_TEMPLATEL" set: grammar_inner
27
+ grammar_inner ->
28
+ - import: comment, js, js_template, ws, regex, l_qmark, l_plus, l_star, kv, l_colon, l_comma, l_pipe, l_parenl, l_parenr, l_arrow, l_dsign, l_dash
29
+ - when: "}}" tag: "L_TEMPLATER" pop: 1
30
+ lexer ->
31
+ - import: ws, l_colon
32
+ - when: "{{" tag: "L_TEMPLATEL" set: lexer_inner
33
+ lexer_inner ->
34
+ - import: ws, comment, regex, l_comma, l_arrow, l_dash, kv, js
35
+ - when: "}}" tag: "L_TEMPLATER" pop: 1
36
+ js ->
37
+ - when: "${" tag: "L_JSL" goto: js_wrap
38
+ js_wrap ->
39
+ default: "T_JSBODY"
40
+ unmatched: "T_JSBODY"
41
+ - import: jsignore
42
+ - when: "{" tag: "T_JSBODY" goto: js_literal
43
+ - when: "}" tag: "L_JSR" pop: 1
44
+ js_literal ->
45
+ default: "T_JSBODY"
46
+ unmatched: "T_JSBODY"
47
+ - import: jsignore
48
+ - when: "{" tag: "T_JSBODY" goto: js_literal
49
+ - when: "}" tag: "T_JSBODY" pop: 1
50
+ js_template ->
51
+ - when: "{{" tag: "L_TEMPLATEL" goto: js_template_inner
52
+ js_template_inner ->
53
+ default: "T_JSBODY"
54
+ unmatched: "T_JSBODY"
55
+ - import: jsignore
56
+ - when: "{" tag: "T_JSBODY" goto: js_literal
57
+ - when: "}}" tag: "L_TEMPLATER" pop: 1
58
+ kv ->
59
+ - import: string, ws, word, l_colon, integer
60
+ jsignore ->
61
+ - when: /"(?:[^"\\\r\n]|\\.)*"/ tag: "T_JSBODY"
62
+ - when: /'(?:[^'\\\r\n]|\\.)*'/ tag: "T_JSBODY"
63
+ - when: /`(?:[^`\\]|\\.)*`/ tag: "T_JSBODY"
64
+ - when: /\/(?:[^\/\\\r\n]|\\.)+\/[gmiyu]*/ tag: "T_JSBODY"
65
+ - when: /\/\/[^\n]*/ tag: "T_JSBODY"
66
+ - when: /\/\*.*\*\// tag: "T_JSBODY"
67
+ string ->
68
+ - when: /"(?:[^"\\\r\n]|\\.)*"/ tag: "T_STRING"
69
+ string2 ->
70
+ - when: /'(?:[^'\\\r\n]|\\.)*'/ tag: "T_STRING"
71
+ string3 ->
72
+ - when: /`(?:[^`\\]|\\.)*`/ tag: "T_STRING"
73
+ regex ->
74
+ - when: /\/(?:[^\/\\\r\n]|\\.)+\// tag: "T_REGEX"
75
+ integer ->
76
+ - when: /\d+/ tag: "T_INTEGER"
77
+ word ->
78
+ - when: /[a-zA-Z_][a-zA-Z_\d]*/ tag: "T_WORD"
79
+ ws ->
80
+ - when: /\s+/ tag: "T_WS"
81
+ l_colon ->
82
+ - when: ":" tag: "L_COLON"
83
+ l_scolon ->
84
+ - when: ";" tag: "L_SCOLON"
85
+ l_qmark ->
86
+ - when: "?" tag: "L_QMARK"
87
+ l_plus ->
88
+ - when: "+" tag: "L_PLUS"
89
+ l_star ->
90
+ - when: "*" tag: "L_STAR"
91
+ l_comma ->
92
+ - when: "," tag: "L_COMMA"
93
+ l_pipe ->
94
+ - when: "|" tag: "L_PIPE"
95
+ l_parenl ->
96
+ - when: "(" tag: "L_PARENL"
97
+ l_parenr ->
98
+ - when: ")" tag: "L_PARENR"
99
+ l_templatel ->
100
+ - when: "{{" tag: "L_TEMPLATEL"
101
+ l_templater ->
102
+ - when: "}}" tag: "L_TEMPLATER"
103
+ l_arrow ->
104
+ - when: "->" tag: "L_ARROW"
105
+ l_dsign ->
106
+ - when: "$" tag: "L_DSIGN"
107
+ l_dash ->
108
+ - when: "-" tag: "L_DASH"
109
+ comment ->
110
+ - when: /\/\/[^\n]*/ tag: "T_COMMENT"
111
+ commentmulti ->
112
+ - when: /\/\*.*\*\// tag: "T_COMMENT"
113
+
114
+ }}
115
+
116
+ grammar: {{
117
+
118
+ main ->
119
+ _ section_list _ : {{ $1 }}
120
+
121
+ section_list ->
122
+ section : {{ [$0] }}
123
+ | section T_WS section_list : {{ [$0].concat($2) }}
124
+
125
+ section ->
126
+ K_CONFIG _ L_COLON _ L_TEMPLATEL _ kv_list _ L_TEMPLATER : {{ { config: Object.assign(...$4) } }}
127
+ | K_IMPORT _ L_STAR _ K_FROM __ T_WORD:import _ L_SCOLON : {{ { import: $import } }}
128
+ | K_IMPORT _ L_STAR _ K_FROM __ T_STRING:import _ L_SCOLON : {{ { import: $import, path: true } }}
129
+ | K_LEXER _ L_COLON _ L_TEMPLATEL _ lexer:lexer _ L_TEMPLATER : {{ { lexer: Object.assign(...$lexer) } }}
130
+ | K_GRAMMAR _ L_COLON _ L_TEMPLATEL _ grammar:grammar _ L_TEMPLATER : {{ { grammar: $grammar } }}
131
+ | K_BODY _ L_COLON _ T_JS:js : {{ { body: $js } }}
132
+ | K_BODY _ L_COLON _ T_STRING:js : {{ { body: $js, path: true } }}
133
+ | K_HEAD _ L_COLON _ T_JS:js : {{ { head: $js } }}
134
+ | K_HEAD _ L_COLON _ T_STRING:js : {{ { head: $js, path: true } }}
135
+
136
+ lexer ->
137
+ kv_list _ state_list : {{ $0.concat({ states: $2 }) }}
138
+ | state_list : {{ [{ states: $0 }] }}
139
+
140
+ state_list ->
141
+ state : {{ data }}
142
+ | state _ state_list : {{ [$0].concat($2) }}
143
+
144
+ state ->
145
+ state_declare _ state_definition : {{ Object.assign({ name: $0 }, $2) }}
146
+
147
+ state_declare ->
148
+ T_WORD _ L_ARROW : {{ $0 }}
149
+
150
+ state_definition ->
151
+ kv_list _ token_list : {{ Object.assign(...$0, { rules: $2 }) }}
152
+ | token_list : {{ { rules: $0 } }}
153
+
154
+ token_list ->
155
+ token : {{ data }}
156
+ | token _ token_list : {{ [$0].concat($2) }}
157
+
158
+ token ->
159
+ L_DASH _ K_IMPORT _ L_COLON _ word_list : {{ { import: $6 } }}
160
+ | L_DASH _ token_definition_list : {{ Object.assign(...$2) }}
161
+
162
+ token_definition_list ->
163
+ token_definition : {{ data }}
164
+ | token_definition _ token_definition_list : {{ [$0].concat($2) }}
165
+
166
+ token_definition ->
167
+ K_TAG _ L_COLON _ string_list : {{ { tag: $4 } }}
168
+ | K_WHEN _ L_COLON _ T_STRING : {{ { when: $4 } }}
169
+ | K_WHEN _ L_COLON _ T_REGEX : {{ { when: $4 } }}
170
+ | K_POP : {{ { pop: 1 } }}
171
+ | K_POP _ L_COLON _ T_INTEGER : {{ { pop: parseInt($4) } }}
172
+ | K_POP _ L_COLON _ K_ALL : {{ { pop: "all" } }}
173
+ | K_INSET : {{ { inset: 1 } }}
174
+ | K_INSET _ L_COLON _ T_INTEGER : {{ { inset: parseInt($4) } }}
175
+ | K_SET _ L_COLON _ T_WORD : {{ { set: $4 } }}
176
+ | K_GOTO _ L_COLON _ T_WORD : {{ { goto: $4 } }}
177
+ | K_TYPE _ L_COLON _ T_STRING : {{ { type: $4 } }}
178
+
179
+ grammar ->
180
+ kv_list _ grammar_rule_list : {{ { config: Object.assign(...$0), rules: $2 } }}
181
+ | grammar_rule_list : {{ { rules: $0 } }}
182
+
183
+ grammar_rule_list ->
184
+ grammar_rule : {{ [$0] }}
185
+ | grammar_rule _ grammar_rule_list : {{ [$0].concat($2) }}
186
+
187
+ grammar_rule ->
188
+ T_WORD _ L_ARROW _ expression_list : {{ { name: $0, expressions: $4 } }}
189
+ | T_WORD __ L_COLON _ T_JS:js _ L_ARROW _ expression_list:expressions : {{ { name: $0, expressions: $expressions, postprocess: $js } }}
190
+ | T_WORD __ L_COLON _ T_GRAMMAR_TEMPLATE:template _ L_ARROW _ expression_list:expressions : {{ { name: $0, expressions: $expressions, postprocess: $template } }}
191
+
192
+ expression_list ->
193
+ expression
194
+ | expression_list _ L_PIPE _ expression : {{ $0.concat([$4]) }}
195
+
196
+ expression ->
197
+ expression_symbol_list : {{ { symbols: $0 } }}
198
+ | expression_symbol_list __ L_COLON _ T_JS:js : {{ { symbols: $0, postprocess: $js } }}
199
+ | expression_symbol_list __ L_COLON _ T_GRAMMAR_TEMPLATE:template : {{ { symbols: $0, postprocess: $template } }}
200
+
201
+ expression_symbol_list ->
202
+ expression_symbol
203
+ | expression_symbol_list T_WS expression_symbol : {{ $0.concat([$2]) }}
204
+
205
+
206
+ expression_symbol ->
207
+ expression_symbol_match : {{ $0 }}
208
+ | expression_symbol_match L_COLON T_WORD : {{ { ...$0, alias: $2 } }}
209
+ | expression_symbol_match expression_repeater : {{ { expression: $0, repeat: $1 } }}
210
+ | expression_symbol_match expression_repeater L_COLON T_WORD : {{ { expression: $0, repeat: $1, alias: $4 } }}
211
+
212
+
213
+ expression_symbol_match ->
214
+ T_WORD : {{ { rule: $0 } }}
215
+ | T_STRING "i"? : {{ { literal: $0, insensitive: !!$1 } }}
216
+ | L_DSIGN T_WORD : {{ { token: $1} }}
217
+ | L_DSIGN T_STRING : {{ { token: $1} }}
218
+ | T_REGEX : {{ $0 }}
219
+ | L_PARENL _ expression_list _ L_PARENR : {{ { subexpression: $2 } }}
220
+ | T_JS : {{ $0 }}
221
+
222
+ expression_repeater : {{ $0[0].value }} ->
223
+ L_QMARK
224
+ | L_PLUS
225
+ | L_STAR
226
+
227
+ kv_list ->
228
+ kv : {{ data }}
229
+ | kv _ kv_list : {{ [$0].concat($2) }}
230
+
231
+ kv ->
232
+ T_WORD _ L_COLON _ ( T_WORD| T_STRING| T_INTEGER | T_JS | T_GRAMMAR_TEMPLATE) : {{ { [$0]: $4[0] } }}
233
+
234
+ string_list ->
235
+ T_STRING : {{ [$0] }}
236
+ | T_STRING _ L_COMMA _ string_list : {{ [$0].concat($4) }}
237
+
238
+ word_list ->
239
+ T_WORD : {{ [$0] }}
240
+ | T_WORD _ L_COMMA _ word_list : {{ [$0].concat($4) }}
241
+
242
+ _ ->
243
+ T_WS? : {{ null }}
244
+ __ ->
245
+ T_WS+ : {{ null }}
246
+
247
+ L_COLON -> $L_COLON
248
+ L_SCOLON -> $L_SCOLON
249
+ L_QMARK -> $L_QMARK
250
+ L_PLUS -> $L_PLUS
251
+ L_STAR -> $L_STAR
252
+ L_COMMA -> $L_COMMA
253
+ L_PIPE -> $L_PIPE
254
+ L_PARENL -> $L_PARENL
255
+ L_PARENR -> $L_PARENR
256
+ L_TEMPLATEL -> $L_TEMPLATEL
257
+ L_TEMPLATER -> $L_TEMPLATER
258
+ L_ARROW -> $L_ARROW
259
+ L_DSIGN -> $L_DSIGN
260
+ L_DASH -> $L_DASH
261
+
262
+ K_ALL -> "all"
263
+ K_TAG -> "tag"
264
+ K_FROM -> "from"
265
+ K_TYPE -> "type"
266
+ K_WHEN -> "when"
267
+ K_POP -> "pop"
268
+ K_INSET -> "inset"
269
+ K_SET -> "set"
270
+ K_GOTO -> "goto"
271
+ K_CONFIG -> "config"
272
+ K_LEXER -> "lexer"
273
+ K_GRAMMAR -> "grammar"
274
+ K_IMPORT -> "import"
275
+ K_BODY -> "body"
276
+ K_HEAD -> "head"
277
+
278
+ T_JS -> $L_JSL $T_JSBODY* $L_JSR : {{ { js: $1.map(v=>v.value).join('') } }}
279
+ T_GRAMMAR_TEMPLATE -> $L_TEMPLATEL _ $T_JSBODY* _ $L_TEMPLATER : {{ { template: $2.map(v=>v.value).join('').trim() } }}
280
+ T_STRING -> $T_STRING : {{ JSON.parse($0.value) }}
281
+ T_WORD -> $T_WORD : {{ $0.value }}
282
+ T_REGEX -> $T_REGEX /[gmiuy]/* : {{ { regex: $0.value.replace(/\\\\\//g,'/').slice(1,-1), flags: $1.join('') } }}
283
+ T_COMMENT -> $T_COMMENT
284
+ T_INTEGER -> $T_INTEGER : {{ $0.value }}
285
+ T_WS -> $T_WS : {{ null }}
286
+ }}
287
+
288
+ ```
package/bootstrap.ts ADDED
@@ -0,0 +1,35 @@
1
+ import { readdirSync, readFileSync, writeFileSync } from "fs";
2
+ import { resolve } from "path";
3
+ import { Compile } from "./src";
4
+ const BaseDir = './src/grammars';
5
+
6
+ (async () => {
7
+ const files = readdirSync(BaseDir);
8
+ for (const file of files) {
9
+ try {
10
+ console.log(fullpath(file))
11
+ if (/\.gwell$/.test(file)) {
12
+ const json = await Compile(read(file), { format: 'json' });
13
+ const js = await Compile(read(file), { exportName: 'grammar', format: 'esmodule' });
14
+ write(file.replace(/.gwell$/, '.json'), json);
15
+ write(file.replace(/.gwell$/, '.js'), js);
16
+ }
17
+ } catch (error) {
18
+ console.log(file);
19
+ console.log(error);
20
+ throw error;
21
+ }
22
+ }
23
+ })();
24
+
25
+ function read(filename) {
26
+ return readFileSync(fullpath(filename), 'utf-8')
27
+ }
28
+
29
+ function write(filename, body) {
30
+ return writeFileSync(fullpath(filename), body, 'utf8');
31
+ }
32
+
33
+ function fullpath(file: string) {
34
+ return resolve(BaseDir, file)
35
+ }
@@ -0,0 +1,48 @@
1
+ import { CompileOptions, GrammarBuilderContext, OutputFormat, LanguageDirective } from "../typings";
2
+ import { ESMOutput, JavascriptOutput } from "./outputs/javascript";
3
+ import { TypescriptFormat } from "./outputs/typescript";
4
+ import { JSONFormatter } from "./outputs/json";
5
+ import { Generator } from "./generator";
6
+ declare const OutputFormats: {
7
+ _default: typeof JavascriptOutput;
8
+ object: (grammar: any, exportName: any) => {
9
+ grammar: any;
10
+ exportName: any;
11
+ };
12
+ json: typeof JSONFormatter;
13
+ js: typeof JavascriptOutput;
14
+ javascript: typeof JavascriptOutput;
15
+ module: typeof ESMOutput;
16
+ esmodule: typeof ESMOutput;
17
+ ts: typeof TypescriptFormat;
18
+ typescript: typeof TypescriptFormat;
19
+ };
20
+ export declare function Compile(rules: string | LanguageDirective | (LanguageDirective[]), config?: CompileOptions): Promise<string | {
21
+ grammar: any;
22
+ exportName: any;
23
+ }>;
24
+ export declare class GrammarBuilder {
25
+ private config;
26
+ private parser;
27
+ private context;
28
+ generator: Generator;
29
+ constructor(config?: CompileOptions, context?: GrammarBuilderContext);
30
+ export<T extends OutputFormat = '_default'>(format: T, name?: string): ReturnType<typeof OutputFormats[T]>;
31
+ import(source: string): Promise<void>;
32
+ import(directive: LanguageDirective): Promise<void>;
33
+ import(directives: LanguageDirective[]): Promise<void>;
34
+ private processImportDirective;
35
+ private processConfigDirective;
36
+ private processGrammarDirective;
37
+ private processLexerDirective;
38
+ private importBuiltIn;
39
+ private importGrammar;
40
+ private mergeLanguageDefinitionString;
41
+ private buildRules;
42
+ private buildRule;
43
+ private buildSymbol;
44
+ private buildCharacterRules;
45
+ private buildSubExpressionRules;
46
+ private buildRepeatRules;
47
+ }
48
+ export {};
@@ -0,0 +1,227 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GrammarBuilder = exports.Compile = void 0;
4
+ const parser_1 = require("../parser/parser");
5
+ const import_resolver_1 = require("./import-resolver");
6
+ const gwell_1 = require("../grammars/gwell");
7
+ const javascript_1 = require("./outputs/javascript");
8
+ const typescript_1 = require("./outputs/typescript");
9
+ const json_1 = require("./outputs/json");
10
+ const number = require("../grammars/number.json");
11
+ const string = require("../grammars/string.json");
12
+ const whitespace = require("../grammars/whitespace.json");
13
+ const generator_1 = require("./generator");
14
+ const BuiltInRegistry = {
15
+ number,
16
+ string,
17
+ whitespace,
18
+ };
19
+ const OutputFormats = {
20
+ _default: javascript_1.JavascriptOutput,
21
+ object: (grammar, exportName) => ({ grammar, exportName }),
22
+ json: json_1.JSONFormatter,
23
+ js: javascript_1.JavascriptOutput,
24
+ javascript: javascript_1.JavascriptOutput,
25
+ module: javascript_1.ESMOutput,
26
+ esmodule: javascript_1.ESMOutput,
27
+ ts: typescript_1.TypescriptFormat,
28
+ typescript: typescript_1.TypescriptFormat
29
+ };
30
+ async function Compile(rules, config = {}) {
31
+ const builder = new GrammarBuilder(config);
32
+ await builder.import(rules);
33
+ return builder.export(config.format);
34
+ }
35
+ exports.Compile = Compile;
36
+ class GrammarBuilder {
37
+ config;
38
+ parser = new parser_1.Parser((0, gwell_1.default)());
39
+ context;
40
+ generator = new generator_1.Generator();
41
+ constructor(config = {}, context) {
42
+ this.config = config;
43
+ this.context = context || {
44
+ alreadyCompiled: new Set(),
45
+ resolver: config.resolverInstance ? config.resolverInstance : config.resolver ? new config.resolver(config.basedir) : new import_resolver_1.FileSystemResolver(config.basedir),
46
+ uuids: {}
47
+ };
48
+ this.generator.state.grammar.uuids = this.context.uuids;
49
+ }
50
+ export(format, name = 'GWLanguage') {
51
+ const grammar = this.generator.state;
52
+ const output = format || grammar.config.preprocessor || '_default';
53
+ if (OutputFormats[output]) {
54
+ return OutputFormats[output](this.generator, name);
55
+ }
56
+ throw new Error("No such preprocessor: " + output);
57
+ }
58
+ async import(directives) {
59
+ if (typeof directives == 'string') {
60
+ await this.mergeLanguageDefinitionString(directives);
61
+ return;
62
+ }
63
+ directives = Array.isArray(directives) ? directives : [directives];
64
+ for (const directive of directives) {
65
+ if ("head" in directive) {
66
+ this.generator.state.head.push(directive.head.js);
67
+ }
68
+ else if ("body" in directive) {
69
+ this.generator.state.body.push(directive.body.js);
70
+ }
71
+ else if ("import" in directive) {
72
+ await this.processImportDirective(directive);
73
+ }
74
+ else if ("config" in directive) {
75
+ this.processConfigDirective(directive);
76
+ }
77
+ else if ("grammar" in directive) {
78
+ this.processGrammarDirective(directive);
79
+ }
80
+ else if ("lexer" in directive) {
81
+ this.processLexerDirective(directive);
82
+ }
83
+ }
84
+ }
85
+ async processImportDirective(directive) {
86
+ if (directive.path) {
87
+ await this.importGrammar(directive.import);
88
+ }
89
+ else {
90
+ this.importBuiltIn(directive.import);
91
+ }
92
+ }
93
+ processConfigDirective(directive) {
94
+ Object.assign(this.generator.state.config, directive.config);
95
+ }
96
+ processGrammarDirective(directive) {
97
+ if (directive.grammar.config) {
98
+ this.generator.state.grammar.start = directive.grammar.config.start || this.generator.state.grammar.start;
99
+ }
100
+ for (const rule of directive.grammar.rules) {
101
+ this.buildRules(rule.name, rule.expressions, rule);
102
+ this.generator.state.grammar.start = this.generator.state.grammar.start || rule.name;
103
+ }
104
+ }
105
+ processLexerDirective(directive) {
106
+ if (!this.generator.state.lexer) {
107
+ this.generator.state.lexer = {
108
+ start: '',
109
+ states: {}
110
+ };
111
+ }
112
+ this.generator.state.lexer.start = directive.lexer.start || this.generator.state.lexer.start || (directive.lexer.states.length ? directive.lexer.states[0].name : '');
113
+ for (const state of directive.lexer.states) {
114
+ this.generator.addLexerState(state);
115
+ }
116
+ }
117
+ importBuiltIn(name) {
118
+ name = name.toLowerCase();
119
+ if (!this.context.alreadyCompiled.has(name)) {
120
+ this.context.alreadyCompiled.add(name);
121
+ if (!BuiltInRegistry[name])
122
+ return;
123
+ this.generator.merge(BuiltInRegistry[name].state);
124
+ }
125
+ }
126
+ async importGrammar(name) {
127
+ const resolver = this.context.resolver;
128
+ const path = resolver.path(name);
129
+ if (!this.context.alreadyCompiled.has(path)) {
130
+ this.context.alreadyCompiled.add(path);
131
+ await this.mergeLanguageDefinitionString(await resolver.body(path));
132
+ }
133
+ }
134
+ async mergeLanguageDefinitionString(body) {
135
+ const builder = new GrammarBuilder(this.config, this.context);
136
+ await builder.import(this.parser.run(body).results[0]);
137
+ this.generator.merge(builder.generator.state);
138
+ return;
139
+ }
140
+ buildRules(name, expressions, rule) {
141
+ for (const expression of expressions) {
142
+ this.generator.addGrammarRule(this.buildRule(name, expression, rule));
143
+ }
144
+ }
145
+ buildRule(name, expression, rule) {
146
+ const symbols = [];
147
+ for (let i = 0; i < expression.symbols.length; i++) {
148
+ const symbol = this.buildSymbol(name, expression.symbols[i]);
149
+ if (symbol)
150
+ symbols.push(symbol);
151
+ }
152
+ return { name, symbols, postprocess: expression.postprocess || rule?.postprocess };
153
+ }
154
+ buildSymbol(name, symbol) {
155
+ if ('repeat' in symbol) {
156
+ return this.buildRepeatRules(name, symbol);
157
+ }
158
+ if ('rule' in symbol) {
159
+ return symbol;
160
+ }
161
+ if ('regex' in symbol) {
162
+ return symbol;
163
+ }
164
+ if ('token' in symbol) {
165
+ return symbol;
166
+ }
167
+ if ('literal' in symbol) {
168
+ if (!symbol.literal.length) {
169
+ return null;
170
+ }
171
+ if (symbol.literal.length === 1 || this.generator.state.lexer) {
172
+ return symbol;
173
+ }
174
+ return this.buildCharacterRules(name, symbol);
175
+ }
176
+ if ('subexpression' in symbol) {
177
+ return this.buildSubExpressionRules(name, symbol);
178
+ }
179
+ }
180
+ buildCharacterRules(name, symbol) {
181
+ const id = this.generator.grammarUUID(name + "$STR");
182
+ this.buildRules(id, [
183
+ {
184
+ symbols: symbol.literal
185
+ .split("")
186
+ .map((literal) => {
187
+ if (symbol.insensitive && literal.toLowerCase() != literal.toUpperCase())
188
+ return { regex: literal, flags: 'i' };
189
+ return { literal };
190
+ }),
191
+ postprocess: { builtin: "join" }
192
+ }
193
+ ]);
194
+ return { rule: id };
195
+ }
196
+ buildSubExpressionRules(name, symbol) {
197
+ const id = this.generator.grammarUUID(name + "$SUB");
198
+ this.buildRules(id, symbol.subexpression);
199
+ return { rule: id };
200
+ }
201
+ buildRepeatRules(name, symbol) {
202
+ let id;
203
+ const expr1 = { symbols: [] };
204
+ const expr2 = { symbols: [] };
205
+ if (symbol.repeat == '+') {
206
+ id = this.generator.grammarUUID(name + "$RPT1N");
207
+ expr1.symbols = [symbol.expression];
208
+ expr2.symbols = [{ rule: id }, symbol.expression];
209
+ expr2.postprocess = { builtin: "concat" };
210
+ }
211
+ else if (symbol.repeat == '*') {
212
+ id = this.generator.grammarUUID(name + "$RPT0N");
213
+ expr2.symbols = [{ rule: id }, symbol.expression];
214
+ expr2.postprocess = { builtin: "concat" };
215
+ }
216
+ else if (symbol.repeat == '?') {
217
+ id = this.generator.grammarUUID(name + "$RPT01");
218
+ expr1.symbols = [symbol.expression];
219
+ expr1.postprocess = { builtin: "first" };
220
+ expr2.postprocess = { builtin: "null" };
221
+ }
222
+ this.buildRules(id, [expr1, expr2]);
223
+ return { rule: id };
224
+ }
225
+ }
226
+ exports.GrammarBuilder = GrammarBuilder;
227
+ //# sourceMappingURL=compiler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compiler.js","sourceRoot":"","sources":["../../src/compiler/compiler.ts"],"names":[],"mappings":";;;AAEA,6CAA0C;AAC1C,uDAAuD;AACvD,6CAAyC;AAEzC,qDAAmE;AACnE,qDAAwD;AACxD,yCAA+C;AAE/C,kDAAkD;AAClD,kDAAkD;AAClD,0DAA0D;AAC1D,2CAAwC;AAExC,MAAM,eAAe,GAAG;IACpB,MAAM;IACN,MAAM;IACN,UAAU;CACb,CAAA;AACD,MAAM,aAAa,GAAG;IAClB,QAAQ,EAAE,6BAAgB;IAC1B,MAAM,EAAE,CAAC,OAAO,EAAE,UAAU,EAAE,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;IAC1D,IAAI,EAAE,oBAAa;IACnB,EAAE,EAAE,6BAAgB;IACpB,UAAU,EAAE,6BAAgB;IAC5B,MAAM,EAAE,sBAAS;IACjB,QAAQ,EAAE,sBAAS;IACnB,EAAE,EAAE,6BAAgB;IACpB,UAAU,EAAE,6BAAgB;CAC/B,CAAA;AAEM,KAAK,UAAU,OAAO,CAAC,KAA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,GAAG,QAAQ,CAAC,CAAC;YACjD,KAAK,CAAC,OAAO,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YACpC,KAAK,CAAC,WAAW,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;YACzC,KAAK,CAAC,WAAW,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;SAC3C;QACD,IAAI,CAAC,UAAU,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;QACpC,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;IACxB,CAAC;CACJ;AAxMD,wCAwMC"}
@@ -0,0 +1,23 @@
1
+ import { GeneratorState, GeneratorGrammarRule, LexerStateDefinition } from "../typings";
2
+ export declare class Generator {
3
+ state: GeneratorState;
4
+ serializeHead(): string;
5
+ serializeBody(): string;
6
+ serializeLanguage(depth?: number): string;
7
+ merge(state: GeneratorState): void;
8
+ grammarUUID(name: string): string;
9
+ addGrammarRule(rule: GeneratorGrammarRule): void;
10
+ addLexerState(state: LexerStateDefinition): void;
11
+ private serializeGrammar;
12
+ private serializeGrammarRules;
13
+ private serializeSymbol;
14
+ private serializeGrammarRule;
15
+ private serializePostProcess;
16
+ private templatePostProcess;
17
+ private serializeLexerConfig;
18
+ private serializeLexerConfigStates;
19
+ private serializeLexerConfigStateRules;
20
+ private newLine;
21
+ private pretty;
22
+ private isVal;
23
+ }