@@ -26,18 +26,15 @@ module.exports = function(code) {
26
26
var insideTuple = [ ] ;
27
27
var insideInvocation = [ ] ;
28
28
var insideInitialization = [ ] ;
29
- // TODO - scope
30
29
31
- // advances the position of i by specified number of positions
30
+ // helper functions to advance the lexer's position in the input code
31
+ // and clear the chunk
32
32
var advance = function ( positions ) {
33
33
i += positions ;
34
34
} ;
35
-
36
35
var clearChunk = function ( ) {
37
36
chunk = '' ;
38
37
} ;
39
-
40
- // advances the position of i by specified number of positions and clears chunk
41
38
var advanceAndClear = function ( positions ) {
42
39
i += positions ;
43
40
clearChunk ( ) ;
@@ -51,24 +48,22 @@ module.exports = function(code) {
51
48
nextCol = code [ i + 1 ] ;
52
49
nextNextCol = code [ i + 2 ] ;
53
50
var lastToken = tokens [ tokens . length - 1 ] ;
54
- var lastCollectionIndex = insideCollection . length - 1 ;
55
- var lastCollection = insideCollection [ lastCollectionIndex ] ;
56
- var lastFunctionIndex = insideFunction . length - 1 ;
57
- var lastFunction = insideFunction [ lastFunctionIndex ] ;
51
+ var lastCollection = insideCollection [ insideCollection . length - 1 ] ;
52
+ var lastFunction = insideFunction [ insideFunction . length - 1 ] ;
58
53
59
54
// console.log(chunk);
60
55
// console.log(currCol);
61
56
// console.log(nextCol);
62
57
// console.log(tokens);
63
58
// console.log(emptyLine);
64
59
65
- // newline handling
60
+ // handles new lines
66
61
if ( lexerFunctions . handleNewLine ( emptyLine , tokens , lastToken , currCol ) ) {
67
62
advanceAndClear ( 1 ) ;
68
63
continue
69
64
}
70
65
71
- // comment handling
66
+ // handles comments
72
67
if ( lexerFunctions . checkForCommentStart ( insideComment , chunk , tokens ,
73
68
currCol , nextCol ) ) {
74
69
advanceAndClear ( 2 ) ;
@@ -83,50 +78,37 @@ module.exports = function(code) {
83
78
continue ;
84
79
}
85
80
86
- // ignoring whitespace
87
- if ( chunk === ' ' ) {
81
+ // ignores chunks that are solely whitespace
82
+ if ( lexerFunctions . checkForWhitespace ( chunk ) ) {
88
83
advanceAndClear ( 1 ) ;
89
84
continue ;
90
85
}
91
86
92
- // tracks state: whether inside a string
87
+ // tracks whether inside a string
93
88
if ( currCol === '"' && insideString . status ) {
94
89
insideString . status = false ;
95
90
} else if ( currCol === '"' ) {
96
91
insideString . status = true ;
97
92
}
98
93
99
- // number handling
94
+ // handles numbers
100
95
if ( lexerFunctions . handleNumber ( insideString , insideNumber , chunk , tokens , nextCol , nextNextCol ) === true ) {
101
96
advanceAndClear ( 1 ) ;
102
97
continue ;
103
98
} else if ( lexerFunctions . handleNumber ( insideString , insideNumber , chunk , tokens , nextCol , nextNextCol ) === "skip" ) {
104
- advance ( 2 ) ;
105
99
lexerFunctions . handleEndOfFile ( nextCol , tokens ) ;
100
+ advance ( 2 ) ;
106
101
continue ;
107
102
}
108
103
109
- // handle ranges
110
- if ( ! insideString . status && ! lexerFunctions . checkIfInsideComment ( insideComment ) ) {
111
- if ( currCol === '.' && nextCol === '.' && nextNextCol === '.' ) {
112
- if ( insideFunction . length && insideFunction [ insideFunction . length - 1 ] . insideParams === true ) {
113
- lexerFunctions . checkFor ( 'FUNCTION_DECLARATION' , '...' , tokens ) ;
114
- advanceAndClear ( 3 ) ;
115
- continue ;
116
- } else {
117
- lexerFunctions . checkFor ( 'RANGES' , '...' , tokens ) ;
118
- advanceAndClear ( 3 ) ;
119
- continue ;
120
- }
121
- }
122
- if ( currCol === '.' && nextCol === '.' && nextNextCol === '<' ) {
123
- lexerFunctions . checkFor ( 'RANGES' , '..<' , tokens ) ;
124
- advanceAndClear ( 3 ) ;
125
- continue ;
126
- }
104
+ // handles ranges
105
+ if ( lexerFunctions . handleRange ( insideString , insideFunction , insideComment ,
106
+ tokens , currCol , nextCol , nextNextCol ) ) {
107
+ advanceAndClear ( 3 ) ;
108
+ continue ;
127
109
}
128
110
129
- // string interpolation handling
111
+ // handles string interpolation
130
112
if ( lexerFunctions . checkForStringInterpolationStart ( stringInterpolation ,
131
113
insideString , chunk , tokens , nextCol , nextNextCol ) ) {
132
114
advanceAndClear ( 3 ) ;
@@ -196,15 +178,13 @@ module.exports = function(code) {
196
178
lexerFunctions . handleEndOfFile ( nextCol , tokens ) ;
197
179
continue ;
198
180
}
199
-
200
181
if ( insideInvocation . length && chunk === '(' && ( insideInvocation [ insideInvocation . length - 1 ] ) . status ) {
201
182
lexerFunctions . checkFor ( 'PUNCTUATION' , chunk , tokens ) ;
202
183
var last = insideInvocation [ insideInvocation . length - 1 ] ;
203
184
last . parens ++ ;
204
185
advanceAndClear ( 1 ) ;
205
186
continue ;
206
187
}
207
-
208
188
if ( insideInvocation . length && chunk === ')' && ( insideInvocation [ insideInvocation . length - 1 ] ) . status ) {
209
189
lexerFunctions . checkFor ( 'PUNCTUATION' , chunk , tokens ) ;
210
190
var last = insideInvocation [ insideInvocation . length - 1 ] ;
@@ -264,8 +244,8 @@ module.exports = function(code) {
264
244
continue ;
265
245
}
266
246
267
- if ( tokens . length >= 2 && tokens [ tokens . length - 2 ] [ ' type' ] === 'PUNCTUATION' &&
268
- tokens [ tokens . length - 2 ] [ ' value' ] === '(' && lastFunction && lastFunction . insideReturnStatement === true ) {
247
+ if ( tokens . length >= 2 && tokens [ tokens . length - 2 ] . type === 'PUNCTUATION' &&
248
+ tokens [ tokens . length - 2 ] . value === '(' && lastFunction && lastFunction . insideReturnStatement === true ) {
269
249
tokens [ tokens . length - 2 ] . type = 'PARAMS_START' ;
270
250
}
271
251
@@ -304,7 +284,7 @@ module.exports = function(code) {
304
284
continue ;
305
285
}
306
286
307
- // collection initializer syntax handling
287
+ // collection initializer handling
308
288
if ( tokens . length && currCol === '(' &&
309
289
( lastToken . type === 'ARRAY_END' || lastToken . type === 'DICTIONARY_END' ) ) {
310
290
lexerFunctions . checkFor ( 'FUNCTION_INVOCATION' , currCol , tokens ) ;
@@ -317,63 +297,17 @@ module.exports = function(code) {
317
297
continue ;
318
298
}
319
299
320
- /////////////////////////////////////////////
321
- // //
322
- // classes and structures handling //
323
- // //
324
- ///////////////////////////////////////////////////////////////////////////
325
-
326
- // handles inheritance operators
300
+ // handles colons functioning as inheritance operators
327
301
if ( tokens . length > 2 && tokens [ tokens . length - 2 ] . value === ':' &&
328
302
CLASS_NAMES [ lastToken . value ] && CLASS_NAMES [ tokens [ tokens . length - 3 ] . value ] ) {
329
303
tokens [ tokens . length - 2 ] . type = 'INHERITANCE_OPERATOR' ;
330
304
}
331
- if ( insideClass . length && insideClass [ insideClass . length - 1 ] . curly === 0 &&
332
- chunk === '{' ) {
333
- lexerFunctions . checkFor ( 'CLASS_DEFINITION' , chunk , tokens ) ;
334
- insideClass [ insideClass . length - 1 ] . curly ++ ;
335
- advanceAndClear ( 1 ) ;
336
- continue ;
337
- }
338
- if ( insideClass . length && insideClass [ insideClass . length - 1 ] . curly === 1 &&
339
- chunk === '}' ) {
340
- lexerFunctions . checkFor ( 'CLASS_DEFINITION' , chunk , tokens ) ;
341
- insideClass . pop ( ) ;
342
- advanceAndClear ( 1 ) ;
343
- lexerFunctions . handleEndOfFile ( nextCol , tokens ) ;
344
- continue ;
345
- }
346
- if ( insideStruct . length && insideStruct [ insideStruct . length - 1 ] . curly === 0 &&
347
- chunk === '{' ) {
348
- lexerFunctions . checkFor ( 'STRUCT_DEFINITION' , chunk , tokens ) ;
349
- insideStruct [ insideStruct . length - 1 ] . curly ++ ;
350
- advanceAndClear ( 1 ) ;
351
- continue ;
352
- }
353
- if ( insideStruct . length && insideStruct [ insideStruct . length - 1 ] . curly === 1 &&
354
- chunk === '}' ) {
355
- lexerFunctions . checkFor ( 'STRUCT_DEFINITION' , chunk , tokens ) ;
356
- insideStruct . pop ( ) ;
357
- advanceAndClear ( 1 ) ;
358
- lexerFunctions . handleEndOfFile ( nextCol , tokens ) ;
359
- continue ;
360
- }
361
- if ( tokens . length && ( CLASS_NAMES [ lastToken . value ] ||
362
- STRUCT_NAMES [ lastToken . value ] ) && chunk === '(' ) {
363
- lexerFunctions . checkFor ( 'INITIALIZATION' , chunk , tokens )
364
- var temp = { } ;
365
- temp . status = true ;
366
- temp . parens = 1 ;
367
- insideInitialization . push ( temp ) ;
368
- advanceAndClear ( 1 ) ;
369
- continue ;
370
- }
371
- if ( chunk === ')' && insideInitialization . length &&
372
- insideInitialization [ insideInitialization . length - 1 ] . parens === 1 ) {
373
- lexerFunctions . checkFor ( 'INITIALIZATION' , chunk , tokens ) ;
374
- insideInitialization . pop ( ) ;
305
+
306
+ // handles classes and structs
307
+ if ( lexerFunctions . handleClassOrStruct ( insideClass , insideStruct ,
308
+ insideInitialization , chunk , tokens , lastToken ,
309
+ nextCol , CLASS_NAMES , STRUCT_NAMES ) ) {
375
310
advanceAndClear ( 1 ) ;
376
- lexerFunctions . handleEndOfFile ( nextCol , tokens ) ;
377
311
continue ;
378
312
}
379
313
@@ -396,8 +330,6 @@ module.exports = function(code) {
396
330
continue ;
397
331
}
398
332
399
- ///////////////////////////////////////////////////////////////////////////
400
-
401
333
// main evaluation block
402
334
if ( ! insideString . status && ! insideNumber . status &&
403
335
lexerFunctions . checkForEvaluationPoint ( currCol , nextCol ) ) {
@@ -448,4 +380,4 @@ module.exports = function(code) {
448
380
// console.log(tokens);
449
381
return tokens ;
450
382
451
- } ;
383
+ } ;
0 commit comments