parse method

Program parse()

Implementation

Program parse() {
  final imports = <ImportStatement>[];
  final body = <Statement>[];

  while (_isNotAtEnd) {
    try {
      if (body.isEmpty) {
        if (_match(TokenType.importKeyword)) {
          imports.add(_import());
          continue;
        }
      }

      _consume(
        TokenType.typeKeyword,
        ExpectAfterError(
          token: _peek,
          expectation: ExpectationType.oneOf(
            expectations: [
              if (body.isEmpty) ExpectationType.token(token: TokenType.importKeyword),
              ExpectationType.token(token: TokenType.typeKeyword),
            ],
          ),
          after: body.isEmpty //
              ? ExpectationType.token(token: TokenType.identifier)
              : ExpectationType.statement(statement: body[body.length - 1]),
        ),
      );

      body.add(_typeDefinition());
    } on ParseError {
      _synchronize();
    }
  }

  return Program(imports, body);
}