class NumericLiteral
A node that represents a numeric literal.
@author Marco Marchiò <marco.mm89@gmail.com>
Hierarchy
- class \Peast\Syntax\Node\Node implements \Peast\Syntax\Node\JSONSerializable
- class \Peast\Syntax\Node\Literal extends \Peast\Syntax\Node\Node implements \Peast\Syntax\Node\Expression
- class \Peast\Syntax\Node\NumericLiteral extends \Peast\Syntax\Node\Literal
- class \Peast\Syntax\Node\Literal extends \Peast\Syntax\Node\Node implements \Peast\Syntax\Node\Expression
Expanded class hierarchy of NumericLiteral
1 string reference to 'NumericLiteral'
- Parser::parseNumericLiteral in vendor/
mck89/ peast/ lib/ Peast/ Syntax/ Parser.php - Parses a numeric literal
File
-
vendor/
mck89/ peast/ lib/ Peast/ Syntax/ Node/ NumericLiteral.php, line 17
Namespace
Peast\Syntax\NodeView source
class NumericLiteral extends Literal {
/**
* Map of node properties
*
* @var array
*/
protected $propertiesMap = array(
"format" => false,
);
//Format constants
/**
* Decimal number format
*/
const DECIMAL = "decimal";
/**
* Hexadecimal number format
*/
const HEXADECIMAL = "hexadecimal";
/**
* Octal number format
*/
const OCTAL = "octal";
/**
* Binary number format
*/
const BINARY = "binary";
/**
* Node's numeric format
*
* @var string
*/
protected $format = self::DECIMAL;
/**
* Numeric forms conversion rules
*
* @var array
*/
protected $forms = array(
"b" => array(
"check" => "/^0b[01]+[01_]*\$/i",
"conv" => "bindec",
"format" => self::BINARY,
),
"o" => array(
"check" => "/^0o[0-7]+[0-7_]*\$/i",
"conv" => "octdec",
"format" => self::OCTAL,
),
"x" => array(
"check" => "/^0x[0-9a-f]+[0-9a-f_]*\$/i",
"conv" => "hexdec",
"format" => self::HEXADECIMAL,
),
);
/**
* Sets node's value
*
* @param float $value Value
*
* @return $this
*/
public function setValue($value) {
$value = (double) $value;
$intValue = (int) $value;
if ($value == $intValue) {
$value = $intValue;
}
$this->value = $value;
//Force recalculation of the raw value
return $this->setFormat($this->format);
}
/**
* Sets node's raw value
*
* @param mixed $raw Raw value
*
* @return $this
*
* @throws \Exception
*/
public function setRaw($raw) {
$value = $raw;
$format = self::DECIMAL;
if (is_string($value) && $value !== "") {
//Hexadecimal, binary or octal
$startZero = $value[0] === "0";
$form = $startZero && isset($value[1]) ? strtolower($value[1]) : null;
//Numeric separator cannot appear at the beginning or at the end of the number
if (preg_match("/^_|_\$/", $value)) {
throw new \Exception("Invalid numeric value");
}
elseif (isset($this->forms[$form])) {
$formDef = $this->forms[$form];
if (!preg_match($formDef["check"], $value)) {
throw new \Exception("Invalid " . $formDef["format"]);
}
$value = str_replace("_", "", $value);
$value = $formDef["conv"]($value);
$format = $formDef["format"];
}
elseif ($startZero && preg_match("/^0[0-7_]+\$/", $value)) {
//Legacy octal form
$value = str_replace("_", "", $value);
$value = octdec($value);
$format = self::OCTAL;
}
elseif (preg_match("/^([\\d_]*\\.?[\\d_]*)(?:e[+\\-]?[\\d_]+)?\$/i", $value, $match) && $match[1] !== "" && $match[1] !== "." && !preg_match("/_e|e[+-]?_|_\$/", $value)) {
$value = str_replace("_", "", $value);
}
else {
throw new \Exception("Invalid numeric value");
}
}
elseif (!is_int($value) && !is_float($value)) {
throw new \Exception("Invalid numeric value");
}
$value = (double) $value;
$intValue = (int) $value;
if ($value == $intValue) {
$value = $intValue;
}
$this->format = $format;
$this->value = $value;
$this->raw = $raw;
return $this;
}
/**
* Returns node's numeric format
*
* @return string
*/
public function getFormat() {
return $this->format;
}
/**
* Sets node's numeric format
*
* @param string $format Format, one of the format constants
*
* @return $this
*/
public function setFormat($format) {
$this->format = $format;
switch ($format) {
case self::BINARY:
$this->raw = "0b" . decbin($this->value);
break;
case self::OCTAL:
$this->raw = "0o" . decoct($this->value);
break;
case self::HEXADECIMAL:
$this->raw = "0x" . dechex($this->value);
break;
default:
$this->raw = (string) $this->value;
break;
}
return $this;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
Literal::$raw | protected | property | Node's raw value | 1 | |
Literal::$value | protected | property | Node's value | 1 | |
Literal::getRaw | public | function | Return node's raw value | 1 | |
Literal::getType | public | function | Returns node's type | Overrides Node::getType | 1 |
Literal::getValue | public | function | Returns node's value | 1 | |
Node::$leadingComments | protected | property | Leading comments array | ||
Node::$location | public | property | Node location in the source code | ||
Node::$trailingComments | protected | property | Trailing comments array | ||
Node::assertArrayOf | protected | function | Asserts that the given value is an array of defined type | ||
Node::assertType | protected | function | Asserts that the given value respects the defined type | ||
Node::getLeadingComments | public | function | Returns leading comments array | ||
Node::getLocation | public | function | Returns node location in the source code | ||
Node::getTrailingComments | public | function | Returns trailing comments array | ||
Node::jsonSerialize | public | function | Returns a serializable version of the node | 2 | |
Node::render | public | function | Renders the current node | ||
Node::setEndPosition | public | function | Sets the end position of the node in the source code | ||
Node::setLeadingComments | public | function | Sets leading comments array | 1 | |
Node::setStartPosition | public | function | Sets the start position of the node in the source code | ||
Node::setTrailingComments | public | function | Sets trailing comments array | 1 | |
Node::traverse | public | function | Traverses the current node and all its child nodes using the given function |
||
Node::typeError | protected | function | Throws an error if the defined type is not supported b | ||
Node::__construct | public | function | Class constructor | ||
NumericLiteral::$format | protected | property | Node's numeric format | ||
NumericLiteral::$forms | protected | property | Numeric forms conversion rules | ||
NumericLiteral::$propertiesMap | protected | property | Map of node properties | Overrides Literal::$propertiesMap | |
NumericLiteral::BINARY | constant | Binary number format | |||
NumericLiteral::DECIMAL | constant | Decimal number format | |||
NumericLiteral::getFormat | public | function | Returns node's numeric format | ||
NumericLiteral::HEXADECIMAL | constant | Hexadecimal number format | |||
NumericLiteral::OCTAL | constant | Octal number format | |||
NumericLiteral::setFormat | public | function | Sets node's numeric format | ||
NumericLiteral::setRaw | public | function | Sets node's raw value | Overrides Literal::setRaw | |
NumericLiteral::setValue | public | function | Sets node's value | Overrides Literal::setValue |