parseFor method

Statement parseFor()

Implementation

Statement parseFor() {
  int? start = token!.startOffset;
  int? line = token!.line;
  assert(token!.text == 'for');
  consume(Token.NAME);
  consume(Token.LPAREN);
  Node? exp1;
  if (peekName('var')) {
    exp1 = parseVariableDeclarationList(allowIn: false);
  } else if (token!.type != Token.SEMICOLON) {
    exp1 = parseExpression(allowIn: false);
  }
  if (exp1 != null && tryName('in')) {
    if (exp1 is VariableDeclaration && exp1.declarations.length > 1) {
      fail(message: 'Multiple vars declared in for-in loop');
    }
    Expression exp2 = parseExpression();
    consume(Token.RPAREN);
    Statement body = parseStatement();
    return ForInStatement(exp1, exp2, body)
      ..start = start
      ..end = endOffset
      ..line = line;
  } else {
    consume(Token.SEMICOLON);
    Expression? exp2, exp3;
    if (token!.type != Token.SEMICOLON) {
      exp2 = parseExpression();
    }
    consume(Token.SEMICOLON);
    if (token!.type != Token.RPAREN) {
      exp3 = parseExpression();
    }
    consume(Token.RPAREN);
    Statement body = parseStatement();
    return ForStatement(exp1, exp2, exp3, body)
      ..start = start
      ..end = endOffset
      ..line = line;
  }
}