grammar-well 1.1.10 → 1.2.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.
- package/README.md +209 -7
- package/bootstrap.ts +1 -1
- package/build/compiler/compiler.d.ts +1 -1
- package/build/compiler/compiler.js +2 -1
- package/build/compiler/compiler.js.map +1 -1
- package/build/compiler/generator/artifacts/lr.d.ts +51 -0
- package/build/{utility → compiler/generator/artifacts}/lr.js +53 -58
- package/build/compiler/generator/artifacts/lr.js.map +1 -0
- package/build/compiler/generator/artifacts/standard.d.ts +7 -0
- package/build/compiler/generator/artifacts/standard.js +28 -0
- package/build/compiler/generator/artifacts/standard.js.map +1 -0
- package/build/compiler/generator/generator.d.ts +24 -0
- package/build/compiler/{generator.js → generator/generator.js} +71 -75
- package/build/compiler/generator/generator.js.map +1 -0
- package/build/compiler/outputs/javascript.d.ts +1 -1
- package/build/compiler/outputs/json.d.ts +1 -1
- package/build/compiler/outputs/typescript.d.ts +1 -1
- package/build/grammars/gwell.js +1 -1
- package/build/parser/algorithms/lr.d.ts +2 -5
- package/build/parser/algorithms/lr.js +41 -14
- package/build/parser/algorithms/lr.js.map +1 -1
- package/build/typings.d.ts +18 -1
- package/build/utility/general.d.ts +3 -4
- package/build/utility/general.js +6 -10
- package/build/utility/general.js.map +1 -1
- package/package.json +1 -1
- package/src/compiler/compiler.ts +3 -2
- package/src/compiler/generator/artifacts/lr.ts +155 -0
- package/src/compiler/generator/artifacts/standard.ts +26 -0
- package/src/compiler/{generator.ts → generator/generator.ts} +75 -75
- package/src/compiler/outputs/javascript.ts +1 -1
- package/src/compiler/outputs/json.ts +1 -1
- package/src/compiler/outputs/typescript.ts +1 -1
- package/src/grammars/gwell.gwell +1 -1
- package/src/grammars/gwell.js +1 -1
- package/src/grammars/gwell.json +1 -1
- package/src/parser/algorithms/lr.ts +55 -18
- package/src/typings.ts +17 -1
- package/src/utility/general.ts +6 -9
- package/build/compiler/generator.d.ts +0 -23
- package/build/compiler/generator.js.map +0 -1
- package/build/utility/lr.d.ts +0 -52
- package/build/utility/lr.js.map +0 -1
- package/src/utility/lr.ts +0 -152
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# Grammar Well
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Grammar Well is a cross-platform compiler, parser, and/or interpreter. It originated as a port of the popular library [Nearley](https://github.com/kach/nearley) to TypeScript but has since evolved to include additional functionality, such as a built-in lexer and support for various parsing algorithms including LR0 and CKY. It also offers a range of quality of life features.
|
|
4
|
+
|
|
5
|
+
Check out the Demo/Live Editor https://0x6563.github.io/grammar-well-editor
|
|
3
6
|
|
|
4
7
|
# Quick Start
|
|
5
8
|
### Install
|
|
@@ -104,9 +107,208 @@ const input = `{"a":"string","b":true,"c":2}`
|
|
|
104
107
|
console.log(await GrammarWellRunner(source, input))
|
|
105
108
|
```
|
|
106
109
|
|
|
107
|
-
#
|
|
108
|
-
|
|
110
|
+
# Quick Guide
|
|
111
|
+
Below is a quick guide highlighting some of the grammars features.
|
|
112
|
+
|
|
113
|
+
# Terminology
|
|
114
|
+
Thse are some terms you may find used around the documentation.
|
|
115
|
+
#### Word
|
|
116
|
+
A **word** refers to an alphanumeric string that matches `/[a-zA-Z_][a-zA-Z_\d]*/`
|
|
117
|
+
|
|
118
|
+
#### Symbol
|
|
119
|
+
A **Symbol** refers to **Terminals** and **Non Terminals** found in EBNF expressions.
|
|
120
|
+
|
|
121
|
+
# Grammar Sections
|
|
122
|
+
Grammar Well's grammar is broken into 4 sections **head**, **body**, **lexer**, and **grammar**
|
|
123
|
+
.
|
|
124
|
+
## Head/Body
|
|
125
|
+
Both the **head** and **body** section have the same syntax. These sections refer to literal javascript to embed in the compiled version of the grammar. The difference between the two is that **head** will be placed outside of the parse function and the **body** will be plaed within the parse function. This allows to have static variables that are persisted across executions, or not, depending on your use case.
|
|
126
|
+
|
|
127
|
+
_example using both head and body to keep track the number of parses_
|
|
128
|
+
```
|
|
129
|
+
head: ${
|
|
130
|
+
let parses = 0;
|
|
131
|
+
|
|
132
|
+
export function GetParseCount(){
|
|
133
|
+
return parses;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
body :${
|
|
138
|
+
parses++;
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
## Lexer
|
|
144
|
+
Grammar Well utilizes a stateful lexer, which is optional but highly recommended due to its significant assistance in constructing production rules for the grammar. The lexer configuration comprises two subsections: config and states. It is important to note that the configuration section must be placed at the top. **Currently**, the sole configuration option available is the optional setting start, which determines the initial lexer state to begin with.
|
|
145
|
+
|
|
146
|
+
```
|
|
147
|
+
lexer: {{
|
|
148
|
+
start: "root"
|
|
149
|
+
|
|
150
|
+
root ->
|
|
151
|
+
- import: string, ws
|
|
152
|
+
- when: /\/(?:[^\/\\\r\n]|\\.)+\// tag: "REGEX" highlight: "regexp"
|
|
153
|
+
- when: "{" tag: "CURLY_L" goto: body
|
|
154
|
+
|
|
155
|
+
body->
|
|
156
|
+
- import: string, ws
|
|
157
|
+
- when: "}" tag: "CURLY_R" pop: 1
|
|
158
|
+
|
|
159
|
+
string ->
|
|
160
|
+
- when: /"(?:[^"\\\r\n]|\\.)*"/ tag: "T_STRING" highlight: "string"
|
|
161
|
+
|
|
162
|
+
ws ->
|
|
163
|
+
- when: /\s+/ tag: "T_WS"
|
|
164
|
+
}}
|
|
165
|
+
```
|
|
166
|
+
In the above example, we start with a state named `root` followed by `-` delimited list of rules. There are two type of rules _import_ rules and _token matching_ rules. Order is important.
|
|
167
|
+
|
|
168
|
+
### Import Rule
|
|
169
|
+
```
|
|
170
|
+
- import: string, ws
|
|
171
|
+
```
|
|
172
|
+
The first rule we see is in the example is an _import_ rule. The import rule expects a comma delimited list of states whose rules are to be imported in to this state. This is a convenient way of keeping your rules DRY.
|
|
173
|
+
|
|
174
|
+
### Token Matching Rule
|
|
175
|
+
```
|
|
176
|
+
- when: /\/(?:[^\/\\\r\n]|\\.)+\// tag: "REGEX" highlight: "regexp"
|
|
177
|
+
- when: "{" tag: "CURLY_L" goto: body
|
|
178
|
+
|
|
179
|
+
```
|
|
180
|
+
This other rules we see in the example are _token matching_ rules. As the name implies, we declare what to match in the input stream. Below is a list of fields that are available.
|
|
181
|
+
|
|
182
|
+
#### Fields
|
|
183
|
+
- `when` [ _string | regex_] **required** what to match in the input stream
|
|
184
|
+
- `tag` [_comma delimited strings_] Applies tags to the matched token, these can be referenced in the grammar
|
|
185
|
+
- `goto` [_word_] Moves to the defined state and adds the current state onto the stack
|
|
186
|
+
- `pop` [_number|nothing_] Pops 1 or the number of states off the stack
|
|
187
|
+
- `highlight` [_string_] This isn't used directly but can be used to help generate syntax highlighting.
|
|
188
|
+
- `inset` [_number|nothing_] Adds the current state onto the stack 1 or the number of times defined.
|
|
189
|
+
|
|
190
|
+
# Grammar
|
|
191
|
+
The **grammar** section is heavily based on the EBNF notation with some additional syntax to help with generating output. If you aren't familiar with EBNF it would be good to start there before continuing.
|
|
192
|
+
|
|
193
|
+
The basic syntax is a **Rule Name** followed by `->` and then a **Expression** (combinations of **Terminals** and **Non Terminals**). Multiple expressions can be declared for a rule by seperating them with a `|`. Depending on the parsing algorithm you choose there maybe additional constraints on the expression.
|
|
194
|
+
|
|
195
|
+
_In this example.
|
|
196
|
+
There are 4 Non Terminals; `Start`, `HelloGoodbye`, `Target`, and `__`.
|
|
197
|
+
As well as 5 Terminals; `"Hello"`, `"Goodbye"`, `"World"`, `/[a-zA-Z]+/`, and `$ws`_
|
|
198
|
+
```
|
|
199
|
+
Start -> HelloGoodbye __ Target
|
|
200
|
+
|
|
201
|
+
HelloGoodbye -> "Hello"
|
|
202
|
+
| "Goodbye"
|
|
203
|
+
|
|
204
|
+
Target -> "World"
|
|
205
|
+
| /[a-zA-Z]+/
|
|
206
|
+
|
|
207
|
+
__ -> $ws
|
|
208
|
+
```
|
|
209
|
+
### Post Processors
|
|
210
|
+
In addition to the standard anatomy of EBNF. Grammar Well supports **Post Processors**. **Post Processors** are used to either evaluate or transform a matched expression. They can follow a rule name but before the `->` seperator to apply to each expression as the default but overridable postprocessor. They can follow an expression to only apply to that segment.
|
|
211
|
+
|
|
212
|
+
_In this example, you see two different types of post processors_
|
|
213
|
+
```
|
|
214
|
+
Target : ${ ({data}) => data[0].value } ->
|
|
215
|
+
"World"
|
|
216
|
+
| /[a-zA-Z]+/ : {{ $0.value }}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
**JavaScript Literal** ` : ${ JavaScriptFunction }`
|
|
220
|
+
|
|
221
|
+
The JavaScript Literal versions expects a JavaScript function.
|
|
222
|
+
|
|
223
|
+
**JavaScript Template** ` : {{ JavaScriptFunctionBody }}`
|
|
224
|
+
|
|
225
|
+
**Ordinal References**
|
|
226
|
+
|
|
227
|
+
The Javascript Template version expects a function body and is provided a variable `data`. It will also do simple string replacements. For example any `$` followed by a number will be replaced with `data[number]`.
|
|
228
|
+
|
|
229
|
+
_example displaying different syntax but equal functionality_
|
|
230
|
+
```
|
|
231
|
+
Rule -> "Hello" : {{ $0.value }}
|
|
232
|
+
|
|
233
|
+
Rule -> "Hello" : ${ ({data}) => data[0].value }
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
**Aliased References**
|
|
237
|
+
|
|
238
|
+
Keeping tracking of the ordinal index of your symbols in an expression can be tedious, so Grammar Well also provides aliasing. Any symbol in an expression can be suffixed with `:word`. That **word** can then be referenced in the template.
|
|
239
|
+
|
|
240
|
+
_example displaying different syntax but equal functionality_
|
|
241
|
+
```
|
|
242
|
+
Rule-> "Hello":hello : {{ $hello.value }}
|
|
243
|
+
|
|
244
|
+
Rule-> "Hello" : ${ ({data}) => data[0].value }
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
### Rule Name
|
|
250
|
+
The name of a rule must be a **word** (regex: `/[a-zA-Z_][a-zA-Z_\d]+/`)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
### Expression
|
|
254
|
+
Besides having Terminals and Non Terminals. Each expression can also be followed by a **PostProcessor**.
|
|
255
|
+
|
|
256
|
+
_example of a rule with 2 expressions with postprocessors for each_
|
|
257
|
+
```
|
|
258
|
+
Target -> "World" : ${ (data) => data[0].value }
|
|
259
|
+
| /[a-zA-Z]+/ : {{ $0.value }}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### Non Terminals
|
|
263
|
+
**Non Terminals** are simple. They are **word**s that refer to another rule.
|
|
264
|
+
|
|
265
|
+
### Terminals
|
|
266
|
+
There are three different types of terminals that Grammar Well supports:
|
|
267
|
+
|
|
268
|
+
**Literals**
|
|
269
|
+
|
|
270
|
+
Literals are double quoted strings that are case strings that are matched in the lexer stream. Optionaly strings can be modified to allow case insensitive matching by appending an `i` after the end quote.
|
|
271
|
+
|
|
272
|
+
_example of having a case insensitive literal_
|
|
273
|
+
```
|
|
274
|
+
Rule-> "Hello"i
|
|
275
|
+
```
|
|
276
|
+
**Regular Expressions**
|
|
277
|
+
|
|
278
|
+
Grammar Well is written in TypeScript, which leads two things.
|
|
279
|
+
1. The syntax for regex matches that of JavaScript.
|
|
280
|
+
2. It is limited to the capabilities of JavaScript's regex.
|
|
281
|
+
|
|
282
|
+
```
|
|
283
|
+
Rule-> /[a-zA-Z]+/
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
**Token Tags**
|
|
287
|
+
|
|
288
|
+
If the lexer is used than you can refer to the tags defined by prefixing their name with an `$`
|
|
289
|
+
|
|
290
|
+
```
|
|
291
|
+
Rule-> $token
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### Grouping
|
|
295
|
+
Symbols can be grouped with `()` seperating each option with a `|`
|
|
296
|
+
```
|
|
297
|
+
Rule -> ("Hello" | "Goodbye")
|
|
298
|
+
```
|
|
299
|
+
### Modifiers
|
|
300
|
+
EBNF modifiers like `?`,`*`, and `+` are all supported
|
|
301
|
+
```
|
|
302
|
+
Whitespace -> " "*
|
|
303
|
+
```
|
|
304
|
+
### Aliasing
|
|
305
|
+
As noted above in post processors alias allow named references to symbols to be used with in a template
|
|
306
|
+
```
|
|
307
|
+
Rule-> "Hello":hello : {{ $hello.value }}
|
|
308
|
+
```
|
|
109
309
|
|
|
310
|
+
# The Big Example
|
|
311
|
+
_Grammar Well's Grammar File_
|
|
110
312
|
|
|
111
313
|
```
|
|
112
314
|
lexer: {{
|
|
@@ -226,7 +428,7 @@ grammar: {{
|
|
|
226
428
|
| section T_WS section_list : {{ [$0].concat($2) }}
|
|
227
429
|
|
|
228
430
|
section ->
|
|
229
|
-
K_CONFIG _ L_COLON _ L_TEMPLATEL _ kv_list _ L_TEMPLATER : {{ { config: Object.assign(...$
|
|
431
|
+
K_CONFIG _ L_COLON _ L_TEMPLATEL _ kv_list:list _ L_TEMPLATER : {{ { config: Object.assign(...$list) } }}
|
|
230
432
|
| K_IMPORT _ L_STAR _ K_FROM __ T_WORD:import _ L_SCOLON : {{ { import: $import } }}
|
|
231
433
|
| K_IMPORT _ L_STAR _ K_FROM __ T_STRING:import _ L_SCOLON : {{ { import: $import, path: true } }}
|
|
232
434
|
| K_LEXER _ L_COLON _ L_TEMPLATEL _ lexer:lexer _ L_TEMPLATER : {{ { lexer: Object.assign(...$lexer) } }}
|
|
@@ -306,7 +508,7 @@ grammar: {{
|
|
|
306
508
|
expression_symbol
|
|
307
509
|
| expression_symbol_list T_WS expression_symbol : {{ $0.concat([$2]) }}
|
|
308
510
|
|
|
309
|
-
|
|
511
|
+
|
|
310
512
|
expression_symbol ->
|
|
311
513
|
expression_symbol_match : {{ $0 }}
|
|
312
514
|
| expression_symbol_match L_COLON T_WORD : {{ { ...$0, alias: $2 } }}
|
|
@@ -344,7 +546,7 @@ grammar: {{
|
|
|
344
546
|
| T_WORD _ L_COMMA _ word_list : {{ [$0].concat($4) }}
|
|
345
547
|
|
|
346
548
|
_ ->
|
|
347
|
-
( T_WS | T_COMMENT )* : {{ null }}
|
|
549
|
+
( T_WS | T_COMMENT )* : {{ null }}
|
|
348
550
|
|
|
349
551
|
__ ->
|
|
350
552
|
( T_WS | T_COMMENT )+ : {{ null }}
|
|
@@ -378,7 +580,7 @@ grammar: {{
|
|
|
378
580
|
K_LEXER -> "lexer"
|
|
379
581
|
K_GRAMMAR -> "grammar"
|
|
380
582
|
K_IMPORT -> "import"
|
|
381
|
-
K_BODY -> "body"
|
|
583
|
+
K_BODY -> "body"
|
|
382
584
|
K_HEAD -> "head"
|
|
383
585
|
|
|
384
586
|
T_JS -> $L_JSL $T_JSBODY* $L_JSR : {{ { js: $1.map(v=>v.value).join('') } }}
|
package/bootstrap.ts
CHANGED
|
@@ -10,7 +10,7 @@ const BaseDir = './src/grammars';
|
|
|
10
10
|
console.log(fullpath(file))
|
|
11
11
|
if (/\.gwell$/.test(file)) {
|
|
12
12
|
const json = await Compile(read(file), { template: 'json' });
|
|
13
|
-
const js = await Compile(read(file), { exportName: 'grammar', template: 'esmodule' });
|
|
13
|
+
const js = await Compile(read(file), { exportName: 'grammar', template: 'esmodule', overrides: {} });
|
|
14
14
|
write(file.replace(/.gwell$/, '.json'), json);
|
|
15
15
|
write(file.replace(/.gwell$/, '.js'), js);
|
|
16
16
|
}
|
|
@@ -2,7 +2,7 @@ import { CompileOptions, GrammarBuilderContext, TemplateFormat, LanguageDirectiv
|
|
|
2
2
|
import { ESMOutput, JavascriptOutput } from "./outputs/javascript";
|
|
3
3
|
import { TypescriptFormat } from "./outputs/typescript";
|
|
4
4
|
import { JSONFormatter } from "./outputs/json";
|
|
5
|
-
import { Generator } from "./generator";
|
|
5
|
+
import { Generator } from "./generator/generator";
|
|
6
6
|
declare const TemplateFormats: {
|
|
7
7
|
_default: typeof JavascriptOutput;
|
|
8
8
|
object: (grammar: any, exportName: any) => {
|
|
@@ -10,7 +10,7 @@ const json_1 = require("./outputs/json");
|
|
|
10
10
|
const number = require("../grammars/number.json");
|
|
11
11
|
const string = require("../grammars/string.json");
|
|
12
12
|
const whitespace = require("../grammars/whitespace.json");
|
|
13
|
-
const generator_1 = require("./generator");
|
|
13
|
+
const generator_1 = require("./generator/generator");
|
|
14
14
|
const BuiltInRegistry = {
|
|
15
15
|
number,
|
|
16
16
|
string,
|
|
@@ -31,6 +31,7 @@ const TemplateFormats = {
|
|
|
31
31
|
async function Compile(rules, config = {}) {
|
|
32
32
|
const builder = new GrammarBuilder(config);
|
|
33
33
|
await builder.import(rules);
|
|
34
|
+
Object.assign(builder.generator.state.config, config.overrides);
|
|
34
35
|
return builder.export(config.template);
|
|
35
36
|
}
|
|
36
37
|
exports.Compile = Compile;
|
|
@@ -1 +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,
|
|
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,qDAAkD;AAElD,MAAM,eAAe,GAAG;IACpB,MAAM;IACN,MAAM;IACN,UAAU;CACb,CAAA;AACD,MAAM,eAAe,GAAG;IACpB,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,GAAG,EAAE,sBAAS;IACd,EAAE,EAAE,6BAAgB;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AAI,GAAG,QAAQ,CAAC,CAAC;YACjD,KAAK,CAAC,OAAO,GAAG,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC;YAClD,KAAK,CAAC,WAAW,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC;SAC7C;aAAM,IAAI,MAAM,CAAC,MAAM,IAAI,GAAG,EAAE;YAC7B,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,IAAI,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;AA3MD,wCA2MC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Generator } from "../generator";
|
|
2
|
+
import { Dictionary, GeneratorGrammarRule, GeneratorGrammarSymbol } from "../../../typings";
|
|
3
|
+
import { Collection, GeneratorSymbolCollection } from "../../../utility/general";
|
|
4
|
+
export declare class LRParseTableBuilder {
|
|
5
|
+
generator: Generator;
|
|
6
|
+
rules: Collection<GeneratorGrammarRule>;
|
|
7
|
+
table: Dictionary<StateBuilder>;
|
|
8
|
+
symbols: GeneratorSymbolCollection;
|
|
9
|
+
constructor(generator: Generator);
|
|
10
|
+
addState(seed: StateItem[]): StateBuilder;
|
|
11
|
+
encodeRule(rule: GeneratorGrammarRule, dot: number): string;
|
|
12
|
+
encodeStateItems(seed: StateItem[]): string;
|
|
13
|
+
serialize(depth?: number): string;
|
|
14
|
+
serializeState(state: State, depth?: number): string;
|
|
15
|
+
serializeNext(next: Next, depth: number): string;
|
|
16
|
+
}
|
|
17
|
+
declare class StateBuilder {
|
|
18
|
+
private collection;
|
|
19
|
+
isFinal: boolean;
|
|
20
|
+
outputs: StateOut;
|
|
21
|
+
queue: {
|
|
22
|
+
[key: string]: StateItem[];
|
|
23
|
+
};
|
|
24
|
+
actions: Map<GeneratorGrammarSymbol, string>;
|
|
25
|
+
goto: Map<GeneratorGrammarSymbol, string>;
|
|
26
|
+
reduce?: GeneratorGrammarRule;
|
|
27
|
+
constructor(collection: LRParseTableBuilder, items: StateItem[]);
|
|
28
|
+
private closure;
|
|
29
|
+
serialize(): State;
|
|
30
|
+
}
|
|
31
|
+
interface State {
|
|
32
|
+
actions: Next[];
|
|
33
|
+
goto: {
|
|
34
|
+
[key: string]: string;
|
|
35
|
+
};
|
|
36
|
+
reduce: GeneratorGrammarRule;
|
|
37
|
+
isFinal: boolean;
|
|
38
|
+
}
|
|
39
|
+
type Next = {
|
|
40
|
+
symbol: GeneratorGrammarSymbol;
|
|
41
|
+
next: string;
|
|
42
|
+
};
|
|
43
|
+
type StateItem = {
|
|
44
|
+
rule: GeneratorGrammarRule;
|
|
45
|
+
dot: number;
|
|
46
|
+
};
|
|
47
|
+
interface StateOut {
|
|
48
|
+
action: Dictionary<StateItem[]>;
|
|
49
|
+
goto: Dictionary<StateItem[]>;
|
|
50
|
+
}
|
|
51
|
+
export {};
|
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
const general_1 = require("
|
|
6
|
-
class
|
|
7
|
-
|
|
3
|
+
exports.LRParseTableBuilder = void 0;
|
|
4
|
+
const generator_1 = require("../generator");
|
|
5
|
+
const general_1 = require("../../../utility/general");
|
|
6
|
+
class LRParseTableBuilder {
|
|
7
|
+
generator;
|
|
8
8
|
rules = new general_1.Collection();
|
|
9
|
-
|
|
10
|
-
symbols = new general_1.
|
|
11
|
-
constructor(
|
|
12
|
-
this.
|
|
13
|
-
const augmented = { name: Symbol(), symbols: [grammar.start] };
|
|
14
|
-
grammar.rules[augmented.name] = [augmented];
|
|
9
|
+
table = Object.create(null);
|
|
10
|
+
symbols = new general_1.GeneratorSymbolCollection();
|
|
11
|
+
constructor(generator) {
|
|
12
|
+
this.generator = generator;
|
|
13
|
+
const augmented = { name: Symbol(), symbols: [{ rule: generator.state.grammar.start }] };
|
|
14
|
+
generator.state.grammar.rules[augmented.name] = [augmented];
|
|
15
15
|
this.addState([{ rule: augmented, dot: 0 }]);
|
|
16
16
|
}
|
|
17
17
|
addState(seed) {
|
|
18
18
|
const id = this.encodeStateItems(seed);
|
|
19
|
-
if (id in this.
|
|
20
|
-
return this.
|
|
21
|
-
const state = new
|
|
22
|
-
this.
|
|
19
|
+
if (id in this.table)
|
|
20
|
+
return this.table[id];
|
|
21
|
+
const state = new StateBuilder(this, seed);
|
|
22
|
+
this.table[id] = state;
|
|
23
23
|
for (const q in state.queue) {
|
|
24
24
|
this.addState(state.queue[q]);
|
|
25
25
|
}
|
|
@@ -31,9 +31,30 @@ class CanonicalCollection {
|
|
|
31
31
|
encodeStateItems(seed) {
|
|
32
32
|
return Array.from(new Set(seed)).map(v => this.encodeRule(v.rule, v.dot)).sort().join();
|
|
33
33
|
}
|
|
34
|
+
serialize(depth = 0) {
|
|
35
|
+
const map = {};
|
|
36
|
+
for (const key in this.table) {
|
|
37
|
+
map[key] = this.serializeState(this.table[key].serialize(), depth + 1);
|
|
38
|
+
}
|
|
39
|
+
return generator_1.Generator.Pretty(map, depth);
|
|
40
|
+
}
|
|
41
|
+
serializeState(state, depth = 0) {
|
|
42
|
+
return generator_1.Generator.Pretty({
|
|
43
|
+
actions: state.actions.map(v => this.serializeNext(v, depth + 1)),
|
|
44
|
+
goto: generator_1.Generator.Pretty(state.goto, depth + 1),
|
|
45
|
+
reduce: state.reduce ? this.generator.serializeGrammarRule(state.reduce) : null,
|
|
46
|
+
isFinal: state.isFinal ? 'true' : 'false'
|
|
47
|
+
}, depth);
|
|
48
|
+
}
|
|
49
|
+
serializeNext(next, depth) {
|
|
50
|
+
return generator_1.Generator.Pretty({
|
|
51
|
+
symbol: generator_1.Generator.SerializeSymbol(next.symbol),
|
|
52
|
+
next: JSON.stringify(next.next)
|
|
53
|
+
}, -1);
|
|
54
|
+
}
|
|
34
55
|
}
|
|
35
|
-
exports.
|
|
36
|
-
class
|
|
56
|
+
exports.LRParseTableBuilder = LRParseTableBuilder;
|
|
57
|
+
class StateBuilder {
|
|
37
58
|
collection;
|
|
38
59
|
isFinal = false;
|
|
39
60
|
outputs = {
|
|
@@ -51,12 +72,7 @@ class State {
|
|
|
51
72
|
this.closure(item.rule, item.dot, visited);
|
|
52
73
|
}
|
|
53
74
|
if (this.isFinal) {
|
|
54
|
-
|
|
55
|
-
this.reduce = items[0].rule;
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
throw 'Conflict Detected';
|
|
59
|
-
}
|
|
75
|
+
this.reduce = items[0].rule;
|
|
60
76
|
}
|
|
61
77
|
for (const k in this.outputs.goto) {
|
|
62
78
|
const seed = this.outputs.goto[k];
|
|
@@ -74,57 +90,36 @@ class State {
|
|
|
74
90
|
closure(rule, dot, visited) {
|
|
75
91
|
const isFinal = rule.symbols.length == dot;
|
|
76
92
|
this.isFinal = isFinal || this.isFinal;
|
|
77
|
-
const
|
|
93
|
+
const symbol = rule.symbols[dot];
|
|
78
94
|
if (isFinal || visited.has(symbol))
|
|
79
95
|
return;
|
|
80
96
|
visited.add(symbol);
|
|
81
97
|
const stateItem = { rule, dot: dot + 1 };
|
|
82
|
-
if (
|
|
98
|
+
if (generator_1.Generator.SymbolIsTerminal(symbol)) {
|
|
83
99
|
const id = this.collection.symbols.encode(symbol);
|
|
84
100
|
this.outputs.action[id] = this.outputs.action[id] || [];
|
|
85
101
|
this.outputs.action[id].push(stateItem);
|
|
86
102
|
}
|
|
87
103
|
else {
|
|
88
104
|
const id = this.collection.symbols.encode(symbol);
|
|
105
|
+
const name = typeof symbol === 'string' ? symbol : symbol.rule;
|
|
89
106
|
this.outputs.goto[id] = this.outputs.goto[id] || [];
|
|
90
107
|
this.outputs.goto[id].push(stateItem);
|
|
91
|
-
for (const rule of this.collection.grammar.rules[
|
|
108
|
+
for (const rule of this.collection.generator.state.grammar.rules[name]) {
|
|
92
109
|
this.closure(rule, 0, visited);
|
|
93
110
|
}
|
|
94
111
|
}
|
|
95
112
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
this.current.state = state;
|
|
107
|
-
}
|
|
108
|
-
reduce(rule) {
|
|
109
|
-
const n = new LRStackItem();
|
|
110
|
-
const l = rule.symbols.length;
|
|
111
|
-
n.children = this.stack.splice(l * -1, l);
|
|
112
|
-
n.children.forEach(v => delete v.state);
|
|
113
|
-
n.rule = rule;
|
|
114
|
-
n.symbol = rule.name;
|
|
115
|
-
this.stack.push(n);
|
|
116
|
-
}
|
|
117
|
-
add(symbol) {
|
|
118
|
-
this.stack.push(new LRStackItem());
|
|
119
|
-
this.current.symbol = symbol;
|
|
113
|
+
serialize() {
|
|
114
|
+
const actions = [];
|
|
115
|
+
const goto = {};
|
|
116
|
+
for (const [symbol, next] of this.actions) {
|
|
117
|
+
actions.push({ symbol, next });
|
|
118
|
+
}
|
|
119
|
+
for (const [symbol, next] of this.goto) {
|
|
120
|
+
goto[typeof symbol == 'object' ? symbol.rule : symbol] = JSON.stringify(next);
|
|
121
|
+
}
|
|
122
|
+
return { actions, goto, reduce: this.reduce, isFinal: this.isFinal };
|
|
120
123
|
}
|
|
121
124
|
}
|
|
122
|
-
exports.LRStack = LRStack;
|
|
123
|
-
class LRStackItem {
|
|
124
|
-
children = [];
|
|
125
|
-
state;
|
|
126
|
-
symbol;
|
|
127
|
-
rule;
|
|
128
|
-
value;
|
|
129
|
-
}
|
|
130
125
|
//# sourceMappingURL=lr.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lr.js","sourceRoot":"","sources":["../../../../src/compiler/generator/artifacts/lr.ts"],"names":[],"mappings":";;;AAAA,4CAAyC;AAEzC,sDAAiF;AAGjF,MAAa,mBAAmB;IAKT;IAJnB,KAAK,GAAqC,IAAI,oBAAU,EAAE,CAAC;IAC3D,KAAK,GAA6B,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACrD,OAAO,GAA8B,IAAI,mCAAyB,EAAE,CAAC;IAErE,YAAmB,SAAoB;QAApB,cAAS,GAAT,SAAS,CAAW;QACnC,MAAM,SAAS,GAAG,EAAE,IAAI,EAAE,MAAM,EAAuB,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,EAAE,CAAA;QAC7G,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAC5D,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IACjD,CAAC;IAED,QAAQ,CAAC,IAAiB;QACtB,MAAM,EAAE,GAAG,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;QACvC,IAAI,EAAE,IAAI,IAAI,CAAC,KAAK;YAChB,OAAO,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAE1B,MAAM,KAAK,GAAG,IAAI,YAAY,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC3C,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC;QACvB,KAAK,MAAM,CAAC,IAAI,KAAK,CAAC,KAAK,EAAE;YACzB,IA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AAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;SAClC;QACD,KAAK,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,EAAE;YACpC,IAAI,CAAC,OAAO,MAAM,IAAI,QAAQ,CAAC,CAAC,CAAE,MAAc,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;SAC1F;QAED,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC;IACzE,CAAC;CACJ"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StandardGrammar = void 0;
|
|
4
|
+
const generator_1 = require("../generator");
|
|
5
|
+
class StandardGrammar {
|
|
6
|
+
generator;
|
|
7
|
+
constructor(generator) {
|
|
8
|
+
this.generator = generator;
|
|
9
|
+
}
|
|
10
|
+
serialize(depth = 0) {
|
|
11
|
+
if (!this.generator.state.grammar) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
return generator_1.Generator.Pretty({
|
|
15
|
+
start: JSON.stringify(this.generator.state.grammar.start),
|
|
16
|
+
rules: this.serializeGrammarRules(depth + 1)
|
|
17
|
+
}, depth);
|
|
18
|
+
}
|
|
19
|
+
serializeGrammarRules(depth = 0) {
|
|
20
|
+
const map = {};
|
|
21
|
+
for (const rule in this.generator.state.grammar.rules) {
|
|
22
|
+
map[rule] = this.generator.state.grammar.rules[rule].map(v => this.generator.serializeGrammarRule(v));
|
|
23
|
+
}
|
|
24
|
+
return generator_1.Generator.Pretty(map, depth);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.StandardGrammar = StandardGrammar;
|
|
28
|
+
//# sourceMappingURL=standard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard.js","sourceRoot":"","sources":["../../../../src/compiler/generator/artifacts/standard.ts"],"names":[],"mappings":";;;AACA,4CAAyC;AAEzC,MAAa,eAAe;IAEJ;IAApB,YAAoB,SAAoB;QAApB,cAAS,GAAT,SAAS,CAAW;IAAI,CAAC;IAE7C,SAAS,CAAC,QAAgB,CAAC;QACvB,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,EAAE;YAC/B,OAAO,IAAI,CAAC;SACf;QACD,OAAO,qBAAS,CAAC,MAAM,CAAC;YACpB,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;YACzD,KAAK,EAAE,IAAI,CAAC,qBAAqB,CAAC,KAAK,GAAG,CAAC,CAAC;SAC/C,EAAE,KAAK,CAAC,CAAC;IACd,CAAC;IAEO,qBAAqB,CAAC,QAAgB,CAAC;QAC3C,MAAM,GAAG,GAAG,EAAE,CAAC;QACf,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE;YACnD,GAAG,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAA;SACxG;QACD,OAAO,qBAAS,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC;CAEJ;AAtBD,0CAsBC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Dictionary, GeneratorState, GeneratorGrammarRule, GeneratorGrammarSymbol, 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
|
+
serializePostProcess(postprocess: GeneratorGrammarRule['postprocess'], alias: Dictionary<number>): any;
|
|
12
|
+
private templatePostProcess;
|
|
13
|
+
private serializeLexerConfig;
|
|
14
|
+
private serializeLexerConfigStates;
|
|
15
|
+
private serializeLexerConfigStateRules;
|
|
16
|
+
serializeGrammarRule(rule: GeneratorGrammarRule): string;
|
|
17
|
+
static NewLine(depth: number): string;
|
|
18
|
+
static Pretty(obj: string[] | {
|
|
19
|
+
[key: string]: string | (string[]);
|
|
20
|
+
}, depth?: number): string;
|
|
21
|
+
static IsVal(value: any): boolean;
|
|
22
|
+
static SerializeSymbol(s: GeneratorGrammarSymbol): string;
|
|
23
|
+
static SymbolIsTerminal(s: GeneratorGrammarSymbol): boolean;
|
|
24
|
+
}
|