-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathinheritance.js
241 lines (207 loc) · 6.02 KB
/
inheritance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Example Of using Grammar complex grammar inheritance to implement
* 'Structured natural language' supporting multiple 'spoken languages' using grammar inheritance.
*
* 1. An "Abstract" Base Grammar with two concrete grammars extending it.
* 2. Each concrete grammar has a different lexer
* 3. This also shows an example of using Token inheritance
*/
import { createToken, Lexer, CstParser } from "chevrotain";
// ----------------- lexer -----------------
const RelationWord = createToken({ name: "RelationWord", pattern: Lexer.NA });
// Token inheritance CONSUME(RelationWord) will work on any Token extending RelationWord
const And = createToken({
name: "And",
pattern: /and/,
categories: RelationWord,
});
const Before = createToken({
name: "Before",
pattern: /before/,
categories: RelationWord,
});
const After = createToken({
name: "After",
pattern: /after/,
categories: RelationWord,
});
const Und = createToken({
name: "Und",
pattern: /und/,
categories: RelationWord,
});
const Vor = createToken({
name: "Vor",
pattern: /vor/,
categories: RelationWord,
});
const Nach = createToken({
name: "Nach",
pattern: /nach/,
categories: RelationWord,
});
/// English Tokens
const Cook = createToken({ name: "Cook", pattern: /cooking|cook/ });
const Some = createToken({ name: "Some", pattern: /some/ });
const Sausages = createToken({ name: "Sausages", pattern: /sausages/ });
const Clean = createToken({ name: "Clean", pattern: /clean/ });
const The = createToken({ name: "The", pattern: /the/ });
const Room = createToken({ name: "Room", pattern: /room/ });
// German Tokens
const Kochen = createToken({ name: "Kochen", pattern: /kochen/ });
const Wurstchen = createToken({ name: "Wurstchen", pattern: /wurstchen/ });
const Wurst = createToken({ name: "Wurst", pattern: /wurst/ });
const Raum = createToken({ name: "Raum", pattern: /raum/ });
const Auf = createToken({ name: "Auf", pattern: /auf/ });
const Den = createToken({ name: "Den", pattern: /den/ });
const WhiteSpace = createToken({
name: "WhiteSpace",
pattern: /\s+/,
group: Lexer.SKIPPED,
});
const abstractTokens = [RelationWord];
const englishTokens = [
WhiteSpace,
RelationWord,
And,
Before,
After,
Cook,
Some,
Sausages,
Clean,
The,
Room,
];
const germanTokens = [
WhiteSpace,
RelationWord,
Und,
Vor,
Nach,
Kochen,
Wurstchen,
Wurst,
Raum,
Auf,
Den,
];
// We can define a different Lexer for each of the sub grammars.
const EnglishLexer = new Lexer(englishTokens);
const GermanLexer = new Lexer(germanTokens);
// ----------------- parser -----------------
// Extending the base chevrotain CstParser class
class AbstractCommandsParser extends CstParser {
constructor(tokenVocabulary) {
// combining the token vocabularies of parent and child.
super(abstractTokens.concat(tokenVocabulary));
const $ = this;
$.RULE("commands", () => {
$.SUBRULE($.command);
$.MANY(() => {
$.CONSUME(RelationWord);
$.SUBRULE2($.command);
});
});
$.RULE("command", () => {
// The cook and clean commands must be implemented in each sub grammar
$.OR([
{ ALT: () => $.SUBRULE($.cookCommand) },
{ ALT: () => $.SUBRULE($.cleanCommand) },
]);
});
// this is an "abstract" base grammar it should not be instantiated directly
// therefor it does not invoke "performSelfAnalysis"
}
}
class EnglishCommandsParser extends AbstractCommandsParser {
constructor() {
super(englishTokens);
const $ = this;
// implementing the 'cookCommand' referenced in the AbstractCommandsParser
$.RULE("cookCommand", () => {
$.CONSUME(Cook);
$.OPTION(() => {
$.CONSUME(Some);
});
$.CONSUME(Sausages);
});
// implementing the 'cleanCommand' referenced in the AbstractCommandsParser
$.RULE("cleanCommand", () => {
$.CONSUME(Clean);
$.CONSUME(The);
$.CONSUME(Room);
});
// very important to call this after all the rules have been defined.
// otherwise the parser may not work correctly as it will lack information
// derived during the self analysis phase.
this.performSelfAnalysis();
}
}
class GermanCommandsParser extends AbstractCommandsParser {
constructor() {
super(germanTokens);
const $ = this;
// implementing the 'cookCommand' referenced in the AbstractCommandsParser
$.RULE("cookCommand", () => {
$.CONSUME(Kochen);
$.OR([
{ ALT: () => $.CONSUME(Wurstchen) },
{ ALT: () => $.CONSUME(Wurst) },
]);
});
// implementing the 'cleanCommand' referenced in the AbstractCommandsParser
$.RULE("cleanCommand", () => {
$.CONSUME(Raum);
$.CONSUME(Den);
$.CONSUME2(Raum);
$.CONSUME(Auf);
});
// very important to call this after all the rules have been defined.
// otherwise the parser may not work correctly as it will lack information
// derived during the self analysis phase.
this.performSelfAnalysis();
}
}
// ----------------- wrapping it all together -----------------
// reuse the same parser instances.
const englishParser = new EnglishCommandsParser();
const germanParser = new GermanCommandsParser();
export function parseCommand(text, language) {
// lex
let lexer;
// match language and lexer.
switch (language) {
case "english":
lexer = EnglishLexer;
break;
case "german":
lexer = GermanLexer;
break;
default:
throw Error("no valid language chosen");
}
const lexResult = lexer.tokenize(text);
// parse
let parser;
// match language and parser.
switch (language) {
case "english":
parser = englishParser;
break;
case "german":
parser = germanParser;
break;
default:
throw Error("no valid language chosen");
}
// setting a new input will RESET the parser instance's state.
parser.input = lexResult.tokens;
// any top level rule may be used as an entry point
const cst = parser.commands();
return {
cst: cst,
lexErrors: lexResult.errors,
parseErrors: parser.errors,
};
}