Changeset View
Changeset View
Standalone View
Standalone View
src/parser/xhpast/api/XHPASTNode.php
Show All 27 Lines | final class XHPASTNode extends AASTNode { | ||||
} | } | ||||
public function evalStatic() { | public function evalStatic() { | ||||
switch ($this->getTypeName()) { | switch ($this->getTypeName()) { | ||||
case 'n_STATEMENT': | case 'n_STATEMENT': | ||||
return $this->getChildByIndex(0)->evalStatic(); | return $this->getChildByIndex(0)->evalStatic(); | ||||
break; | break; | ||||
case 'n_STRING_SCALAR': | case 'n_STRING_SCALAR': | ||||
return (string)$this->getStringLiteralValue(); | return phutil_string_cast($this->getStringLiteralValue()); | ||||
case 'n_HEREDOC': | |||||
return phutil_string_cast($this->getStringLiteralValue()); | |||||
case 'n_NUMERIC_SCALAR': | case 'n_NUMERIC_SCALAR': | ||||
$value = $this->getSemanticString(); | $value = $this->getSemanticString(); | ||||
if (preg_match('/^0x/i', $value)) { | if (preg_match('/^0x/i', $value)) { | ||||
// Hex | // Hex | ||||
$value = base_convert(substr($value, 2), 16, 10); | $value = base_convert(substr($value, 2), 16, 10); | ||||
} else if (preg_match('/^0\d+$/i', $value)) { | } else if (preg_match('/^0\d+$/i', $value)) { | ||||
// Octal | // Octal | ||||
$value = base_convert(substr($value, 1), 8, 10); | $value = base_convert(substr($value, 1), 8, 10); | ||||
▲ Show 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | public function getStringVariables() { | ||||
} | } | ||||
$names = ipull($names, 0, 1); | $names = ipull($names, 0, 1); | ||||
return $names; | return $names; | ||||
} | } | ||||
public function getStringLiteralValue() { | public function getStringLiteralValue() { | ||||
if ($this->getTypeName() != 'n_STRING_SCALAR') { | $type_name = $this->getTypeName(); | ||||
return null; | |||||
if ($type_name === 'n_HEREDOC') { | |||||
$value = $this->getSemanticString(); | |||||
$value = phutil_split_lines($value); | |||||
$value = array_slice($value, 1, -1); | |||||
$value = implode('', $value); | |||||
// Strip the final newline from value, this isn't part of the string | |||||
// literal. | |||||
$value = preg_replace('/(\r|\n|\r\n)\z/', '', $value); | |||||
return $this->newStringLiteralFromSemanticString($value); | |||||
} | } | ||||
if ($type_name === 'n_STRING_SCALAR') { | |||||
$value = $this->getSemanticString(); | $value = $this->getSemanticString(); | ||||
$type = $value[0]; | $type = $value[0]; | ||||
$value = preg_replace('/^b?[\'"]|[\'"]$/i', '', $value); | $value = preg_replace('/^b?[\'"]|[\'"]$/i', '', $value); | ||||
$esc = false; | |||||
$len = strlen($value); | |||||
$out = ''; | |||||
if ($type == "'") { | if ($type == "'") { | ||||
// Single quoted strings treat everything as a literal except "\\" and | // Single quoted strings treat everything as a literal except "\\" and | ||||
// "\'". | // "\'". | ||||
return str_replace( | return str_replace( | ||||
array('\\\\', '\\\''), | array('\\\\', '\\\''), | ||||
array('\\', "'"), | array('\\', "'"), | ||||
$value); | $value); | ||||
} | } | ||||
return $this->newStringLiteralFromSemanticString($value); | |||||
} | |||||
return null; | |||||
} | |||||
private function newStringLiteralFromSemanticString($value) { | |||||
// Double quoted strings treat "\X" as a literal if X isn't specifically | // Double quoted strings treat "\X" as a literal if X isn't specifically | ||||
// a character which needs to be escaped -- e.g., "\q" and "\'" are | // a character which needs to be escaped -- e.g., "\q" and "\'" are | ||||
// literally "\q" and "\'". stripcslashes() is too aggressive, so find | // literally "\q" and "\'". stripcslashes() is too aggressive, so find | ||||
// all these under-escaped backslashes and escape them. | // all these under-escaped backslashes and escape them. | ||||
$len = strlen($value); | |||||
$esc = false; | |||||
$out = ''; | |||||
for ($ii = 0; $ii < $len; $ii++) { | for ($ii = 0; $ii < $len; $ii++) { | ||||
$c = $value[$ii]; | $c = $value[$ii]; | ||||
if ($esc) { | if ($esc) { | ||||
$esc = false; | $esc = false; | ||||
switch ($c) { | switch ($c) { | ||||
case 'x': | case 'x': | ||||
$u = isset($value[$ii + 1]) ? $value[$ii + 1] : null; | $u = isset($value[$ii + 1]) ? $value[$ii + 1] : null; | ||||
if (!preg_match('/^[a-f0-9]/i', $u)) { | if (!preg_match('/^[a-f0-9]/i', $u)) { | ||||
▲ Show 20 Lines • Show All 99 Lines • Show Last 20 Lines |