parseArray function

ValueIndex<ArrayNode>? parseArray(
  1. String input,
  2. List<Token> tokenList,
  3. int index,
  4. Settings settings,
)

Implementation

ValueIndex<ArrayNode>? parseArray(
    String input, List<Token> tokenList, int index, Settings settings) {
  // array: LEFT_BRACKET (value (COMMA value)*)? RIGHT_BRACKET
  late Token startToken;
  final array = new ArrayNode();
  var state = _ArrayState._START_;
  Token token;
  while (index < tokenList.length) {
    token = tokenList[index];
    switch (state) {
      case _ArrayState._START_:
        {
          if (token.type == TokenType.LEFT_BRACKET) {
            startToken = token;
            state = _ArrayState.OPEN_ARRAY;
            index++;
          } else {
            return null;
          }
          break;
        }

      case _ArrayState.OPEN_ARRAY:
        {
          if (token.type == TokenType.RIGHT_BRACKET) {
            if (settings.loc != null) {
              final src = settings.source ?? "";
              array.loc = Location.create(
                  startToken.loc!.start.line,
                  startToken.loc!.start.column,
                  startToken.loc!.start.offset,
                  token.loc!.end.line,
                  token.loc!.end.column,
                  token.loc!.end.offset,
                  src);
            }
            return new ValueIndex(array, index + 1);
          } else {
            final value = _parseValue(input, tokenList, index, settings);
            index = value.index;
            array.children.add(value.value);
            state = _ArrayState.VALUE;
          }
          break;
        }

      case _ArrayState.VALUE:
        {
          if (token.type == TokenType.RIGHT_BRACKET) {
            if (settings.loc != null) {
              final src = settings.source ?? "";
              array.loc = Location.create(
                  startToken.loc!.start.line,
                  startToken.loc!.start.column,
                  startToken.loc!.start.offset,
                  token.loc!.end.line,
                  token.loc!.end.column,
                  token.loc!.end.offset,
                  src);
            }
            return new ValueIndex(array, index + 1);
          } else if (token.type == TokenType.COMMA) {
            state = _ArrayState.COMMA;
            index++;
          } else {
            final src = settings.source ?? "";
            final msg = unexpectedToken(
                substring(
                    input, token.loc!.start.offset, token.loc!.end.offset),
                src,
                token.loc!.start.line,
                token.loc!.start.column);
            throw new JSONASTException(msg, input, src, token.loc!.start.line,
                token.loc!.start.column);
          }
          break;
        }

      case _ArrayState.COMMA:
        {
          final value = _parseValue(input, tokenList, index, settings);
          index = value.index;
          array.children.add(value.value);
          state = _ArrayState.VALUE;
          break;
        }
    }
  }
  throw errorEof(input, tokenList, settings);
}