parseString function

Token? parseString(
  1. String input,
  2. int index,
  3. int line,
  4. int column,
)

Implementation

Token? parseString(String input, int index, int line, int column) {
  final startIndex = index;
  // final buffer = new StringBuffer();
  _StringState state = _StringState._START_;

  while (index < input.length) {
    final char = input[index];

    switch (state) {
      case _StringState._START_:
        {
          if (char == '"') {
            index++;
            state = _StringState.START_QUOTE_OR_CHAR;
          } else {
            return null;
          }
          break;
        }

      case _StringState.START_QUOTE_OR_CHAR:
        {
          if (char == '\\') {
            // buffer.write(char);
            index++;
            state = _StringState.ESCAPE;
          } else if (char == '"') {
            index++;
            return new Token(
                TokenType.STRING,
                line,
                column + index - startIndex,
                index,
                safeSubstring(input, startIndex, index));
          } else {
            // buffer.write(char);
            index++;
          }
          break;
        }

      case _StringState.ESCAPE:
        {
          if (escapes.containsKey(char)) {
            // buffer.write(char);
            index++;
            if (char == 'u') {
              for (int i = 0; i < 4; i++) {
                final curChar = input[index];
                if (curChar != '' && isHex(curChar)) {
                  // buffer.write(char);
                  index++;
                } else {
                  return null;
                }
              }
            }
            state = _StringState.START_QUOTE_OR_CHAR;
          } else {
            return null;
          }
          break;
        }
    }
  }
  return null;
}