grammar-well 1.3.2 → 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 (262) hide show
  1. package/LICENSE +165 -0
  2. package/README.md +76 -560
  3. package/build/generator/artifacts/basic.d.ts +7 -0
  4. package/build/generator/artifacts/basic.js +24 -0
  5. package/build/generator/artifacts/basic.js.map +1 -0
  6. package/build/generator/artifacts/lexer.d.ts +13 -0
  7. package/build/generator/artifacts/lexer.js +204 -0
  8. package/build/generator/artifacts/lexer.js.map +1 -0
  9. package/build/{compiler/generator → generator}/artifacts/lr.d.ts +14 -14
  10. package/build/{compiler/generator → generator}/artifacts/lr.js +18 -22
  11. package/build/generator/artifacts/lr.js.map +1 -0
  12. package/build/generator/builtin/json.d.ts +183 -0
  13. package/build/generator/builtin/json.js +124 -0
  14. package/build/generator/builtin/json.js.map +1 -0
  15. package/build/generator/builtin/registry.json +1 -0
  16. package/build/generator/builtin/whitespace.d.ts +61 -0
  17. package/build/generator/builtin/whitespace.js +37 -0
  18. package/build/generator/builtin/whitespace.js.map +1 -0
  19. package/build/generator/generator.d.ts +38 -0
  20. package/build/generator/generator.js +368 -0
  21. package/build/generator/generator.js.map +1 -0
  22. package/build/generator/grammars/index.d.ts +2 -0
  23. package/build/generator/grammars/index.js +3 -0
  24. package/build/generator/grammars/index.js.map +1 -0
  25. package/build/generator/grammars/v1.d.ts +1190 -0
  26. package/build/generator/grammars/v1.js +614 -0
  27. package/build/generator/grammars/v1.js.map +1 -0
  28. package/build/generator/grammars/v2.d.ts +1367 -0
  29. package/build/generator/grammars/v2.js +695 -0
  30. package/build/generator/grammars/v2.js.map +1 -0
  31. package/build/generator/import-resolvers/auto.d.ts +2 -0
  32. package/build/generator/import-resolvers/auto.js +11 -0
  33. package/build/generator/import-resolvers/auto.js.map +1 -0
  34. package/build/generator/import-resolvers/browser.d.ts +7 -0
  35. package/build/generator/import-resolvers/browser.js +13 -0
  36. package/build/generator/import-resolvers/browser.js.map +1 -0
  37. package/build/generator/import-resolvers/default.d.ts +2 -0
  38. package/build/generator/import-resolvers/default.js +11 -0
  39. package/build/generator/import-resolvers/default.js.map +1 -0
  40. package/build/generator/import-resolvers/filesystem.d.ts +7 -0
  41. package/build/generator/import-resolvers/filesystem.js +15 -0
  42. package/build/generator/import-resolvers/filesystem.js.map +1 -0
  43. package/build/generator/index.d.ts +3 -0
  44. package/build/generator/index.js +4 -0
  45. package/build/generator/index.js.map +1 -0
  46. package/build/generator/state.d.ts +28 -0
  47. package/build/generator/state.js +73 -0
  48. package/build/generator/state.js.map +1 -0
  49. package/build/generator/stringify/common.d.ts +22 -0
  50. package/build/generator/stringify/common.js +83 -0
  51. package/build/generator/stringify/common.js.map +1 -0
  52. package/build/generator/stringify/exports/javascript.d.ts +3 -0
  53. package/build/generator/stringify/exports/javascript.js +26 -0
  54. package/build/generator/stringify/exports/javascript.js.map +1 -0
  55. package/build/generator/stringify/exports/json.d.ts +2 -0
  56. package/build/generator/stringify/exports/json.js +4 -0
  57. package/build/generator/stringify/exports/json.js.map +1 -0
  58. package/build/generator/stringify/exports/registry.d.ts +20 -0
  59. package/build/generator/stringify/exports/registry.js +17 -0
  60. package/build/generator/stringify/exports/registry.js.map +1 -0
  61. package/build/generator/stringify/exports/typescript.d.ts +2 -0
  62. package/build/generator/stringify/exports/typescript.js +16 -0
  63. package/build/generator/stringify/exports/typescript.js.map +1 -0
  64. package/build/generator/stringify/grammar/v2.d.ts +20 -0
  65. package/build/generator/stringify/grammar/v2.js +211 -0
  66. package/build/generator/stringify/grammar/v2.js.map +1 -0
  67. package/build/generator/stringify/javascript.d.ts +13 -0
  68. package/build/generator/stringify/javascript.js +83 -0
  69. package/build/generator/stringify/javascript.js.map +1 -0
  70. package/build/index.d.ts +7 -6
  71. package/build/index.js +7 -24
  72. package/build/index.js.map +1 -1
  73. package/build/lexers/character-lexer.d.ts +2 -2
  74. package/build/lexers/character-lexer.js +1 -5
  75. package/build/lexers/character-lexer.js.map +1 -1
  76. package/build/lexers/stateful-lexer.d.ts +12 -17
  77. package/build/lexers/stateful-lexer.js +38 -186
  78. package/build/lexers/stateful-lexer.js.map +1 -1
  79. package/build/lexers/token-buffer.d.ts +8 -8
  80. package/build/lexers/token-buffer.js +1 -5
  81. package/build/lexers/token-buffer.js.map +1 -1
  82. package/build/parser/algorithms/cyk.d.ts +6 -6
  83. package/build/parser/algorithms/cyk.js +10 -13
  84. package/build/parser/algorithms/cyk.js.map +1 -1
  85. package/build/parser/algorithms/earley.d.ts +7 -7
  86. package/build/parser/algorithms/earley.js +7 -11
  87. package/build/parser/algorithms/earley.js.map +1 -1
  88. package/build/parser/algorithms/lrk/algorithm.d.ts +3 -3
  89. package/build/parser/algorithms/lrk/algorithm.js +10 -13
  90. package/build/parser/algorithms/lrk/algorithm.js.map +1 -1
  91. package/build/parser/algorithms/lrk/bimap.js +1 -5
  92. package/build/parser/algorithms/lrk/bimap.js.map +1 -1
  93. package/build/parser/algorithms/lrk/canonical-collection.d.ts +7 -7
  94. package/build/parser/algorithms/lrk/canonical-collection.js +11 -15
  95. package/build/parser/algorithms/lrk/canonical-collection.js.map +1 -1
  96. package/build/parser/algorithms/lrk/closure.d.ts +3 -3
  97. package/build/parser/algorithms/lrk/closure.js +3 -7
  98. package/build/parser/algorithms/lrk/closure.js.map +1 -1
  99. package/build/parser/algorithms/lrk/stack.d.ts +6 -6
  100. package/build/parser/algorithms/lrk/stack.js +1 -5
  101. package/build/parser/algorithms/lrk/stack.js.map +1 -1
  102. package/build/parser/algorithms/lrk/state.d.ts +5 -5
  103. package/build/parser/algorithms/lrk/state.js +1 -2
  104. package/build/parser/parse.d.ts +10 -0
  105. package/build/parser/parse.js +34 -0
  106. package/build/parser/parse.js.map +1 -0
  107. package/build/parser/parser.d.ts +6 -17
  108. package/build/parser/parser.js +26 -42
  109. package/build/parser/parser.js.map +1 -1
  110. package/build/tsconfig.tsbuildinfo +1 -0
  111. package/build/typings/ast.d.ts +134 -0
  112. package/build/typings/ast.js +3 -0
  113. package/build/typings/ast.js.map +1 -0
  114. package/build/typings/common.d.ts +4 -0
  115. package/build/typings/common.js +2 -0
  116. package/build/typings/common.js.map +1 -0
  117. package/build/typings/generator.d.ts +58 -0
  118. package/build/typings/generator.js +3 -0
  119. package/build/typings/generator.js.map +1 -0
  120. package/build/typings/index.d.ts +43 -0
  121. package/build/typings/index.js +5 -0
  122. package/build/typings/index.js.map +1 -0
  123. package/build/typings/runtime.d.ts +70 -0
  124. package/build/typings/runtime.js +2 -0
  125. package/build/typings/runtime.js.map +1 -0
  126. package/build/utility/format.d.ts +1 -0
  127. package/build/utility/format.js +12 -0
  128. package/build/utility/format.js.map +1 -0
  129. package/build/utility/formatter.d.ts +1 -0
  130. package/build/utility/formatter.js +12 -0
  131. package/build/utility/formatter.js.map +1 -0
  132. package/build/utility/general.d.ts +1 -1
  133. package/build/utility/general.js +5 -13
  134. package/build/utility/general.js.map +1 -1
  135. package/build/utility/index.d.ts +4 -1
  136. package/build/utility/index.js +4 -17
  137. package/build/utility/index.js.map +1 -1
  138. package/build/utility/lint.d.ts +2 -2
  139. package/build/utility/lint.js +2 -6
  140. package/build/utility/lint.js.map +1 -1
  141. package/build/utility/migrate.d.ts +1 -0
  142. package/build/utility/migrate.js +10 -0
  143. package/build/utility/migrate.js.map +1 -0
  144. package/build/utility/monarch.d.ts +2 -2
  145. package/build/utility/monarch.js +33 -38
  146. package/build/utility/monarch.js.map +1 -1
  147. package/build/utility/parsing.d.ts +6 -0
  148. package/build/utility/parsing.js +26 -0
  149. package/build/utility/parsing.js.map +1 -0
  150. package/build/utility/text-format.d.ts +6 -6
  151. package/build/utility/text-format.js +2 -6
  152. package/build/utility/text-format.js.map +1 -1
  153. package/package.json +17 -24
  154. package/src/generator/artifacts/basic.ts +26 -0
  155. package/src/generator/artifacts/lexer.ts +228 -0
  156. package/src/{compiler/generator → generator}/artifacts/lr.ts +25 -24
  157. package/src/generator/builtin/character.well +7 -0
  158. package/src/generator/builtin/json.well +85 -0
  159. package/src/generator/builtin/number.well +21 -0
  160. package/src/generator/builtin/registry.json +1 -0
  161. package/src/generator/builtin/string.well +54 -0
  162. package/src/generator/builtin/whitespace.well +16 -0
  163. package/src/generator/generator.ts +401 -0
  164. package/src/generator/grammars/index.ts +2 -0
  165. package/src/generator/grammars/v1.ts +620 -0
  166. package/src/generator/grammars/v1.well +422 -0
  167. package/src/generator/grammars/v2.ts +701 -0
  168. package/src/generator/grammars/v2.well +413 -0
  169. package/src/generator/import-resolvers/auto.ts +12 -0
  170. package/src/generator/import-resolvers/browser.ts +13 -0
  171. package/src/generator/import-resolvers/filesystem.ts +18 -0
  172. package/src/generator/index.ts +3 -0
  173. package/src/generator/state.ts +89 -0
  174. package/src/generator/stringify/common.ts +90 -0
  175. package/src/generator/stringify/exports/javascript.ts +29 -0
  176. package/src/generator/stringify/exports/json.ts +5 -0
  177. package/src/generator/stringify/exports/registry.ts +20 -0
  178. package/src/generator/stringify/exports/typescript.ts +17 -0
  179. package/src/generator/stringify/grammar/v2.ts +223 -0
  180. package/src/generator/stringify/javascript.ts +94 -0
  181. package/src/index.ts +7 -6
  182. package/src/lexers/character-lexer.ts +2 -2
  183. package/src/lexers/stateful-lexer.ts +46 -203
  184. package/src/lexers/token-buffer.ts +3 -3
  185. package/src/parser/algorithms/cyk.ts +13 -12
  186. package/src/parser/algorithms/earley.ts +10 -10
  187. package/src/parser/algorithms/lrk/algorithm.ts +8 -7
  188. package/src/parser/algorithms/lrk/canonical-collection.ts +14 -14
  189. package/src/parser/algorithms/lrk/closure.ts +6 -6
  190. package/src/parser/algorithms/lrk/stack.ts +6 -6
  191. package/src/parser/algorithms/lrk/state.ts +5 -5
  192. package/src/parser/parse.ts +45 -0
  193. package/src/typings/ast.ts +148 -0
  194. package/src/typings/common.ts +2 -0
  195. package/src/typings/generator.ts +62 -0
  196. package/src/typings/index.ts +38 -0
  197. package/src/typings/runtime.ts +82 -0
  198. package/src/utility/format.ts +12 -0
  199. package/src/utility/general.ts +1 -2
  200. package/src/utility/index.ts +4 -1
  201. package/src/utility/lint.ts +5 -5
  202. package/src/utility/monarch.ts +34 -34
  203. package/src/utility/parsing.ts +30 -0
  204. package/src/utility/text-format.ts +7 -7
  205. package/.eslintrc.cjs +0 -14
  206. package/bootstrap.ts +0 -45
  207. package/build/compiler/builtin.json +0 -1
  208. package/build/compiler/compiler.d.ts +0 -50
  209. package/build/compiler/compiler.js +0 -249
  210. package/build/compiler/compiler.js.map +0 -1
  211. package/build/compiler/generator/artifacts/lr.js.map +0 -1
  212. package/build/compiler/generator/artifacts/standard.d.ts +0 -7
  213. package/build/compiler/generator/artifacts/standard.js +0 -28
  214. package/build/compiler/generator/artifacts/standard.js.map +0 -1
  215. package/build/compiler/generator/generator.d.ts +0 -24
  216. package/build/compiler/generator/generator.js +0 -217
  217. package/build/compiler/generator/generator.js.map +0 -1
  218. package/build/compiler/gwell.d.ts +0 -1112
  219. package/build/compiler/gwell.js +0 -576
  220. package/build/compiler/gwell.js.map +0 -1
  221. package/build/compiler/import-resolver.d.ts +0 -15
  222. package/build/compiler/import-resolver.js +0 -37
  223. package/build/compiler/import-resolver.js.map +0 -1
  224. package/build/compiler/index.d.ts +0 -2
  225. package/build/compiler/index.js +0 -19
  226. package/build/compiler/index.js.map +0 -1
  227. package/build/compiler/outputs/javascript.d.ts +0 -3
  228. package/build/compiler/outputs/javascript.js +0 -29
  229. package/build/compiler/outputs/javascript.js.map +0 -1
  230. package/build/compiler/outputs/json.d.ts +0 -2
  231. package/build/compiler/outputs/json.js +0 -8
  232. package/build/compiler/outputs/json.js.map +0 -1
  233. package/build/compiler/outputs/typescript.d.ts +0 -2
  234. package/build/compiler/outputs/typescript.js +0 -21
  235. package/build/compiler/outputs/typescript.js.map +0 -1
  236. package/build/parser/algorithms/lr.d.ts +0 -7
  237. package/build/parser/algorithms/lr.js +0 -108
  238. package/build/parser/algorithms/lr.js.map +0 -5
  239. package/build/parser/algorithms/lr0.d.ts +0 -7
  240. package/build/parser/algorithms/lr0.js +0 -156
  241. package/build/parser/algorithms/lr0.js.map +0 -1
  242. package/build/typings.d.ts +0 -226
  243. package/build/typings.js +0 -3
  244. package/build/typings.js.map +0 -1
  245. package/src/compiler/builtin/json.gwell +0 -74
  246. package/src/compiler/builtin/number.gwell +0 -20
  247. package/src/compiler/builtin/string.gwell +0 -48
  248. package/src/compiler/builtin/whitespace.gwell +0 -10
  249. package/src/compiler/builtin.json +0 -1
  250. package/src/compiler/compiler.ts +0 -265
  251. package/src/compiler/generator/artifacts/standard.ts +0 -26
  252. package/src/compiler/generator/generator.ts +0 -237
  253. package/src/compiler/gwell.gwell +0 -294
  254. package/src/compiler/gwell.js +0 -578
  255. package/src/compiler/import-resolver.ts +0 -36
  256. package/src/compiler/index.ts +0 -2
  257. package/src/compiler/outputs/javascript.ts +0 -27
  258. package/src/compiler/outputs/json.ts +0 -5
  259. package/src/compiler/outputs/typescript.ts +0 -18
  260. package/src/parser/parser.ts +0 -77
  261. package/src/typings.ts +0 -248
  262. package/testing.ts +0 -18
@@ -0,0 +1,413 @@
1
+ lexer {
2
+ start: "main"
3
+
4
+ [main]
5
+ - import string, ws, comment, l_star, lifecycle
6
+ - when r:{lexer(?![a-zA-Z\d_])} tag "T_WORD" highlight "tag" goto lexer
7
+ - when r:{grammar(?![a-zA-Z\d_])} tag "T_WORD" highlight "tag" goto grammar
8
+ - when r:{config(?![a-zA-Z\d_])} tag "T_WORD" highlight "tag" goto config
9
+ - import kv
10
+
11
+ [lifecycle] span {
12
+
13
+ [start]
14
+ - when r:{on(?![a-zA-Z\d_])} tag "T_WORD" highlight "tag"
15
+
16
+ [span]
17
+ - import js_body, l_colon, word
18
+
19
+ [stop]
20
+ - before r:{[\'"`]}
21
+
22
+ }
23
+
24
+ [config] span {
25
+ transition: "set"
26
+
27
+ [start]
28
+ - when r:{\s+} tag "T_WS" stay
29
+ - when "{"
30
+
31
+ [span]
32
+ - import comment, kv
33
+
34
+ [stop]
35
+ - when "}"
36
+
37
+ }
38
+
39
+ [grammar] span {
40
+ transition: "set"
41
+
42
+ [start]
43
+ - when r:{\s+} tag "T_WS" stay
44
+ - when "{"
45
+
46
+ [span]
47
+ - import comment, section_word, js_template, ws, insensitive, regex, l_qmark, l_plus, l_star, kv, l_colon, l_at, l_comma, l_pipe, l_parenl, l_parenr, l_abracketl, l_abracketr, l_arrow, l_dsign, l_dash
48
+
49
+ [stop]
50
+ - when "}"
51
+
52
+ }
53
+
54
+ [lexer] span {
55
+ transition: "set"
56
+
57
+ [start]
58
+ - when r:{\s+} tag "T_WS" stay
59
+ - when "{"
60
+
61
+ [span]
62
+ - import ws, comment, section_word, regex, l_comma, l_arrow, l_dash, kv, l_colon, lexer_span
63
+
64
+ [stop]
65
+ - when "}"
66
+
67
+ }
68
+
69
+ [lexer_span] span {
70
+
71
+ [start]
72
+ - when "{"
73
+
74
+ [span]
75
+ - import ws, comment, section_word, regex, l_comma, l_arrow, l_dash, kv
76
+
77
+ [stop]
78
+ - when "}"
79
+
80
+ }
81
+
82
+ [js_body]
83
+ - import ws
84
+ - when "{" tag "T_JSBODY" highlight "annotation" set js_literal
85
+
86
+ [js_literal]
87
+ default: tag "T_JSBODY";
88
+ unmatched: tag "T_JSBODY";
89
+ - import jsignore
90
+ - when "{" highlight "annotation" goto js_literal
91
+ - when "}" highlight "annotation" pop
92
+ - when "(" highlight "annotation" goto js_literal
93
+ - when ")" highlight "annotation" pop
94
+ - when "[" highlight "annotation" goto js_literal
95
+ - when "]" highlight "annotation" pop
96
+
97
+ [js_template]
98
+ - when "=>" highlight "annotation" goto js_template_inner
99
+
100
+ [js_template_inner]
101
+ - import ws
102
+ - when "${" highlight "annotation" set js_literal
103
+ - when "(" highlight "annotation" set js_literal
104
+ - when "{" highlight "annotation" set js_literal
105
+ - when "[" highlight "annotation" set js_literal
106
+
107
+ [kv]
108
+ - import string, ws, word, l_colon, integer, l_scolon
109
+
110
+ [jsignore]
111
+ - when r:{"(?:\\.|[^"\\\r\n])*"} tag "T_JSBODY"
112
+ - when r:{'(?:\\.|[^'\\\r\n])*'} tag "T_JSBODY"
113
+ - when r:{`(?:\\.|[^`\\])*`} tag "T_JSBODY"
114
+ - when r:{/(?:\\.|[^/\\\r\n])+/[gmiyu]*} tag "T_JSBODY"
115
+ - when r:{//[^\n]*} tag "T_JSBODY"
116
+ - when r:{/\*.*\*/} tag "T_JSBODY"
117
+
118
+ [string]
119
+ - when r:{"(?:\\.|[^"\\\r\n])*"} tag "T_STRING" highlight "string"
120
+
121
+ [string2]
122
+ - when r:{'(?:\\.|[^'\\\r\n])*'} tag "T_STRING" highlight "string"
123
+
124
+ [string3]
125
+ - when r:{`(?:\\.|[^`\\])*`} tag "T_STRING" highlight "string"
126
+
127
+ [regex] span {
128
+
129
+ [start]
130
+ - when r:{r[gmiuy]*:\{} tag "REGEX_DEFSTART" highlight "annotation"
131
+
132
+ [span]
133
+ - when r:{\\.} tag "T_REGEX" highlight "string.escape"
134
+ - import regex_charclass
135
+ - when r:{\{[\d\s,]+\}} tag "T_REGEX" highlight "number"
136
+ - when r:{[?!:^$+*.|]} tag "T_REGEX" highlight "keyword"
137
+ - when r:{[()]} tag "T_REGEX" highlight "delimiter"
138
+ - when r:{[^\}]+?} tag "T_REGEX" highlight "string"
139
+
140
+ [stop]
141
+ - when "}" tag "REGEX_DEFEND" highlight "annotation"
142
+
143
+ }
144
+
145
+ [regex_charclass] span {
146
+
147
+ [start]
148
+ - when "[^" tag "T_REGEX" highlight "regexp"
149
+ - when "[" tag "T_REGEX" highlight "regexp"
150
+
151
+ [span]
152
+ - when r:{\\.} tag "T_REGEX" highlight "string.escape"
153
+ - when "-" tag "T_REGEX" highlight "keyword"
154
+ - when r:{[^\]]+?} tag "T_REGEX" highlight "regexp"
155
+
156
+ [stop]
157
+ - when "]" tag "T_REGEX" highlight "regexp"
158
+
159
+ }
160
+
161
+ [insensitive]
162
+ - when "i:" highlight "constant"
163
+
164
+ [integer]
165
+ - when r:{\d+} tag "T_INTEGER" highlight "number"
166
+
167
+ [word]
168
+ - when r:{[a-zA-Z_][a-zA-Z_\d]*} tag "T_WORD"
169
+
170
+ [section_word]
171
+ - when r:{\[\s*[a-zA-Z_][a-zA-Z_\d]*\s*\]} tag "T_SECTWORD" highlight "type.identifier"
172
+
173
+ [ws]
174
+ - when r:{\s+} tag "T_WS"
175
+
176
+ [l_at]
177
+ - when "@" highlight "keyword"
178
+
179
+ [l_colon]
180
+ - when ":" highlight "keyword"
181
+
182
+ [l_scolon]
183
+ - when ";"
184
+
185
+ [l_qmark]
186
+ - when "?"
187
+
188
+ [l_plus]
189
+ - when "+"
190
+
191
+ [l_star]
192
+ - when "*"
193
+
194
+ [l_comma]
195
+ - when ","
196
+
197
+ [l_pipe]
198
+ - when "|" highlight "keyword"
199
+
200
+ [l_parenl]
201
+ - when "("
202
+
203
+ [l_parenr]
204
+ - when ")"
205
+
206
+ [l_cbracketl]
207
+ - when "{"
208
+
209
+ [l_cbracketr]
210
+ - when "}"
211
+
212
+ [l_abracketl]
213
+ - when "<"
214
+
215
+ [l_abracketr]
216
+ - when ">"
217
+
218
+ [l_squarel]
219
+ - when "[" highlight "variable"
220
+
221
+ [l_squarer]
222
+ - when "]" highlight "variable"
223
+
224
+ [l_arrow]
225
+ - when "->" highlight "keyword"
226
+
227
+ [l_dsign]
228
+ - when "$"
229
+
230
+ [l_dash]
231
+ - when "-"
232
+
233
+ [comment]
234
+ - when r:{//[^\n]*} tag "T_COMMENT" highlight "comment"
235
+
236
+ [commentmulti]
237
+ - when r:{/\*.*\*/} tag "T_COMMENT" highlight "comment"
238
+
239
+ }
240
+
241
+ grammar {
242
+
243
+ [main]
244
+ | _ section_list _ => ( $1 )
245
+
246
+ [section_list]
247
+ | section => ( [$0] )
248
+ | section __ section_list => ( [$0].concat($2) )
249
+
250
+ [section]
251
+ | "config" _ "{" _ kv_list@list _ "}" => ( { config: Object.assign(...$list) } )
252
+ | "import" _ "*" _ "from" __ T_WORD@import _ ";" => ( { import: $import } )
253
+ | "import" _ "*" _ "from" __ T_STRING@import _ ";" => ( { import: $import, path: true } )
254
+ | "import" _ "*" _ "as" _ T_WORD@alias _ "from" __ T_WORD@import _ ";" => ( { import: $import, alias: $alias} )
255
+ | "import" _ "*" _ "as" _ T_WORD@alias _ "from" __ T_STRING@import _ ";" => ( { import: $import, path: true, alias: $alias} )
256
+ | "lexer" _ "{" _ lexer@lexer _ "}" => ( { lexer: Object.assign(...$lexer) } )
257
+ | "grammar" _ "{" _ grammar@grammar _ "}" => ( { grammar: $grammar } )
258
+ | r:{on} ":" T_WORD@lifecycle _ T_JS@js => ( { lifecycle: $lifecycle, js: $js } )
259
+ | r:{on} ":" T_WORD@lifecycle _ T_STRING@js => ( { lifecycle: $lifecycle, js: $js, path: true } )
260
+
261
+ [lexer]
262
+ | kv_list _ state_list => ( $0.concat({ states: $2 }) )
263
+ | state_list => ( [{ states: $0 }] )
264
+
265
+ [state_list]
266
+ | state => ( [$0] )
267
+ | state _ state_list => ( [$0].concat($2) )
268
+
269
+ [state]
270
+ | T_SECTWORD _ state_definition => ( { name: $0, state: $2 } )
271
+
272
+ [state_definition]
273
+ | state_config_list _ token_list => ( Object.assign($0, { rules: $2 }) )
274
+ | token_list => ( { rules: $0 } )
275
+ | "span" _ "{" _ kv_list@kv _ state_list@span _ "}" => ( { config: Object.assign(...$kv), span: $span} )
276
+ | "span" _ "{" _ state_list@span _ "}" => ( { span: $span} )
277
+
278
+ [state_config_list]
279
+ | state_config => ( $0 )
280
+ | state_config _ state_config_list => ( Object.assign($0, $2) )
281
+
282
+ [state_config]
283
+ | T_WORD _ ":" _ token_definition_list _ ";" => ( {[$0]: Object.assign(...$4) } )
284
+
285
+ [token_list]
286
+ | token => ( data )
287
+ | token _ token_list => ( [$0].concat($2) )
288
+
289
+ [token]
290
+ | "-" _ "import" _ word_list => ( { import: $4 } )
291
+ | "-" _ token_definition_list => ( Object.assign(...$2) )
292
+
293
+ [token_definition_list]
294
+ | token_definition => ( data )
295
+ | token_definition _ token_definition_list => ( [$0].concat($2) )
296
+
297
+ [token_definition]
298
+ | "open" _ T_STRING => ( { open: $2 } )
299
+ | "close" _ T_STRING => ( { close: $2 } )
300
+ | "tag" _ string_list => ( { tag: $2 } )
301
+ | "when" _ T_STRING => ( { when: $2 } )
302
+ | "when" _ T_REGEX => ( { when: $2 } )
303
+ | "before" _ T_STRING => ( { when: $2, before: true } )
304
+ | "before" _ T_REGEX => ( { when: $2, before: true } )
305
+ | "skip" _ T_STRING => ( { when: $2, skip: true } )
306
+ | "skip" _ T_REGEX => ( { when: $2, skip: true } )
307
+ | "stay" => ( { stay: true } )
308
+ | "pop" => ( { pop: 1 } )
309
+ | "pop" _ T_INTEGER => ( { pop: parseInt($2) } )
310
+ | "pop" _ "all" => ( { pop: "all" } )
311
+ | "highlight" _ T_STRING => ( { highlight: $2 } )
312
+ | "embed" _ T_STRING => ( { embed: $2 } )
313
+ | "unembed" => ( { unembed: true } )
314
+ | "inset" => ( { inset: 1 } )
315
+ | "inset" _ T_INTEGER => ( { inset: parseInt($2) } )
316
+ | "set" _ T_WORD => ( { set: $2 } )
317
+ | "goto" _ T_WORD => ( { goto: $2 } )
318
+ | "type" _ T_STRING => ( { type: $2 } )
319
+
320
+ [grammar]
321
+ | kv_list _ grammar_rule_list => ( { config: Object.assign(...$0), rules: $2 } )
322
+ | grammar_rule_list => ( { rules: $0 } )
323
+
324
+ [grammar_rule_list]
325
+ | grammar_rule => ( [$0] )
326
+ | grammar_rule _ grammar_rule_list => ( [$0].concat($2) )
327
+
328
+ [grammar_rule]
329
+ | grammar_rule_name _ expression_list => ( { name: $0, expressions: $2 } )
330
+ | grammar_rule_name __ POSTPROCESSOR@template _ expression_list@expressions => ( { name: $0, expressions: $expressions, postprocess: $template } )
331
+
332
+ [grammar_rule_name]
333
+ | T_SECTWORD => ( $0 )
334
+
335
+ [expression_list]
336
+ | expression => ( [$0] )
337
+ | "|" _ expression => ( [$2] )
338
+ | expression_list _ "|" _ expression => ( $0.concat([$4]) )
339
+
340
+ [expression]
341
+ | expression_symbol_list => ( { symbols: $0 } )
342
+ | expression_symbol_list _ POSTPROCESSOR@template => ( { symbols: $0, postprocess: $template } )
343
+
344
+ [expression_symbol_list]
345
+ | expression_symbol
346
+ | expression_symbol_list __ expression_symbol => ( $0.concat([$2]) )
347
+
348
+ [expression_symbol]
349
+ | expression_symbol_match => ( $0 )
350
+ | expression_symbol_match "@" T_WORD => ( { ...$0, alias: $2 } )
351
+ | expression_symbol_match expression_repeater => ( { expression: $0, repeat: $1 } )
352
+ | expression_symbol_match expression_repeater "@" T_WORD => ( { expression: $0, repeat: $1, alias: $4 } )
353
+
354
+ [expression_symbol_match]
355
+ | T_WORD => ( { rule: $0 } )
356
+ | T_STRING => ( { literal: $0, insensitive: false } )
357
+ | "i:" T_STRING => ( { literal: $2, insensitive: true } )
358
+ | "<" _ T_WORD@token _ ">" => ( { token: $token} )
359
+ | "<" _ T_STRING@token _ ">" => ( { token: $token} )
360
+ | T_REGEX => ( $0 )
361
+ | "(" _ expression_list _ ")" => ( { subexpression: $2 } )
362
+
363
+ [expression_repeater] => ( $0.value )
364
+ | "?"
365
+ | "+"
366
+ | "*"
367
+
368
+ [kv_list]
369
+ | kv => ( data )
370
+ | kv _ kv_list => ( [$0].concat($2) )
371
+
372
+ [kv]
373
+ | T_WORD _ ":" _ (T_WORD | T_STRING | T_INTEGER | T_JS) => ( { [$0]: $4[0] } )
374
+
375
+ [string_list]
376
+ | T_STRING => ( [$0] )
377
+ | T_STRING _ "," _ string_list => ( [$0].concat($4) )
378
+
379
+ [word_list]
380
+ | T_WORD => ( [$0] )
381
+ | T_WORD _ "," _ word_list => ( [$0].concat($4) )
382
+
383
+ [_]
384
+ | (<T_WS> | <T_COMMENT>)* => ( null )
385
+
386
+ [__]
387
+ | (<T_WS> | <T_COMMENT>)+ => ( null )
388
+
389
+ [T_JS]
390
+ | "{" <T_JSBODY>* "}" => ( { js: $1.map(v=>v.value).join('') } )
391
+
392
+ [POSTPROCESSOR] => ( { template: $2.value + $3.map(v=>v.value).join('').trim() + $4.value} )
393
+ | "=>" _ "(" <T_JSBODY>* ")"
394
+ | "=>" _ "{" <T_JSBODY>* "}"
395
+ | "=>" _ "[" <T_JSBODY>* "]"
396
+ | "=>" _ "${" <T_JSBODY>* "}" => ( { js: $3.map(v=>v.value).join('').trim() } )
397
+
398
+ [T_STRING]
399
+ | <T_STRING> => ( JSON.parse($0.value) )
400
+
401
+ [T_WORD]
402
+ | <T_WORD> => ( $0.value )
403
+
404
+ [T_SECTWORD]
405
+ | <T_SECTWORD> => ( $0.value.substring(1, $0.value.length-1).trim() )
406
+
407
+ [T_REGEX]
408
+ | <REGEX_DEFSTART> <T_REGEX>* <REGEX_DEFEND> => ( { regex: $1.map(v=>v.value).join(''), flags: $0.value.slice(1,-3) } )
409
+
410
+ [T_INTEGER]
411
+ | <T_INTEGER> => ( $0.value )
412
+
413
+ }
@@ -0,0 +1,12 @@
1
+ import { ImportResolverConstructor } from "../../typings/index.js";
2
+ let resolver;
3
+
4
+ if (typeof process && process?.release?.name?.search(/node|io\.js/) >= 0) {
5
+ const { FileSystemResolver } = await import("./filesystem.js");
6
+ resolver = FileSystemResolver;
7
+ } else {
8
+ const { BrowserImportResolver } = await import("./browser.js");
9
+ resolver = BrowserImportResolver;
10
+ }
11
+
12
+ export const AutoImportResolver = resolver as ImportResolverConstructor;
@@ -0,0 +1,13 @@
1
+ import { ImportResolver } from "../../typings/index.js";
2
+
3
+ export class BrowserImportResolver implements ImportResolver {
4
+ constructor(private baseURL: string) { }
5
+
6
+ path(path: string) {
7
+ return (new URL(path, this.baseURL)).href;
8
+ }
9
+
10
+ async body(path: string) {
11
+ return (await fetch(path)).text();
12
+ }
13
+ }
@@ -0,0 +1,18 @@
1
+ import { readFile } from 'fs/promises';
2
+ import { dirname, resolve } from 'path';
3
+ import { ImportResolver } from "../../typings/index.js";
4
+
5
+ export class FileSystemResolver implements ImportResolver {
6
+ private baseDir: string;
7
+ constructor(baseDir: string) {
8
+ this.baseDir = baseDir ? dirname(baseDir) : process?.cwd();
9
+ }
10
+
11
+ path(path: string) {
12
+ return resolve(this.baseDir, path);
13
+ }
14
+
15
+ body(path: string) {
16
+ return readFile(path, 'utf-8');
17
+ }
18
+ }
@@ -0,0 +1,3 @@
1
+ export * from './generator.js';
2
+ export { default as V1Grammar } from './grammars/v1.js';
3
+ export { default as V2Grammar } from './grammars/v2.js';
@@ -0,0 +1,89 @@
1
+ import { GeneratorGrammarProductionRule, GeneratorLexerConfig, GeneratorLexerState, GeneratorStateGrammar } from "../typings/index.js";
2
+
3
+ export class GeneratorState {
4
+ grammar?: GeneratorStateGrammar;
5
+ lexer?: GeneratorLexerConfig;
6
+ lifecycle: {
7
+ import?: string;
8
+ new?: string;
9
+ } = {}
10
+
11
+ config = {};
12
+ version: string = 'unknown';
13
+
14
+ merge(state: GeneratorState) {
15
+ if (state.grammar) {
16
+ this.initializeGrammar();
17
+ Object.assign(this.grammar.rules, state.grammar.rules);
18
+ this.grammar.start = state.grammar.start || this.grammar.start;
19
+ this.grammar.config.postprocessorDefault = state.grammar.config.postprocessorDefault || this.grammar.config.postprocessorDefault;
20
+ this.grammar.config.postprocessorOverride = state.grammar.config.postprocessorOverride || this.grammar.config.postprocessorOverride;
21
+ }
22
+
23
+ if (state.lexer) {
24
+ this.initializeLexer();
25
+ Object.assign(this.lexer.states, state.lexer.states);
26
+ this.lexer.start = state.lexer.start || this.lexer.start;
27
+ }
28
+
29
+ for (const key in state.lifecycle) {
30
+ this.addLifecycle(key, state.lifecycle[key]);
31
+ }
32
+
33
+ Object.assign(this.config, state.config);
34
+ }
35
+
36
+ grammarUUID(name: string) {
37
+ this.grammar.uuids[name] = (this.grammar.uuids[name] || 0) + 1;
38
+ return name + 'x' + this.grammar.uuids[name];
39
+ }
40
+
41
+ initializeGrammar() {
42
+ if (!this.grammar) {
43
+ this.grammar = {
44
+ start: '',
45
+ config: {},
46
+ rules: {},
47
+ uuids: {},
48
+ }
49
+ }
50
+ }
51
+
52
+ addGrammarRule(rule: GeneratorGrammarProductionRule) {
53
+ this.grammar.rules[rule.name] = this.grammar.rules[rule.name] || [];
54
+ this.grammar.rules[rule.name].push(rule);
55
+ }
56
+
57
+ initializeLexer() {
58
+ if (!this.lexer) {
59
+ this.lexer = {
60
+ start: '',
61
+ states: {}
62
+ };
63
+ }
64
+ }
65
+
66
+ addLexerState(name: string, state?: GeneratorLexerState) {
67
+ this.lexer.states[name] = this.lexer.states[name] || { rules: [] }
68
+ if (state) {
69
+ const target = this.lexer.states[name];
70
+ target.unmatched = typeof state.unmatched != "undefined" ? state.unmatched : target.unmatched;
71
+ target.rules.push(...state.rules);
72
+ }
73
+ }
74
+
75
+ addLifecycle(lifecycle: string, literal: string) {
76
+ this.lifecycle[lifecycle] = this.lifecycle[lifecycle] || '';
77
+ this.lifecycle[lifecycle] += literal;
78
+ }
79
+
80
+ export() {
81
+ return {
82
+ grammar: this.grammar,
83
+ lexer: this.lexer,
84
+ lifecycle: this.lifecycle,
85
+ config: this.config,
86
+ version: this.version
87
+ }
88
+ }
89
+ }
@@ -0,0 +1,90 @@
1
+ import { ASTGrammarSymbolLiteral, ASTGrammarSymbolNonTerminal, ASTGrammarSymbolRegex, ASTGrammarSymbolToken, GeneratorGrammarSymbol } from "../../typings/index.js";
2
+ import { GeneratorState } from "../state.js";
3
+
4
+ export class CommonGenerator {
5
+ constructor(public state: GeneratorState) { }
6
+
7
+ static NewLine(indent: number) {
8
+ return '\n' + ' '.repeat(indent * 4);
9
+ }
10
+
11
+ static SmartIndent(current: number, increment = 1) {
12
+ if (current >= 0) {
13
+ return CommonGenerator.NewLine(current + increment);
14
+ }
15
+ return ' ';
16
+ }
17
+
18
+ static JSON(obj: string[] | { [key: string]: string | (string[]) }, indent = 0) {
19
+ return Array.isArray(obj) ? CommonGenerator.JSONArray(obj, indent) : CommonGenerator.JSONObject(obj, indent);
20
+ }
21
+
22
+ static JSONArray(obj: string[], indent = 0) {
23
+ let r = `[`;
24
+ for (let i = 0; i < obj.length; i++) {
25
+ const value = obj[i];
26
+ r += `${CommonGenerator.SmartIndent(indent)}${value}${(CommonGenerator.IsVal(obj[i + 1]) ? ',' : '')}`;
27
+ }
28
+ r += `${CommonGenerator.SmartIndent(indent, 0)}]`;
29
+ return r;
30
+ }
31
+
32
+ static JSONObject(obj: { [key: string]: string | (string[]) }, indent = 0) {
33
+ let r = `{`;
34
+ let seperator = '';
35
+ const prefix = CommonGenerator.SmartIndent(indent);
36
+ const keys = Object.keys(obj).sort();
37
+ for (const k of keys) {
38
+ const v = obj[k];
39
+ if (CommonGenerator.IsVal(v)) {
40
+ const key = /^[a-z_][a-z\d_$]*$/i.test(k) ? k : JSON.stringify(k);
41
+ const value = Array.isArray(v) ? CommonGenerator.JSON(v, indent + 1 || -1) : v;
42
+ r += `${seperator}${prefix}${key}: ${value}`;
43
+ seperator = ','
44
+ }
45
+ }
46
+ r += `${CommonGenerator.SmartIndent(indent, 0)}}`;
47
+ return r;
48
+ }
49
+
50
+ static IsVal(value) {
51
+ return typeof value !== 'undefined' && value !== null;
52
+ }
53
+
54
+ static SerializeSymbol(s: GeneratorGrammarSymbol) {
55
+ if (typeof s === 'string') {
56
+ return JSON.stringify(s);
57
+ } else if ('rule' in s) {
58
+ return CommonGenerator.SerializeSymbolNonTerminal(s);
59
+ } else if ('regex' in s) {
60
+ return CommonGenerator.SerializeSymbolRegex(s);
61
+ } else if ('token' in s) {
62
+ return CommonGenerator.SerializeSymbolToken(s);
63
+ } else if ('literal' in s) {
64
+ return CommonGenerator.SerializeSymbolLiteral(s);
65
+ } else {
66
+ return JSON.stringify(s);
67
+ }
68
+ }
69
+
70
+ static SerializeSymbolNonTerminal(s: ASTGrammarSymbolNonTerminal) {
71
+ return JSON.stringify(s.rule);
72
+ }
73
+
74
+ static SerializeSymbolRegex(s: ASTGrammarSymbolRegex) {
75
+ return `/${(new RegExp(s.regex, s.flags)).source}/${s.flags || ''}`;
76
+ }
77
+
78
+ static SerializeSymbolToken(s: ASTGrammarSymbolToken) {
79
+ return `{ token: ${JSON.stringify(s.token)} }`;
80
+ }
81
+
82
+ static SerializeSymbolLiteral(s: ASTGrammarSymbolLiteral) {
83
+ return `{ literal: ${JSON.stringify(s.literal)} }`;
84
+ }
85
+
86
+ static SymbolIsTerminal(s: GeneratorGrammarSymbol) {
87
+ return !(typeof s === 'string' || 'rule' in s);
88
+ }
89
+
90
+ }
@@ -0,0 +1,29 @@
1
+ import { JavaScriptGenerator } from "../javascript.js";
2
+
3
+ export function CJSOutput(generator: JavaScriptGenerator) {
4
+ const exportName = generator.name();
5
+ return `${Generate(generator)}
6
+
7
+ if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
8
+ module.exports = ${exportName};
9
+ } else {
10
+ window.${exportName} = ${exportName};
11
+ }`;
12
+ }
13
+
14
+ export function ESMOutput(generator: JavaScriptGenerator) {
15
+ return `${Generate(generator)}\n\nexport default ${generator.name()};`;
16
+ }
17
+
18
+ function Generate(generator: JavaScriptGenerator) {
19
+ const exportName = generator.name();
20
+ return `// Generated automatically by Grammar-Well, version ${generator.state.version}
21
+ // https://github.com/0x6563/grammar-well
22
+
23
+ ${generator.lifecycle('import')}
24
+
25
+ class ${exportName} {
26
+ artifacts = ${generator.artifacts(1)}
27
+ constructor(){${generator.lifecycle('new')}}
28
+ }`;
29
+ }
@@ -0,0 +1,5 @@
1
+ import { JavaScriptGenerator } from "../javascript.js";
2
+
3
+ export function JSONFormatter(generator: JavaScriptGenerator) {
4
+ return JSON.stringify({ state: generator.state.export(), output: generator.options });
5
+ }
@@ -0,0 +1,20 @@
1
+
2
+ import { JavaScriptGenerator } from "../javascript.js";
3
+ import { CJSOutput, ESMOutput } from "./javascript.js";
4
+ import { JSONFormatter } from "./json.js";
5
+ import { TypescriptFormat } from "./typescript.js";
6
+
7
+
8
+ export const ExportsRegistry = {
9
+ object: (generator: JavaScriptGenerator) => ({ state: generator.state, output: generator.options }),
10
+ json: JSONFormatter,
11
+ js: CJSOutput,
12
+ cjs: CJSOutput,
13
+ javascript: CJSOutput,
14
+ commonjs: CJSOutput,
15
+ module: ESMOutput,
16
+ esmodule: ESMOutput,
17
+ esm: ESMOutput,
18
+ ts: TypescriptFormat,
19
+ typescript: TypescriptFormat
20
+ }