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: leftBracket (value (comma value)*)? rightBracket
  late Token startToken;
  var array = ArrayNode();
  var state = _ArrayState.start;
  Token token;
  while (index < tokenList.length) {
    token = tokenList[index];
    switch (state) {
      case _ArrayState.start:
        {
          if (token.type == TokenType.leftBracket) {
            startToken = token;
            state = _ArrayState.openArray;
            index++;
          } else {
            return null;
          }
          break;
        }

      case _ArrayState.openArray:
        {
          if (token.type == TokenType.rightBracket) {
            if (settings.loc) {
              array = array.copyWith(
                  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,
                      settings.source));
            }
            return ValueIndex(array, index + 1);
          } else {
            final value = _parseValue<Node>(input, tokenList, index, settings);
            index = value.index;
            array.children.add(value.value);
            state = _ArrayState.value;
          }
          break;
        }

      case _ArrayState.value:
        {
          if (token.type == TokenType.rightBracket) {
            if (settings.loc) {
              array = array.copyWith(
                  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,
                      settings.source));
            }
            return ValueIndex(array, index + 1);
          } else if (token.type == TokenType.comma) {
            state = _ArrayState.comma;
            index++;
          } else {
            final msg = unexpectedToken(
                substring(
                    input, token.loc!.start.offset!, token.loc!.end.offset),
                settings.source,
                token.loc!.start.line,
                token.loc!.start.column);
            throw JSONASTException(msg, input, settings.source,
                token.loc!.start.line, token.loc!.start.column);
          }
          break;
        }

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