parseNumber function

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

Implementation

Token? parseNumber(String input, int index, int line, int column) {
  final startIndex = index;
  int passedValueIndex = index;
  _NumberState state = _NumberState._START_;

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

    switch (state) {
      case _NumberState._START_:
        {
          if (char == '-') {
            state = _NumberState.MINUS;
          } else if (char == '0') {
            passedValueIndex = index + 1;
            state = _NumberState.ZERO;
          } else if (isDigit1to9(char)) {
            passedValueIndex = index + 1;
            state = _NumberState.DIGIT;
          } else {
            return null;
          }
          break;
        }

      case _NumberState.MINUS:
        {
          if (char == '0') {
            passedValueIndex = index + 1;
            state = _NumberState.ZERO;
          } else if (isDigit1to9(char)) {
            passedValueIndex = index + 1;
            state = _NumberState.DIGIT;
          } else {
            return null;
          }
          break;
        }

      case _NumberState.ZERO:
        {
          if (char == '.') {
            state = _NumberState.POINT;
          } else if (isExp(char)) {
            state = _NumberState.EXP;
          } else {
            break iterator;
          }
          break;
        }

      case _NumberState.DIGIT:
        {
          if (isDigit(char)) {
            passedValueIndex = index + 1;
          } else if (char == '.') {
            state = _NumberState.POINT;
          } else if (isExp(char)) {
            state = _NumberState.EXP;
          } else {
            break iterator;
          }
          break;
        }

      case _NumberState.POINT:
        {
          if (isDigit(char)) {
            passedValueIndex = index + 1;
            state = _NumberState.DIGIT_FRACTION;
          } else {
            break iterator;
          }
          break;
        }

      case _NumberState.DIGIT_FRACTION:
        {
          if (isDigit(char)) {
            passedValueIndex = index + 1;
          } else if (isExp(char)) {
            state = _NumberState.EXP;
          } else {
            break iterator;
          }
          break;
        }

      case _NumberState.EXP:
        {
          if (char == '+' || char == '-') {
            state = _NumberState.EXP_DIGIT_OR_SIGN;
          } else if (isDigit(char)) {
            passedValueIndex = index + 1;
            state = _NumberState.EXP_DIGIT_OR_SIGN;
          } else {
            break iterator;
          }
          break;
        }

      case _NumberState.EXP_DIGIT_OR_SIGN:
        {
          if (isDigit(char)) {
            passedValueIndex = index + 1;
          } else {
            break iterator;
          }
          break;
        }
    }

    index++;
  }

  if (passedValueIndex > 0) {
    return new Token(
        TokenType.NUMBER,
        line,
        column + passedValueIndex - startIndex,
        passedValueIndex,
        safeSubstring(input, startIndex, passedValueIndex));
  }

  return null;
}