Automattic\WooCommerce\Vendor\GraphQL\Language

Lexer::readNumberprivateWC 1.0

Reads a number token from the source file, either a float or an int depending on whether a decimal point appears.

Int: -?(0|[1-9][0-9]) Float: -?(0|[1-9][0-9])(.[0-9]+)?((E|e)(+|-)?[0-9]+)?

Method of the class: Lexer{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->readNumber( $line, $col, $prev ): Token;
$line(int) (required)
.
$col(int) (required)
.
$prev(Token) (required)
.

Lexer::readNumber() code WC 11.0.1

private function readNumber(int $line, int $col, Token $prev): Token
{
    $value = '';
    $start = $this->position;
    [$char, $code] = $this->readChar();

    $isFloat = false;

    if ($code === 45) { // -
        $value .= $char;
        [$char, $code] = $this->moveStringCursor(1, 1)->readChar();
    }

    // guard against leading zero's
    if ($code === 48) { // 0
        $value .= $char;
        [$char, $code] = $this->moveStringCursor(1, 1)->readChar();

        if ($code >= 48 && $code <= 57) {
            throw new SyntaxError($this->source, $this->position, 'Invalid number, unexpected digit after 0: ' . Utils::printCharCode($code));
        }
    } else {
        $value .= $this->readDigits();
        [$char, $code] = $this->readChar();
    }

    if ($code === 46) { // .
        $isFloat = true;
        $this->moveStringCursor(1, 1);

        $value .= $char;
        $value .= $this->readDigits();
        [$char, $code] = $this->readChar();
    }

    if ($code === 69 || $code === 101) { // E e
        $isFloat = true;
        $value .= $char;
        [$char, $code] = $this->moveStringCursor(1, 1)->readChar();

        if ($code === 43 || $code === 45) { // + -
            $value .= $char;
            $this->moveStringCursor(1, 1);
        }

        $value .= $this->readDigits();
    }

    return new Token(
        $isFloat ? Token::FLOAT : Token::INT,
        $start,
        $this->position,
        $line,
        $col,
        $prev,
        $value
    );
}