php-parser/lib/PHPParser/Node/Scalar/LNumber.php
nikic 933bebb9b8 Fix parsing of special int syntaxes
Also add the means to test code parsing.
2011-11-26 16:09:39 +01:00

56 lines
1.4 KiB
PHP

<?php
/**
* @property int $value Number value
*/
class PHPParser_Node_Scalar_LNumber extends PHPParser_Node_Scalar
{
/**
* Constructs an integer number scalar node.
*
* @param int $value Value of the number
* @param int $line Line
* @param null|string $docComment Nearest doc comment
*/
public function __construct($value = 0, $line = -1, $docComment = null) {
parent::__construct(
array(
'value' => $value
),
$line, $docComment
);
}
/**
* Parses an LNUMBER token (dec, hex, oct and bin notations) like PHP would.
*
* @param string $str A string number
*
* @return int The parsed number
*/
public static function parse($str) {
// handle plain 0 specially
if ('0' === $str) {
return 0;
}
// if first char is 0 (and number isn't 0) it's a special syntax
if ('0' === $str[0]) {
// hex
if ('x' === $str[1] || 'X' === $str[1]) {
return hexdec($str);
}
// bin
if ('b' === $str[1] || 'B' === $str[1]) {
return bindec($str);
}
// oct (intval instead of octdec to get proper cutting behavior with malformed numbers)
return intval($str, 8);
}
// dec
return (int) $str;
}
}