-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
300 lines (234 loc) · 8.04 KB
/
parser.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
const finiteAutomata = require('finite-automata');
/*
* Start : Alternation TOKEN_END
* ;
*
* Alternation : Concatenation
* : Concatenation TOKEN_PIPE Alternation
* ;
*
* Concatenation : Quantifier
* : Quantifier Concatenation
* ;
*
* Quantifier : Term
* : Term TOKEN_QUESTION_MARK
* : Term TOKEN_STAR
* : Term TOKEN_PLUS
* : Term TOKEN_OPEN_CURLY MatchOptions TOKEN_CLOSE_CURLY
* ;
*
* Term : TOKEN_CHAR
* : TOKEN_CHAR Term
* : TOKEN_OPEN_PAREN Alternation TOKEN_CLOSE_PAREN
* ;
*
* MatchOptions : TOKEN_NUMBER
* : TOKEN_NUMBER TOKEN_COMMA
* : TOKEN_NUMBER TOKEN_COMMA TOKEN_NUMBER
* ;
*/
class ParseContext {
constructor(input, finiteAutomata) {
this._input = input;
this._pos = 0;
this._lastAccepted = null;
this._finiteAutomata = finiteAutomata;
}
acceptEnd() {
if (this._pos === this._input.length) {
this._lastAccepted = null;
return true;
}
return false;
}
acceptToken(token) {
if (this._pos === this._input.length) {
return false;
}
if (this._input[this._pos] === token) {
this._lastAccepted = token;
++this._pos;
return true;
}
return false;
}
acceptChar() {
if (this._pos === this._input.length) {
return false;
}
const char = this._input[this._pos];
if (char === '|' || char === '(' || char === ')' || char === '?' || char === '*' || char === '+') {
return false;
}
this._lastAccepted = char;
++this._pos;
return true;
}
acceptRegex(regex) {
if (this._pos === this._input.length) {
return false;
}
regex.lastIndex = this._pos;
let matchData = regex.exec(this._input);
if (matchData !== null && matchData.index === this._pos) {
this._lastAccepted = matchData[0];
this._pos += matchData[0].length;
return true;
}
return false;
}
peekChar() {
if (this._pos === this._input.length) {
return [null, null];
}
return [this._input[this._pos], this._pos];
}
getLastAccepted() {
return this._lastAccepted;
}
getFiniteAutomata() {
return this._finiteAutomata;
}
}
const ERROR_TYPE = {
UNEXPECTED_CHARACTER: 0
};
const _buildError = (errorType, parseContext) => {
if (errorType === ERROR_TYPE.UNEXPECTED_CHARACTER) {
const [peekChar, peekPos] = parseContext.peekChar();
return `Unexpected character ${peekChar} at position ${peekPos}`;
}
};
let _grammarStart, _grammarAlternation, _grammarConcatenation, _grammarQuantifier, _grammarTerm;
let _peekStart, _peekAlternation, _peekConcatenation, _peekQuantifier, _peekTerm;
_grammarStart = (parseContext) => {
const [err, rootState, acceptingState] = _grammarAlternation(parseContext);
if (err) {
return [err];
}
if (!parseContext.acceptEnd()) {
console.log('Building error');
return [_buildError(ERROR_TYPE.UNEXPECTED_CHARACTER, parseContext)];
}
return [null, rootState, acceptingState];
};
_peekStart = (parseContext) => {
return _peekAlternation();
};
_grammarAlternation = (parseContext) => {
let [err, rootState, acceptingState] = _grammarConcatenation(parseContext);
if (err) {
return [err];
}
let alternations = [[rootState, acceptingState]];
while (parseContext.acceptToken('|')) {
[err, rootState, acceptingState] = _grammarConcatenation(parseContext);
if (err) {
return [err];
}
alternations.push([rootState, acceptingState]);
}
if (alternations.length === 1) {
return [null, rootState, acceptingState];
}
const newRootState = new finiteAutomata.FiniteAutomataState({ finiteAutomata: parseContext.getFiniteAutomata() });
const newAcceptingState = new finiteAutomata.FiniteAutomataState({ finiteAutomata: parseContext.getFiniteAutomata() });
for (const alternation of alternations) {
newRootState.addEpsilonTransition(alternation[0]);
alternation[1].addEpsilonTransition(newAcceptingState);
}
return [null, newRootState, newAcceptingState];
};
_peekAlternation = (parseContext) => {
return _peekConcatenation(parseContext);
};
_grammarConcatenation = (parseContext) => {
let [err, rootState, acceptingState] = _grammarQuantifier(parseContext);
if (err) {
return [err];
}
let concatenations = [[rootState, acceptingState]];
while (_peekConcatenation(parseContext)) {
[err, rootState, acceptingState] = _grammarQuantifier(parseContext);
if (err) {
return [err];
}
concatenations.push([rootState, acceptingState]);
}
if (concatenations.length === 1) {
return [null, rootState, acceptingState];
}
let middleState = concatenations[0][1];
for (let i = 1; i < concatenations.length; ++i) {
const concatenation = concatenations[i];
middleState.addEpsilonTransition(concatenation[0]);
middleState = concatenation[1];
}
return [null, concatenations[0][0], middleState];
};
_peekConcatenation = (parseContext) => {
return _peekQuantifier(parseContext);
};
_grammarQuantifier = (parseContext) => {
const [err, rootState, acceptingState] = _grammarTerm(parseContext);
if (err) {
return [err];
}
if (parseContext.acceptToken('?')) {
rootState.addEpsilonTransition(acceptingState);
} else if (parseContext.acceptToken('*')) {
rootState.addEpsilonTransition(acceptingState);
acceptingState.addEpsilonTransition(rootState);
} else if (parseContext.acceptToken('+')) {
acceptingState.addEpsilonTransition(rootState);
}
return [null, rootState, acceptingState];
};
_peekQuantifier = (parseContext) => {
return _peekTerm(parseContext);
};
_grammarTerm = (parseContext) => {
if (parseContext.acceptChar()) {
let chars = [parseContext.getLastAccepted()];
while (parseContext.acceptChar()) {
chars.push(parseContext.getLastAccepted());
}
const newRootState = new finiteAutomata.FiniteAutomataState({ finiteAutomata: parseContext.getFiniteAutomata() });
let newAcceptingState = newRootState;
for (const char of chars) {
const middleState = new finiteAutomata.FiniteAutomataState({ finiteAutomata: parseContext.getFiniteAutomata() });
newAcceptingState.addTransition(char, middleState);
newAcceptingState = middleState;
}
return [null, newRootState, newAcceptingState];
} else if (parseContext.acceptToken('(')) {
const [err, rootState, acceptingState] = _grammarAlternation(parseContext);
if (err) {
return [err];
}
if (!parseContext.acceptToken(')')) {
return [_buildError(ERROR_TYPE.UNEXPECTED_CHARACTER, parseContext)];
}
return [null, rootState, acceptingState];
} else {
return [_buildError(ERROR_TYPE.UNEXPECTED_CHARACTER, parseContext)];
}
};
_peekTerm = (parseContext) => {
let char = parseContext.peekChar()[0];
return (char !== null && char !== '|' && char !== ')' && char !== '?' && char !== '*' && char !== '+');
};
const parse = (input) => {
let parsedFiniteAutomata = new finiteAutomata.FiniteAutomata;
const parseContext = new ParseContext(input, parsedFiniteAutomata);
const [err, rootState, acceptingState] = _grammarStart(parseContext);
if (err) {
console.log(err);
return null;
}
parsedFiniteAutomata.setRootState(rootState);
parsedFiniteAutomata.prettyPrint();
return parsedFiniteAutomata;
};
parse('(ab)*');