grammar-well 1.2.0 → 1.2.2

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 (67) hide show
  1. package/bootstrap.ts +14 -7
  2. package/build/compiler/builtin.json +1 -0
  3. package/build/compiler/compiler.d.ts +2 -1
  4. package/build/compiler/compiler.js +43 -24
  5. package/build/compiler/compiler.js.map +1 -1
  6. package/build/compiler/gwell.d.ts +1050 -0
  7. package/build/compiler/gwell.js +555 -0
  8. package/build/compiler/gwell.js.map +1 -0
  9. package/build/index.d.ts +1 -0
  10. package/build/index.js +1 -0
  11. package/build/index.js.map +1 -1
  12. package/build/parser/algorithms/cyk.js +1 -1
  13. package/build/parser/algorithms/cyk.js.map +1 -1
  14. package/build/parser/algorithms/earley.js +10 -8
  15. package/build/parser/algorithms/earley.js.map +1 -1
  16. package/build/parser/algorithms/lr.js +47 -0
  17. package/build/parser/algorithms/lr.js.map +5 -1
  18. package/build/parser/algorithms/lr0.d.ts +7 -0
  19. package/build/parser/algorithms/lr0.js +156 -0
  20. package/build/parser/algorithms/lr0.js.map +1 -0
  21. package/build/parser/algorithms/lrk/algorithm.d.ts +7 -0
  22. package/build/parser/algorithms/lrk/algorithm.js +35 -0
  23. package/build/parser/algorithms/lrk/algorithm.js.map +1 -0
  24. package/build/parser/algorithms/lrk/bimap.d.ts +6 -0
  25. package/build/parser/algorithms/lrk/bimap.js +19 -0
  26. package/build/parser/algorithms/lrk/bimap.js.map +1 -0
  27. package/build/parser/algorithms/lrk/canonical-collection.d.ts +14 -0
  28. package/build/parser/algorithms/lrk/canonical-collection.js +73 -0
  29. package/build/parser/algorithms/lrk/canonical-collection.js.map +1 -0
  30. package/build/parser/algorithms/lrk/closure.d.ts +10 -0
  31. package/build/parser/algorithms/lrk/closure.js +30 -0
  32. package/build/parser/algorithms/lrk/closure.js.map +1 -0
  33. package/build/parser/algorithms/lrk/stack.d.ts +19 -0
  34. package/build/parser/algorithms/lrk/stack.js +39 -0
  35. package/build/parser/algorithms/lrk/stack.js.map +1 -0
  36. package/build/parser/algorithms/lrk/state.d.ts +12 -0
  37. package/build/parser/algorithms/lrk/state.js +3 -0
  38. package/build/parser/algorithms/lrk/state.js.map +1 -0
  39. package/build/parser/parser.d.ts +3 -3
  40. package/build/parser/parser.js +3 -3
  41. package/build/parser/parser.js.map +1 -1
  42. package/build/typings.d.ts +1 -0
  43. package/build/utility/monarch.d.ts +5 -0
  44. package/build/utility/monarch.js +42 -0
  45. package/build/utility/monarch.js.map +1 -0
  46. package/package.json +1 -1
  47. package/src/compiler/builtin/json.gwell +74 -0
  48. package/src/compiler/builtin/number.gwell +20 -0
  49. package/src/compiler/builtin/string.gwell +48 -0
  50. package/src/compiler/builtin/whitespace.gwell +10 -0
  51. package/src/compiler/builtin.json +1 -0
  52. package/src/compiler/compiler.ts +45 -24
  53. package/src/compiler/gwell.gwell +283 -0
  54. package/src/compiler/gwell.js +557 -0
  55. package/src/index.ts +2 -1
  56. package/src/parser/algorithms/cyk.ts +1 -1
  57. package/src/parser/algorithms/earley.ts +10 -10
  58. package/src/parser/algorithms/lrk/algorithm.ts +36 -0
  59. package/src/parser/algorithms/lrk/bimap.ts +17 -0
  60. package/src/parser/algorithms/lrk/canonical-collection.ts +79 -0
  61. package/src/parser/algorithms/lrk/closure.ts +37 -0
  62. package/src/parser/algorithms/lrk/stack.ts +53 -0
  63. package/src/parser/algorithms/lrk/state.ts +10 -0
  64. package/src/parser/parser.ts +5 -5
  65. package/src/typings.ts +1 -0
  66. package/src/utility/monarch.ts +36 -0
  67. package/src/parser/algorithms/lr.ts +0 -74
@@ -0,0 +1,283 @@
1
+ lexer: {{
2
+ start: "start"
3
+
4
+ start ->
5
+ - import: string, js, ws, comment, l_scolon, l_star
6
+ - when: /lexer(?![a-zA-Z\d_])/ tag: "T_WORD" goto: lexer highlight: "type"
7
+ - when: /grammar(?![a-zA-Z\d_])/ tag: "T_WORD" goto: grammar highlight: "type"
8
+ - when: /config(?![a-zA-Z\d_])/ tag: "T_WORD" goto: config highlight: "type"
9
+ - import: kv
10
+ config ->
11
+ - import: ws, l_colon
12
+ - when: "{{" tag: "L_TEMPLATEL" set: config_inner
13
+ config_inner ->
14
+ - import: comment, kv
15
+ - when: "}}" tag: "L_TEMPLATER" pop: 1
16
+ grammar ->
17
+ - import: ws, l_colon
18
+ - when: "{{" tag: "L_TEMPLATEL" set: grammar_inner
19
+ grammar_inner ->
20
+ - 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
21
+ - when: "}}" tag: "L_TEMPLATER" pop: 1
22
+ lexer ->
23
+ - import: ws, l_colon
24
+ - when: "{{" tag: "L_TEMPLATEL" set: lexer_inner
25
+ lexer_inner ->
26
+ - import: ws, comment, regex, l_comma, l_arrow, l_dash, kv, js
27
+ - when: "}}" tag: "L_TEMPLATER" pop: 1
28
+ js ->
29
+ - when: "${" tag: "L_JSL" goto: js_wrap
30
+ js_wrap ->
31
+ default: "T_JSBODY"
32
+ unmatched: "T_JSBODY"
33
+ - import: jsignore
34
+ - when: "{" tag: "T_JSBODY" goto: js_literal
35
+ - when: "}" tag: "L_JSR" pop: 1
36
+ js_literal ->
37
+ default: "T_JSBODY"
38
+ unmatched: "T_JSBODY"
39
+ - import: jsignore
40
+ - when: "{" tag: "T_JSBODY" goto: js_literal
41
+ - when: "}" tag: "T_JSBODY" pop: 1
42
+ js_template ->
43
+ - when: "{{" tag: "L_TEMPLATEL" goto: js_template_inner
44
+ js_template_inner ->
45
+ default: "T_JSBODY"
46
+ unmatched: "T_JSBODY"
47
+ - import: jsignore
48
+ - when: "{" tag: "T_JSBODY" goto: js_literal
49
+ - when: "}}" tag: "L_TEMPLATER" pop: 1
50
+ kv ->
51
+ - import: string, ws, word, l_colon, integer
52
+ jsignore ->
53
+ - when: /"(?:[^"\\\r\n]|\\.)*"/ tag: "T_JSBODY"
54
+ - when: /'(?:[^'\\\r\n]|\\.)*'/ tag: "T_JSBODY"
55
+ - when: /`(?:[^`\\]|\\.)*`/ tag: "T_JSBODY"
56
+ - when: /\/(?:[^\/\\\r\n]|\\.)+\/[gmiyu]*/ tag: "T_JSBODY"
57
+ - when: /\/\/[^\n]*/ tag: "T_JSBODY"
58
+ - when: /\/\*.*\*\// tag: "T_JSBODY"
59
+ string ->
60
+ - when: /"(?:[^"\\\r\n]|\\.)*"/ tag: "T_STRING" highlight: "string"
61
+ string2 ->
62
+ - when: /'(?:[^'\\\r\n]|\\.)*'/ tag: "T_STRING" highlight: "string"
63
+ string3 ->
64
+ - when: /`(?:[^`\\]|\\.)*`/ tag: "T_STRING" highlight: "string"
65
+ regex ->
66
+ - when: /\/(?:[^\/\\\r\n]|\\.)+\// tag: "T_REGEX" highlight: "regexp"
67
+ integer ->
68
+ - when: /\d+/ tag: "T_INTEGER" highlight: "number"
69
+ word ->
70
+ - when: /[a-zA-Z_][a-zA-Z_\d]*/ tag: "T_WORD"
71
+ ws ->
72
+ - when: /\s+/ tag: "T_WS"
73
+ l_colon ->
74
+ - when: ":" tag: "L_COLON" highlight: "keyword"
75
+ l_scolon ->
76
+ - when: ";" tag: "L_SCOLON"
77
+ l_qmark ->
78
+ - when: "?" tag: "L_QMARK"
79
+ l_plus ->
80
+ - when: "+" tag: "L_PLUS"
81
+ l_star ->
82
+ - when: "*" tag: "L_STAR"
83
+ l_comma ->
84
+ - when: "," tag: "L_COMMA"
85
+ l_pipe ->
86
+ - when: "|" tag: "L_PIPE" highlight: "keyword"
87
+ l_parenl ->
88
+ - when: "(" tag: "L_PARENL"
89
+ l_parenr ->
90
+ - when: ")" tag: "L_PARENR"
91
+ l_templatel ->
92
+ - when: "{{" tag: "L_TEMPLATEL"
93
+ l_templater ->
94
+ - when: "}}" tag: "L_TEMPLATER"
95
+ l_arrow ->
96
+ - when: "->" tag: "L_ARROW" highlight: "keyword"
97
+ l_dsign ->
98
+ - when: "$" tag: "L_DSIGN"
99
+ l_dash ->
100
+ - when: "-" tag: "L_DASH"
101
+ comment ->
102
+ - when: /\/\/[^\n]*/ tag: "T_COMMENT" highlight: "comment"
103
+ commentmulti ->
104
+ - when: /\/\*.*\*\// tag: "T_COMMENT" highlight: "comment"
105
+
106
+ }}
107
+
108
+ grammar: {{
109
+
110
+ main ->
111
+ _ section_list _ : {{ $1 }}
112
+
113
+ section_list ->
114
+ section : {{ [$0] }}
115
+ | section T_WS section_list : {{ [$0].concat($2) }}
116
+
117
+ section ->
118
+ K_CONFIG _ L_COLON _ L_TEMPLATEL _ kv_list:list _ L_TEMPLATER : {{ { config: Object.assign(...$list) } }}
119
+ | K_IMPORT _ L_STAR _ K_FROM __ T_WORD:import _ L_SCOLON : {{ { import: $import } }}
120
+ | K_IMPORT _ L_STAR _ K_FROM __ T_STRING:import _ L_SCOLON : {{ { import: $import, path: true } }}
121
+ | K_IMPORT _ L_STAR _ "as" _ T_WORD:alias _ K_FROM __ T_WORD:import _ L_SCOLON : {{ { import: $import, alias: $alias} }}
122
+ | K_IMPORT _ L_STAR _ "as" _ T_WORD:alias _ K_FROM __ T_STRING:import _ L_SCOLON : {{ { import: $import, path: true, alias: $alias} }}
123
+ | K_LEXER _ L_COLON _ L_TEMPLATEL _ lexer:lexer _ L_TEMPLATER : {{ { lexer: Object.assign(...$lexer) } }}
124
+ | K_GRAMMAR _ L_COLON _ L_TEMPLATEL _ grammar:grammar _ L_TEMPLATER : {{ { grammar: $grammar } }}
125
+ | K_BODY _ L_COLON _ T_JS:js : {{ { body: $js } }}
126
+ | K_BODY _ L_COLON _ T_STRING:js : {{ { body: $js, path: true } }}
127
+ | K_HEAD _ L_COLON _ T_JS:js : {{ { head: $js } }}
128
+ | K_HEAD _ L_COLON _ T_STRING:js : {{ { head: $js, path: true } }}
129
+
130
+ lexer ->
131
+ kv_list _ state_list : {{ $0.concat({ states: $2 }) }}
132
+ | state_list : {{ [{ states: $0 }] }}
133
+
134
+ state_list ->
135
+ state : {{ data }}
136
+ | state _ state_list : {{ [$0].concat($2) }}
137
+
138
+ state ->
139
+ state_declare _ state_definition : {{ Object.assign({ name: $0 }, $2) }}
140
+
141
+ state_declare ->
142
+ T_WORD _ L_ARROW : {{ $0 }}
143
+
144
+ state_definition ->
145
+ kv_list _ token_list : {{ Object.assign(...$0, { rules: $2 }) }}
146
+ | token_list : {{ { rules: $0 } }}
147
+
148
+ token_list ->
149
+ token : {{ data }}
150
+ | token _ token_list : {{ [$0].concat($2) }}
151
+
152
+ token ->
153
+ L_DASH _ K_IMPORT _ L_COLON _ word_list : {{ { import: $6 } }}
154
+ | L_DASH _ token_definition_list : {{ Object.assign(...$2) }}
155
+
156
+ token_definition_list ->
157
+ token_definition : {{ data }}
158
+ | token_definition _ token_definition_list : {{ [$0].concat($2) }}
159
+
160
+ token_definition ->
161
+ K_TAG _ L_COLON _ string_list : {{ { tag: $4 } }}
162
+ | K_WHEN _ L_COLON _ T_STRING : {{ { when: $4 } }}
163
+ | K_WHEN _ L_COLON _ T_REGEX : {{ { when: $4 } }}
164
+ | K_POP : {{ { pop: 1 } }}
165
+ | K_POP _ L_COLON _ T_INTEGER : {{ { pop: parseInt($4) } }}
166
+ | K_POP _ L_COLON _ K_ALL : {{ { pop: "all" } }}
167
+ | K_HIGHLIGHT _ L_COLON _ T_STRING : {{ { highlight: $4 } }}
168
+ | K_INSET : {{ { inset: 1 } }}
169
+ | K_INSET _ L_COLON _ T_INTEGER : {{ { inset: parseInt($4) } }}
170
+ | K_SET _ L_COLON _ T_WORD : {{ { set: $4 } }}
171
+ | K_GOTO _ L_COLON _ T_WORD : {{ { goto: $4 } }}
172
+ | K_TYPE _ L_COLON _ T_STRING : {{ { type: $4 } }}
173
+
174
+ grammar ->
175
+ kv_list _ grammar_rule_list : {{ { config: Object.assign(...$0), rules: $2 } }}
176
+ | grammar_rule_list : {{ { rules: $0 } }}
177
+
178
+ grammar_rule_list ->
179
+ grammar_rule : {{ [$0] }}
180
+ | grammar_rule _ grammar_rule_list : {{ [$0].concat($2) }}
181
+
182
+ grammar_rule ->
183
+ T_WORD _ L_ARROW _ expression_list : {{ { name: $0, expressions: $4 } }}
184
+ | T_WORD __ L_COLON _ T_JS:js _ L_ARROW _ expression_list:expressions : {{ { name: $0, expressions: $expressions, postprocess: $js } }}
185
+ | T_WORD __ L_COLON _ T_GRAMMAR_TEMPLATE:template _ L_ARROW _ expression_list:expressions : {{ { name: $0, expressions: $expressions, postprocess: $template } }}
186
+
187
+ expression_list ->
188
+ expression
189
+ | expression_list _ L_PIPE _ expression : {{ $0.concat([$4]) }}
190
+
191
+ expression ->
192
+ expression_symbol_list : {{ { symbols: $0 } }}
193
+ | expression_symbol_list __ L_COLON _ T_JS:js : {{ { symbols: $0, postprocess: $js } }}
194
+ | expression_symbol_list __ L_COLON _ T_GRAMMAR_TEMPLATE:template : {{ { symbols: $0, postprocess: $template } }}
195
+
196
+ expression_symbol_list ->
197
+ expression_symbol
198
+ | expression_symbol_list T_WS expression_symbol : {{ $0.concat([$2]) }}
199
+
200
+
201
+ expression_symbol ->
202
+ expression_symbol_match : {{ $0 }}
203
+ | expression_symbol_match L_COLON T_WORD : {{ { ...$0, alias: $2 } }}
204
+ | expression_symbol_match expression_repeater : {{ { expression: $0, repeat: $1 } }}
205
+ | expression_symbol_match expression_repeater L_COLON T_WORD : {{ { expression: $0, repeat: $1, alias: $4 } }}
206
+
207
+
208
+ expression_symbol_match ->
209
+ T_WORD : {{ { rule: $0 } }}
210
+ | T_STRING "i"? : {{ { literal: $0, insensitive: !!$1 } }}
211
+ | L_DSIGN T_WORD : {{ { token: $1} }}
212
+ | L_DSIGN T_STRING : {{ { token: $1} }}
213
+ | T_REGEX : {{ $0 }}
214
+ | L_PARENL _ expression_list _ L_PARENR : {{ { subexpression: $2 } }}
215
+ | T_JS : {{ $0 }}
216
+
217
+ expression_repeater : {{ $0[0].value }} ->
218
+ L_QMARK
219
+ | L_PLUS
220
+ | L_STAR
221
+
222
+ kv_list ->
223
+ kv : {{ data }}
224
+ | kv _ kv_list : {{ [$0].concat($2) }}
225
+
226
+ kv ->
227
+ T_WORD _ L_COLON _ ( T_WORD| T_STRING| T_INTEGER | T_JS | T_GRAMMAR_TEMPLATE) : {{ { [$0]: $4[0] } }}
228
+
229
+ string_list ->
230
+ T_STRING : {{ [$0] }}
231
+ | T_STRING _ L_COMMA _ string_list : {{ [$0].concat($4) }}
232
+
233
+ word_list ->
234
+ T_WORD : {{ [$0] }}
235
+ | T_WORD _ L_COMMA _ word_list : {{ [$0].concat($4) }}
236
+
237
+ _ ->
238
+ ( T_WS | T_COMMENT )* : {{ null }}
239
+
240
+ __ ->
241
+ ( T_WS | T_COMMENT )+ : {{ null }}
242
+
243
+ L_COLON -> $L_COLON
244
+ L_SCOLON -> $L_SCOLON
245
+ L_QMARK -> $L_QMARK
246
+ L_PLUS -> $L_PLUS
247
+ L_STAR -> $L_STAR
248
+ L_COMMA -> $L_COMMA
249
+ L_PIPE -> $L_PIPE
250
+ L_PARENL -> $L_PARENL
251
+ L_PARENR -> $L_PARENR
252
+ L_TEMPLATEL -> $L_TEMPLATEL
253
+ L_TEMPLATER -> $L_TEMPLATER
254
+ L_ARROW -> $L_ARROW
255
+ L_DSIGN -> $L_DSIGN
256
+ L_DASH -> $L_DASH
257
+
258
+ K_ALL -> "all"
259
+ K_TAG -> "tag"
260
+ K_FROM -> "from"
261
+ K_TYPE -> "type"
262
+ K_WHEN -> "when"
263
+ K_POP -> "pop"
264
+ K_HIGHLIGHT -> "highlight"
265
+ K_INSET -> "inset"
266
+ K_SET -> "set"
267
+ K_GOTO -> "goto"
268
+ K_CONFIG -> "config"
269
+ K_LEXER -> "lexer"
270
+ K_GRAMMAR -> "grammar"
271
+ K_IMPORT -> "import"
272
+ K_BODY -> "body"
273
+ K_HEAD -> "head"
274
+
275
+ T_JS -> $L_JSL $T_JSBODY* $L_JSR : {{ { js: $1.map(v=>v.value).join('') } }}
276
+ T_GRAMMAR_TEMPLATE -> $L_TEMPLATEL _ $T_JSBODY* _ $L_TEMPLATER : {{ { template: $2.map(v=>v.value).join('').trim() } }}
277
+ T_STRING -> $T_STRING : {{ JSON.parse($0.value) }}
278
+ T_WORD -> $T_WORD : {{ $0.value }}
279
+ T_REGEX -> $T_REGEX /[gmiuy]/* : {{ { regex: $0.value.replace(/\\\\\//g,'/').slice(1,-1), flags: $1.map(v=>v.value).join('').trim() } }}
280
+ T_COMMENT -> $T_COMMENT
281
+ T_INTEGER -> $T_INTEGER : {{ $0.value }}
282
+ T_WS -> $T_WS : {{ null }}
283
+ }}