grammar-well 1.1.2 → 1.1.4
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/build/compiler/compiler.d.ts +49 -49
- package/build/compiler/compiler.js +227 -227
- package/build/compiler/generator.d.ts +23 -23
- package/build/compiler/generator.js +213 -212
- package/build/compiler/generator.js.map +1 -1
- package/build/compiler/import-resolver.d.ts +15 -15
- package/build/compiler/import-resolver.js +36 -36
- package/build/compiler/outputs/javascript.d.ts +3 -3
- package/build/compiler/outputs/javascript.js +14 -14
- package/build/compiler/outputs/json.d.ts +2 -2
- package/build/compiler/outputs/json.js +7 -7
- package/build/compiler/outputs/typescript.d.ts +2 -2
- package/build/compiler/outputs/typescript.js +9 -8
- package/build/compiler/outputs/typescript.js.map +1 -1
- package/build/grammars/gwell.d.ts +1023 -997
- package/build/grammars/gwell.js +540 -536
- package/build/grammars/gwell.js.map +1 -1
- package/build/grammars/json.d.ts +151 -151
- package/build/grammars/json.js +111 -111
- package/build/grammars/number.d.ts +239 -239
- package/build/grammars/number.js +114 -114
- package/build/grammars/number.json +1 -1
- package/build/grammars/string.d.ts +116 -116
- package/build/grammars/string.js +49 -49
- package/build/grammars/string.json +1 -1
- package/build/grammars/whitespace.d.ts +51 -51
- package/build/grammars/whitespace.js +29 -29
- package/build/grammars/whitespace.json +1 -1
- package/build/index.d.ts +4 -4
- package/build/index.js +20 -20
- package/build/lexers/character-lexer.d.ts +27 -27
- package/build/lexers/character-lexer.js +70 -70
- package/build/lexers/stateful-lexer.d.ts +48 -48
- package/build/lexers/stateful-lexer.js +308 -308
- package/build/lexers/token-buffer.d.ts +32 -32
- package/build/lexers/token-buffer.js +91 -91
- package/build/parser/algorithms/cyk.d.ts +16 -16
- package/build/parser/algorithms/cyk.js +57 -57
- package/build/parser/algorithms/earley.d.ts +48 -48
- package/build/parser/algorithms/earley.js +157 -157
- package/build/parser/algorithms/lr.d.ts +10 -10
- package/build/parser/algorithms/lr.js +33 -33
- package/build/parser/parser.d.ts +26 -26
- package/build/parser/parser.js +73 -73
- package/build/typings.d.ts +199 -198
- package/build/typings.js +2 -2
- package/build/utility/general.d.ts +55 -55
- package/build/utility/general.js +175 -165
- package/build/utility/general.js.map +1 -1
- package/build/utility/lint.d.ts +2 -2
- package/build/utility/lint.js +27 -27
- package/build/utility/lr.d.ts +52 -52
- package/build/utility/lr.js +129 -129
- package/build/utility/text-format.d.ts +11 -11
- package/build/utility/text-format.js +83 -83
- package/package.json +1 -1
- package/src/compiler/generator.ts +1 -0
- package/src/compiler/outputs/typescript.ts +2 -1
- package/src/grammars/gwell.gwell +15 -13
- package/src/grammars/gwell.js +17 -13
- package/src/grammars/gwell.json +1 -1
- package/src/typings.ts +1 -0
- package/src/utility/general.ts +31 -19
|
@@ -1,158 +1,158 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Earley = void 0;
|
|
4
|
-
const text_format_1 = require("../../utility/text-format");
|
|
5
|
-
const parser_1 = require("../parser");
|
|
6
|
-
function Earley(language, options = {}) {
|
|
7
|
-
const { tokens } = language;
|
|
8
|
-
const { rules, start } = language.grammar;
|
|
9
|
-
const column = new Column(rules, 0);
|
|
10
|
-
const table = [column];
|
|
11
|
-
column.wants[start] = [];
|
|
12
|
-
column.predict(start);
|
|
13
|
-
column.process();
|
|
14
|
-
let current = 0;
|
|
15
|
-
for (const token of tokens) {
|
|
16
|
-
const previousColumn = table[current];
|
|
17
|
-
if (!(options.keepHistory)) {
|
|
18
|
-
delete table[current - 1];
|
|
19
|
-
}
|
|
20
|
-
current++;
|
|
21
|
-
const nextColumn = new Column(rules, current);
|
|
22
|
-
table.push(nextColumn);
|
|
23
|
-
const literal = token.value;
|
|
24
|
-
const data = token;
|
|
25
|
-
nextColumn.data = literal;
|
|
26
|
-
const { scannable } = previousColumn;
|
|
27
|
-
for (let w = scannable.length; w--;) {
|
|
28
|
-
const state = scannable[w];
|
|
29
|
-
const symbol = state.rule.symbols[state.dot];
|
|
30
|
-
if (parser_1.ParserUtility.TokenMatchesSymbol(token, symbol)) {
|
|
31
|
-
const next = state.nextState({ data, token, isToken: true, reference: current - 1 });
|
|
32
|
-
nextColumn.states.push(next);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
nextColumn.process();
|
|
36
|
-
if (nextColumn.states.length === 0) {
|
|
37
|
-
throw text_format_1.TextFormatter.UnexpectedToken(tokens, previousColumn.expects());
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
const results = [];
|
|
41
|
-
const { states } = table[table.length - 1];
|
|
42
|
-
for (const { rule: { name, symbols }, dot, reference, data } of states) {
|
|
43
|
-
if (name === start && dot === symbols.length && reference == 0) {
|
|
44
|
-
results.push(data);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return { results, info: { table } };
|
|
48
|
-
}
|
|
49
|
-
exports.Earley = Earley;
|
|
50
|
-
class Column {
|
|
51
|
-
rules;
|
|
52
|
-
index;
|
|
53
|
-
data;
|
|
54
|
-
states = [];
|
|
55
|
-
wants = Object.create(null);
|
|
56
|
-
scannable = [];
|
|
57
|
-
completed = Object.create(null);
|
|
58
|
-
constructor(rules, index) {
|
|
59
|
-
this.rules = rules;
|
|
60
|
-
this.index = index;
|
|
61
|
-
}
|
|
62
|
-
process() {
|
|
63
|
-
let w = 0;
|
|
64
|
-
let state;
|
|
65
|
-
while (state = this.states[w++]) {
|
|
66
|
-
if (state.isComplete) {
|
|
67
|
-
state.finish();
|
|
68
|
-
const { wantedBy } = state;
|
|
69
|
-
for (let i = wantedBy.length; i--;) {
|
|
70
|
-
this.complete(wantedBy[i], state);
|
|
71
|
-
}
|
|
72
|
-
if (state.reference === this.index) {
|
|
73
|
-
const { name } = state.rule;
|
|
74
|
-
this.completed[name] = this.completed[name] || [];
|
|
75
|
-
this.completed[name].push(state);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
const exp = state.rule.symbols[state.dot];
|
|
80
|
-
if (typeof exp !== 'string') {
|
|
81
|
-
this.scannable.push(state);
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
if (this.wants[exp]) {
|
|
85
|
-
this.wants[exp].push(state);
|
|
86
|
-
if (this.completed[exp]) {
|
|
87
|
-
for (const right of this.completed[exp]) {
|
|
88
|
-
this.complete(state, right);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
this.wants[exp] = [state];
|
|
94
|
-
this.predict(exp);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
predict(exp) {
|
|
100
|
-
if (!this.rules[exp])
|
|
101
|
-
return;
|
|
102
|
-
for (const rule of this.rules[exp]) {
|
|
103
|
-
this.states.push(new State(rule, 0, this.index, this.wants[exp]));
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
expects() {
|
|
107
|
-
return this.states
|
|
108
|
-
.filter((state) => {
|
|
109
|
-
const nextSymbol = state.rule.symbols[state.dot];
|
|
110
|
-
return nextSymbol && typeof nextSymbol !== "string";
|
|
111
|
-
})
|
|
112
|
-
.map(v => ({ ...v.rule, index: v.dot }));
|
|
113
|
-
}
|
|
114
|
-
complete(left, right) {
|
|
115
|
-
const copy = left.nextState(right);
|
|
116
|
-
this.states.push(copy);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
class State {
|
|
120
|
-
rule;
|
|
121
|
-
dot;
|
|
122
|
-
reference;
|
|
123
|
-
wantedBy;
|
|
124
|
-
isComplete;
|
|
125
|
-
data = [];
|
|
126
|
-
left;
|
|
127
|
-
right;
|
|
128
|
-
constructor(rule, dot, reference, wantedBy) {
|
|
129
|
-
this.rule = rule;
|
|
130
|
-
this.dot = dot;
|
|
131
|
-
this.reference = reference;
|
|
132
|
-
this.wantedBy = wantedBy;
|
|
133
|
-
this.isComplete = this.dot === rule.symbols.length;
|
|
134
|
-
}
|
|
135
|
-
nextState(child) {
|
|
136
|
-
const state = new State(this.rule, this.dot + 1, this.reference, this.wantedBy);
|
|
137
|
-
state.left = this;
|
|
138
|
-
state.right = child;
|
|
139
|
-
if (state.isComplete) {
|
|
140
|
-
state.data = state.build();
|
|
141
|
-
state.right = undefined;
|
|
142
|
-
}
|
|
143
|
-
return state;
|
|
144
|
-
}
|
|
145
|
-
finish() {
|
|
146
|
-
this.data = parser_1.ParserUtility.PostProcess(this.rule, this.data, { reference: this.reference, dot: this.dot });
|
|
147
|
-
}
|
|
148
|
-
build() {
|
|
149
|
-
const children = [];
|
|
150
|
-
let node = this;
|
|
151
|
-
do {
|
|
152
|
-
children[node.dot - 1] = node.right.data;
|
|
153
|
-
node = node.left;
|
|
154
|
-
} while (node.left);
|
|
155
|
-
return children;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Earley = void 0;
|
|
4
|
+
const text_format_1 = require("../../utility/text-format");
|
|
5
|
+
const parser_1 = require("../parser");
|
|
6
|
+
function Earley(language, options = {}) {
|
|
7
|
+
const { tokens } = language;
|
|
8
|
+
const { rules, start } = language.grammar;
|
|
9
|
+
const column = new Column(rules, 0);
|
|
10
|
+
const table = [column];
|
|
11
|
+
column.wants[start] = [];
|
|
12
|
+
column.predict(start);
|
|
13
|
+
column.process();
|
|
14
|
+
let current = 0;
|
|
15
|
+
for (const token of tokens) {
|
|
16
|
+
const previousColumn = table[current];
|
|
17
|
+
if (!(options.keepHistory)) {
|
|
18
|
+
delete table[current - 1];
|
|
19
|
+
}
|
|
20
|
+
current++;
|
|
21
|
+
const nextColumn = new Column(rules, current);
|
|
22
|
+
table.push(nextColumn);
|
|
23
|
+
const literal = token.value;
|
|
24
|
+
const data = token;
|
|
25
|
+
nextColumn.data = literal;
|
|
26
|
+
const { scannable } = previousColumn;
|
|
27
|
+
for (let w = scannable.length; w--;) {
|
|
28
|
+
const state = scannable[w];
|
|
29
|
+
const symbol = state.rule.symbols[state.dot];
|
|
30
|
+
if (parser_1.ParserUtility.TokenMatchesSymbol(token, symbol)) {
|
|
31
|
+
const next = state.nextState({ data, token, isToken: true, reference: current - 1 });
|
|
32
|
+
nextColumn.states.push(next);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
nextColumn.process();
|
|
36
|
+
if (nextColumn.states.length === 0) {
|
|
37
|
+
throw text_format_1.TextFormatter.UnexpectedToken(tokens, previousColumn.expects());
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const results = [];
|
|
41
|
+
const { states } = table[table.length - 1];
|
|
42
|
+
for (const { rule: { name, symbols }, dot, reference, data } of states) {
|
|
43
|
+
if (name === start && dot === symbols.length && reference == 0) {
|
|
44
|
+
results.push(data);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return { results, info: { table } };
|
|
48
|
+
}
|
|
49
|
+
exports.Earley = Earley;
|
|
50
|
+
class Column {
|
|
51
|
+
rules;
|
|
52
|
+
index;
|
|
53
|
+
data;
|
|
54
|
+
states = [];
|
|
55
|
+
wants = Object.create(null);
|
|
56
|
+
scannable = [];
|
|
57
|
+
completed = Object.create(null);
|
|
58
|
+
constructor(rules, index) {
|
|
59
|
+
this.rules = rules;
|
|
60
|
+
this.index = index;
|
|
61
|
+
}
|
|
62
|
+
process() {
|
|
63
|
+
let w = 0;
|
|
64
|
+
let state;
|
|
65
|
+
while (state = this.states[w++]) {
|
|
66
|
+
if (state.isComplete) {
|
|
67
|
+
state.finish();
|
|
68
|
+
const { wantedBy } = state;
|
|
69
|
+
for (let i = wantedBy.length; i--;) {
|
|
70
|
+
this.complete(wantedBy[i], state);
|
|
71
|
+
}
|
|
72
|
+
if (state.reference === this.index) {
|
|
73
|
+
const { name } = state.rule;
|
|
74
|
+
this.completed[name] = this.completed[name] || [];
|
|
75
|
+
this.completed[name].push(state);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
const exp = state.rule.symbols[state.dot];
|
|
80
|
+
if (typeof exp !== 'string') {
|
|
81
|
+
this.scannable.push(state);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (this.wants[exp]) {
|
|
85
|
+
this.wants[exp].push(state);
|
|
86
|
+
if (this.completed[exp]) {
|
|
87
|
+
for (const right of this.completed[exp]) {
|
|
88
|
+
this.complete(state, right);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
this.wants[exp] = [state];
|
|
94
|
+
this.predict(exp);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
predict(exp) {
|
|
100
|
+
if (!this.rules[exp])
|
|
101
|
+
return;
|
|
102
|
+
for (const rule of this.rules[exp]) {
|
|
103
|
+
this.states.push(new State(rule, 0, this.index, this.wants[exp]));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
expects() {
|
|
107
|
+
return this.states
|
|
108
|
+
.filter((state) => {
|
|
109
|
+
const nextSymbol = state.rule.symbols[state.dot];
|
|
110
|
+
return nextSymbol && typeof nextSymbol !== "string";
|
|
111
|
+
})
|
|
112
|
+
.map(v => ({ ...v.rule, index: v.dot }));
|
|
113
|
+
}
|
|
114
|
+
complete(left, right) {
|
|
115
|
+
const copy = left.nextState(right);
|
|
116
|
+
this.states.push(copy);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
class State {
|
|
120
|
+
rule;
|
|
121
|
+
dot;
|
|
122
|
+
reference;
|
|
123
|
+
wantedBy;
|
|
124
|
+
isComplete;
|
|
125
|
+
data = [];
|
|
126
|
+
left;
|
|
127
|
+
right;
|
|
128
|
+
constructor(rule, dot, reference, wantedBy) {
|
|
129
|
+
this.rule = rule;
|
|
130
|
+
this.dot = dot;
|
|
131
|
+
this.reference = reference;
|
|
132
|
+
this.wantedBy = wantedBy;
|
|
133
|
+
this.isComplete = this.dot === rule.symbols.length;
|
|
134
|
+
}
|
|
135
|
+
nextState(child) {
|
|
136
|
+
const state = new State(this.rule, this.dot + 1, this.reference, this.wantedBy);
|
|
137
|
+
state.left = this;
|
|
138
|
+
state.right = child;
|
|
139
|
+
if (state.isComplete) {
|
|
140
|
+
state.data = state.build();
|
|
141
|
+
state.right = undefined;
|
|
142
|
+
}
|
|
143
|
+
return state;
|
|
144
|
+
}
|
|
145
|
+
finish() {
|
|
146
|
+
this.data = parser_1.ParserUtility.PostProcess(this.rule, this.data, { reference: this.reference, dot: this.dot });
|
|
147
|
+
}
|
|
148
|
+
build() {
|
|
149
|
+
const children = [];
|
|
150
|
+
let node = this;
|
|
151
|
+
do {
|
|
152
|
+
children[node.dot - 1] = node.right.data;
|
|
153
|
+
node = node.left;
|
|
154
|
+
} while (node.left);
|
|
155
|
+
return children;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
158
|
//# sourceMappingURL=earley.js.map
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { TokenBuffer } from "../../lexers/token-buffer";
|
|
2
|
-
import { LanguageDefinition } from "../../typings";
|
|
3
|
-
import { CanonicalCollection } from "../../utility/lr";
|
|
4
|
-
export declare function LR(language: LanguageDefinition & {
|
|
5
|
-
tokens: TokenBuffer;
|
|
6
|
-
canonical?: CanonicalCollection;
|
|
7
|
-
}, _options?: {}): {
|
|
8
|
-
results: any[];
|
|
9
|
-
canonical: CanonicalCollection;
|
|
10
|
-
};
|
|
1
|
+
import { TokenBuffer } from "../../lexers/token-buffer";
|
|
2
|
+
import { LanguageDefinition } from "../../typings";
|
|
3
|
+
import { CanonicalCollection } from "../../utility/lr";
|
|
4
|
+
export declare function LR(language: LanguageDefinition & {
|
|
5
|
+
tokens: TokenBuffer;
|
|
6
|
+
canonical?: CanonicalCollection;
|
|
7
|
+
}, _options?: {}): {
|
|
8
|
+
results: any[];
|
|
9
|
+
canonical: CanonicalCollection;
|
|
10
|
+
};
|
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LR = void 0;
|
|
4
|
-
const lr_1 = require("../../utility/lr");
|
|
5
|
-
const parser_1 = require("../parser");
|
|
6
|
-
function LR(language, _options = {}) {
|
|
7
|
-
const { grammar, tokens, canonical } = language;
|
|
8
|
-
const collection = canonical || new lr_1.CanonicalCollection(grammar);
|
|
9
|
-
const stack = new lr_1.LRStack();
|
|
10
|
-
const s = collection.states['0.0'];
|
|
11
|
-
stack.add(null);
|
|
12
|
-
stack.shift(s);
|
|
13
|
-
let token;
|
|
14
|
-
while (token = tokens.next()) {
|
|
15
|
-
for (const [symbol, state] of stack.current.state.actions) {
|
|
16
|
-
if (parser_1.ParserUtility.TokenMatchesSymbol(token, symbol)) {
|
|
17
|
-
stack.add(symbol);
|
|
18
|
-
stack.shift(collection.states[state]);
|
|
19
|
-
stack.current.value = token;
|
|
20
|
-
break;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
while (stack.current.state?.isFinal) {
|
|
24
|
-
const rule = stack.current.state.reduce;
|
|
25
|
-
stack.reduce(rule);
|
|
26
|
-
stack.current.value = parser_1.ParserUtility.PostProcess(rule, stack.current.children.map(v => v.value));
|
|
27
|
-
const s = stack.previous.state.goto.get(rule.name);
|
|
28
|
-
stack.shift(collection.states[s]);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return { results: [stack.current.value], canonical: collection };
|
|
32
|
-
}
|
|
33
|
-
exports.LR = LR;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LR = void 0;
|
|
4
|
+
const lr_1 = require("../../utility/lr");
|
|
5
|
+
const parser_1 = require("../parser");
|
|
6
|
+
function LR(language, _options = {}) {
|
|
7
|
+
const { grammar, tokens, canonical } = language;
|
|
8
|
+
const collection = canonical || new lr_1.CanonicalCollection(grammar);
|
|
9
|
+
const stack = new lr_1.LRStack();
|
|
10
|
+
const s = collection.states['0.0'];
|
|
11
|
+
stack.add(null);
|
|
12
|
+
stack.shift(s);
|
|
13
|
+
let token;
|
|
14
|
+
while (token = tokens.next()) {
|
|
15
|
+
for (const [symbol, state] of stack.current.state.actions) {
|
|
16
|
+
if (parser_1.ParserUtility.TokenMatchesSymbol(token, symbol)) {
|
|
17
|
+
stack.add(symbol);
|
|
18
|
+
stack.shift(collection.states[state]);
|
|
19
|
+
stack.current.value = token;
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
while (stack.current.state?.isFinal) {
|
|
24
|
+
const rule = stack.current.state.reduce;
|
|
25
|
+
stack.reduce(rule);
|
|
26
|
+
stack.current.value = parser_1.ParserUtility.PostProcess(rule, stack.current.children.map(v => v.value));
|
|
27
|
+
const s = stack.previous.state.goto.get(rule.name);
|
|
28
|
+
stack.shift(collection.states[s]);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return { results: [stack.current.value], canonical: collection };
|
|
32
|
+
}
|
|
33
|
+
exports.LR = LR;
|
|
34
34
|
//# sourceMappingURL=lr.js.map
|
package/build/parser/parser.d.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import { GrammarRule, GrammarRuleSymbol, LanguageDefinition, LexerToken, ParserAlgorithm } from "../typings";
|
|
2
|
-
declare const ParserRegistry: {
|
|
3
|
-
[key: string]: ParserAlgorithm;
|
|
4
|
-
};
|
|
5
|
-
export declare function Parse(language: LanguageDefinition, input: string, options?: ParserOptions): {
|
|
6
|
-
results: any[];
|
|
7
|
-
};
|
|
8
|
-
export declare class Parser {
|
|
9
|
-
private language;
|
|
10
|
-
private options;
|
|
11
|
-
constructor(language: LanguageDefinition, options?: ParserOptions);
|
|
12
|
-
run(input: string): {
|
|
13
|
-
results: any[];
|
|
14
|
-
};
|
|
15
|
-
private getTokenQueue;
|
|
16
|
-
}
|
|
17
|
-
export declare class ParserUtility {
|
|
18
|
-
static TokenMatchesSymbol(token: LexerToken, symbol: GrammarRuleSymbol): boolean;
|
|
19
|
-
static SymbolIsTerminal<T extends GrammarRuleSymbol>(symbol: T): boolean;
|
|
20
|
-
static PostProcess(rule: GrammarRule, data: any, meta?: any): any;
|
|
21
|
-
}
|
|
22
|
-
interface ParserOptions {
|
|
23
|
-
algorithm: (keyof typeof ParserRegistry) | ParserAlgorithm;
|
|
24
|
-
parserOptions?: any;
|
|
25
|
-
}
|
|
26
|
-
export {};
|
|
1
|
+
import { GrammarRule, GrammarRuleSymbol, LanguageDefinition, LexerToken, ParserAlgorithm } from "../typings";
|
|
2
|
+
declare const ParserRegistry: {
|
|
3
|
+
[key: string]: ParserAlgorithm;
|
|
4
|
+
};
|
|
5
|
+
export declare function Parse(language: LanguageDefinition, input: string, options?: ParserOptions): {
|
|
6
|
+
results: any[];
|
|
7
|
+
};
|
|
8
|
+
export declare class Parser {
|
|
9
|
+
private language;
|
|
10
|
+
private options;
|
|
11
|
+
constructor(language: LanguageDefinition, options?: ParserOptions);
|
|
12
|
+
run(input: string): {
|
|
13
|
+
results: any[];
|
|
14
|
+
};
|
|
15
|
+
private getTokenQueue;
|
|
16
|
+
}
|
|
17
|
+
export declare class ParserUtility {
|
|
18
|
+
static TokenMatchesSymbol(token: LexerToken, symbol: GrammarRuleSymbol): boolean;
|
|
19
|
+
static SymbolIsTerminal<T extends GrammarRuleSymbol>(symbol: T): boolean;
|
|
20
|
+
static PostProcess(rule: GrammarRule, data: any, meta?: any): any;
|
|
21
|
+
}
|
|
22
|
+
interface ParserOptions {
|
|
23
|
+
algorithm: (keyof typeof ParserRegistry) | ParserAlgorithm;
|
|
24
|
+
parserOptions?: any;
|
|
25
|
+
}
|
|
26
|
+
export {};
|
package/build/parser/parser.js
CHANGED
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ParserUtility = exports.Parser = exports.Parse = void 0;
|
|
4
|
-
const character_lexer_1 = require("../lexers/character-lexer");
|
|
5
|
-
const stateful_lexer_1 = require("../lexers/stateful-lexer");
|
|
6
|
-
const token_buffer_1 = require("../lexers/token-buffer");
|
|
7
|
-
const cyk_1 = require("./algorithms/cyk");
|
|
8
|
-
const earley_1 = require("./algorithms/earley");
|
|
9
|
-
const lr_1 = require("./algorithms/lr");
|
|
10
|
-
const ParserRegistry = {
|
|
11
|
-
earley: earley_1.Earley,
|
|
12
|
-
cyk: cyk_1.CYK,
|
|
13
|
-
lr: lr_1.LR
|
|
14
|
-
};
|
|
15
|
-
function Parse(language, input, options) {
|
|
16
|
-
const i = new Parser(language, options);
|
|
17
|
-
return i.run(input);
|
|
18
|
-
}
|
|
19
|
-
exports.Parse = Parse;
|
|
20
|
-
class Parser {
|
|
21
|
-
language;
|
|
22
|
-
options;
|
|
23
|
-
constructor(language, options = { algorithm: 'earley', parserOptions: {} }) {
|
|
24
|
-
this.language = language;
|
|
25
|
-
this.options = options;
|
|
26
|
-
}
|
|
27
|
-
run(input) {
|
|
28
|
-
const tokenQueue = this.getTokenQueue();
|
|
29
|
-
tokenQueue.feed(input);
|
|
30
|
-
if (typeof this.options.algorithm == 'function')
|
|
31
|
-
return this.options.algorithm({ ...this.language, tokens: tokenQueue, utility: ParserUtility }, this.options.parserOptions);
|
|
32
|
-
return ParserRegistry[this.options.algorithm]({ ...this.language, tokens: tokenQueue, utility: ParserUtility }, this.options.parserOptions);
|
|
33
|
-
}
|
|
34
|
-
getTokenQueue() {
|
|
35
|
-
const { lexer } = this.language;
|
|
36
|
-
if (!lexer) {
|
|
37
|
-
return new token_buffer_1.TokenBuffer(new character_lexer_1.CharacterLexer());
|
|
38
|
-
}
|
|
39
|
-
else if ("feed" in lexer && typeof lexer.feed == 'function') {
|
|
40
|
-
return new token_buffer_1.TokenBuffer(lexer);
|
|
41
|
-
}
|
|
42
|
-
else if ('states' in lexer) {
|
|
43
|
-
return new token_buffer_1.TokenBuffer(new stateful_lexer_1.StatefulLexer(lexer));
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
exports.Parser = Parser;
|
|
48
|
-
class ParserUtility {
|
|
49
|
-
static TokenMatchesSymbol(token, symbol) {
|
|
50
|
-
if (typeof symbol === 'string')
|
|
51
|
-
throw 'Attempted to match token against non-terminal';
|
|
52
|
-
if (typeof symbol == 'function')
|
|
53
|
-
return symbol(token);
|
|
54
|
-
if (!symbol)
|
|
55
|
-
return;
|
|
56
|
-
if ("test" in symbol)
|
|
57
|
-
return symbol.test(token.value);
|
|
58
|
-
if ("token" in symbol)
|
|
59
|
-
return symbol.token === token.type || token.tag?.has(symbol.token);
|
|
60
|
-
if ("literal" in symbol)
|
|
61
|
-
return symbol.literal === token.value;
|
|
62
|
-
}
|
|
63
|
-
static SymbolIsTerminal(symbol) {
|
|
64
|
-
return typeof symbol != 'string';
|
|
65
|
-
}
|
|
66
|
-
static PostProcess(rule, data, meta) {
|
|
67
|
-
if (rule.postprocess) {
|
|
68
|
-
return rule.postprocess({ rule, data, meta });
|
|
69
|
-
}
|
|
70
|
-
return data;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
exports.ParserUtility = ParserUtility;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ParserUtility = exports.Parser = exports.Parse = void 0;
|
|
4
|
+
const character_lexer_1 = require("../lexers/character-lexer");
|
|
5
|
+
const stateful_lexer_1 = require("../lexers/stateful-lexer");
|
|
6
|
+
const token_buffer_1 = require("../lexers/token-buffer");
|
|
7
|
+
const cyk_1 = require("./algorithms/cyk");
|
|
8
|
+
const earley_1 = require("./algorithms/earley");
|
|
9
|
+
const lr_1 = require("./algorithms/lr");
|
|
10
|
+
const ParserRegistry = {
|
|
11
|
+
earley: earley_1.Earley,
|
|
12
|
+
cyk: cyk_1.CYK,
|
|
13
|
+
lr: lr_1.LR
|
|
14
|
+
};
|
|
15
|
+
function Parse(language, input, options) {
|
|
16
|
+
const i = new Parser(language, options);
|
|
17
|
+
return i.run(input);
|
|
18
|
+
}
|
|
19
|
+
exports.Parse = Parse;
|
|
20
|
+
class Parser {
|
|
21
|
+
language;
|
|
22
|
+
options;
|
|
23
|
+
constructor(language, options = { algorithm: 'earley', parserOptions: {} }) {
|
|
24
|
+
this.language = language;
|
|
25
|
+
this.options = options;
|
|
26
|
+
}
|
|
27
|
+
run(input) {
|
|
28
|
+
const tokenQueue = this.getTokenQueue();
|
|
29
|
+
tokenQueue.feed(input);
|
|
30
|
+
if (typeof this.options.algorithm == 'function')
|
|
31
|
+
return this.options.algorithm({ ...this.language, tokens: tokenQueue, utility: ParserUtility }, this.options.parserOptions);
|
|
32
|
+
return ParserRegistry[this.options.algorithm]({ ...this.language, tokens: tokenQueue, utility: ParserUtility }, this.options.parserOptions);
|
|
33
|
+
}
|
|
34
|
+
getTokenQueue() {
|
|
35
|
+
const { lexer } = this.language;
|
|
36
|
+
if (!lexer) {
|
|
37
|
+
return new token_buffer_1.TokenBuffer(new character_lexer_1.CharacterLexer());
|
|
38
|
+
}
|
|
39
|
+
else if ("feed" in lexer && typeof lexer.feed == 'function') {
|
|
40
|
+
return new token_buffer_1.TokenBuffer(lexer);
|
|
41
|
+
}
|
|
42
|
+
else if ('states' in lexer) {
|
|
43
|
+
return new token_buffer_1.TokenBuffer(new stateful_lexer_1.StatefulLexer(lexer));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.Parser = Parser;
|
|
48
|
+
class ParserUtility {
|
|
49
|
+
static TokenMatchesSymbol(token, symbol) {
|
|
50
|
+
if (typeof symbol === 'string')
|
|
51
|
+
throw 'Attempted to match token against non-terminal';
|
|
52
|
+
if (typeof symbol == 'function')
|
|
53
|
+
return symbol(token);
|
|
54
|
+
if (!symbol)
|
|
55
|
+
return;
|
|
56
|
+
if ("test" in symbol)
|
|
57
|
+
return symbol.test(token.value);
|
|
58
|
+
if ("token" in symbol)
|
|
59
|
+
return symbol.token === token.type || token.tag?.has(symbol.token);
|
|
60
|
+
if ("literal" in symbol)
|
|
61
|
+
return symbol.literal === token.value;
|
|
62
|
+
}
|
|
63
|
+
static SymbolIsTerminal(symbol) {
|
|
64
|
+
return typeof symbol != 'string';
|
|
65
|
+
}
|
|
66
|
+
static PostProcess(rule, data, meta) {
|
|
67
|
+
if (rule.postprocess) {
|
|
68
|
+
return rule.postprocess({ rule, data, meta });
|
|
69
|
+
}
|
|
70
|
+
return data;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.ParserUtility = ParserUtility;
|
|
74
74
|
//# sourceMappingURL=parser.js.map
|