@@ -14,6 +14,8 @@ function create_stream() {
14
14
// singleton!
15
15
var Advance = new Object
16
16
17
+ var DEBUG = false
18
+
17
19
var _ = 0
18
20
, STMT = _ ++
19
21
, STMTLIST = _ ++
@@ -45,9 +47,9 @@ var NO_ASSIGN_ALLOWED = false
45
47
46
48
// map of tokens to stmt types
47
49
var token_map = {
48
- 'block-comment' : COMMENT
49
- , 'line-comment' : COMMENT
50
- , 'preprocessor' : PREPROCESSOR
50
+ 'block-comment' : COMMENT
51
+ , 'line-comment' : COMMENT
52
+ , 'preprocessor' : PREPROCESSOR
51
53
}
52
54
53
55
// map of stmt types to human
@@ -137,15 +139,16 @@ function parser() {
137
139
// stream functions ---------------------------------------------
138
140
139
141
function write ( input ) {
140
- if ( input . type === 'whitespace' ) {
142
+ if ( input . type === 'whitespace' || input . type === 'line-comment' || input . type === 'block-comment' ) {
141
143
whitespace . push ( input )
142
144
return
143
145
}
144
146
tokens . push ( input )
145
147
token = token || tokens [ 0 ]
146
148
147
- if ( node ) {
148
- node . whitespace . concat ( whitespace . splice ( 0 , whitespace . length ) )
149
+ if ( token && whitespace . length ) {
150
+ token . preceding = token . preceding || [ ]
151
+ token . preceding . concat ( whitespace . splice ( 0 , whitespace . length ) )
149
152
}
150
153
151
154
while ( take ( ) ) switch ( state [ 0 ] . mode ) {
@@ -205,10 +208,22 @@ function parser() {
205
208
state . shift ( )
206
209
}
207
210
208
- function special_unshift ( _node ) {
211
+ function special_unshift ( _node , add_child ) {
212
+ _node . parent = state [ 0 ]
213
+
209
214
var ret = [ ] . unshift . call ( this , _node )
210
215
211
- node . children . push ( _node )
216
+ add_child = add_child === undefined ? true : add_child
217
+
218
+ if ( DEBUG ) {
219
+ var pad = ''
220
+ for ( var i = 0 , len = this . length - 1 ; i < len ; ++ i ) {
221
+ pad += ' |'
222
+ }
223
+ console . log ( pad , '\\' + _node . type )
224
+ }
225
+
226
+ if ( add_child ) node . children . push ( _node )
212
227
node = _node
213
228
214
229
return ret
@@ -219,6 +234,14 @@ function parser() {
219
234
, okay = check [ this . length ]
220
235
, emit = false
221
236
237
+ if ( DEBUG ) {
238
+ var pad = ''
239
+ for ( var i = 0 , len = this . length ; i < len ; ++ i ) {
240
+ pad += ' |'
241
+ }
242
+ console . log ( pad , '/' + _node . type )
243
+ }
244
+
222
245
if ( check . length ) {
223
246
if ( okay !== undefined ) {
224
247
emit = okay . test ? okay . test ( _node . type ) : okay === _node . type
@@ -227,7 +250,7 @@ function parser() {
227
250
emit = true
228
251
}
229
252
230
- if ( emit ) stream . emit ( 'data' , node )
253
+ if ( emit ) stream . emit ( 'data' , _node )
231
254
232
255
node = _node . parent
233
256
return _node
@@ -248,8 +271,6 @@ function parser() {
248
271
return state . scope . exit ( ) , state . shift ( )
249
272
}
250
273
switch ( token . type ) {
251
- case 'block-comment' :
252
- case 'line-comment' :
253
274
case 'preprocessor' :
254
275
state . fake ( adhoc ( ) )
255
276
tokens . shift ( )
@@ -329,10 +350,6 @@ function parser() {
329
350
state [ 0 ] . got = [ ]
330
351
}
331
352
332
- if ( token . type === 'block-comment' || token . type === 'line-comment' ) {
333
- return tokens . shift ( )
334
- }
335
-
336
353
state [ 0 ] . got . push ( tokens . shift ( ) )
337
354
338
355
if ( state [ 0 ] . need !== state [ 0 ] . got . length )
@@ -344,8 +361,20 @@ function parser() {
344
361
token = state [ 0 ] . got . shift ( ) , state . fake ( keyword ( ) )
345
362
} else {
346
363
state . fake ( keyword ( '<default>' ) )
347
- }
348
- token = state [ 0 ] . got . shift ( ) , state . fake ( keyword ( ) )
364
+ }
365
+ token = state [ 0 ] . got . shift ( )
366
+
367
+ if ( token . type === 'keyword' ) {
368
+ state . fake ( keyword ( ) )
369
+ } else {
370
+ var lookup = state . scope . find ( token . data )
371
+
372
+ if ( ! lookup ) return
373
+
374
+ lookup = Object . create ( lookup )
375
+ lookup . token = token
376
+ state . fake ( lookup )
377
+ }
349
378
350
379
while ( state [ 0 ] . got . length )
351
380
tokens . unshift ( token = state [ 0 ] . got . pop ( ) )
@@ -354,7 +383,19 @@ function parser() {
354
383
}
355
384
356
385
// it's a function
357
- token = state [ 0 ] . got . shift ( ) , state . fake ( keyword ( ) )
386
+ token = state [ 0 ] . got . shift ( )
387
+
388
+ if ( token . type === 'keyword' ) {
389
+ state . fake ( keyword ( ) )
390
+ } else {
391
+ var lookup = state . scope . find ( token . data )
392
+
393
+ if ( ! lookup ) return
394
+
395
+ lookup = Object . create ( lookup )
396
+ lookup . token = token
397
+ state . fake ( lookup )
398
+ }
358
399
// replace the last two tokens
359
400
tokens . unshift ( state [ 0 ] . got . pop ( ) )
360
401
tokens . unshift ( token = state [ 0 ] . got . pop ( ) )
@@ -364,10 +405,6 @@ function parser() {
364
405
365
406
function parse_decllist ( ) {
366
407
// grab ident
367
- if ( token . type === 'line-comment' || token . type === 'block-comment' ) {
368
- return tokens . shift ( )
369
- }
370
-
371
408
if ( token . type === 'ident' ) {
372
409
var name = token . data
373
410
state . unshift ( ident ( ) )
@@ -432,10 +469,6 @@ function parser() {
432
469
function parse_expr ( ) {
433
470
var expecting = state [ 0 ] . expecting
434
471
435
- if ( token . type === 'block-comment' || token . type === 'line-comment' )
436
- return tokens . shift ( )
437
-
438
-
439
472
state [ 0 ] . tokens = state [ 0 ] . tokens || [ ]
440
473
441
474
if ( state [ 0 ] . parenlevel === undefined ) {
@@ -520,7 +553,7 @@ function parser() {
520
553
function assert_null_string_or_array ( x , y ) {
521
554
switch ( typeof x ) {
522
555
case 'string' : if ( y !== x ) {
523
- unexpected ( 'expected `' + x + '`, got ' + y ) ;
556
+ unexpected ( 'expected `' + x + '`, got ' + y + '\n' + token . data ) ;
524
557
} return ! errored
525
558
526
559
case 'object' : if ( x && x . indexOf ( y ) === - 1 ) {
@@ -540,10 +573,6 @@ function parser() {
540
573
return function ( ) {
541
574
var current = state [ 0 ]
542
575
543
- if ( token . type === 'line-comment' || token . type === 'block-comment' ) {
544
- return tokens . shift ( )
545
- }
546
-
547
576
current . stage || ( current . stage = 0 )
548
577
549
578
step = steps [ current . stage ]
@@ -570,7 +599,12 @@ function parser() {
570
599
var name = token . data
571
600
return assert ( 'ident' ) && ( state . unshift ( ident ( ) ) , state . scope . define ( name ) , Advance )
572
601
} : function ( ) {
573
- return assert ( 'ident' ) && ( state . fake ( state . scope . find ( token . data ) ) , tokens . shift ( ) , Advance )
602
+ if ( ! assert ( 'ident' ) ) return
603
+
604
+ var s = Object . create ( state . scope . find ( token . data ) )
605
+ s . token = token
606
+
607
+ return ( tokens . shift ( ) , Advance )
574
608
}
575
609
}
576
610
@@ -744,7 +778,6 @@ function mknode(mode, sourcetoken, parent) {
744
778
, token : sourcetoken
745
779
, parent : parent
746
780
, children : [ ]
747
- , whitespace : [ ]
748
781
, type : stmt_type [ mode ]
749
782
, id : ( Math . random ( ) * 0xFFFFFFFF ) . toString ( 16 )
750
783
}
0 commit comments