8000 refactor how blocks are stored · repos-javascript-compilers/JSX@c62f500 · GitHub
[go: up one dir, main page]

Skip to content

Commit c62f500

Browse files
committed
refactor how blocks are stored
1 parent ca0663d commit c62f500

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

lib/Parser.js

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var Parser = module.exports = Class.extend({
5656
this._classDefs = [];
5757
// use for function parsing
5858
this._locals = [];
59-
this._statements = null;
59+
this._statements = [];
6060
},
6161

6262
parse: function () {
@@ -331,38 +331,18 @@ var Parser = module.exports = Class.extend({
331331
return null;
332332
// take care of abstract function
333333
if ((flags & ClassDefinition.IS_ABSTRACT) != 0) {
334-
if (this._expectkeyword(";", " for abstract function definition") == null)
334+
if (this._expectKeyword(";", " for abstract function definition") == null)
335335
return null;
336336
return new MemberFunctionDefinition(name, flags, returnType, args, null);
337337
}
338338
// body
339+
if (this._expectKeyword("{") == null)
340+
return null;
339341
this._locals = [];
340-
var statements = this._parseBlock(false);
342+
this._statements = [];
343+
this._block();
341344
// 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);
366346
},
367347

368348
_typeDeclaration: function () {
@@ -373,6 +353,12 @@ var Parser = module.exports = Class.extend({
373353
return new TypeDeclaration(type);
374354
},
375355

356+
_block: function () {
357+
while (this._expectKeywordOpt("}") == null)
358+
if (! this._statement())
359+
this._skipStatement();
360+
},
361+
376362
_statement: function () {
377363
if (this._expectKeywordOpt("{") != null)
378364
return this._block();
@@ -405,8 +391,9 @@ var Parser = module.exports = Class.extend({
405391
// labelled or expression statement
406392
var identifier = this._expectIdentifierOpt();
407393
if (identifier != null && this._expectKeywordOpt(":") != null) {
394+
// label is treated as a separate statement (FIXME should label be an attribute of a statement?)
408395
this._statements.push(new LabelStatement(identifier));
409-
return this._statement();
396+
return true;
410397
}
411398
this._ungetToken();
412399
// expression statement
@@ -433,10 +420,16 @@ var Parser = module.exports = Class.extend({
433420
return false;
434421
if (this._expectKeyword(")") == null)
435422
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);
437427
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+
}
440433
this._statements.push(new IfStatement(expr, onTrueStatements, onFalseStatements));
441434
return true;
442435
},

0 commit comments

Comments
 (0)
0