arrayOrMap method

Expr arrayOrMap()

Implementation

Expr arrayOrMap() {
  if (match([TokenType.LEFT_BRACKET])) {
    List<Expr> elements = [];
    if (!check(TokenType.RIGHT_BRACKET)) {
      do {
        if (check(TokenType.RIGHT_BRACKET)) break;
        elements.add(expression());
      } while (match([TokenType.COMMA]));
    }
    consume(TokenType.RIGHT_BRACKET, "Expect ']' after array elements.");
    return Array(elements);
  } else if (match([TokenType.LEFT_BRACE])) {
    Map<Expr, Expr> entries = {};
    if (!check(TokenType.RIGHT_BRACE)) {
      do {
        if (check(TokenType.RIGHT_BRACE)) break;
        Expr key = term();
        consume(TokenType.COLON, "Expect ':' after dictionary key.");
        entries[key] = expression();
      } while (match([TokenType.COMMA]));
    }
    consume(TokenType.RIGHT_BRACE, "Expect '}' after array elements.");
    return Dict(entries);
  }
  return primary();
}