Skip to content

Commit

Permalink
handle correctly break and continue statement
Browse files Browse the repository at this point in the history
  • Loading branch information
fdecampredon committed Jan 8, 2015
1 parent 9a45bb1 commit fcbb623
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/keywords.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
var KeyWords = {
AS: "as",
BREAK: "break",
CASE: "case",
CATCH: "catch",
CLASS: "class",
CONST: "const",
CONTINUE: "continue",
DEFAULT: "default",
DELETE: "delete",
DO: "do",
Expand Down
2 changes: 2 additions & 0 deletions lib/nodeKind.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var NodeKind = {
B_OR: "b-or",
B_XOR: "b-xor",
BLOCK: "block",
BREAK: "break",
CALL: "call",
CASE: "case",
CASES: "cases",
Expand All @@ -24,6 +25,7 @@ var NodeKind = {
CONST: "const",
CONST_LIST: "const-list",
CONTENT: "content",
CONTINUE: "continue",
DEFAULT: "default",
DELETE: "delete",
DO: "do",
Expand Down
39 changes: 39 additions & 0 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ var AS3Parser = (function () {
else if (this.tokIs(KeyWords.THROW)) {
result = this.parseThrowStatement();
}
else if (this.tokIs(KeyWords.BREAK) || this.tokIs(KeyWords.CONTINUE)) {
result = this.parseBreakOrContinueStatement();
}
else if (this.tokIs(Operators.SEMI_COLUMN)) {
result = this.parseEmptyStatement();
}
Expand Down Expand Up @@ -1486,6 +1489,42 @@ var AS3Parser = (function () {
return new Node(NodeKind.RETURN, tok.index, expr.end, null, [expr]);
;
};
AS3Parser.prototype.parseBreakOrContinueStatement = function () {
var _this = this;
var tok = this.tok;
var kind;
if (this.tokIs(KeyWords.BREAK) || this.tokIs(KeyWords.CONTINUE)) {
kind = this.tokIs(KeyWords.BREAK) ? NodeKind.BREAK : NodeKind.CONTINUE;
this.nextToken();
}
else {
var pos = getLineAndCharacterFromPosition(this.tok.index, this.lineMap);
throw new Error('unexpected token : ' + this.tok.text + '(' + pos.line + ',' + pos.col + ')' + ' in file ' + this.fileName + 'expected: continue or break');
}
var result;
if (this.tokIs(NEW_LINE) || this.tokIs(Operators.SEMI_COLUMN)) {
this.nextToken(true);
result = new Node(kind, tok.index, tok.end, "");
}
else {
var ident = this.tryParse(function () {
var expr = _this.parsePrimaryExpression();
if (expr.kind === NodeKind.IDENTIFIER) {
return expr;
}
else {
throw new Error();
}
});
if (!ident) {
var pos = getLineAndCharacterFromPosition(this.tok.index, this.lineMap);
throw new Error('unexpected token : ' + this.tok.text + '(' + pos.line + ',' + pos.col + ')' + ' in file ' + this.fileName + 'expected: ident');
}
result = new Node(kind, tok.index, ident.end, null, [ident]);
}
this.skip(Operators.SEMI_COLUMN);
return result;
};
AS3Parser.prototype.parseShiftExpression = function () {
var result = new Node(NodeKind.SHIFT, this.tok.index, -1, null, [this.parseAdditiveExpression()]);
while (this.tokIs(Operators.DOUBLE_SHIFT_LEFT) || this.tokIs(Operators.TRIPLE_SHIFT_LEFT) || this.tokIs(Operators.DOUBLE_SHIFT_RIGHT) || this.tokIs(Operators.TRIPLE_SHIFT_RIGHT)) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

var KeyWords = {
AS : "as",
BREAK: "break",
CASE : "case",
CATCH : "catch",
CLASS : "class",
CONST : "const",
CONTINUE: "continue",
DEFAULT : "default",
DELETE : "delete",
DO : "do",
Expand Down
2 changes: 2 additions & 0 deletions src/main/nodeKind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var NodeKind = {
B_OR: "b-or",
B_XOR: "b-xor",
BLOCK: "block",
BREAK: "break",
CALL: "call",
CASE: "case",
CASES: "cases",
Expand All @@ -26,6 +27,7 @@ var NodeKind = {
CONST: "const",
CONST_LIST: "const-list",
CONTENT: "content",
CONTINUE: "continue",
DEFAULT: "default",
DELETE: "delete",
DO: "do",
Expand Down
44 changes: 44 additions & 0 deletions src/main/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,9 @@ class AS3Parser {
else if (this.tokIs(KeyWords.THROW)) {
result = this.parseThrowStatement();
}
else if (this.tokIs(KeyWords.BREAK) || this.tokIs(KeyWords.CONTINUE)) {
result = this.parseBreakOrContinueStatement();
}
else if (this.tokIs(Operators.SEMI_COLUMN)) {
result = this.parseEmptyStatement();
}
Expand Down Expand Up @@ -1671,6 +1674,47 @@ class AS3Parser {

return new Node(NodeKind.RETURN, tok.index, expr.end, null, [expr]);;
}

private parseBreakOrContinueStatement(): Node {
var tok: Token = this.tok;
var kind: string;
if (this.tokIs(KeyWords.BREAK) || this.tokIs(KeyWords.CONTINUE)) {
kind = this.tokIs(KeyWords.BREAK)? NodeKind.BREAK : NodeKind.CONTINUE;
this.nextToken();
} else {
var pos = getLineAndCharacterFromPosition(this.tok.index, this.lineMap);
throw new Error('unexpected token : ' +
this.tok.text + '(' + pos.line + ',' + pos.col + ')' +
' in file ' + this.fileName +
'expected: continue or break'
);
}
var result: Node;
if (this.tokIs(NEW_LINE) || this.tokIs(Operators.SEMI_COLUMN)) {
this.nextToken(true);
result = new Node(kind, tok.index, tok.end, "");
} else {
var ident = this.tryParse(() => {
var expr = this.parsePrimaryExpression();
if (expr.kind === NodeKind.IDENTIFIER) {
return expr;
} else {
throw new Error();
}
})
if (!ident) {
var pos = getLineAndCharacterFromPosition(this.tok.index, this.lineMap);
throw new Error('unexpected token : ' +
this.tok.text + '(' + pos.line + ',' + pos.col + ')' +
' in file ' + this.fileName +
'expected: ident'
);
}
result = new Node(kind, tok.index, ident.end, null, [ident]);
}
this.skip(Operators.SEMI_COLUMN);
return result;
}

private parseShiftExpression(): Node {
var result: Node = new Node(NodeKind.SHIFT, this.tok.index, -1, null, [this.parseAdditiveExpression()]);
Expand Down

0 comments on commit fcbb623

Please sign in to comment.