@@ -56,7 +56,7 @@ var Parser = module.exports = Class.extend({
56
56
this . _classDefs = [ ] ;
57
57
// use for function parsing
58
58
this . _locals = [ ] ;
59
- this . _statements = null ;
59
+ this . _statements = [ ] ;
60
60
} ,
61
61
62
62
parse : function ( ) {
@@ -331,38 +331,18 @@ var Parser = module.exports = Class.extend({
331
331
return null ;
332
332
// take care of abstract function
333
333
if ( ( flags & ClassDefinition . IS_ABSTRACT ) != 0 ) {
334
- if ( this . _expectkeyword ( ";" , " for abstract function definition" ) == null )
334
+ if ( this . _expectKeyword ( ";" , " for abstract function definition" ) == null )
335
335
return null ;
336
336
return new MemberFunctionDefinition ( name , flags , returnType , args , null ) ;
337
337
}
338
338
// body
339
+ if ( this . _expectKeyword ( "{" ) == null )
340
+ return null ;
339
341
this . _locals = [ ] ;
340
- var statements = this . _parseBlock ( false ) ;
342
+ this . _statements = [ ] ;
343
+ this . _block ( ) ;
341
344
// done
342
- return new MemberFunctionDefinition ( name , flags , returnType , args , this . _locals , statements ) ;
343
- } ,
344
-
345
- _parseBlock : function ( allowStatement ) {
346
- var isBlock ;
347
- if ( allowStatement && this . _expectKeywordOpt ( "{" ) == null )
348
- isBlock = false ;
349
- else {
350
- if ( this . _expectKeyword ( "{" ) == null )
351
- return null ;
352
- isBlock = true ;
353
- }
354
- var outerStatements = this . _statements ;
355
- var statements = this . _statements = [ ] ;
356
- if ( isBlock ) {
357
- while ( this . _expectKeywordOpt ( "}" ) == null )
358
- if ( ! this . _statement ( ) )
359
- this . _skipStatement ( ) ;
360
- } else {
361
- if ( ! this . _statement ( ) )
362
- this . _skipStatement ( ) ;
363
- }
364
- this . _statements = outerStatements ;
365
- return statements ;
345
+ return new MemberFunctionDefinition ( name , flags , returnType , args , this . _locals , this . _statements ) ;
366
346
} ,
367
347
368
348
_typeDeclaration : function ( ) {
@@ -373,6 +353,12 @@ var Parser = module.exports = Class.extend({
373
353
return new TypeDeclaration ( type ) ;
374
354
} ,
375
355
356
+ _block : function ( ) {
357
+ while ( this . _expectKeywordOpt ( "}" ) == null )
358
+ if ( ! this . _statement ( ) )
359
+ this . _skipStatement ( ) ;
360
+ } ,
361
+
376
362
_statement : function ( ) {
377
363
if ( this . _expectKeywordOpt ( "{" ) != null )
378
364
return this . _block ( ) ;
@@ -405,8 +391,9 @@ var Parser = module.exports = Class.extend({
405
391
// labelled or expression statement
406
392
var identifier = this . _expectIdentifierOpt ( ) ;
407
393
if ( identifier != null && this . _expectKeywordOpt ( ":" ) != null ) {
394
+ // label is treated as a separate statement (FIXME should label be an attribute of a statement?)
408
395
this . _statements . push ( new LabelStatement ( identifier ) ) ;
409
- return this . _statement ( ) ;
396
+ return true ;
410
397
}
411
398
this . _ungetToken ( ) ;
412
399
// expression statement
@@ -433,10 +420,16 @@ var Parser = module.exports = Class.extend({
433
420
return false ;
434
421
if ( this . _expectKeyword ( ")" ) == null )
435
422
return false ;
436
- var onTrueStatements = this . _parseBlock ( true ) ;
423
+ var statementIndex = this . _statements . length ;
424
+ if ( ! this . _statement ( ) )
425
+ this . _skipStatement ( ) ;
426
+ var onTrueStatements = this . _statements . splice ( statementIndex ) ;
437
427
var onFalseStatements = null ;
438
- if ( this . _expectKeywordOpt ( "else" ) )
439
- onFalseStatements = this . _parseBlock ( true ) ;
428
+ if ( this . _expectKeywordOpt ( "else" ) ) {
429
+ if ( ! this . _statement ( ) )
430
+ this . _skipStatement ( ) ;
431
+ onFalseStatements = this . _statements . splice ( statementIndex ) ;
432
+ }
440
433
this . _statements . push ( new IfStatement ( expr , onTrueStatements , onFalseStatements ) ) ;
441
434
return true ;
442
435
} ,
0 commit comments