2011-05-27 18:20:44 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Node\Scalar;
|
|
|
|
|
|
|
|
use PhpParser\Node\Scalar;
|
|
|
|
|
2011-05-27 22:57:55 +02:00
|
|
|
/**
|
2011-08-09 14:55:45 +02:00
|
|
|
* @property string $value String value
|
2011-05-27 22:57:55 +02:00
|
|
|
*/
|
2014-02-06 14:44:16 +01:00
|
|
|
class String extends Scalar
|
2011-05-27 18:20:44 +02:00
|
|
|
{
|
2011-12-04 16:52:43 +01:00
|
|
|
protected static $replacements = array(
|
|
|
|
'\\' => '\\',
|
|
|
|
'$' => '$',
|
|
|
|
'n' => "\n",
|
|
|
|
'r' => "\r",
|
|
|
|
't' => "\t",
|
|
|
|
'f' => "\f",
|
|
|
|
'v' => "\v",
|
2011-12-04 17:35:30 +01:00
|
|
|
'e' => "\x1B",
|
2011-12-04 16:52:43 +01:00
|
|
|
);
|
|
|
|
|
2011-08-09 14:55:45 +02:00
|
|
|
/**
|
|
|
|
* Constructs a string scalar node.
|
|
|
|
*
|
2012-04-29 23:32:09 +02:00
|
|
|
* @param string $value Value of the string
|
|
|
|
* @param array $attributes Additional attributes
|
2011-08-09 14:55:45 +02:00
|
|
|
*/
|
2012-04-29 23:32:09 +02:00
|
|
|
public function __construct($value = '', array $attributes = array()) {
|
2011-08-09 14:55:45 +02:00
|
|
|
parent::__construct(
|
|
|
|
array(
|
|
|
|
'value' => $value
|
|
|
|
),
|
2012-04-29 23:32:09 +02:00
|
|
|
$attributes
|
2011-08-09 14:55:45 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-05-28 00:21:12 +02:00
|
|
|
/**
|
2014-09-30 20:23:25 +02:00
|
|
|
* @internal
|
|
|
|
*
|
2012-10-19 15:17:08 +02:00
|
|
|
* Parses a string token.
|
2011-05-28 00:21:12 +02:00
|
|
|
*
|
2012-10-19 15:17:08 +02:00
|
|
|
* @param string $str String token content
|
2011-06-01 22:37:10 +02:00
|
|
|
*
|
2012-10-19 15:17:08 +02:00
|
|
|
* @return string The parsed string
|
2011-05-28 00:21:12 +02:00
|
|
|
*/
|
2012-10-19 15:17:08 +02:00
|
|
|
public static function parse($str) {
|
2011-08-09 14:19:44 +02:00
|
|
|
$bLength = 0;
|
2011-08-20 10:40:27 +02:00
|
|
|
if ('b' === $str[0]) {
|
2011-08-09 14:19:44 +02:00
|
|
|
$bLength = 1;
|
2011-05-28 00:21:12 +02:00
|
|
|
}
|
|
|
|
|
2011-08-20 10:40:27 +02:00
|
|
|
if ('\'' === $str[$bLength]) {
|
2012-10-19 15:17:08 +02:00
|
|
|
return str_replace(
|
2011-05-28 00:21:12 +02:00
|
|
|
array('\\\\', '\\\''),
|
|
|
|
array( '\\', '\''),
|
2011-08-20 10:40:27 +02:00
|
|
|
substr($str, $bLength + 1, -1)
|
2011-05-28 00:21:12 +02:00
|
|
|
);
|
|
|
|
} else {
|
2012-10-19 15:17:08 +02:00
|
|
|
return self::parseEscapeSequences(substr($str, $bLength + 1, -1), '"');
|
2011-05-28 00:21:12 +02:00
|
|
|
}
|
2011-05-29 19:38:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-30 20:23:25 +02:00
|
|
|
* @internal
|
|
|
|
*
|
2011-08-20 10:40:27 +02:00
|
|
|
* Parses escape sequences in strings (all string types apart from single quoted).
|
2011-05-29 19:38:04 +02:00
|
|
|
*
|
2011-08-20 10:40:27 +02:00
|
|
|
* @param string $str String without quotes
|
|
|
|
* @param null|string $quote Quote type
|
2011-06-01 22:37:10 +02:00
|
|
|
*
|
2011-05-29 19:38:04 +02:00
|
|
|
* @return string String with escape sequences parsed
|
|
|
|
*/
|
2011-08-20 10:40:27 +02:00
|
|
|
public static function parseEscapeSequences($str, $quote) {
|
|
|
|
if (null !== $quote) {
|
|
|
|
$str = str_replace('\\' . $quote, $quote, $str);
|
|
|
|
}
|
2011-05-29 19:38:04 +02:00
|
|
|
|
2011-08-20 10:40:27 +02:00
|
|
|
return preg_replace_callback(
|
2011-12-04 17:35:30 +01:00
|
|
|
'~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3})~',
|
2011-08-20 10:40:27 +02:00
|
|
|
array(__CLASS__, 'parseCallback'),
|
|
|
|
$str
|
2011-05-29 19:38:04 +02:00
|
|
|
);
|
2011-05-28 00:21:12 +02:00
|
|
|
}
|
2011-08-20 10:40:27 +02:00
|
|
|
|
2014-09-30 20:23:25 +02:00
|
|
|
private static function parseCallback($matches) {
|
2011-08-20 10:40:27 +02:00
|
|
|
$str = $matches[1];
|
|
|
|
|
|
|
|
if (isset(self::$replacements[$str])) {
|
|
|
|
return self::$replacements[$str];
|
|
|
|
} elseif ('x' === $str[0] || 'X' === $str[0]) {
|
|
|
|
return chr(hexdec($str));
|
|
|
|
} else {
|
|
|
|
return chr(octdec($str));
|
|
|
|
}
|
|
|
|
}
|
2011-12-04 16:52:43 +01:00
|
|
|
|
|
|
|
/**
|
2014-09-30 20:23:25 +02:00
|
|
|
* @internal
|
|
|
|
*
|
2011-12-04 16:52:43 +01:00
|
|
|
* Parses a constant doc string.
|
|
|
|
*
|
|
|
|
* @param string $startToken Doc string start token content (<<<SMTHG)
|
|
|
|
* @param string $str String token content
|
|
|
|
*
|
|
|
|
* @return string Parsed string
|
|
|
|
*/
|
|
|
|
public static function parseDocString($startToken, $str) {
|
|
|
|
// strip last newline (thanks tokenizer for sticking it into the string!)
|
|
|
|
$str = preg_replace('~(\r\n|\n|\r)$~', '', $str);
|
|
|
|
|
|
|
|
// nowdoc string
|
|
|
|
if (false !== strpos($startToken, '\'')) {
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::parseEscapeSequences($str, null);
|
|
|
|
}
|
2011-05-27 18:20:44 +02:00
|
|
|
}
|