function _readNumber()

in packages/angular_devkit/core/src/json/parser.ts [229:315]


function _readNumber(context: JsonParserContext, comments = _readBlanks(context)): JsonAstNumber {
  let str = '';
  let dotted = false;
  const start = context.position;

  // read until `e` or end of line.
  while (true) {
    const char = _token(context);

    // Read tokens, one by one.
    if (char == '-') {
      if (str != '') {
        throw new InvalidJsonCharacterException(context);
      }
    } else if (
      char == 'I' &&
      (str == '-' || str == '' || str == '+') &&
      (context.mode & JsonParseMode.NumberConstantsAllowed) != 0
    ) {
      // Infinity?
      // _token(context, 'I'); Already read.
      _token(context, 'n');
      _token(context, 'f');
      _token(context, 'i');
      _token(context, 'n');
      _token(context, 'i');
      _token(context, 't');
      _token(context, 'y');

      str += 'Infinity';
      break;
    } else if (char == '0') {
      if (str == '0' || str == '-0') {
        throw new InvalidJsonCharacterException(context);
      }
    } else if (
      char == '1' ||
      char == '2' ||
      char == '3' ||
      char == '4' ||
      char == '5' ||
      char == '6' ||
      char == '7' ||
      char == '8' ||
      char == '9'
    ) {
      if (str == '0' || str == '-0') {
        throw new InvalidJsonCharacterException(context);
      }
    } else if (char == '+' && str == '') {
      // Pass over.
    } else if (char == '.') {
      if (dotted) {
        throw new InvalidJsonCharacterException(context);
      }
      dotted = true;
    } else if (char == 'e' || char == 'E') {
      return _readExpNumber(context, start, str + char, comments);
    } else if (
      char == 'x' &&
      (str == '0' || str == '-0') &&
      (context.mode & JsonParseMode.HexadecimalNumberAllowed) != 0
    ) {
      return _readHexaNumber(context, str == '-0', start, comments);
    } else {
      // We read one too many characters, so rollback the last character.
      context.position = context.previous;
      break;
    }

    str += char;
  }

  // We're done reading this number.
  if (str.endsWith('.') && (context.mode & JsonParseMode.HexadecimalNumberAllowed) == 0) {
    throw new InvalidJsonCharacterException(context);
  }

  return {
    kind: 'number',
    start,
    end: context.position,
    text: context.original.substring(start.offset, context.position.offset),
    value: Number.parseFloat(str),
    comments,
  };
}