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;
  final property = new PropertyNode();
  _PropertyState 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));
            final key = new ValueNode(value, token.value);
            if (settings.loc != null) {
              key.loc = token.loc;
            }
            startToken = token;
            property.key = key;
            state = _PropertyState.KEY;
            index++;
          } else {
            return null;
          }
          break;
        }

      case _PropertyState.KEY:
        {
          if (token.type == TokenType.COLON) {
            state = _PropertyState.COLON;
            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 _PropertyState.COLON:
        {
          final value = _parseValue(input, tokenList, index, settings);
          property.value = value.value;
          if (settings.loc != null) {
            final src = settings.source ?? "";
            property.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,
                src);
          }
          return new ValueIndex(property, value.index);
        }
    }
  }
  return null;
}