forked from daKuleMune/nodebnf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BnfRules.js
375 lines (364 loc) · 10.2 KB
/
BnfRules.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//Some of these more complex rules can be removed if a token can generate a group rule. Once that is added they will be removed!
exports.bnfRules = {
BLANK(){
return true;
},
DIGIT( token ){
if( token.CharCodeRange( 48, 57 ) ){
token.SetChar();
return true;
}
return false;
},
ALPHA( token ){
if( token.CharCodeRange( 65, 90 ) || token.CharCodeRange( 97, 122 ) ){
token.SetChar();
return true;
}
return false;
},
SYMBOL( token ){
if( token.CharIs( 33 )
|| token.CharCodeRange( 35, 38 )
|| token.CharCodeRange( 40, 47 )
|| token.CharCodeRange( 58, 64 )
|| token.CharIs( 91 )
|| token.CharCodeRange( 93, 95 )
|| token.CharCodeRange( 123, 126 ) ){
token.SetChar();
return true;
}
return false;
},
ANYCHAR( token ){
return token.Or( [
token.Rule( "ALPHA" ),
token.Rule( "DIGIT" ),
token.Rule( "SYMBOL" ),
token.Rule( "ONEWSP" )
]);
},
//Should change to range rule @LHF
DIGITS( token ){
return token.Or( [
token.Rule( "DIGIT" ),
( t ) => t.And( [ token.Rule( "DIGIT" ), token.Rule( "DIGITS" ) ] )
]);
},
NUMBER( token ){
return token.Or( [
( t ) => t.And( [ t.Rule( "DIGITS" ), t.Grammar( "LITERAL", "." ), t.Rule( "DIGITS" ) ] ),
( t ) => t.And( [ t.Grammar( "LITERAL", "." ), t.Rule( "DIGITS" ) ] ),
token.Rule( "DIGITS" )
]);
},
TAB( token ){
return token.TryChar( 9 );
},
SPACE( token ){
return token.TryChar( 32 );
},
//Should change to range rule @LHF
WSP( token ){
return token.Or( [
token.Rule( "ONEWSP" ),
( t ) => t.And( [ token.Rule( "ONEWSP" ), token.Rule( "WSP" ) ] )
]);
},
//MIGHT BE REMOVED IN THIS MAJOR VERSION! DO NOT USE!
ONEWSP( token ){
return token.Or( [
token.Rule( "SPACE" ),
token.Rule( "TAB" )
]);
},
CR( token ){
return token.TryChar( 13 );
},
LF( token ){
return token.TryChar( 10 );
},
CRLF( token ){
return token.Or( [
( t ) => t.And( [ t.Rule( "CR" ), t.Rule( "LF" ) ] ),
token.Rule( "CR" ),
token.Rule( "LF" )
]);
},
OWSP( token ){
return token.Or( [
token.Rule( "WSP" ), token.Rule( "BLANK" )
]);
},
BLANK( token ){
return true;
},
//MIGHT BE REMOVED IN THIS MAJOR VERSION! DO NOT USE!
ONEWSPCRLF( token ){
return token.Or( [
token.Rule( "ONEWSP" ), token.Rule( "CRLF" )
]);
},
//MIGHT BE REMOVED IN THIS MAJOR VERSION! DO NOT USE!
ONEORMOREWSP( token ){
return token.Or( [
token.Rule( "ONEWSPCRLF" ),
( t ) => t.And( [ token.Rule( "ONEWSPCRLF" ), token.Rule( "ONEORMOREWSP" ) ] )
]);
},
ANYWSP( token ){
return token.Or( [
token.Rule( "ONEORMOREWSP" ), token.Rule( "BLANK" )
]);
},
ESCAPE( token ){
return token.TryChar( 92 );
},
QUOTE( token ){
return token.TryChar( 34 );
},
SQUOTE( token ){
return token.TryChar( 39 );
},
AQUOTE( token ){
return token.TryChar( 96 );
},
ESCQUOTE( token ){
return token.TryString( Buffer.from( [ 92, 34 ] ) );
},
ESCSQUOTE( token ){
return token.TryString( Buffer.from( [ 92, 39 ] ) );
},
ESCAQUOTE( token ){
return token.TryString( Buffer.from( [ 92, 96 ] ) );
},
//DO BE REMOVED IN THIS MAJOR VERSION! DO NOT USE!
SQEANYCHAR( token ){
return token.Or( [
token.Rule( "ESCSQUOTE" ),
token.Rule( "ANYCHAR" ),
token.Rule( "QUOTE" ),
token.Rule( "AQUOTE" ),
token.Rule( "ESCAPE" )
]);
},
//DO BE REMOVED IN THIS MAJOR VERSION! DO NOT USE!
SQLITERALCHARS( token ){
return token.Or( [
token.Rule( "SQEANYCHAR" ),
( t ) => t.And( [ token.Rule( "SQEANYCHAR" ), token.Rule( "SQLITERALCHARS" ) ] )
]);
},
SQLITERAL( token ){
return token.And( [
token.Rule( "SQUOTE" ),
token.Rule( "SQLITERALCHARS" ),
token.Rule( "SQUOTE" )
]);
},
//DO BE REMOVED IN THIS MAJOR VERSION! DO NOT USE!
QEANYCHAR( token ){
return token.Or( [
token.Rule( "ESCQUOTE" ),
token.Rule( "ANYCHAR" ),
token.Rule( "SQUOTE" ),
token.Rule( "AQUOTE" ),
token.Rule( "ESCAPE" )
]);
},
//DO BE REMOVED IN THIS MAJOR VERSION! DO NOT USE!
QLITERALCHARS( token ){
return token.Or( [
token.Rule( "QEANYCHAR" ),
( t ) => t.And( [ token.Rule( "QEANYCHAR" ), token.Rule( "QLITERALCHARS" ) ] )
]);
},
QLITERAL( token ){
return token.And( [
token.Rule( "QUOTE" ),
token.Rule( "QLITERALCHARS" ),
token.Rule( "QUOTE" )
]);
},
//DO BE REMOVED IN THIS MAJOR VERSION! DO NOT USE!
AQEANYCHAR( token ){
return token.Or( [
token.Rule( "ESCAQUOTE" ),
token.Rule( "ANYCHAR" ),
token.Rule( "SQUOTE" ),
token.Rule( "QUOTE" ),
token.Rule( "CRLF" ),
token.Rule( "ESCAPE" )
]);
},
//DO BE REMOVED IN THIS MAJOR VERSION! DO NOT USE!
AQLITERALCHARS( token ){
return token.Or( [
token.Rule( "AQEANYCHAR" ),
( t ) => t.And( [ token.Rule( "AQEANYCHAR" ), token.Rule( "AQLITERALCHARS" ) ] )
] );
/*
This should work as the new way of doing an AQLITERALCHAR which can then be moved into AQLITERAL which is planned to remain.
return token.Grammar( "RANGE", {
rule : ( t ) =>
t.Grammar( "GROUP", ( t1 ) =>
t1.Or( [
t1.Rule( "ESCAQUOTE" ),
t1.Rule( "ANYCHAR" ),
t1.Rule( "SQUOTE" ),
t1.Rule( "QUOTE" ),
t1.Rule( "CRLF" ),
t1.Rule( "ESCAPE" )
] )
),
range : [ 1, -1 ]
} );
*/
},
AQLITERAL( token ){
return token.And( [
token.Rule( "AQUOTE" ),
token.Rule( "AQLITERALCHARS" ),
token.Rule( "AQUOTE" )
]);
},
LITERAL( token ){
return token.Or( [
token.Rule( "SQLITERAL" ),
token.Rule( "QLITERAL" )
]);
},
ANYLITERAL( token ){
return token.Or( [
token.Rule( "SQLITERAL" ),
token.Rule( "QLITERAL" ),
token.Rule( "AQLITERAL" )
]);
},
EOF( token ){
if( token.point === token.eof || token.script.subscript ){
return true;
}
return false;
},
SCRIPT( token ){
return token.And( [ token.Rule( "SYNTAX" ), token.Rule( "EOF" ) ] );
}
};
exports.parserRules = {
"&OR"( token ){
let rules = token.ruleSyntax;
let resetPoint = token.point;
let tokenPoints = {};
for( let i = 0; i < rules.length; i++ ){
token.point = resetPoint;
tokenPoints[i] = {
result : rules[i]( token ),
point : token.point
};
token._tokenTrees.push( [] );
token._currentTokenTree = token._tokenTrees[token._tokenTrees.length - 1];
}
let topWeightIndex = -1;
let topWeight = -1;
for( let i = 0; i < token._tokenTrees.length - 1; i++ ){
if( tokenPoints[i].result === true ){
let weight = 0;
token._tokenTrees[i].map( x => weight += x.weight );
if( weight > topWeight ){
topWeight = weight;
topWeightIndex = i;
}
}
}
if( topWeightIndex !== -1 ){
token.point = tokenPoints[topWeightIndex].point;
token._tokenTrees[0] = token._tokenTrees[topWeightIndex];
return true;
}
else{
//This can be optimized @LHF
for( let i = 0; i < token._tokenTrees.length - 1; i++ ){
for( let t = 0; t < token._tokenTrees[i].length; t++ ){
for( let line in token._tokenTrees[i][t].expected ){
for( let char in token._tokenTrees[i][t].expected[line] ){
for( let x = 0; x < token._tokenTrees[i][t].expected[line][char].length; x++ ){
token.AddExpected( token._tokenTrees[i][t].expected[line][char][x], line, char );
}
}
}
}
}
token._tokenTrees[0] = [];
return false;
}
},
"&AND"( token ){
let rules = token.ruleSyntax;
let resetPoint = token.point;
for( let i = 0; i < rules.length; i++ ){
if( !rules[i]( token ) ){
token.point = resetPoint;
return false;
}
}
return true;
},
"&LITERAL"( token ){
let scriptLit = token.GetLitString( token.ruleSyntax.length );
if( token.ruleSyntax === scriptLit ){
token.SetValue( token.ruleSyntax );
return true;
}
else{
token.AddExpected( token.ruleSyntax );
return false;
}
},
"&OPTIONAL"( token ){
return token.Or( [
token.ruleSyntax,
token.Rule( "BLANK" )
]);
},
"&GROUP"( token ){
return token.ruleSyntax( token );
},
"&RANGE"( token ){
if( token.ruleSyntax[0] === token.ruleSyntax[1] ){
return token.TryChar( token.ruleSyntax[0] );
}
else{
return token.TryCharRange( token.ruleSyntax[0], token.ruleSyntax[1] );
}
},
"&NOT"( token ){
return !token.ruleSyntax( token );
},
"&REPEAT"( token ){
while( token.ruleSyntax.rule( token ) ){
}
//Somehow the group def needs to be preservered to know the syntax that was expected.
let expected = token._currentTokenTree[0].name;
token._currentTokenTree.splice( token._currentTokenTree.length - 1, 1 );
let result = token._currentTokenTree.length >= token.ruleSyntax.range[0] && ( token.ruleSyntax.range[1] === -1 || token._currentTokenTree.length <= token.ruleSyntax.range[1] );
if( !result ){
token.AddExpected( "Repeating " + expected + " " + token.ruleSyntax.range[0] + "-" + ( token.ruleSyntax.range[1] === -1 ? "any" : token.ruleSyntax.range[1] ) );
}
return result;
},
"&SCRIPT"( token ){
let script = token.script.SubScript( token.ruleSyntax );
script.subscript = true;
script.rootToken.point = token.point;
let result = script.rootToken.Evaluate();
token._currentTokenTree.push( script.rootToken );
script.rootToken.parent = token;
if( result ){
token.point = script.rootToken.endPoint;
return true;
}
token.AddExpected( "Language " + token.ruleSyntax );
return false;
}
};