parser-lr 0.0.0 → 0.2.1
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 +112 -1
- package/bin/parser-lr.js +12502 -16
- package/bin/parser-lr.js.map +1 -1
- package/dist/lib/ast/ast-node.d.ts +59 -0
- package/dist/lib/ast/ast-node.d.ts.map +1 -0
- package/dist/lib/ast/ast-node.js +66 -0
- package/dist/lib/ast/ast-node.js.map +1 -0
- package/dist/lib/errors/index.d.ts +2 -0
- package/dist/lib/errors/index.d.ts.map +1 -0
- package/dist/lib/errors/index.js +2 -0
- package/dist/lib/errors/index.js.map +1 -0
- package/dist/lib/errors/parser-lr-error.d.ts +30 -0
- package/dist/lib/errors/parser-lr-error.d.ts.map +1 -0
- package/dist/lib/errors/parser-lr-error.js +45 -0
- package/dist/lib/errors/parser-lr-error.js.map +1 -0
- package/dist/lib/grammar/ast-schema.d.ts +28 -0
- package/dist/lib/grammar/ast-schema.d.ts.map +1 -0
- package/dist/lib/grammar/ast-schema.js +39 -0
- package/dist/lib/grammar/ast-schema.js.map +1 -0
- package/dist/lib/grammar/ast-type.d.ts +12 -0
- package/dist/lib/grammar/ast-type.d.ts.map +1 -0
- package/dist/lib/grammar/ast-type.js +2 -0
- package/dist/lib/grammar/ast-type.js.map +1 -0
- package/dist/lib/grammar/expression.d.ts +69 -0
- package/dist/lib/grammar/expression.d.ts.map +1 -0
- package/dist/lib/grammar/expression.js +2 -0
- package/dist/lib/grammar/expression.js.map +1 -0
- package/dist/lib/grammar/grammar-from-cst.d.ts +10 -0
- package/dist/lib/grammar/grammar-from-cst.d.ts.map +1 -0
- package/dist/lib/grammar/grammar-from-cst.js +638 -0
- package/dist/lib/grammar/grammar-from-cst.js.map +1 -0
- package/dist/lib/grammar/grammar-literals.d.ts +18 -0
- package/dist/lib/grammar/grammar-literals.d.ts.map +1 -0
- package/dist/lib/grammar/grammar-literals.js +67 -0
- package/dist/lib/grammar/grammar-literals.js.map +1 -0
- package/dist/lib/grammar/grammar.d.ts +45 -0
- package/dist/lib/grammar/grammar.d.ts.map +1 -0
- package/dist/lib/grammar/grammar.js +60 -0
- package/dist/lib/grammar/grammar.js.map +1 -0
- package/dist/lib/grammar/grammar.json +7950 -0
- package/dist/lib/grammar/index.d.ts +13 -0
- package/dist/lib/grammar/index.d.ts.map +1 -0
- package/dist/lib/grammar/index.js +7 -0
- package/dist/lib/grammar/index.js.map +1 -0
- package/dist/lib/grammar/meta-grammar-table.d.ts +19 -0
- package/dist/lib/grammar/meta-grammar-table.d.ts.map +1 -0
- package/dist/lib/grammar/meta-grammar-table.js +32 -0
- package/dist/lib/grammar/meta-grammar-table.js.map +1 -0
- package/dist/lib/grammar/production.d.ts +9 -0
- package/dist/lib/grammar/production.d.ts.map +1 -0
- package/dist/lib/grammar/production.js +2 -0
- package/dist/lib/grammar/production.js.map +1 -0
- package/dist/lib/grammar/read-grammar-error.d.ts +15 -0
- package/dist/lib/grammar/read-grammar-error.d.ts.map +1 -0
- package/dist/lib/grammar/read-grammar-error.js +18 -0
- package/dist/lib/grammar/read-grammar-error.js.map +1 -0
- package/dist/lib/grammar/read-grammar.d.ts +9 -0
- package/dist/lib/grammar/read-grammar.d.ts.map +1 -0
- package/dist/lib/grammar/read-grammar.js +21 -0
- package/dist/lib/grammar/read-grammar.js.map +1 -0
- package/dist/lib/grammar/token-rule.d.ts +11 -0
- package/dist/lib/grammar/token-rule.d.ts.map +1 -0
- package/dist/lib/grammar/token-rule.js +2 -0
- package/dist/lib/grammar/token-rule.js.map +1 -0
- package/dist/lib/grammar/transform-expression.d.ts +55 -0
- package/dist/lib/grammar/transform-expression.d.ts.map +1 -0
- package/dist/lib/grammar/transform-expression.js +2 -0
- package/dist/lib/grammar/transform-expression.js.map +1 -0
- package/dist/lib/grammar/transform-rule.d.ts +16 -0
- package/dist/lib/grammar/transform-rule.d.ts.map +1 -0
- package/dist/lib/grammar/transform-rule.js +2 -0
- package/dist/lib/grammar/transform-rule.js.map +1 -0
- package/dist/lib/grammar/transform-schema.d.ts +28 -0
- package/dist/lib/grammar/transform-schema.d.ts.map +1 -0
- package/dist/lib/grammar/transform-schema.js +39 -0
- package/dist/lib/grammar/transform-schema.js.map +1 -0
- package/dist/lib/index.d.ts +19 -0
- package/dist/lib/index.d.ts.map +1 -1
- package/dist/lib/index.js +13 -0
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/lexer/index.d.ts +10 -0
- package/dist/lib/lexer/index.d.ts.map +1 -0
- package/dist/lib/lexer/index.js +8 -0
- package/dist/lib/lexer/index.js.map +1 -0
- package/dist/lib/lexer/lexer-compile-error.d.ts +15 -0
- package/dist/lib/lexer/lexer-compile-error.d.ts.map +1 -0
- package/dist/lib/lexer/lexer-compile-error.js +18 -0
- package/dist/lib/lexer/lexer-compile-error.js.map +1 -0
- package/dist/lib/lexer/lexer-compile.d.ts +94 -0
- package/dist/lib/lexer/lexer-compile.d.ts.map +1 -0
- package/dist/lib/lexer/lexer-compile.js +189 -0
- package/dist/lib/lexer/lexer-compile.js.map +1 -0
- package/dist/lib/lexer/lexer-error.d.ts +15 -0
- package/dist/lib/lexer/lexer-error.d.ts.map +1 -0
- package/dist/lib/lexer/lexer-error.js +18 -0
- package/dist/lib/lexer/lexer-error.js.map +1 -0
- package/dist/lib/lexer/lexer-input-error.d.ts +11 -0
- package/dist/lib/lexer/lexer-input-error.d.ts.map +1 -0
- package/dist/lib/lexer/lexer-input-error.js +13 -0
- package/dist/lib/lexer/lexer-input-error.js.map +1 -0
- package/dist/lib/lexer/lexer-state-error.d.ts +13 -0
- package/dist/lib/lexer/lexer-state-error.d.ts.map +1 -0
- package/dist/lib/lexer/lexer-state-error.js +15 -0
- package/dist/lib/lexer/lexer-state-error.js.map +1 -0
- package/dist/lib/lexer/lexer.d.ts +111 -0
- package/dist/lib/lexer/lexer.d.ts.map +1 -0
- package/dist/lib/lexer/lexer.js +270 -0
- package/dist/lib/lexer/lexer.js.map +1 -0
- package/dist/lib/lexer/token.d.ts +34 -0
- package/dist/lib/lexer/token.d.ts.map +1 -0
- package/dist/lib/lexer/token.js +38 -0
- package/dist/lib/lexer/token.js.map +1 -0
- package/dist/lib/parse-context-error.d.ts +13 -0
- package/dist/lib/parse-context-error.d.ts.map +1 -0
- package/dist/lib/parse-context-error.js +15 -0
- package/dist/lib/parse-context-error.js.map +1 -0
- package/dist/lib/parse-context.d.ts +106 -0
- package/dist/lib/parse-context.d.ts.map +1 -0
- package/dist/lib/parse-context.js +133 -0
- package/dist/lib/parse-context.js.map +1 -0
- package/dist/lib/parse-output-error.d.ts +13 -0
- package/dist/lib/parse-output-error.d.ts.map +1 -0
- package/dist/lib/parse-output-error.js +15 -0
- package/dist/lib/parse-output-error.js.map +1 -0
- package/dist/lib/parse-output.d.ts +10 -0
- package/dist/lib/parse-output.d.ts.map +1 -0
- package/dist/lib/parse-output.js +15 -0
- package/dist/lib/parse-output.js.map +1 -0
- package/dist/lib/parse-table/analysis/first-follow.d.ts +124 -0
- package/dist/lib/parse-table/analysis/first-follow.d.ts.map +1 -0
- package/dist/lib/parse-table/analysis/first-follow.js +274 -0
- package/dist/lib/parse-table/analysis/first-follow.js.map +1 -0
- package/dist/lib/parse-table/analysis/index.d.ts +2 -0
- package/dist/lib/parse-table/analysis/index.d.ts.map +1 -0
- package/dist/lib/parse-table/analysis/index.js +2 -0
- package/dist/lib/parse-table/analysis/index.js.map +1 -0
- package/dist/lib/parse-table/bnf/bnf-grammar.d.ts +65 -0
- package/dist/lib/parse-table/bnf/bnf-grammar.d.ts.map +1 -0
- package/dist/lib/parse-table/bnf/bnf-grammar.js +136 -0
- package/dist/lib/parse-table/bnf/bnf-grammar.js.map +1 -0
- package/dist/lib/parse-table/bnf/bnf-production.d.ts +12 -0
- package/dist/lib/parse-table/bnf/bnf-production.d.ts.map +1 -0
- package/dist/lib/parse-table/bnf/bnf-production.js +2 -0
- package/dist/lib/parse-table/bnf/bnf-production.js.map +1 -0
- package/dist/lib/parse-table/bnf/bnf-symbol.d.ts +40 -0
- package/dist/lib/parse-table/bnf/bnf-symbol.d.ts.map +1 -0
- package/dist/lib/parse-table/bnf/bnf-symbol.js +35 -0
- package/dist/lib/parse-table/bnf/bnf-symbol.js.map +1 -0
- package/dist/lib/parse-table/bnf/desugar-ebnf.d.ts +104 -0
- package/dist/lib/parse-table/bnf/desugar-ebnf.d.ts.map +1 -0
- package/dist/lib/parse-table/bnf/desugar-ebnf.js +217 -0
- package/dist/lib/parse-table/bnf/desugar-ebnf.js.map +1 -0
- package/dist/lib/parse-table/bnf/index.d.ts +6 -0
- package/dist/lib/parse-table/bnf/index.d.ts.map +1 -0
- package/dist/lib/parse-table/bnf/index.js +4 -0
- package/dist/lib/parse-table/bnf/index.js.map +1 -0
- package/dist/lib/parse-table/build-lr-table.d.ts +11 -0
- package/dist/lib/parse-table/build-lr-table.d.ts.map +1 -0
- package/dist/lib/parse-table/build-lr-table.js +158 -0
- package/dist/lib/parse-table/build-lr-table.js.map +1 -0
- package/dist/lib/parse-table/index.d.ts +22 -0
- package/dist/lib/parse-table/index.d.ts.map +1 -0
- package/dist/lib/parse-table/index.js +15 -0
- package/dist/lib/parse-table/index.js.map +1 -0
- package/dist/lib/parse-table/lr-algorithm-error.d.ts +13 -0
- package/dist/lib/parse-table/lr-algorithm-error.d.ts.map +1 -0
- package/dist/lib/parse-table/lr-algorithm-error.js +15 -0
- package/dist/lib/parse-table/lr-algorithm-error.js.map +1 -0
- package/dist/lib/parse-table/lr-algorithm.d.ts +19 -0
- package/dist/lib/parse-table/lr-algorithm.d.ts.map +1 -0
- package/dist/lib/parse-table/lr-algorithm.js +27 -0
- package/dist/lib/parse-table/lr-algorithm.js.map +1 -0
- package/dist/lib/parse-table/lr0/index.d.ts +3 -0
- package/dist/lib/parse-table/lr0/index.d.ts.map +1 -0
- package/dist/lib/parse-table/lr0/index.js +2 -0
- package/dist/lib/parse-table/lr0/index.js.map +1 -0
- package/dist/lib/parse-table/lr0/lr0-item-set.d.ts +106 -0
- package/dist/lib/parse-table/lr0/lr0-item-set.d.ts.map +1 -0
- package/dist/lib/parse-table/lr0/lr0-item-set.js +235 -0
- package/dist/lib/parse-table/lr0/lr0-item-set.js.map +1 -0
- package/dist/lib/parse-table/lr1/index.d.ts +3 -0
- package/dist/lib/parse-table/lr1/index.d.ts.map +1 -0
- package/dist/lib/parse-table/lr1/index.js +2 -0
- package/dist/lib/parse-table/lr1/index.js.map +1 -0
- package/dist/lib/parse-table/lr1/lr1-item-set.d.ts +157 -0
- package/dist/lib/parse-table/lr1/lr1-item-set.d.ts.map +1 -0
- package/dist/lib/parse-table/lr1/lr1-item-set.js +380 -0
- package/dist/lib/parse-table/lr1/lr1-item-set.js.map +1 -0
- package/dist/lib/parse-table/parse-table-build-error.d.ts +17 -0
- package/dist/lib/parse-table/parse-table-build-error.d.ts.map +1 -0
- package/dist/lib/parse-table/parse-table-build-error.js +20 -0
- package/dist/lib/parse-table/parse-table-build-error.js.map +1 -0
- package/dist/lib/parse-table/parse-table-error.d.ts +13 -0
- package/dist/lib/parse-table/parse-table-error.d.ts.map +1 -0
- package/dist/lib/parse-table/parse-table-error.js +15 -0
- package/dist/lib/parse-table/parse-table-error.js.map +1 -0
- package/dist/lib/parse-table/parse-table-json.d.ts +50 -0
- package/dist/lib/parse-table/parse-table-json.d.ts.map +1 -0
- package/dist/lib/parse-table/parse-table-json.js +9 -0
- package/dist/lib/parse-table/parse-table-json.js.map +1 -0
- package/dist/lib/parse-table/parse-table.d.ts +154 -0
- package/dist/lib/parse-table/parse-table.d.ts.map +1 -0
- package/dist/lib/parse-table/parse-table.js +314 -0
- package/dist/lib/parse-table/parse-table.js.map +1 -0
- package/dist/lib/parse-table/slr/index.d.ts +4 -0
- package/dist/lib/parse-table/slr/index.d.ts.map +1 -0
- package/dist/lib/parse-table/slr/index.js +3 -0
- package/dist/lib/parse-table/slr/index.js.map +1 -0
- package/dist/lib/parse-table/slr/parse-action.d.ts +3 -0
- package/dist/lib/parse-table/slr/parse-action.d.ts.map +1 -0
- package/dist/lib/parse-table/slr/parse-action.js +2 -0
- package/dist/lib/parse-table/slr/parse-action.js.map +1 -0
- package/dist/lib/parse-table/slr/slr-table.d.ts +14 -0
- package/dist/lib/parse-table/slr/slr-table.d.ts.map +1 -0
- package/dist/lib/parse-table/slr/slr-table.js +14 -0
- package/dist/lib/parse-table/slr/slr-table.js.map +1 -0
- package/dist/lib/parse-table/table/index.d.ts +5 -0
- package/dist/lib/parse-table/table/index.d.ts.map +1 -0
- package/dist/lib/parse-table/table/index.js +4 -0
- package/dist/lib/parse-table/table/index.js.map +1 -0
- package/dist/lib/parse-table/table/lr-parse-table.d.ts +92 -0
- package/dist/lib/parse-table/table/lr-parse-table.d.ts.map +1 -0
- package/dist/lib/parse-table/table/lr-parse-table.js +144 -0
- package/dist/lib/parse-table/table/lr-parse-table.js.map +1 -0
- package/dist/lib/parse-table/table/parse-action.d.ts +47 -0
- package/dist/lib/parse-table/table/parse-action.d.ts.map +1 -0
- package/dist/lib/parse-table/table/parse-action.js +49 -0
- package/dist/lib/parse-table/table/parse-action.js.map +1 -0
- package/dist/lib/parse-table/table/table-builder-base.d.ts +63 -0
- package/dist/lib/parse-table/table/table-builder-base.d.ts.map +1 -0
- package/dist/lib/parse-table/table/table-builder-base.js +129 -0
- package/dist/lib/parse-table/table/table-builder-base.js.map +1 -0
- package/dist/lib/parse-table/token-inventory.d.ts +9 -0
- package/dist/lib/parse-table/token-inventory.d.ts.map +1 -0
- package/dist/lib/parse-table/token-inventory.js +15 -0
- package/dist/lib/parse-table/token-inventory.js.map +1 -0
- package/dist/lib/parser-lr.d.ts +96 -2
- package/dist/lib/parser-lr.d.ts.map +1 -1
- package/dist/lib/parser-lr.js +130 -2
- package/dist/lib/parser-lr.js.map +1 -1
- package/dist/lib/shift-reduce/index.d.ts +3 -0
- package/dist/lib/shift-reduce/index.d.ts.map +1 -0
- package/dist/lib/shift-reduce/index.js +2 -0
- package/dist/lib/shift-reduce/index.js.map +1 -0
- package/dist/lib/shift-reduce/shift-reduce-engine.d.ts +82 -0
- package/dist/lib/shift-reduce/shift-reduce-engine.d.ts.map +1 -0
- package/dist/lib/shift-reduce/shift-reduce-engine.js +204 -0
- package/dist/lib/shift-reduce/shift-reduce-engine.js.map +1 -0
- package/dist/lib/transform/binding-map.d.ts +32 -0
- package/dist/lib/transform/binding-map.d.ts.map +1 -0
- package/dist/lib/transform/binding-map.js +59 -0
- package/dist/lib/transform/binding-map.js.map +1 -0
- package/dist/lib/transform/cst-transformer.d.ts +158 -0
- package/dist/lib/transform/cst-transformer.d.ts.map +1 -0
- package/dist/lib/transform/cst-transformer.js +381 -0
- package/dist/lib/transform/cst-transformer.js.map +1 -0
- package/dist/lib/transform/index.d.ts +4 -0
- package/dist/lib/transform/index.d.ts.map +1 -0
- package/dist/lib/transform/index.js +3 -0
- package/dist/lib/transform/index.js.map +1 -0
- package/docs/grammar.md +170 -0
- package/package.json +12 -4
package/dist/lib/parser-lr.d.ts
CHANGED
|
@@ -1,10 +1,104 @@
|
|
|
1
|
+
import type { AstNode } from './ast/ast-node.js';
|
|
2
|
+
import type { Grammar } from './grammar/grammar.js';
|
|
3
|
+
import { Lexer } from './lexer/lexer.js';
|
|
4
|
+
import type { Token } from './lexer/token.js';
|
|
5
|
+
import type { LrAlgorithm } from './parse-table/lr-algorithm.js';
|
|
6
|
+
import { ParseTable } from './parse-table/parse-table.js';
|
|
1
7
|
/**
|
|
2
8
|
* Shift-reduce parser for EBNF grammars.
|
|
3
9
|
*/
|
|
4
10
|
export declare class ParserLr {
|
|
11
|
+
readonly grammar: Grammar;
|
|
12
|
+
private readonly table;
|
|
5
13
|
/**
|
|
6
|
-
* Creates a parser
|
|
14
|
+
* Creates a parser bound to a grammar and optional parse table.
|
|
15
|
+
*
|
|
16
|
+
* @param grammar - Parsed `.grammar` file supplying lexer and parser rules.
|
|
17
|
+
* @param table - Optional parse table used for shift-reduce parsing.
|
|
7
18
|
*/
|
|
8
|
-
constructor();
|
|
19
|
+
constructor(grammar: Grammar, table?: ParseTable | null);
|
|
20
|
+
/**
|
|
21
|
+
* Creates a parser from a serialized parse table.
|
|
22
|
+
*
|
|
23
|
+
* @param table - Parse table containing lexer metadata and parser entries.
|
|
24
|
+
* @returns A parser that can lex and parse using the table.
|
|
25
|
+
*/
|
|
26
|
+
static fromParseTable(table: ParseTable): ParserLr;
|
|
27
|
+
/**
|
|
28
|
+
* Creates a stream lexer for this parser's grammar.
|
|
29
|
+
*
|
|
30
|
+
* @returns A fresh stream lexer instance.
|
|
31
|
+
*/
|
|
32
|
+
createLexer(): Lexer;
|
|
33
|
+
/**
|
|
34
|
+
* Lexes source text into tokens using the grammar's token and skip rules.
|
|
35
|
+
*
|
|
36
|
+
* @param source - Input text to tokenize.
|
|
37
|
+
* @returns Matched tokens in source order, ending with `$eof`.
|
|
38
|
+
*/
|
|
39
|
+
lex(source: string): readonly Token[];
|
|
40
|
+
/**
|
|
41
|
+
* Lexes synchronous source chunks into a token stream ending with `$eof`.
|
|
42
|
+
*
|
|
43
|
+
* @param chunks - Source text fragments in order.
|
|
44
|
+
* @returns Token iterator including `$eof`.
|
|
45
|
+
*/
|
|
46
|
+
lexChunks(chunks: Iterable<string>): IterableIterator<Token>;
|
|
47
|
+
/**
|
|
48
|
+
* Lexes asynchronous source chunks into a token stream ending with `$eof`.
|
|
49
|
+
*
|
|
50
|
+
* @param chunks - Source text fragments in order.
|
|
51
|
+
* @returns Async token iterator including `$eof`.
|
|
52
|
+
*/
|
|
53
|
+
lexChunksAsync(chunks: AsyncIterable<string>): AsyncIterableIterator<Token>;
|
|
54
|
+
/**
|
|
55
|
+
* Collects all tokens from a synchronous chunk stream.
|
|
56
|
+
*
|
|
57
|
+
* @param chunks - Source text fragments in order.
|
|
58
|
+
* @returns Matched tokens including `$eof`.
|
|
59
|
+
*/
|
|
60
|
+
lexChunkStream(chunks: Iterable<string>): readonly Token[];
|
|
61
|
+
/**
|
|
62
|
+
* Collects all tokens from an asynchronous chunk stream.
|
|
63
|
+
*
|
|
64
|
+
* @param chunks - Source text fragments in order.
|
|
65
|
+
* @returns Matched tokens including `$eof`.
|
|
66
|
+
*/
|
|
67
|
+
lexChunkStreamAsync(chunks: AsyncIterable<string>): Promise<readonly Token[]>;
|
|
68
|
+
/**
|
|
69
|
+
* Parses a token stream into a concrete syntax tree.
|
|
70
|
+
*
|
|
71
|
+
* @param tokens - Token stream ending with `$eof`.
|
|
72
|
+
* @returns Parse tree rooted at the grammar start symbol, or null on syntax error.
|
|
73
|
+
*/
|
|
74
|
+
parseCst(tokens: readonly Token[]): AstNode | null;
|
|
75
|
+
/**
|
|
76
|
+
* Parses a token stream into an AST when transform rules are declared.
|
|
77
|
+
*
|
|
78
|
+
* @param tokens - Token stream ending with `$eof`.
|
|
79
|
+
* @returns Transformed AST, CST when no transform section exists, or null on failure.
|
|
80
|
+
*/
|
|
81
|
+
parseAst(tokens: readonly Token[]): AstNode | null;
|
|
82
|
+
/**
|
|
83
|
+
* Parses a token stream, applying CST-to-AST transforms when configured.
|
|
84
|
+
*
|
|
85
|
+
* @param tokens - Token stream ending with `$eof`.
|
|
86
|
+
* @returns AST or CST parse tree, or null on syntax error.
|
|
87
|
+
*/
|
|
88
|
+
parse(tokens: readonly Token[]): AstNode | null;
|
|
89
|
+
/**
|
|
90
|
+
* Lexes and parses source text, applying CST-to-AST transforms when configured.
|
|
91
|
+
*
|
|
92
|
+
* @param source - Input text to parse.
|
|
93
|
+
* @returns AST or CST parse tree, or null on syntax error.
|
|
94
|
+
*/
|
|
95
|
+
parseSource(source: string): AstNode | null;
|
|
96
|
+
/**
|
|
97
|
+
* Builds a parse table from the bound grammar, including the token inventory.
|
|
98
|
+
*
|
|
99
|
+
* @param algorithm - LR algorithm used to build the table.
|
|
100
|
+
* @returns Parse table metadata ready for serialization.
|
|
101
|
+
*/
|
|
102
|
+
buildParseTable(algorithm?: LrAlgorithm): ParseTable;
|
|
9
103
|
}
|
|
10
104
|
//# sourceMappingURL=parser-lr.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser-lr.d.ts","sourceRoot":"","sources":["../../src/lib/parser-lr.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,QAAQ;
|
|
1
|
+
{"version":3,"file":"parser-lr.d.ts","sourceRoot":"","sources":["../../src/lib/parser-lr.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EACH,KAAK,EAKR,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AACjE,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAI1D;;GAEG;AACH,qBAAa,QAAQ;aASG,OAAO,EAAE,OAAO;IAChC,OAAO,CAAC,QAAQ,CAAC,KAAK;IAR1B;;;;;OAKG;gBAEiB,OAAO,EAAE,OAAO,EACf,KAAK,GAAE,UAAU,GAAG,IAAW;IAKpD;;;;;OAKG;WACW,cAAc,CAAC,KAAK,EAAE,UAAU,GAAG,QAAQ;IAKzD;;;;OAIG;IACI,WAAW,IAAI,KAAK;IAK3B;;;;;OAKG;IACI,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,KAAK,EAAE;IAK5C;;;;;OAKG;IACI,SAAS,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,GAAG,gBAAgB,CAAC,KAAK,CAAC;IAKnE;;;;;OAKG;IACI,cAAc,CAAC,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,qBAAqB,CAAC,KAAK,CAAC;IAKlF;;;;;OAKG;IACI,cAAc,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,GAAG,SAAS,KAAK,EAAE;IAKjE;;;;;OAKG;IACU,mBAAmB,CAAC,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,SAAS,KAAK,EAAE,CAAC;IAK1F;;;;;OAKG;IACI,QAAQ,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,GAAG,OAAO,GAAG,IAAI;IAUzD;;;;;OAKG;IACI,QAAQ,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,GAAG,OAAO,GAAG,IAAI;IAiBzD;;;;;OAKG;IACI,KAAK,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,GAAG,OAAO,GAAG,IAAI;IAKtD;;;;;OAKG;IACI,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI;IAKlD;;;;;OAKG;IACI,eAAe,CAAC,SAAS,GAAE,WAAmB,GAAG,UAAU;CAIrE"}
|
package/dist/lib/parser-lr.js
CHANGED
|
@@ -1,11 +1,139 @@
|
|
|
1
|
+
import { Lexer, lexChunkStream, lexChunkStreamAsync, lexChunks, lexChunksAsync, } from './lexer/lexer.js';
|
|
2
|
+
import { ParseTable } from './parse-table/parse-table.js';
|
|
3
|
+
import { parseWithTable } from './shift-reduce/shift-reduce-engine.js';
|
|
4
|
+
import { transformCst } from './transform/cst-transformer.js';
|
|
1
5
|
/**
|
|
2
6
|
* Shift-reduce parser for EBNF grammars.
|
|
3
7
|
*/
|
|
4
8
|
export class ParserLr {
|
|
9
|
+
grammar;
|
|
10
|
+
table;
|
|
5
11
|
/**
|
|
6
|
-
* Creates a parser
|
|
12
|
+
* Creates a parser bound to a grammar and optional parse table.
|
|
13
|
+
*
|
|
14
|
+
* @param grammar - Parsed `.grammar` file supplying lexer and parser rules.
|
|
15
|
+
* @param table - Optional parse table used for shift-reduce parsing.
|
|
7
16
|
*/
|
|
8
|
-
constructor() {
|
|
17
|
+
constructor(grammar, table = null) {
|
|
18
|
+
this.grammar = grammar;
|
|
19
|
+
this.table = table;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Creates a parser from a serialized parse table.
|
|
23
|
+
*
|
|
24
|
+
* @param table - Parse table containing lexer metadata and parser entries.
|
|
25
|
+
* @returns A parser that can lex and parse using the table.
|
|
26
|
+
*/
|
|
27
|
+
static fromParseTable(table) {
|
|
28
|
+
return new ParserLr(table.toGrammar(), table);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Creates a stream lexer for this parser's grammar.
|
|
32
|
+
*
|
|
33
|
+
* @returns A fresh stream lexer instance.
|
|
34
|
+
*/
|
|
35
|
+
createLexer() {
|
|
36
|
+
return new Lexer(this.grammar);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Lexes source text into tokens using the grammar's token and skip rules.
|
|
40
|
+
*
|
|
41
|
+
* @param source - Input text to tokenize.
|
|
42
|
+
* @returns Matched tokens in source order, ending with `$eof`.
|
|
43
|
+
*/
|
|
44
|
+
lex(source) {
|
|
45
|
+
return this.createLexer().lex(source);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Lexes synchronous source chunks into a token stream ending with `$eof`.
|
|
49
|
+
*
|
|
50
|
+
* @param chunks - Source text fragments in order.
|
|
51
|
+
* @returns Token iterator including `$eof`.
|
|
52
|
+
*/
|
|
53
|
+
lexChunks(chunks) {
|
|
54
|
+
return lexChunks(this.grammar, chunks);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Lexes asynchronous source chunks into a token stream ending with `$eof`.
|
|
58
|
+
*
|
|
59
|
+
* @param chunks - Source text fragments in order.
|
|
60
|
+
* @returns Async token iterator including `$eof`.
|
|
61
|
+
*/
|
|
62
|
+
lexChunksAsync(chunks) {
|
|
63
|
+
return lexChunksAsync(this.grammar, chunks);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Collects all tokens from a synchronous chunk stream.
|
|
67
|
+
*
|
|
68
|
+
* @param chunks - Source text fragments in order.
|
|
69
|
+
* @returns Matched tokens including `$eof`.
|
|
70
|
+
*/
|
|
71
|
+
lexChunkStream(chunks) {
|
|
72
|
+
return lexChunkStream(this.grammar, chunks);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Collects all tokens from an asynchronous chunk stream.
|
|
76
|
+
*
|
|
77
|
+
* @param chunks - Source text fragments in order.
|
|
78
|
+
* @returns Matched tokens including `$eof`.
|
|
79
|
+
*/
|
|
80
|
+
async lexChunkStreamAsync(chunks) {
|
|
81
|
+
return lexChunkStreamAsync(this.grammar, chunks);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Parses a token stream into a concrete syntax tree.
|
|
85
|
+
*
|
|
86
|
+
* @param tokens - Token stream ending with `$eof`.
|
|
87
|
+
* @returns Parse tree rooted at the grammar start symbol, or null on syntax error.
|
|
88
|
+
*/
|
|
89
|
+
parseCst(tokens) {
|
|
90
|
+
if (this.table === null) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return parseWithTable(this.table, tokens);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Parses a token stream into an AST when transform rules are declared.
|
|
97
|
+
*
|
|
98
|
+
* @param tokens - Token stream ending with `$eof`.
|
|
99
|
+
* @returns Transformed AST, CST when no transform section exists, or null on failure.
|
|
100
|
+
*/
|
|
101
|
+
parseAst(tokens) {
|
|
102
|
+
const cst = this.parseCst(tokens);
|
|
103
|
+
if (cst === null) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
if (this.grammar.transformSchema === null || this.table === null) {
|
|
107
|
+
return cst;
|
|
108
|
+
}
|
|
109
|
+
return transformCst(cst, this.grammar.transformSchema, this.table);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Parses a token stream, applying CST-to-AST transforms when configured.
|
|
113
|
+
*
|
|
114
|
+
* @param tokens - Token stream ending with `$eof`.
|
|
115
|
+
* @returns AST or CST parse tree, or null on syntax error.
|
|
116
|
+
*/
|
|
117
|
+
parse(tokens) {
|
|
118
|
+
return this.parseAst(tokens);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Lexes and parses source text, applying CST-to-AST transforms when configured.
|
|
122
|
+
*
|
|
123
|
+
* @param source - Input text to parse.
|
|
124
|
+
* @returns AST or CST parse tree, or null on syntax error.
|
|
125
|
+
*/
|
|
126
|
+
parseSource(source) {
|
|
127
|
+
return this.parse(this.lex(source));
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Builds a parse table from the bound grammar, including the token inventory.
|
|
131
|
+
*
|
|
132
|
+
* @param algorithm - LR algorithm used to build the table.
|
|
133
|
+
* @returns Parse table metadata ready for serialization.
|
|
134
|
+
*/
|
|
135
|
+
buildParseTable(algorithm = 'lr1') {
|
|
136
|
+
return ParseTable.fromGrammar(this.grammar, algorithm);
|
|
9
137
|
}
|
|
10
138
|
}
|
|
11
139
|
//# sourceMappingURL=parser-lr.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser-lr.js","sourceRoot":"","sources":["../../src/lib/parser-lr.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"parser-lr.js","sourceRoot":"","sources":["../../src/lib/parser-lr.ts"],"names":[],"mappings":"AAEA,OAAO,EACH,KAAK,EACL,cAAc,EACd,mBAAmB,EACnB,SAAS,EACT,cAAc,GACjB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,MAAM,uCAAuC,CAAC;AACvE,OAAO,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AAE9D;;GAEG;AACH,MAAM,OAAO,QAAQ;IASG;IACC;IARrB;;;;;OAKG;IACH,YACoB,OAAgB,EACf,QAA2B,IAAI;QADhC,YAAO,GAAP,OAAO,CAAS;QACf,UAAK,GAAL,KAAK,CAA0B;IAGpD,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,cAAc,CAAC,KAAiB;QAE1C,OAAO,IAAI,QA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I,IAAI,IAAI,CAAC,KAAK,KAAK,IAAI,EAChE,CAAC;YACG,OAAO,GAAG,CAAC;QACf,CAAC;QAED,OAAO,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,eAAe,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;IACvE,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,MAAwB;QAEjC,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IACjC,CAAC;IAED;;;;;OAKG;IACI,WAAW,CAAC,MAAc;QAE7B,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IACxC,CAAC;IAED;;;;;OAKG;IACI,eAAe,CAAC,YAAyB,KAAK;QAEjD,OAAO,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IAC3D,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/shift-reduce/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,gBAAgB,EAChB,mBAAmB,EACnB,cAAc,EACd,oBAAoB,EACpB,iBAAiB,EACjB,cAAc,GACjB,MAAM,0BAA0B,CAAC;AAClC,YAAY,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/shift-reduce/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,gBAAgB,EAChB,mBAAmB,EACnB,cAAc,EACd,oBAAoB,EACpB,iBAAiB,EACjB,cAAc,GACjB,MAAM,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { SourceLocation } from '../ast/ast-node.js';
|
|
2
|
+
import { AstNode } from '../ast/ast-node.js';
|
|
3
|
+
import { type Token } from '../lexer/token.js';
|
|
4
|
+
import type { ParseTable } from '../parse-table/parse-table.js';
|
|
5
|
+
/**
|
|
6
|
+
* Returns the ACTION table key for a lexer token.
|
|
7
|
+
*
|
|
8
|
+
* @param token - Current input token.
|
|
9
|
+
*/
|
|
10
|
+
export declare function tokenActionKey(token: Token): string;
|
|
11
|
+
/**
|
|
12
|
+
* Builds a terminal CST node from a shifted token.
|
|
13
|
+
*
|
|
14
|
+
* @param token - Lexer token that was shifted.
|
|
15
|
+
*/
|
|
16
|
+
export declare function astNodeFromToken(token: Token): AstNode;
|
|
17
|
+
/**
|
|
18
|
+
* Merges child spans into one source location.
|
|
19
|
+
*
|
|
20
|
+
* @param children - Reduced child subtrees.
|
|
21
|
+
*/
|
|
22
|
+
export declare function mergeChildLocations(children: readonly AstNode[]): SourceLocation | null;
|
|
23
|
+
/**
|
|
24
|
+
* Outcome of a shift-reduce parse attempt.
|
|
25
|
+
*/
|
|
26
|
+
export interface ShiftReduceParseResult {
|
|
27
|
+
readonly cst: AstNode | null;
|
|
28
|
+
readonly errorOffset: number | null;
|
|
29
|
+
readonly errorMessage: string | null;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Table-driven shift-reduce parser shared by all LR table algorithms.
|
|
33
|
+
*/
|
|
34
|
+
export declare class ShiftReduceEngine {
|
|
35
|
+
/**
|
|
36
|
+
* Parses a token stream using a serialized LR parse table.
|
|
37
|
+
*
|
|
38
|
+
* @param table - Parse table with ACTION, GOTO, and production metadata.
|
|
39
|
+
* @param tokens - Token stream ending with `$eof`.
|
|
40
|
+
* @returns The start symbol parse tree, or null on syntax error.
|
|
41
|
+
*/
|
|
42
|
+
static parse(table: ParseTable, tokens: readonly Token[]): AstNode | null;
|
|
43
|
+
/**
|
|
44
|
+
* Parses a token stream and reports the first syntax error location.
|
|
45
|
+
*
|
|
46
|
+
* @param table - Parse table with ACTION, GOTO, and production metadata.
|
|
47
|
+
* @param tokens - Token stream ending with `$eof`.
|
|
48
|
+
* @returns Parse tree on success, or error offset and message on failure.
|
|
49
|
+
*/
|
|
50
|
+
static parseWithResult(table: ParseTable, tokens: readonly Token[]): ShiftReduceParseResult;
|
|
51
|
+
/**
|
|
52
|
+
* Applies one reduce action and pushes the resulting subtree.
|
|
53
|
+
*
|
|
54
|
+
* @param table - Parse table with GOTO and production metadata.
|
|
55
|
+
* @param productionId - Production to reduce by.
|
|
56
|
+
* @param stateStack - Parser state stack.
|
|
57
|
+
* @param valueStack - Semantic value stack.
|
|
58
|
+
*/
|
|
59
|
+
private static reduce;
|
|
60
|
+
/**
|
|
61
|
+
* Builds a CST node for one reduced production.
|
|
62
|
+
*
|
|
63
|
+
* @param production - Reduced production metadata.
|
|
64
|
+
* @param children - Child subtrees matching the production right-hand side.
|
|
65
|
+
*/
|
|
66
|
+
private static nodeFromProduction;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Parses a token stream using a serialized LR parse table.
|
|
70
|
+
*
|
|
71
|
+
* @param table - Parse table with ACTION, GOTO, and production metadata.
|
|
72
|
+
* @param tokens - Token stream ending with `$eof`.
|
|
73
|
+
*/
|
|
74
|
+
export declare function parseWithTable(table: ParseTable, tokens: readonly Token[]): AstNode | null;
|
|
75
|
+
/**
|
|
76
|
+
* Parses a token stream and reports the first syntax error location.
|
|
77
|
+
*
|
|
78
|
+
* @param table - Parse table with ACTION, GOTO, and production metadata.
|
|
79
|
+
* @param tokens - Token stream ending with `$eof`.
|
|
80
|
+
*/
|
|
81
|
+
export declare function parseWithTableResult(table: ParseTable, tokens: readonly Token[]): ShiftReduceParseResult;
|
|
82
|
+
//# sourceMappingURL=shift-reduce-engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shift-reduce-engine.d.ts","sourceRoot":"","sources":["../../../src/lib/shift-reduce/shift-reduce-engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,OAAO,EA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B"}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { AstNode } from '../ast/ast-node.js';
|
|
2
|
+
import { EOF_TOKEN_NAME, isEofToken } from '../lexer/token.js';
|
|
3
|
+
/**
|
|
4
|
+
* Returns the ACTION table key for a lexer token.
|
|
5
|
+
*
|
|
6
|
+
* @param token - Current input token.
|
|
7
|
+
*/
|
|
8
|
+
export function tokenActionKey(token) {
|
|
9
|
+
if (isEofToken(token)) {
|
|
10
|
+
return EOF_TOKEN_NAME;
|
|
11
|
+
}
|
|
12
|
+
return token.name;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Builds a terminal CST node from a shifted token.
|
|
16
|
+
*
|
|
17
|
+
* @param token - Lexer token that was shifted.
|
|
18
|
+
*/
|
|
19
|
+
export function astNodeFromToken(token) {
|
|
20
|
+
return AstNode.terminal(token.name, token.text, token.location);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Merges child spans into one source location.
|
|
24
|
+
*
|
|
25
|
+
* @param children - Reduced child subtrees.
|
|
26
|
+
*/
|
|
27
|
+
export function mergeChildLocations(children) {
|
|
28
|
+
let offset = null;
|
|
29
|
+
let end = null;
|
|
30
|
+
for (const child of children) {
|
|
31
|
+
if (child.location === null) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (offset === null) {
|
|
35
|
+
offset = child.location.offset;
|
|
36
|
+
end = child.location.offset + child.location.length;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
offset = Math.min(offset, child.location.offset);
|
|
40
|
+
end = Math.max(end ?? child.location.offset, child.location.offset + child.location.length);
|
|
41
|
+
}
|
|
42
|
+
if (offset === null || end === null) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
offset,
|
|
47
|
+
length: end - offset,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Table-driven shift-reduce parser shared by all LR table algorithms.
|
|
52
|
+
*/
|
|
53
|
+
export class ShiftReduceEngine {
|
|
54
|
+
/**
|
|
55
|
+
* Parses a token stream using a serialized LR parse table.
|
|
56
|
+
*
|
|
57
|
+
* @param table - Parse table with ACTION, GOTO, and production metadata.
|
|
58
|
+
* @param tokens - Token stream ending with `$eof`.
|
|
59
|
+
* @returns The start symbol parse tree, or null on syntax error.
|
|
60
|
+
*/
|
|
61
|
+
static parse(table, tokens) {
|
|
62
|
+
return ShiftReduceEngine.parseWithResult(table, tokens).cst;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Parses a token stream and reports the first syntax error location.
|
|
66
|
+
*
|
|
67
|
+
* @param table - Parse table with ACTION, GOTO, and production metadata.
|
|
68
|
+
* @param tokens - Token stream ending with `$eof`.
|
|
69
|
+
* @returns Parse tree on success, or error offset and message on failure.
|
|
70
|
+
*/
|
|
71
|
+
static parseWithResult(table, tokens) {
|
|
72
|
+
if (!table.hasParserTable) {
|
|
73
|
+
return {
|
|
74
|
+
cst: null,
|
|
75
|
+
errorOffset: 0,
|
|
76
|
+
errorMessage: 'Parse table has no parser entries',
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
if (tokens.length === 0 || !isEofToken(tokens[tokens.length - 1])) {
|
|
80
|
+
return {
|
|
81
|
+
cst: null,
|
|
82
|
+
errorOffset: tokens[0]?.location.offset ?? 0,
|
|
83
|
+
errorMessage: 'Token stream must end with $eof',
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const stateStack = [0];
|
|
87
|
+
const valueStack = [];
|
|
88
|
+
let tokenIndex = 0;
|
|
89
|
+
while (true) {
|
|
90
|
+
const state = stateStack[stateStack.length - 1];
|
|
91
|
+
const currentToken = tokens[tokenIndex];
|
|
92
|
+
const action = table.action(state, tokenActionKey(currentToken));
|
|
93
|
+
if (action === null) {
|
|
94
|
+
return {
|
|
95
|
+
cst: null,
|
|
96
|
+
errorOffset: currentToken.location.offset,
|
|
97
|
+
errorMessage: `Unexpected ${currentToken.name}`,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
switch (action.kind) {
|
|
101
|
+
case 'shift':
|
|
102
|
+
{
|
|
103
|
+
valueStack.push(astNodeFromToken(currentToken));
|
|
104
|
+
stateStack.push(action.state);
|
|
105
|
+
tokenIndex += 1;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
case 'reduce':
|
|
109
|
+
{
|
|
110
|
+
const node = ShiftReduceEngine.reduce(table, action.productionId, stateStack, valueStack);
|
|
111
|
+
if (node === null) {
|
|
112
|
+
return {
|
|
113
|
+
cst: null,
|
|
114
|
+
errorOffset: currentToken.location.offset,
|
|
115
|
+
errorMessage: 'Invalid reduce action',
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case 'accept':
|
|
121
|
+
{
|
|
122
|
+
if (valueStack.length === 0) {
|
|
123
|
+
return {
|
|
124
|
+
cst: null,
|
|
125
|
+
errorOffset: currentToken.location.offset,
|
|
126
|
+
errorMessage: 'Empty parse result',
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
cst: valueStack[valueStack.length - 1],
|
|
131
|
+
errorOffset: null,
|
|
132
|
+
errorMessage: null,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Applies one reduce action and pushes the resulting subtree.
|
|
140
|
+
*
|
|
141
|
+
* @param table - Parse table with GOTO and production metadata.
|
|
142
|
+
* @param productionId - Production to reduce by.
|
|
143
|
+
* @param stateStack - Parser state stack.
|
|
144
|
+
* @param valueStack - Semantic value stack.
|
|
145
|
+
*/
|
|
146
|
+
static reduce(table, productionId, stateStack, valueStack) {
|
|
147
|
+
const production = table.production(productionId);
|
|
148
|
+
if (production === null) {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
const rhsLength = production.rhs.length;
|
|
152
|
+
const children = [];
|
|
153
|
+
for (let index = 0; index < rhsLength; index += 1) {
|
|
154
|
+
if (stateStack.length === 0 || valueStack.length === 0) {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
stateStack.pop();
|
|
158
|
+
const child = valueStack.pop();
|
|
159
|
+
if (child === undefined) {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
children.unshift(child);
|
|
163
|
+
}
|
|
164
|
+
if (stateStack.length === 0) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
const gotoState = table.goto(stateStack[stateStack.length - 1], production.name);
|
|
168
|
+
if (gotoState === null) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
const node = ShiftReduceEngine.nodeFromProduction(production, children);
|
|
172
|
+
valueStack.push(node);
|
|
173
|
+
stateStack.push(gotoState);
|
|
174
|
+
return node;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Builds a CST node for one reduced production.
|
|
178
|
+
*
|
|
179
|
+
* @param production - Reduced production metadata.
|
|
180
|
+
* @param children - Child subtrees matching the production right-hand side.
|
|
181
|
+
*/
|
|
182
|
+
static nodeFromProduction(production, children) {
|
|
183
|
+
return AstNode.rule(production.name, children, mergeChildLocations(children), production.variant, production.id, production.origin);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Parses a token stream using a serialized LR parse table.
|
|
188
|
+
*
|
|
189
|
+
* @param table - Parse table with ACTION, GOTO, and production metadata.
|
|
190
|
+
* @param tokens - Token stream ending with `$eof`.
|
|
191
|
+
*/
|
|
192
|
+
export function parseWithTable(table, tokens) {
|
|
193
|
+
return ShiftReduceEngine.parse(table, tokens);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Parses a token stream and reports the first syntax error location.
|
|
197
|
+
*
|
|
198
|
+
* @param table - Parse table with ACTION, GOTO, and production metadata.
|
|
199
|
+
* @param tokens - Token stream ending with `$eof`.
|
|
200
|
+
*/
|
|
201
|
+
export function parseWithTableResult(table, tokens) {
|
|
202
|
+
return ShiftReduceEngine.parseWithResult(table, tokens);
|
|
203
|
+
}
|
|
204
|
+
//# sourceMappingURL=shift-reduce-engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shift-reduce-engine.js","sourceRoot":"","sources":["../../../src/lib/shift-reduce/shift-reduce-engine.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,UAAU,EAAc,MAAM,mBAAmB,CAAC;AAI3E;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,KAAY;IAEvC,IAAI,UAAU,CAAC,KAAK,CAAC,EACrB,CAAC;QACG,OAAO,cAAc,CAAC;IAC1B,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC;AACtB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAY;IAEzC,OAAO,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;AACpE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,QAA4B;IAE5D,IAAI,MAAM,GAAkB,IAAI,CAAC;IACjC,IAAI,GAAG,GAAkB,IAAI,CAAC;IAE9B,KAAK,MAAM,KAAK,IAAI,QAAQ,EAC5B,CAAC;QACG,IAAI,KAAK,CAAC,QAAQ,KAAK,IAAI,EAC3B,CAAC;YACG,SAAS;QACb,CAAC;QAED,IAAI,MAAM,KAAK,IAAI,EACnB,CAAC;YACG,MAAM,GAAG,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC;YAC/B,GAAG,GAAG,KAAK,CAAC,QAAQ,CAAC,MAAM,GAAG,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC;YACpD,SAAS;QACb,CAAC;QAED,MAAM,GAAG,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AAC,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,UAAU,CAAC,IAAI,CAAC,CAAC;QAEjF,IAAI,SAAS,KAAK,IAAI,EACtB,CAAC;YACG,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,MAAM,IAAI,GAAG,iBAAiB,CAAC,kBAAkB,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;QACxE,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACtB,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAE3B,OAAO,IAAI,CAAC;IAChB,CAAC;IAED;;;;;OAKG;IACK,MAAM,CAAC,kBAAkB,CAC7B,UAAoC,EACpC,QAA4B;QAG5B,OAAO,OAAO,CAAC,IAAI,CACf,UAAU,CAAC,IAAI,EACf,QAAQ,EACR,mBAAmB,CAAC,QAAQ,CAAC,EAC7B,UAAU,CAAC,OAAO,EAClB,UAAU,CAAC,EAAE,EACb,UAAU,CAAC,MAAM,CACpB,CAAC;IACN,CAAC;CACJ;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,KAAiB,EAAE,MAAwB;IAEtE,OAAO,iBAAiB,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AAClD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAChC,KAAiB,EACjB,MAAwB;IAGxB,OAAO,iBAAiB,CAAC,eAAe,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AAC5D,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ParseTableProductionJson } from '../parse-table/parse-table-json.js';
|
|
2
|
+
/**
|
|
3
|
+
* One bound or unbound slot on a production right-hand side.
|
|
4
|
+
*/
|
|
5
|
+
export interface ProductionSlot {
|
|
6
|
+
readonly index: number;
|
|
7
|
+
readonly binding: string | null;
|
|
8
|
+
readonly symbol: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Parses a serialized production right-hand side symbol key.
|
|
12
|
+
*
|
|
13
|
+
* @param key - Encoded symbol from a parse table production.
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseRhsSymbolKey(key: string): {
|
|
16
|
+
binding: string | null;
|
|
17
|
+
symbol: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Returns indexed slots for one production right-hand side.
|
|
21
|
+
*
|
|
22
|
+
* @param production - Parse table production metadata.
|
|
23
|
+
*/
|
|
24
|
+
export declare function productionSlots(production: ParseTableProductionJson): readonly ProductionSlot[];
|
|
25
|
+
/**
|
|
26
|
+
* Finds the child index for a transform reference name.
|
|
27
|
+
*
|
|
28
|
+
* @param production - Parse table production metadata.
|
|
29
|
+
* @param reference - Binding or symbol name from a transform expression.
|
|
30
|
+
*/
|
|
31
|
+
export declare function referenceSlotIndex(production: ParseTableProductionJson, reference: string): number | null;
|
|
32
|
+
//# sourceMappingURL=binding-map.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binding-map.d.ts","sourceRoot":"","sources":["../../../src/lib/transform/binding-map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,oCAAoC,CAAC;AAEnF;;GAEG;AACH,MAAM,WAAW,cAAc;IAE3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CAC3B;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG;IAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAwBzF;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,UAAU,EAAE,wBAAwB,GAAG,SAAS,cAAc,EAAE,CAY/F;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAC9B,UAAU,EAAE,wBAAwB,EACpC,SAAS,EAAE,MAAM,GAClB,MAAM,GAAG,IAAI,CAmBf"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses a serialized production right-hand side symbol key.
|
|
3
|
+
*
|
|
4
|
+
* @param key - Encoded symbol from a parse table production.
|
|
5
|
+
*/
|
|
6
|
+
export function parseRhsSymbolKey(key) {
|
|
7
|
+
const bindingMatch = /^\[([A-Za-z_][A-Za-z0-9_]*)\]:(.+)$/.exec(key);
|
|
8
|
+
if (bindingMatch !== null) {
|
|
9
|
+
return {
|
|
10
|
+
binding: bindingMatch[1],
|
|
11
|
+
symbol: bindingMatch[2],
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
if (key.startsWith('"') && key.endsWith('"')) {
|
|
15
|
+
return {
|
|
16
|
+
binding: null,
|
|
17
|
+
symbol: key.slice(1, -1),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
binding: null,
|
|
22
|
+
symbol: key,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Returns indexed slots for one production right-hand side.
|
|
27
|
+
*
|
|
28
|
+
* @param production - Parse table production metadata.
|
|
29
|
+
*/
|
|
30
|
+
export function productionSlots(production) {
|
|
31
|
+
return production.rhs.map((key, index) => {
|
|
32
|
+
const parsed = parseRhsSymbolKey(key);
|
|
33
|
+
return {
|
|
34
|
+
index,
|
|
35
|
+
binding: parsed.binding,
|
|
36
|
+
symbol: parsed.symbol,
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Finds the child index for a transform reference name.
|
|
42
|
+
*
|
|
43
|
+
* @param production - Parse table production metadata.
|
|
44
|
+
* @param reference - Binding or symbol name from a transform expression.
|
|
45
|
+
*/
|
|
46
|
+
export function referenceSlotIndex(production, reference) {
|
|
47
|
+
for (const slot of productionSlots(production)) {
|
|
48
|
+
if (slot.binding === reference) {
|
|
49
|
+
return slot.index;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
for (const slot of productionSlots(production)) {
|
|
53
|
+
if (slot.binding === null && slot.symbol === reference) {
|
|
54
|
+
return slot.index;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=binding-map.js.map
|