cleaned up usage of AST_BlockStatement

The following nodes were instances of AST_BlockStatement: AST_Scope,
AST_SwitchBlock, AST_SwitchBranch.  Also, AST_Try, AST_Catch, AST_Finally
were having a body instanceof AST_BlockStatement.

Overloading the meaning of AST_BlockStatement this way turned out to be a
mess; we now have an AST_Block class that is the base class for things
having a block of statements (might or might not be bracketed).  The
`this.body` of AST_Scope, AST_Try, AST_Catch, AST_Finally is now an array of
statements (as they inherit from AST_Block).

Avoiding calling superclass's _walk function in walkers (turns out we walked
a node multiple times).
This commit is contained in:
Mihai Bazon
2012-09-05 11:31:02 +03:00
parent 1b5183dd5e
commit 8633b0073f
6 changed files with 61 additions and 60 deletions

View File

@@ -790,7 +790,11 @@ function parse($TEXT, exigent_mode) {
case "punc":
switch (S.token.value) {
case "{":
return block_(false);
return new AST_BlockStatement({
start : S.token,
body : block_(),
end : prev()
});
case "[":
case "(":
return simple_statement();
@@ -982,7 +986,7 @@ function parse($TEXT, exigent_mode) {
S.in_directives = true;
S.in_loop = 0;
S.labels = [];
var a = block_(true);
var a = block_();
--S.in_function;
S.in_loop = loop;
S.labels = labels;
@@ -1004,22 +1008,15 @@ function parse($TEXT, exigent_mode) {
});
};
function block_(required) {
var start = S.token;
function block_() {
expect("{");
var a = [];
while (!is("punc", "}")) {
if (is("eof")) unexpected();
a.push(statement());
}
var end = S.token;
next();
return new AST_BlockStatement({
required : required,
start : start,
body : a,
end : end
});
return a;
};
var switch_block_ = embed_tokens(curry(in_loop, function(){
@@ -1056,7 +1053,6 @@ function parse($TEXT, exigent_mode) {
var end = S.token;
next();
return new AST_SwitchBlock({
required : true,
start : start,
body : a,
end : end
@@ -1064,7 +1060,7 @@ function parse($TEXT, exigent_mode) {
}));
function try_() {
var body = block_(true), bcatch = null, bfinally = null;
var body = block_(), bcatch = null, bfinally = null;
if (is("keyword", "catch")) {
var start = S.token;
next();
@@ -1074,7 +1070,7 @@ function parse($TEXT, exigent_mode) {
bcatch = new AST_Catch({
start : start,
argname : name,
body : block_(true),
body : block_(),
end : prev()
});
}
@@ -1083,14 +1079,14 @@ function parse($TEXT, exigent_mode) {
next();
bfinally = new AST_Finally({
start : start,
body : block_(true),
body : block_(),
end : prev()
});
}
if (!bcatch && !bfinally)
croak("Missing catch/finally blocks");
return new AST_Try({
btry : body,
body : body,
bcatch : bcatch,
bfinally : bfinally
});