-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathToken.js
353 lines (301 loc) · 9.69 KB
/
Token.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
const { bnfRules, parserRules } = require( "./BnfRules" );
exports.Token = class Token{
constructor( name, script, parentToken ){
this.name = name;
this.script = script;
this.parent = parentToken;
this.consumerIndex = 0;
this.expended = false;
this.point = 0;
this.tokens = [];
this._expected = {};
this._tokenTrees = [];
this._currentTokenTree = [];
this._value = null;
this._isValid = false;
this.ruleSyntax = null;
//This being done via index lookup will save a few cycles @LHF
this.grammar = this.script.rules[name] || bnfRules[name] || parserRules[name];
}
get eof(){
return this.script.scriptBuffer.length;
}
//These inner loops are brute force at best and can be optimized by the application of a few setters. @LHF
get endPoint(){
let point = this.point;
this.tokens.map( x => point = ( x.endPoint > point ) ? x.endPoint : point );
return point;
}
_ResetTokenTrees(){
this._tokenTrees = [[]];
this._currentTokenTree = this._tokenTrees[0];
}
_PickSyntaxTree(){
this.tokens = this.tokens.concat( this._tokenTrees[0] );
}
CharCodeRange( lowCode, highCode ){
if( this.script.scriptBuffer[this.point] >= lowCode && this.script.scriptBuffer[this.point] <= highCode ){
return true;
}
return false;
}
//One thing that can be done to improve overall performance is to break 'or', and 'and' and into rules.
//Once that is done syntax tree is easier to understand, and it will also be easier to describe
//what when wrong; however that is a large refactor.
Evaluate(){
this._ResetTokenTrees();
let result = false;
try{
result = this.grammar( this );
}
catch( ex ){
console.log( ex );
console.error( this.name, "Grammar not found" );
}
this._PickSyntaxTree();
this._isValid = result;
return result;
}
Child( tokenType ){
for( let i = 0; i < this.tokens.length; i++ ){
if( this.tokens[i].name === tokenType ){
return this.tokens[i];
}
else{
let innerChild = this.tokens[i].Child( tokenType );
if( innerChild && innerChild.name === tokenType ){
return innerChild;
}
}
}
return null;
}
Parent( tokenType ){
if( this.parent === null ){
return null;
}
if( this.parent.name === tokenType ){
return this.parent;
}
else{
return this.parent.Parent( tokenType );
}
}
get valid(){
if( !this._isValid ){
return false;
}
for( let i = 0; i < this.tokens.length; i++ ){
if( !this.tokens[i].valid ){
return false;
}
}
return true;
}
CreateRuleToken( tokenName ){
return new Token( "&" + tokenName, this.script, this );
}
Rule( tokenName ){
return ( token ) => {
let ruleToken = new Token( tokenName, this.script, token );
ruleToken.point = this.point;
this._currentTokenTree.push( ruleToken );
if( ruleToken.Evaluate() ){
token.point = ruleToken.point;
return true;
}
if( !this.name.startsWith( "&" ) ){
this.AddExpected( tokenName );
}
return false;
};
}
Grammar( grammarRuleName, grammarSyntax ){
return ( token ) => {
let ruleToken = token.CreateRuleToken( grammarRuleName );
ruleToken.ruleSyntax = grammarSyntax;
ruleToken.point = this.point;
this._currentTokenTree.push( ruleToken );
if( ruleToken.Evaluate() ){
this.point = ruleToken.point;
return true;
}
return false;
};
}
//This should be moved into the script as that is the only part of the application that uses this anyways @LHF
AddExpected( expected, line = null, char = null ){
line = line || this._GetPointLine();
char = char || this._GetPointChar();
this._expected[line] = this._expected[line] || {};
this._expected[line][char] = this._expected[line][char] || [];
if( this._expected[line][char].indexOf( expected ) === -1 ){
this._expected[line][char].push( expected );
}
}
//This should be tracked to save on look-ups, @LHF.
_GetPointLine(){
return this.script.rawScript.substring( 0, this.point ).split( "\n" ).length;
}
//This should be tracked to save on look-ups, @LHF.
_GetPointChar(){
let charPoint = this.script.rawScript.substring( 0, this.point ).lastIndexOf( "\n" );
return charPoint != -1 ? charPoint : this.point;
}
get expected(){
let expected = JSON.parse( JSON.stringify( this._expected ) );
this.tokens.map( ( x ) => {
let tokenExpect = x.expected;
//This can be optimized but only has one execute use @LHF, but weak LHF.
for( let line in tokenExpect ){
for( let char in tokenExpect[line] ){
tokenExpect[line][char].map( ( y ) => {
expected[line] = expected[line] || {};
expected[line][char] = expected[line][char] || [];
expected[line][char].push( y );
});
}
}
} );
return expected;
}
get weight(){
let weight = this.name !== "BLANK" ? 1 : 0;
this.tokens.map( x => weight += x.weight );
return weight;
}
GetChar(){
let charBuffer = Buffer.alloc( 1 );
charBuffer[0] = this.script.scriptBuffer[this.point];
return charBuffer.toString();
}
GetString( length ){
let stringBuffer = Buffer.alloc( length );
this.script.scriptBuffer.copy( stringBuffer, 0, this.point, length );
return stringBuffer.toString();
}
TryCharRange( charStart, charEnd ){
if( this.CharCodeRange( charStart, charEnd ) ){
this.SetValue( this.GetChar() );
this.point++;
return true;
}
return false;
}
TryChar( char ){
if( this.CharIs( char ) ){
this.SetValue( this.GetChar() );
this.point++;
return true;
}
return false;
}
TryString( charBuffer ){
let stringBuffer = Buffer.alloc( charBuffer.length );
this.script.scriptBuffer.copy( stringBuffer, 0, this.point, charBuffer.length );
if( !stringBuffer.equals( charBuffer ) ){
return false;
}
this.SetValue( stringBuffer.toString() );
this.point += charArray.length;
return true;
}
SetChar( char ){
this.SetValue( this.GetChar() );
this.point++;
}
CharIn( charIndexArray ){
let at = this.script.scriptBuffer[this.point];
return charIndexArray.indexOf( at ) !== -1;
}
CharIs( compare, pointOffset = 0 ){
return this.script.scriptBuffer[this.point + pointOffset] === compare;
}
SetValue( value ){
this._value = value;
}
//This should call the rule engine//
And( rules ){
let resetPoint = this.point;
for( let i = 0; i < rules.length; i++ ){
if( !rules[i]( this ) ){
this.point = resetPoint;
return false;
}
}
return true;
}
//This should call the rule engine//
//The idea was to use syntax trees to determain the best path//
//In thoury this was a good idea because it doesn't matter what the order of operations are//
//However in practice this gives way to unpredictable parsing results//
//The synxtax tree comperison is also quite expinsive with deep recuretion in the execution.//
//Should be rolled back to first true and don't bother attempting to execute the rest.//
//@LHF
Or( rules ){
let resetPoint = this.point;
let tokenPoints = {};
for( let i = 0; i < rules.length; i++ ){
this.point = resetPoint;
tokenPoints[i] = {
result : rules[i]( this ),
point : this.point
};
this._tokenTrees.push( [] );
this._currentTokenTree = this._tokenTrees[this._tokenTrees.length - 1];
}
let topWeightIndex = -1;
let topWeight = -1;
for( let i = 0; i < this._tokenTrees.length - 1; i++ ){
if( tokenPoints[i].result === true ){
let weight = 0;
this._tokenTrees[i].map( x => weight += x.weight );
if( weight > topWeight ){
topWeight = weight;
topWeightIndex = i;
}
}
}
if( topWeightIndex !== -1 ){
this.point = tokenPoints[topWeightIndex].point;
this._tokenTrees[0] = this._tokenTrees[topWeightIndex];
return true;
}
else{
//This can be optimized @LHF
for( let i = 0; i < this._tokenTrees.length - 1; i++ ){
for( let t = 0; t < this._tokenTrees[i].length; t++ ){
for( let line in this._tokenTrees[i][t].expected ){
for( let char in this._tokenTrees[i][t].expected[line] ){
for( let x = 0; x < this._tokenTrees[i][t].expected[line][char].length; x++ ){
this.AddExpected( this._tokenTrees[i][t].expected[line][char][x], line, char );
}
}
}
}
}
this._tokenTrees[0] = [];
return false;
}
}
get value(){
let value = "";
if( this._value !== null ){
value += this._value;
}
this.tokens.map( x => value += x.value );
//@TODO this might need to be tokens[*].value + _value ! _value + tokens[*].value @VERIFY
return value;
}
//Deprecated due to change to use buffers, as @LHF all literal storage should be converted to buffers post language compile.
//This can then change to GetString and TryString in the perspective parts of the rules.
GetLitString( length ){
return this.script.GetString( length, this );
}
GetTokens(){
return this.tokens;
}
Seek( amount ){
this.point += amount;
}
}