Skip to content

Commit

Permalink
Fix try statement syntax node.
Browse files Browse the repository at this point in the history
There are some intricacies around the handlers, catch clause, guard, and
finalizer.

This solves 18 failures in the compatibility tests, bringing down the
total failures to only 158 (80.5% pass rate).

http://code.google.com/p/esprima/issues/detail?id=54.
  • Loading branch information
ariya committed Dec 1, 2011
1 parent f244772 commit 2cf2d31
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 65 deletions.
16 changes: 11 additions & 5 deletions esprima.js
Expand Up @@ -55,6 +55,7 @@ parseStatement: true */
BinaryExpression: 'BinaryExpression',
BreakStatement: 'BreakStatement',
CallExpression: 'CallExpression',
CatchClause: 'CatchClause',
ConditionalExpression: 'ConditionalExpression',
ContinueStatement: 'ContinueStatement',
DoWhileStatement: 'DoWhileStatement',
Expand Down Expand Up @@ -1789,7 +1790,7 @@ parseStatement: true */
// 12.14 The try statement

function parseTryStatement() {
var block, handler = null, guard, finalizer = null;
var block, handlers = [], param, finalizer = null;

expect(Token.Keyword, 'try');

Expand All @@ -1799,11 +1800,16 @@ parseStatement: true */
lex();
expect(Token.Punctuator, '(');
if (!match(')')) {
guard = parseExpression().expression;
param = parseExpression().expression;
}
expect(Token.Punctuator, ')');
handler = parseBlock();
handler.guard = guard;

handlers.push({
type: Syntax.CatchClause,
param: param,
guard: null,
body: parseBlock()
});
}

if (matchKeyword('finally')) {
Expand All @@ -1814,7 +1820,7 @@ parseStatement: true */
return {
type: Syntax.TryStatement,
block: block,
handler: handler,
handlers: handlers,
finalizer: finalizer
};
}
Expand Down
136 changes: 76 additions & 60 deletions test/test.js
Expand Up @@ -2583,14 +2583,18 @@ data = {
type: 'BlockStatement',
body: []
},
handler: {
type: 'BlockStatement',
body: [],
guard: {
handlers: [{
type: 'CatchClause',
param: {
type: 'Identifier',
name: 'e'
},
guard: null,
body: {
type: 'BlockStatement',
body: []
}
},
}],
finalizer: null
},

Expand All @@ -2600,27 +2604,31 @@ data = {
type: 'BlockStatement',
body: []
},
handler: {
type: 'BlockStatement',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'say'
},
'arguments': [{
type: 'Identifier',
name: 'e'
}]
}
}],
guard: {
handlers: [{
type: 'CatchClause',
param: {
type: 'Identifier',
name: 'e'
},
guard: null,
body: {
type: 'BlockStatement',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'say'
},
'arguments': [{
type: 'Identifier',
name: 'e'
}]
}
}]
}
},
}],
finalizer: null
},

Expand All @@ -2630,7 +2638,7 @@ data = {
type: 'BlockStatement',
body: []
},
handler: null,
handlers: [],
finalizer: {
type: 'BlockStatement',
body: [{
Expand Down Expand Up @@ -2666,27 +2674,31 @@ data = {
}
}]
},
handler: {
type: 'BlockStatement',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'say'
},
'arguments': [{
type: 'Identifier',
name: 'e'
}]
}
}],
guard: {
handlers: [{
type: 'CatchClause',
param: {
type: 'Identifier',
name: 'e'
},
guard: null,
body: {
type: 'BlockStatement',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'say'
},
'arguments': [{
type: 'Identifier',
name: 'e'
}]
}
}]
}
},
}],
finalizer: null
},

Expand All @@ -2706,27 +2718,31 @@ data = {
}
}]
},
handler: {
type: 'BlockStatement',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'say'
},
'arguments': [{
type: 'Identifier',
name: 'e'
}]
}
}],
guard: {
handlers: [{
type: 'CatchClause',
param: {
type: 'Identifier',
name: 'e'
},
guard: null,
body: {
type: 'BlockStatement',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'say'
},
'arguments': [{
type: 'Identifier',
name: 'e'
}]
}
}]
}
},
}],
finalizer: {
type: 'BlockStatement',
body: [{
Expand Down

0 comments on commit 2cf2d31

Please sign in to comment.