Support TOK_CONST as a declarator in the parser.
Support TOK_CONST as a declarator in the parser.
1.1 --- a/src/AST.h Sun Jul 29 19:27:26 2012 -0700
1.2 +++ b/src/AST.h Sun Jul 29 19:37:47 2012 -0700
1.3 @@ -164,10 +164,12 @@
1.4 Expression *initialization_;
1.5 ExpressionList *postDimensions_;
1.6 ScopedRoot<String> tag_;
1.7 + TokenKind declarator_;
1.8
1.9 public:
1.10 - VariableDeclaration(const SourcePosition &pos, Variable *var, Handle<String> tag,
1.11 - ExpressionList *postDimensions, Expression *initialization)
1.12 + VariableDeclaration(const SourcePosition &pos, TokenKind declarator, Variable *var,
1.13 + Handle<String> tag, ExpressionList *postDimensions,
1.14 + Expression *initialization)
1.15 : Statement(pos),
1.16 variable_(var),
1.17 initialization_(initialization),
1.18 @@ -194,6 +196,9 @@
1.19 void accept(AstVisitor *visitor) {
1.20 visitor->visit(this);
1.21 }
1.22 + TokenKind declarator() const {
1.23 + return declarator_;
1.24 + }
1.25 };
1.26
1.27 class Parameter : public PoolObject
2.1 --- a/src/Parser.cpp Sun Jul 29 19:27:26 2012 -0700
2.2 +++ b/src/Parser.cpp Sun Jul 29 19:37:47 2012 -0700
2.3 @@ -583,10 +583,10 @@
2.4
2.5 Statement *decl = NULL;
2.6 if (!match(TOK_SEMICOLON)) {
2.7 - if (scanner_.peek() == TOK_NEW) {
2.8 + if (match(TOK_NEW)) {
2.9 if (!enterScope(Scope::BLOCK))
2.10 return NULL;
2.11 - if ((decl = variableDeclaration()) == NULL)
2.12 + if ((decl = variableDeclaration(TOK_NEW)) == NULL)
2.13 return NULL;
2.14 } else {
2.15 if ((decl = expressionStatement()) == NULL)
2.16 @@ -635,11 +635,8 @@
2.17 }
2.18
2.19 VariableDeclaration *
2.20 -Parser::variableDeclaration()
2.21 +Parser::variableDeclaration(TokenKind kind)
2.22 {
2.23 - if (!expect(TOK_NEW))
2.24 - return NULL;
2.25 -
2.26 Local<String> tag(zone_);
2.27 if (matchName(tag.ref(), TOK_LABEL) && !tag)
2.28 return NULL;
2.29 @@ -679,7 +676,7 @@
2.30 return NULL;
2.31 }
2.32
2.33 - return new (pool_) VariableDeclaration(var->pos(), var, tag, postDimensions, expr);
2.34 + return new (pool_) VariableDeclaration(var->pos(), kind, var, tag, postDimensions, expr);
2.35 }
2.36
2.37 Statement *
2.38 @@ -803,9 +800,12 @@
2.39 // Statements which must be followed by a semicolon will break out of the
2.40 // switch. If they may end in BlockStatements, they'll immediately return.
2.41
2.42 - switch (scanner_.peek()) {
2.43 + TokenKind kind = scanner_.peek();
2.44 + switch (kind) {
2.45 + case TOK_CONST:
2.46 case TOK_NEW:
2.47 - if ((statement = variableDeclaration()) == NULL)
2.48 + scanner_.scan();
2.49 + if ((statement = variableDeclaration(kind)) == NULL)
2.50 return NULL;
2.51 break;
2.52
2.53 @@ -1066,7 +1066,8 @@
2.54 for (;;) {
2.55 Statement *statement = NULL;
2.56
2.57 - switch (scanner_.peek()) {
2.58 + TokenKind kind = scanner_.peek();
2.59 + switch (kind) {
2.60 case TOK_ERROR:
2.61 return NULL;
2.62
2.63 @@ -1083,7 +1084,7 @@
2.64 case TOK_NATIVE:
2.65 case TOK_STOCK:
2.66 {
2.67 - TokenKind kind = scanner_.scan();
2.68 + scanner_.scan();
2.69 if ((statement = function(kind)) == NULL)
2.70 return NULL;
2.71 if ((kind == TOK_FORWARD || kind == TOK_NATIVE) && !matchSemicolon())
2.72 @@ -1091,9 +1092,11 @@
2.73 break;
2.74 }
2.75
2.76 + case TOK_CONST:
2.77 case TOK_NEW:
2.78 {
2.79 - if ((statement = variableDeclaration()) == NULL)
2.80 + scanner_.scan();
2.81 + if ((statement = variableDeclaration(kind)) == NULL)
2.82 return NULL;
2.83 if (!matchSemicolon())
2.84 return NULL;
3.1 --- a/src/Parser.h Sun Jul 29 19:27:26 2012 -0700
3.2 +++ b/src/Parser.h Sun Jul 29 19:37:47 2012 -0700
3.3 @@ -45,7 +45,7 @@
3.4
3.5 Scope *enterScope(Scope::Kind kind);
3.6
3.7 - VariableDeclaration *variableDeclaration();
3.8 + VariableDeclaration *variableDeclaration(TokenKind kind);
3.9 bool matchName(String **name, TokenKind kind = TOK_NAME);
3.10 String *expectName();
3.11 NameProxy *bind(Handle<String> name);