src/Parser.cpp
changeset 165 b67697fba413
parent 153 fc91c4a3dfe2
child 167 cf82769eb136
     1.1 --- a/src/Parser.cpp	Sun Aug 05 12:55:51 2012 -0700
     1.2 +++ b/src/Parser.cpp	Sun Aug 05 14:19:05 2012 -0700
     1.3 @@ -641,7 +641,7 @@
     1.4          if (match(TOK_NEW)) {
     1.5              if (!enterScope(Scope::BLOCK))
     1.6                  return NULL;
     1.7 -            if ((decl = variableDeclaration(TOK_NEW)) == NULL)
     1.8 +            if ((decl = vars(TOK_NEW)) == NULL)
     1.9                  return NULL;
    1.10          } else {
    1.11              if ((decl = expressionStatement()) == NULL)
    1.12 @@ -682,7 +682,7 @@
    1.13      scope_ = scope_->enclosing();
    1.14  
    1.15      Scope *declScope = NULL;
    1.16 -    if (decl && decl->isVariableDeclaration()) {
    1.17 +    if (decl && (decl->isVariableDeclaration() || decl->isBlockStatement())) {
    1.18          declScope = scope_->unlinkIfEmpty();
    1.19          scope_ = scope_->enclosing();
    1.20      }
    1.21 @@ -709,7 +709,7 @@
    1.22  }
    1.23  
    1.24  VariableDeclaration *
    1.25 -Parser::variableDeclaration(TokenKind kind)
    1.26 +Parser::var(TokenKind kind)
    1.27  {
    1.28      Local<String> tag(zone_);
    1.29      if (matchName(tag.ref(), TOK_LABEL) && !tag)
    1.30 @@ -748,6 +748,32 @@
    1.31  }
    1.32  
    1.33  Statement *
    1.34 +Parser::vars(TokenKind kind)
    1.35 +{
    1.36 +    VariableDeclaration *decl = var(kind);
    1.37 +    if (!decl)
    1.38 +        return NULL;
    1.39 +
    1.40 +    if (!peek(TOK_COMMA))
    1.41 +        return decl;
    1.42 +
    1.43 +    StatementList *statements = new (pool_) StatementList();
    1.44 +    if (!statements->append(decl))
    1.45 +        return NULL;
    1.46 +
    1.47 +    while (match(TOK_COMMA)) {
    1.48 +        Statement *decl = var(kind);
    1.49 +        if (!decl)
    1.50 +            return NULL;
    1.51 +        if (!statements->append(decl))
    1.52 +            return NULL;
    1.53 +    }
    1.54 +
    1.55 +    // Create a fake-o block with no scope to create a statement list.
    1.56 +    return new (pool_) BlockStatement(decl->pos(), NULL, statements);
    1.57 +}
    1.58 +
    1.59 +Statement *
    1.60  Parser::returnStatement()
    1.61  {
    1.62      SourcePosition pos = scanner_.begin();
    1.63 @@ -873,7 +899,7 @@
    1.64        case TOK_CONST:
    1.65        case TOK_NEW:
    1.66          scanner_.scan();
    1.67 -        if ((statement = variableDeclaration(kind)) == NULL)
    1.68 +        if ((statement = vars(kind)) == NULL)
    1.69              return NULL;
    1.70          break;
    1.71  
    1.72 @@ -1270,8 +1296,11 @@
    1.73            case TOK_CONST:
    1.74            case TOK_NEW:
    1.75            {
    1.76 +            // Note: at global scope, we don't accept comma-delimited
    1.77 +            // declarations yet, to see if we can get away with forcing
    1.78 +            // a nicer top-level style.
    1.79              scanner_.scan();
    1.80 -            if ((statement = variableDeclaration(kind)) == NULL)
    1.81 +            if ((statement = var(kind)) == NULL)
    1.82                 return NULL;
    1.83              if (!matchSemicolon())
    1.84                  return NULL;