Speed up `updateLocationDataIfMissing`.

* Avoid excessive search for missing `locationData`
  * Fix `locationData` for `ELSE IF`.
This commit is contained in:
Marc Häfner 2013-06-21 02:47:29 +02:00
parent 7250fdd576
commit 25c6001a6c
5 changed files with 13 additions and 7 deletions

View File

@ -514,9 +514,9 @@
type: $1
});
}), o('IfBlock ELSE IF Expression Block', function() {
return $1.addElse(new If($4, $5, {
return $1.addElse(LOC(3, 5)(new If($4, $5, {
type: $3
}));
})));
})
],
If: [

View File

@ -229,7 +229,10 @@
Base.prototype.assigns = NO;
Base.prototype.updateLocationDataIfMissing = function(locationData) {
this.locationData || (this.locationData = locationData);
if (this.locationData) {
return this;
}
this.locationData = locationData;
return this.eachChild(function(child) {
return child.updateLocationDataIfMissing(locationData);
});
@ -2852,6 +2855,7 @@
} else {
this.isChain = elseBody instanceof If;
this.elseBody = this.ensureBlock(elseBody);
this.elseBody.updateLocationDataIfMissing(elseBody.locationData);
}
return this;
};

View File

@ -486,9 +486,9 @@ case 180:this.$ = yy.addLocationDataFn(_$[$0-2], _$[$0])(new yy.If($$[$0-1], $$[
type: $$[$0-2]
}));
break;
case 181:this.$ = yy.addLocationDataFn(_$[$0-4], _$[$0])($$[$0-4].addElse(new yy.If($$[$0-1], $$[$0], {
case 181:this.$ = yy.addLocationDataFn(_$[$0-4], _$[$0])($$[$0-4].addElse(yy.addLocationDataFn(_$[$0-2], _$[$0])(new yy.If($$[$0-1], $$[$0], {
type: $$[$0-2]
})));
}))));
break;
case 182:this.$ = $$[$0];
break;

View File

@ -511,7 +511,7 @@ grammar =
# ambiguity.
IfBlock: [
o 'IF Expression Block', -> new If $2, $3, type: $1
o 'IfBlock ELSE IF Expression Block', -> $1.addElse new If $4, $5, type: $3
o 'IfBlock ELSE IF Expression Block', -> $1.addElse LOC(3,5) new If $4, $5, type: $3
]
# The full complement of *if* expressions, including postfix one-liner

View File

@ -176,7 +176,8 @@ exports.Base = class Base
# For this node and all descendents, set the location data to `locationData`
# if the location data is not already set.
updateLocationDataIfMissing: (locationData) ->
@locationData or= locationData
return this if @locationData
@locationData = locationData
@eachChild (child) ->
child.updateLocationDataIfMissing locationData
@ -2006,6 +2007,7 @@ exports.If = class If extends Base
else
@isChain = elseBody instanceof If
@elseBody = @ensureBlock elseBody
@elseBody.updateLocationDataIfMissing elseBody.locationData
this
# The **If** only compiles into a statement if either of its bodies needs