-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex-parser.js
255 lines (217 loc) · 6.7 KB
/
regex-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
'use strict';
/*
* This is the grammar for the regex parser in a yacc format:
*
* Start : Regex
* ;
*
* Regex : Term '|' Regex # Alternation
* ; Term
*
* Term : Factor Term # Concatenation
* | %empty
* ;
*
* Factor : Base '*' # Kleene Star
* | Base '+' # 1 or more
* | Base '?' # 0 or more
* | Base
* ;
*
* Base : CHAR
* | '\' CHAR # Escape reserved characters
* | '(' Regex ')' # Change order of operations
* | '[' CharSet ']' # Character class
* ;
*
* CharSet : CHAR ...
* ;
*/
const Nfa = require('./nfa.js');
class RegexParser {
constructor() {
// stuff
}
parse(input) {
this._input = input;
this._index = 0;
return this._grammarStart();
}
_peekInput(peeks) {
if (!this._isMore()) {
return false;
}
if (typeof peeks === typeof []) {
let peek = this._input.charAt(this._index);
for (let i = 0; i < peeks.length; ++i) {
if (peek === peeks[i]) {
return true;
}
}
return false;
} else {
return this._input.charAt(this._index) === peeks;
}
}
_getInput() {
return this._input.charAt(this._index);
}
_isMore() {
return this._index < this._input.length;
}
_acceptInput(accept) {
if (!accept || this._peekInput(accept)) {
++this._index;
return true;
}
return false;
}
_grammarStart() {
if (this._peekRegex()) {
let regex = this._grammarRegex();
if (!this._isMore()) {
return regex;
}
}
throw new 'Exception placeholder';
}
// TODO: Replace the tail recursion with a loop
_grammarRegex() {
if (this._peekTerm()) {
let term = this._grammarTerm();
if (this._acceptInput('|')) {
if (this._peekRegex()) {
return Nfa.buildUnion(term, this._grammarRegex());
}
} else {
return term;
}
}
throw new 'Exception placeholder';
}
_peekRegex() {
return this._peekTerm();
}
_grammarTerm() {
let factor = null;
while (this._peekFactor()) {
if (!factor) {
factor = this._grammarFactor();
} else {
factor = Nfa.buildDisjunction(factor, this._grammarFactor());
}
}
if (!factor) {
throw new 'Exception placeholder';
}
return factor;
}
_peekTerm() {
return true;
}
_grammarFactor() {
if (this._peekBase()) {
let base = this._grammarBase();
if (this._acceptInput('*')) {
return Nfa.buildKleeneStar(base);
} else if (this._acceptInput('+')) {
return Nfa.buildExtPlus(base);
} else if (this._acceptInput('?')) {
return Nfa.buildExtZeroOrOne(base);
}
return base;
}
throw new 'Exception placeholder';
}
_peekFactor() {
return this._peekBase();
}
_grammarBase() {
if (this._acceptInput('(')) {
if (this._peekRegex()) {
let regex = this._grammarRegex();
if (this._acceptInput(')')) {
return regex;
}
}
} else if (this._acceptInput('[')) {
if (this._peekCharSet()) {
let charSet = this._grammarCharSet();
if (this._acceptInput(']')) {
return Nfa.buildInput(charSet);
}
}
} else {
this._acceptInput('\\');
let symbol = this._getInput();
this._acceptInput();
return Nfa.buildInput([symbol]);
}
throw new 'Exception placeholder';
}
_peekBase() {
return !this._peekInput(['|', ')', '*', ']', '+', '?']) && this._isMore();
}
_grammarCharSet() {
let ranges = {
lowerAlpha: {
root: 'abcdefghijklmnopqrstuvwxyz'
}
, upperAlpha: {
root: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
}
, allAlpha: {
root: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
}
, numeric: {
root: '0123456789'
}
};
Object.keys(ranges).forEach((rangeKey) => {
let charSplit = ranges[rangeKey].root.split('');
ranges[rangeKey].list = charSplit;
ranges[rangeKey].elements = {};
for (let i = 0; i < charSplit.length; ++i) {
ranges[rangeKey].elements[charSplit[i]] = i;
}
});
let charSet = [];
while (!this._peekInput(']')) {
this._acceptInput('\\');
let symbol = this._getInput();
this._acceptInput();
if (symbol === '-' && (!this._peekInput(']') && charSet.length !== 0)) {
let lower = charSet.pop();
let upper = this._getInput();
this._acceptInput();
let range;
let rangeKeys = Object.keys(ranges);
for (let i = 0; i < rangeKeys.length; ++i) {
if (ranges[rangeKeys[i]].elements[lower] !== undefined) {
range = rangeKeys[i];
break;
}
}
if (!range) {
throw new 'Exception placeholder!';
}
if (!ranges[range].elements[upper]) {
throw new 'Exception placeholder!';
}
let lowerVal = ranges[range].elements[lower];
let upperVal = ranges[range].elements[upper];
if (lowerVal > upperVal) {
throw new 'Exception placeholder!';
}
charSet = charSet.concat(ranges[range].list.slice(lowerVal, upperVal+1));
} else {
charSet.push(symbol);
}
}
return charSet;
}
_peekCharSet() {
return !this._peekInput([']']) && this._isMore();
}
};
module.exports = RegexParser;