parseFor method

Statement parseFor()

Implementation

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