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
@@ -1,226 +0,0 @@
1
- import { TokenBuffer } from "./lexers/token-buffer";
2
- import { ParserUtility } from "./parser/parser";
3
- export interface Dictionary<T> {
4
- [key: string]: T;
5
- }
6
- export interface CompileOptions {
7
- version?: string;
8
- noscript?: boolean;
9
- basedir?: string;
10
- resolver?: ImportResolverConstructor;
11
- resolverInstance?: ImportResolver;
12
- exportName?: string;
13
- template?: TemplateFormat;
14
- overrides?: Dictionary<string>;
15
- }
16
- export type TemplateFormat = '_default' | 'object' | 'json' | 'js' | 'javascript' | 'module' | 'esmodule' | 'esm' | 'ts' | 'typescript';
17
- export interface GrammarBuilderContext {
18
- alreadyCompiled: Set<string>;
19
- resolver: ImportResolver;
20
- uuids: Dictionary<number>;
21
- }
22
- export interface ImportResolver {
23
- path(path: string): string;
24
- body(path: string): Promise<string>;
25
- }
26
- export interface ImportResolverConstructor {
27
- new (basePath: string): ImportResolver;
28
- }
29
- export type PostProcessor = (payload: PostProcessorPayload) => any;
30
- interface PostProcessorPayload {
31
- data: any[];
32
- rule: GrammarRule;
33
- meta: any;
34
- }
35
- export type JavascriptDirective = {
36
- body: GrammarTypeJS;
37
- } | {
38
- head: GrammarTypeJS;
39
- };
40
- export interface ImportDirective {
41
- import: string;
42
- path?: boolean;
43
- alias?: string;
44
- }
45
- export interface ConfigDirective {
46
- config: Dictionary<any>;
47
- }
48
- export interface GrammarDirective {
49
- grammar: {
50
- config?: Dictionary<any>;
51
- rules: GrammarBuilderRule[];
52
- };
53
- }
54
- export interface LexerDirective {
55
- lexer: {
56
- start?: string;
57
- states: LexerStateDefinition[];
58
- };
59
- }
60
- export interface GrammarBuilderRule {
61
- name: string;
62
- expressions: GrammarBuilderExpression[];
63
- postprocess?: GrammarTypeJS | GrammarTypeBuiltIn | GrammarTypeTemplate;
64
- }
65
- export interface GrammarBuilderExpression {
66
- symbols: GrammarBuilderSymbol[];
67
- postprocess?: GrammarTypeJS | GrammarTypeBuiltIn | GrammarTypeTemplate;
68
- }
69
- export type GrammarBuilderSymbol = GrammarTypeRule | GrammarTypeRegex | GrammarTypeToken | GrammarTypeLiteral | GrammarBuilderSymbolRepeat | GrammarBuilderSymbolSubexpression;
70
- export interface GrammarBuilderSymbolSubexpression {
71
- subexpression: GrammarBuilderExpression[];
72
- }
73
- export interface GrammarBuilderSymbolRepeat {
74
- expression: GrammarBuilderSymbol;
75
- repeat: "+" | "*" | "?";
76
- }
77
- export interface GrammarTypeRule {
78
- rule: string;
79
- }
80
- export interface GrammarTypeRegex {
81
- regex: string;
82
- flags?: string;
83
- }
84
- export interface GrammarTypeToken {
85
- token: string;
86
- }
87
- export interface GrammarTypeLiteral {
88
- literal: string;
89
- insensitive?: boolean;
90
- }
91
- export type GrammarTypeBuiltIn = {
92
- builtin: string;
93
- };
94
- export type GrammarTypeTemplate = {
95
- template: string;
96
- };
97
- export type GrammarTypeJS = {
98
- js: string;
99
- };
100
- export type ParserAlgorithm = ((language: LanguageDefinition & {
101
- tokens: TokenBuffer;
102
- utility: ParserUtility;
103
- }, options?: any) => {
104
- results: any[];
105
- info?: any;
106
- });
107
- export type LanguageDirective = (JavascriptDirective | ImportDirective | ConfigDirective | GrammarDirective | LexerDirective);
108
- type GrammarRuleSymbolFunction = (data: LexerToken) => boolean;
109
- export interface GrammarRule {
110
- name: string;
111
- symbols: GrammarRuleSymbol[];
112
- postprocess?: PostProcessor;
113
- }
114
- export type GrammarRuleSymbol = string | RegExp | GrammarTypeLiteral | GrammarTypeToken | GrammarRuleSymbolFunction;
115
- export interface GeneratorGrammarRule {
116
- name: string;
117
- symbols: GeneratorGrammarSymbol[];
118
- postprocess?: GrammarTypeTemplate | GrammarTypeBuiltIn | GrammarTypeJS;
119
- }
120
- export type GeneratorGrammarSymbol = {
121
- alias?: string;
122
- } & (GrammarTypeRule | GrammarTypeRegex | GrammarTypeLiteral | GrammarTypeToken);
123
- export interface LanguageDefinition {
124
- lexer?: Lexer | LexerConfig;
125
- grammar: {
126
- start: string;
127
- rules: Dictionary<GrammarRule[]>;
128
- };
129
- lr?: {
130
- k: number;
131
- table: Dictionary<LRState>;
132
- };
133
- }
134
- export interface LRState {
135
- actions: Next[];
136
- goto: {
137
- [key: string]: string;
138
- };
139
- reduce?: GrammarRule;
140
- isFinal: boolean;
141
- }
142
- type Next = {
143
- symbol: GrammarRuleSymbol;
144
- next: string;
145
- };
146
- export interface TQRestorePoint {
147
- historyIndex: number;
148
- offset: number;
149
- }
150
- export interface Lexer {
151
- next(): LexerToken | undefined;
152
- feed(chunk?: string, state?: ReturnType<Lexer['state']>): void;
153
- state(): any;
154
- flush?(): void;
155
- }
156
- export interface LexerToken {
157
- type?: string | undefined;
158
- tag?: Set<string>;
159
- value: string;
160
- offset: number;
161
- line: number;
162
- column: number;
163
- }
164
- export interface LexerStatus {
165
- index: number;
166
- line: number;
167
- column: number;
168
- state: string;
169
- }
170
- export interface LexerStateDefinition {
171
- name: string;
172
- unmatched?: string;
173
- default?: string;
174
- rules: (LexerStateImportRule | LexerStateMatchRule)[];
175
- }
176
- export interface LexerStateImportRule {
177
- import: string[];
178
- }
179
- export interface LexerStateMatchRule {
180
- when: string | RegExp;
181
- type?: string;
182
- tag?: string[];
183
- pop?: number | 'all';
184
- before?: boolean;
185
- highlight?: string;
186
- open?: string;
187
- close?: string;
188
- embed?: string;
189
- unembed?: boolean;
190
- inset?: number;
191
- goto?: string;
192
- set?: string;
193
- }
194
- export interface ResolvedStateDefinition {
195
- name: string;
196
- unmatched?: string;
197
- rules: LexerStateMatchRule[];
198
- }
199
- export interface CompiledStateDefinition {
200
- regexp: RegExp;
201
- unmatched?: LexerStateMatchRule;
202
- rules: LexerStateMatchRule[];
203
- }
204
- export interface LexerConfig {
205
- start?: string;
206
- states: Dictionary<LexerStateDefinition>;
207
- }
208
- export interface GeneratorState {
209
- version: string;
210
- config: Dictionary<string>;
211
- head: string[];
212
- body: string[];
213
- lexer?: LexerConfig;
214
- grammar: {
215
- start: string;
216
- config: {
217
- postprocessorDefault?: GrammarTypeJS | GrammarTypeTemplate;
218
- postprocessorOverride?: GrammarTypeJS | GrammarTypeTemplate;
219
- };
220
- rules: Dictionary<GeneratorGrammarRule[]>;
221
- uuids: {
222
- [key: string]: number;
223
- };
224
- };
225
- }
226
- export {};
package/build/typings.js DELETED
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=typings.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"typings.js","sourceRoot":"","sources":["../src/typings.ts"],"names":[],"mappings":""}
@@ -1,74 +0,0 @@
1
- import * from whitespace;
2
-
3
- lexer: {{
4
- start: "json"
5
-
6
- json ->
7
- - import: whitespace
8
- - when: /-?(?:[0-9]|[1-9][0-9]+)(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\b/ tag: "number"
9
- - when: /"(?:\\["bfnrt\/\\]|\\u[a-fA-F0-9]{4}|[^"\\])*"/ tag: "string"
10
- - when: "{" tag: "{"
11
- - when: "}" tag: "}"
12
- - when: "[" tag: "["
13
- - when: "]" tag: "]"
14
- - when: "," tag: ","
15
- - when: ":" tag: ":"
16
- - when: "true" tag: "true"
17
- - when: "false" tag: "false"
18
- - when: "null" tag: "null"
19
- }}
20
-
21
- grammar: {{
22
- json -> _ (object | array) _ : {{ $1[0] }}
23
-
24
- object -> "{" _ "}" : {{ {} }}
25
- | "{" _ pair (_ "," _ pair)* _ "}" : ${ extractObject }
26
-
27
- array -> "[" _ "]" : {{ [] }}
28
- | "[" _ value (_ "," _ value)* _ "]" : ${ extractArray }
29
-
30
- value : {{ $0 }} ->
31
- object
32
- | array
33
- | number
34
- | string
35
- | "true" : {{ true }}
36
- | "false" : {{ false }}
37
- | "null" : {{ null }}
38
-
39
- number -> $number : {{ parseFloat($0.value) }}
40
-
41
- string -> $string : {{ JSON.parse($0.value) }}
42
-
43
- pair -> key:k _ ":" _ value:v : {{ [$k, $v] }}
44
-
45
- key -> string : {{ $0 }}
46
- }}
47
-
48
- head: ${
49
- function extractPair(kv, output) {
50
- if(kv[0]) { output[kv[0]] = kv[1]; }
51
- }
52
-
53
- function extractObject({data}) {
54
- let output = {};
55
-
56
- extractPair(data[2], output);
57
-
58
- for (let i in data[3]) {
59
- extractPair(data[3][i][3], output);
60
- }
61
-
62
- return output;
63
- }
64
-
65
- function extractArray({data}) {
66
- let output = [data[2]];
67
-
68
- for (let i in data[3]) {
69
- output.push(data[3][i][3]);
70
- }
71
-
72
- return output;
73
- }
74
- }
@@ -1,20 +0,0 @@
1
- grammar: {{
2
-
3
- unsigned_int ->
4
- /[0-9]/+ : {{ parseInt($0.join("")) }}
5
-
6
- int ->
7
- ("-"|"+")? /[0-9]/+ : {{ $0 ? parseInt($0[0]+$1.join("")) : parseInt($1.join("")) }}
8
-
9
- unsigned_decimal ->
10
- /[0-9]/+ ("." /[0-9]/+)? : {{ parseFloat($0.join("") + ($1 ? "."+$1[1].join("") : "")) }}
11
-
12
- decimal ->
13
- "-"? /[0-9]/+ ("." /[0-9]/+)? : {{ parseFloat( ($0 || "") + $1.join("") +($2 ? "."+$2[1].join("") : "")) }}
14
-
15
- percentage ->
16
- decimal "%" : {{ $0/100 }}
17
-
18
- jsonfloat ->
19
- "-"? /[0-9]/+ ("." /[0-9]/+)? (/[eE]/ /[+-]/? /[0-9]/+)? : {{ parseFloat( ($0 || "") + $1.join("") + ($2 ? "."+$2[1].join("") : "") + ($3 ? "e" + ($3[1] || "+") + $3[2].join("") : "")) }}
20
- }}
@@ -1,48 +0,0 @@
1
- lexer: {{
2
- string ->
3
- - import: singleQuoteString, doubleQuoteString
4
-
5
- doubleQuoteString ->
6
- - when: /"/ tag: "dquote" highlight:"string" goto: doubleQuoteStringEnd
7
-
8
- singleQuoteString ->
9
- - when: /'/ tag: "squote" highlight:"string" goto: singleQuoteStringEnd
10
-
11
- doubleQuoteStringEnd ->
12
- - when: /\\[\\\/bnrft]/ tag: "escaped" highlight:"constant"
13
- - when: /\\"/ tag: "quoteEscape"
14
- - when: /\\u[A-Fa-f\d]{4}/ tag: "escaped" highlight:"constant"
15
- - when: /\\./ tag:"badEscape"
16
- - when: /[^"\\]+/ tag: "string" highlight:"string"
17
- - when: "\"" tag: "dquote" highlight:"string" pop
18
-
19
- singleQuoteStringEnd ->
20
- - when: /\\[\\\/bnrft]/ tag: "escaped"
21
- - when: /\\'/ tag: "quoteEscape"
22
- - when: /\\u[A-Fa-f\d]{4}/ tag: "escaped"
23
- - when: /\\./ tag:"badEscape"
24
- - when: /[^'\\]+/ tag: "string" highlight:"string"
25
- - when: "'" tag: "squote" highlight:"string" pop
26
- }}
27
-
28
- grammar: {{
29
- string : {{ $0 }}
30
- -> singleQuoteString
31
- | doubleQuoteString
32
-
33
- doubleQuoteString
34
- -> $dquote stringInner $dquote : {{ $1 }}
35
- | $dquote $dquote : {{ '' }}
36
-
37
- singleQuoteString
38
- -> $squote stringInner $squote : {{ $1 }}
39
- | $squote $squote : {{ '' }}
40
-
41
- stringInner -> stringEscape : {{ $0 }}
42
- | $string : {{ $0.value }}
43
- | stringInner stringEscape : {{ $0 + $1 }}
44
- | stringInner $string : {{ $0 + $1.value }}
45
-
46
- stringEscape -> $escaped : {{ JSON.parse('"' +$0.value + '"') }}
47
- | $quoteEscape : {{ $0.value[1] }}
48
- }}
@@ -1,10 +0,0 @@
1
- lexer: {{
2
- start: "whitespace"
3
- whitespace ->
4
- - when: /\s+/ tag: "whitespace"
5
- }}
6
-
7
- grammar: {{
8
- _ -> $whitespace* : {{ null }}
9
- __ -> $whitespace+ : {{ null }}
10
- }}
@@ -1 +0,0 @@
1
- {"json":"import * from whitespace;\r\n\r\nlexer: {{\r\n start: \"json\"\r\n\r\n json ->\r\n - import: whitespace\r\n - when: /-?(?:[0-9]|[1-9][0-9]+)(?:\\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\\b/ tag: \"number\"\r\n - when: /\"(?:\\\\[\"bfnrt\\/\\\\]|\\\\u[a-fA-F0-9]{4}|[^\"\\\\])*\"/ tag: \"string\"\r\n - when: \"{\" tag: \"{\"\r\n - when: \"}\" tag: \"}\"\r\n - when: \"[\" tag: \"[\"\r\n - when: \"]\" tag: \"]\"\r\n - when: \",\" tag: \",\"\r\n - when: \":\" tag: \":\"\r\n - when: \"true\" tag: \"true\"\r\n - when: \"false\" tag: \"false\"\r\n - when: \"null\" tag: \"null\"\r\n}}\r\n\r\ngrammar: {{\r\n json -> _ (object | array) _ : {{ $1[0] }}\r\n\r\n object -> \"{\" _ \"}\" : {{ {} }}\r\n | \"{\" _ pair (_ \",\" _ pair)* _ \"}\" : ${ extractObject }\r\n\r\n array -> \"[\" _ \"]\" : {{ [] }}\r\n | \"[\" _ value (_ \",\" _ value)* _ \"]\" : ${ extractArray }\r\n\r\n value : {{ $0 }} ->\r\n object\r\n | array\r\n | number\r\n | string\r\n | \"true\" : {{ true }}\r\n | \"false\" : {{ false }}\r\n | \"null\" : {{ null }}\r\n\r\n number -> $number : {{ parseFloat($0.value) }}\r\n\r\n string -> $string : {{ JSON.parse($0.value) }}\r\n\r\n pair -> key:k _ \":\" _ value:v : {{ [$k, $v] }}\r\n\r\n key -> string : {{ $0 }}\r\n}}\r\n\r\nhead: ${\r\n function extractPair(kv, output) {\r\n if(kv[0]) { output[kv[0]] = kv[1]; }\r\n }\r\n\r\n function extractObject({data}) {\r\n let output = {};\r\n\r\n extractPair(data[2], output);\r\n\r\n for (let i in data[3]) {\r\n extractPair(data[3][i][3], output);\r\n }\r\n\r\n return output;\r\n }\r\n\r\n function extractArray({data}) {\r\n let output = [data[2]];\r\n\r\n for (let i in data[3]) {\r\n output.push(data[3][i][3]);\r\n }\r\n\r\n return output;\r\n }\r\n}\r\n","number":"grammar: {{\r\n\r\n unsigned_int -> \r\n /[0-9]/+ : {{ parseInt($0.join(\"\")) }}\r\n\r\n int -> \r\n (\"-\"|\"+\")? /[0-9]/+ : {{ $0 ? parseInt($0[0]+$1.join(\"\")) : parseInt($1.join(\"\")) }}\r\n\r\n unsigned_decimal -> \r\n /[0-9]/+ (\".\" /[0-9]/+)? : {{ parseFloat($0.join(\"\") + ($1 ? \".\"+$1[1].join(\"\") : \"\")) }}\r\n\r\n decimal -> \r\n \"-\"? /[0-9]/+ (\".\" /[0-9]/+)? : {{ parseFloat( ($0 || \"\") + $1.join(\"\") +($2 ? \".\"+$2[1].join(\"\") : \"\")) }}\r\n\r\n percentage -> \r\n decimal \"%\" : {{ $0/100 }}\r\n \r\n jsonfloat -> \r\n \"-\"? /[0-9]/+ (\".\" /[0-9]/+)? (/[eE]/ /[+-]/? /[0-9]/+)? : {{ parseFloat( ($0 || \"\") + $1.join(\"\") + ($2 ? \".\"+$2[1].join(\"\") : \"\") + ($3 ? \"e\" + ($3[1] || \"+\") + $3[2].join(\"\") : \"\")) }}\r\n}}","string":"lexer: {{\r\n string ->\r\n - import: singleQuoteString, doubleQuoteString\r\n\r\n doubleQuoteString ->\r\n - when: /\"/ tag: \"dquote\" highlight:\"string\" goto: doubleQuoteStringEnd \r\n\r\n singleQuoteString ->\r\n - when: /'/ tag: \"squote\" highlight:\"string\" goto: singleQuoteStringEnd \r\n\r\n doubleQuoteStringEnd ->\r\n - when: /\\\\[\\\\\\/bnrft]/ tag: \"escaped\" highlight:\"constant\"\r\n - when: /\\\\\"/ tag: \"quoteEscape\"\r\n - when: /\\\\u[A-Fa-f\\d]{4}/ tag: \"escaped\" highlight:\"constant\"\r\n - when: /\\\\./ tag:\"badEscape\"\r\n - when: /[^\"\\\\]+/ tag: \"string\" highlight:\"string\"\r\n - when: \"\\\"\" tag: \"dquote\" highlight:\"string\" pop\r\n\r\n singleQuoteStringEnd ->\r\n - when: /\\\\[\\\\\\/bnrft]/ tag: \"escaped\"\r\n - when: /\\\\'/ tag: \"quoteEscape\"\r\n - when: /\\\\u[A-Fa-f\\d]{4}/ tag: \"escaped\" \r\n - when: /\\\\./ tag:\"badEscape\"\r\n - when: /[^'\\\\]+/ tag: \"string\" highlight:\"string\"\r\n - when: \"'\" tag: \"squote\" highlight:\"string\" pop\r\n}}\r\n\r\ngrammar: {{\r\n string : {{ $0 }} \r\n -> singleQuoteString \r\n | doubleQuoteString \r\n\r\n doubleQuoteString \r\n -> $dquote stringInner $dquote : {{ $1 }}\r\n | $dquote $dquote : {{ '' }}\r\n \r\n singleQuoteString \r\n -> $squote stringInner $squote : {{ $1 }}\r\n | $squote $squote : {{ '' }}\r\n\r\n stringInner -> stringEscape : {{ $0 }}\r\n | $string : {{ $0.value }}\r\n | stringInner stringEscape : {{ $0 + $1 }}\r\n | stringInner $string : {{ $0 + $1.value }}\r\n \r\n stringEscape -> $escaped : {{ JSON.parse('\"' +$0.value + '\"') }}\r\n | $quoteEscape : {{ $0.value[1] }}\r\n}}","whitespace":"lexer: {{\r\n start: \"whitespace\"\r\n whitespace ->\r\n - when: /\\s+/ tag: \"whitespace\" \r\n}}\r\n\r\ngrammar: {{\r\n _ -> $whitespace* : {{ null }}\r\n __ -> $whitespace+ : {{ null }}\r\n}}"}
@@ -1,265 +0,0 @@
1
- import { CompileOptions, GrammarBuilderContext, TemplateFormat, LanguageDirective, ConfigDirective, GrammarBuilderSymbolRepeat, GrammarBuilderExpression, GeneratorGrammarRule, GrammarDirective, ImportDirective, LexerDirective, GrammarBuilderSymbolSubexpression, GrammarTypeLiteral, GeneratorGrammarSymbol, GrammarBuilderSymbol, GrammarBuilderRule } from "../typings";
2
-
3
- import { Parser } from "../parser/parser";
4
- import { FileSystemResolver } from "./import-resolver";
5
- import Language from './gwell';
6
-
7
- import { ESMOutput, JavascriptOutput } from "./outputs/javascript";
8
- import { TypescriptFormat } from "./outputs/typescript";
9
- import { JSONFormatter } from "./outputs/json";
10
-
11
- import { Generator } from "./generator/generator";
12
- import * as BuiltInRegistry from "./builtin.json"
13
-
14
- const TemplateFormats = {
15
- _default: JavascriptOutput,
16
- object: (grammar, exportName) => ({ grammar, exportName }),
17
- json: JSONFormatter,
18
- js: JavascriptOutput,
19
- javascript: JavascriptOutput,
20
- module: ESMOutput,
21
- esmodule: ESMOutput,
22
- esm: ESMOutput,
23
- ts: TypescriptFormat,
24
- typescript: TypescriptFormat
25
- }
26
-
27
- export async function Compile(rules: string | LanguageDirective | (LanguageDirective[]), config: CompileOptions = {}) {
28
- const builder = new GrammarBuilder(config);
29
- await builder.import(rules as any);
30
- Object.assign(builder.generator.state.config, config.overrides);
31
- return builder.export(config.template);
32
- }
33
-
34
- export class GrammarBuilder {
35
- private parser = new Parser(Language() as any);
36
- private context: GrammarBuilderContext;
37
-
38
- generator = new Generator();
39
-
40
- constructor(private config: CompileOptions = {}, context?: GrammarBuilderContext, private alias: string = '') {
41
- this.context = context || {
42
- alreadyCompiled: new Set(),
43
- resolver: config.resolverInstance ? config.resolverInstance : config.resolver ? new config.resolver(config.basedir) : new FileSystemResolver(config.basedir),
44
- uuids: {}
45
- }
46
- this.generator.state.grammar.uuids = this.context.uuids;
47
- }
48
-
49
- export<T extends TemplateFormat = '_default'>(format: T, name: string = 'GWLanguage'): ReturnType<typeof TemplateFormats[T]> {
50
- const grammar = this.generator.state;
51
- const output = format || grammar.config.preprocessor || '_default';
52
- if (TemplateFormats[output]) {
53
- return TemplateFormats[output](this.generator, name);
54
- }
55
- throw new Error("No such preprocessor: " + output)
56
- }
57
-
58
- async import(source: string): Promise<void>
59
- async import(directive: LanguageDirective): Promise<void>
60
- async import(directives: LanguageDirective[]): Promise<void>
61
- async import(directives: string | LanguageDirective | (LanguageDirective[])): Promise<void> {
62
- if (typeof directives == 'string') {
63
- await this.mergeLanguageDefinitionString(directives);
64
- return;
65
- }
66
- directives = Array.isArray(directives) ? directives : [directives];
67
- for (const directive of directives) {
68
- if ("head" in directive) {
69
- this.generator.state.head.push(directive.head.js);
70
- } else if ("body" in directive) {
71
- this.generator.state.body.push(directive.body.js);
72
- } else if ("import" in directive) {
73
- await this.processImportDirective(directive);
74
- } else if ("config" in directive) {
75
- this.processConfigDirective(directive);
76
- } else if ("grammar" in directive) {
77
- this.processGrammarDirective(directive);
78
- } else if ("lexer" in directive) {
79
- this.processLexerDirective(directive);
80
- }
81
- }
82
- }
83
-
84
- private async processImportDirective(directive: ImportDirective) {
85
- if (directive.path) {
86
- await this.importGrammar(directive.import, this.alias + (directive.alias || ''));
87
- } else {
88
- await this.importBuiltIn(directive.import, this.alias + (directive.alias || ''));
89
- }
90
- }
91
-
92
- private processConfigDirective(directive: ConfigDirective) {
93
- Object.assign(this.generator.state.config, directive.config);
94
- }
95
-
96
- private processGrammarDirective(directive: GrammarDirective) {
97
- if (directive.grammar.config) {
98
- if (directive.grammar.config.start) {
99
- this.generator.state.grammar.start = this.alias + directive.grammar.config.start;
100
- }
101
-
102
- Object.assign(this.generator.state.grammar.config, directive.grammar.config);
103
- // this.generator.state.grammar.postprocessDefault = directive.grammar.config.postprocessDefault || this.generator.state.grammar.postprocessDefault;
104
- // this.generator.state.grammar.postprocessOverride = directive.grammar.config.postprocessOverride || this.generator.state.grammar.postprocessOverride;
105
- }
106
-
107
- if (!this.generator.state.grammar.start && directive.grammar.rules.length) {
108
- this.generator.state.grammar.start = this.alias + directive.grammar.rules[0].name;
109
- }
110
-
111
- for (const rule of directive.grammar.rules) {
112
- rule.name = this.alias + rule.name;
113
- this.buildRules(rule.name, rule.expressions, rule);
114
- }
115
- }
116
-
117
- private processLexerDirective(directive: LexerDirective) {
118
- if (!this.generator.state.lexer) {
119
- this.generator.state.lexer = {
120
- start: '',
121
- states: {}
122
- };
123
- }
124
- if (directive.lexer.start) {
125
- this.generator.state.lexer.start = this.alias + directive.lexer.start;
126
- }
127
-
128
- if (!this.generator.state.lexer.start && directive.lexer.states.length) {
129
- this.generator.state.lexer.start = this.alias + directive.lexer.states[0].name
130
- }
131
-
132
- for (const state of directive.lexer.states) {
133
- state.name = this.alias + state.name;
134
- if (this.alias) {
135
- state.rules.forEach(v => {
136
- if ('import' in v) {
137
- v.import = v.import.map(v2 => this.alias + v2);
138
- }
139
- if ('set' in v) {
140
- v.set = this.alias + v.set;
141
- }
142
- if ('goto' in v) {
143
- v.goto = this.alias + v.goto;
144
- }
145
- })
146
- }
147
- this.generator.addLexerState(state);
148
- }
149
- }
150
-
151
- private async importBuiltIn(name: string, alias?: string) {
152
- name = name.toLowerCase();
153
- if (!this.context.alreadyCompiled.has(name)) {
154
- this.context.alreadyCompiled.add(name);
155
- if (!BuiltInRegistry[name])
156
- return;
157
- await this.mergeLanguageDefinitionString(BuiltInRegistry[name], alias);
158
- }
159
- }
160
-
161
- private async importGrammar(path: string, alias?: string) {
162
- const resolver = this.context.resolver;
163
- const fullPath = resolver.path(path);
164
- if (!this.context.alreadyCompiled.has(fullPath)) {
165
- this.context.alreadyCompiled.add(fullPath);
166
- await this.mergeLanguageDefinitionString(await resolver.body(fullPath), alias);
167
- }
168
- }
169
-
170
- private async mergeLanguageDefinitionString(body: string, alias: string = '') {
171
- const builder = new GrammarBuilder(this.config, this.context, alias);
172
- await builder.import(this.parser.run(body).results[0]);
173
- this.generator.merge(builder.generator.state);
174
- return;
175
- }
176
-
177
- private buildRules(name: string, expressions: GrammarBuilderExpression[], rule?: GrammarBuilderRule) {
178
- for (const expression of expressions) {
179
- this.generator.addGrammarRule(this.buildRule(name, expression, rule));
180
- }
181
- }
182
-
183
- private buildRule(name: string, expression: GrammarBuilderExpression, rule?: GrammarBuilderRule): GeneratorGrammarRule {
184
- const symbols: GeneratorGrammarSymbol[] = [];
185
- for (let i = 0; i < expression.symbols.length; i++) {
186
- const symbol = this.buildSymbol(name, expression.symbols[i]);
187
- if (symbol)
188
- symbols.push(symbol);
189
- }
190
- return { name, symbols, postprocess: expression.postprocess || rule?.postprocess };
191
- }
192
-
193
- private buildSymbol(name: string, symbol: GrammarBuilderSymbol): GeneratorGrammarSymbol {
194
- if ('repeat' in symbol) {
195
- return this.buildRepeatRules(name, symbol);
196
- }
197
- if ('rule' in symbol) {
198
- return { ...symbol, rule: this.alias + symbol.rule };
199
- }
200
- if ('regex' in symbol) {
201
- return symbol;
202
- }
203
- if ('token' in symbol) {
204
- return symbol;
205
- }
206
- if ('literal' in symbol) {
207
- if (!symbol.literal.length) {
208
- return null;
209
- }
210
- if (symbol.literal.length === 1 || this.generator.state.lexer) {
211
- return symbol;
212
- }
213
- return this.buildCharacterRules(name, symbol);
214
- }
215
- if ('subexpression' in symbol) {
216
- return this.buildSubExpressionRules(name, symbol);
217
- }
218
- }
219
-
220
- private buildCharacterRules(name: string, symbol: GrammarTypeLiteral) {
221
- const id = this.generator.grammarUUID(name + "$STR");
222
- this.buildRules(id, [
223
- {
224
- symbols: symbol.literal
225
- .split("")
226
- .map((literal) => {
227
- if (symbol.insensitive && literal.toLowerCase() != literal.toUpperCase())
228
- return { regex: literal, flags: 'i' }
229
- return { literal }
230
- }),
231
- postprocess: { builtin: "join" }
232
- }
233
- ]);
234
- return { rule: id };
235
- }
236
-
237
- private buildSubExpressionRules(name: string, symbol: GrammarBuilderSymbolSubexpression) {
238
- const id = this.generator.grammarUUID(name + "$SUB");
239
- this.buildRules(id, symbol.subexpression);
240
- return { rule: id };
241
- }
242
-
243
- private buildRepeatRules(name: string, symbol: GrammarBuilderSymbolRepeat) {
244
- let id: string;
245
- const expr1: GrammarBuilderExpression = { symbols: [] };
246
- const expr2: GrammarBuilderExpression = { symbols: [] };
247
- if (symbol.repeat == '+') {
248
- id = this.generator.grammarUUID(name + "$RPT1N");
249
- expr1.symbols = [symbol.expression];
250
- expr2.symbols = [{ rule: id }, symbol.expression];
251
- expr2.postprocess = { builtin: "concat" };
252
- } else if (symbol.repeat == '*') {
253
- id = this.generator.grammarUUID(name + "$RPT0N");
254
- expr2.symbols = [{ rule: id }, symbol.expression];
255
- expr2.postprocess = { builtin: "concat" };
256
- } else if (symbol.repeat == '?') {
257
- id = this.generator.grammarUUID(name + "$RPT01");
258
- expr1.symbols = [symbol.expression];
259
- expr1.postprocess = { builtin: "first" };
260
- expr2.postprocess = { builtin: "null" };
261
- }
262
- this.buildRules(id, [expr1, expr2]);
263
- return { rule: id };
264
- }
265
- }