parseProperty function

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

Implementation

ValueIndex<PropertyNode>? parseProperty(
    String input, List<Token> tokenList, int index, Settings settings) {
  // property: STRING COLON value
  late Token startToken;
  var property = PropertyNode();
  var state = _PropertyState._START_;

  while (index < tokenList.length) {
    final token = tokenList[index];

    switch (state) {
      case _PropertyState._START_:
        {
          if (token.type == TokenType.STRING) {
            final value = parseString(safeSubstring(input,
                token.loc!.start.offset! + 1, token.loc!.end.offset! - 1));
            var key = ValueNode(value, token.value);
            if (settings.loc) {
              key = key.copyWith(loc: token.loc);
            }
            startToken = token;
            property = property.copyWith(key: key);
            state = _PropertyState.KEY;
            index++;
          } else {
            return null;
          }
          break;
        }

      case _PropertyState.KEY:
        {
          if (token.type == TokenType.COLON) {
            state = _PropertyState.COLON;
            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 _PropertyState.COLON:
        {
          final value = _parseValue<Node?>(input, tokenList, index, settings);
          property = property.copyWith(value: value.value);
          if (settings.loc) {
            property = property.copyWith(
                loc: Location.create(
                    startToken.loc?.start.line,
                    startToken.loc?.start.column,
                    startToken.loc?.start.offset,
                    value.value?.loc?.end.line,
                    value.value?.loc?.end.column,
                    value.value?.loc?.end.offset,
                    settings.source));
          }
          return ValueIndex(property, value.index);
        }
    }
  }
  return null;
}