2011-05-27 18:20:44 +02:00
|
|
|
<?php
|
|
|
|
|
2011-05-27 22:57:55 +02:00
|
|
|
/**
|
2011-05-28 00:21:12 +02:00
|
|
|
* @property string $value String value
|
2011-05-27 22:57:55 +02:00
|
|
|
*/
|
2011-06-05 18:40:04 +02:00
|
|
|
class PHPParser_Node_Scalar_String extends PHPParser_Node_Scalar
|
2011-05-27 18:20:44 +02:00
|
|
|
{
|
2011-05-28 00:21:12 +02:00
|
|
|
/**
|
|
|
|
* Creates a String node from a string token (parses escape sequences).
|
|
|
|
*
|
2011-06-28 14:16:10 +02:00
|
|
|
* @param string $s String
|
|
|
|
* @param int $line Line
|
2011-06-01 22:37:10 +02:00
|
|
|
*
|
2011-06-05 18:40:04 +02:00
|
|
|
* @return PHPParser_Node_Scalar_String String Node
|
2011-05-28 00:21:12 +02:00
|
|
|
*/
|
2011-06-28 14:16:10 +02:00
|
|
|
public static function create($s, $line) {
|
2011-08-09 14:19:44 +02:00
|
|
|
$bLength = 0;
|
2011-05-28 00:21:12 +02:00
|
|
|
if ('b' === $s[0]) {
|
2011-08-09 14:19:44 +02:00
|
|
|
$bLength = 1;
|
2011-05-28 00:21:12 +02:00
|
|
|
}
|
|
|
|
|
2011-08-09 14:19:44 +02:00
|
|
|
if ('\'' === $s[$bLength]) {
|
2011-05-28 00:21:12 +02:00
|
|
|
$s = str_replace(
|
|
|
|
array('\\\\', '\\\''),
|
|
|
|
array( '\\', '\''),
|
2011-08-09 14:19:44 +02:00
|
|
|
substr($s, $bLength + 1, -1)
|
2011-05-28 00:21:12 +02:00
|
|
|
);
|
|
|
|
} else {
|
2011-08-09 14:19:44 +02:00
|
|
|
$s = self::parseEscapeSequences(substr($s, $bLength + 1, -1));
|
2011-05-28 00:21:12 +02:00
|
|
|
}
|
|
|
|
|
2011-06-28 14:16:10 +02:00
|
|
|
return new self(
|
2011-08-09 14:19:44 +02:00
|
|
|
array('value' => $s),
|
2011-06-28 14:16:10 +02:00
|
|
|
$line
|
|
|
|
);
|
2011-05-29 19:38:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses escape sequences in the content of a doubly quoted string
|
|
|
|
* or heredoc string.
|
|
|
|
*
|
2011-06-01 22:37:10 +02:00
|
|
|
* @param string $s String without quotes
|
|
|
|
*
|
2011-05-29 19:38:04 +02:00
|
|
|
* @return string String with escape sequences parsed
|
|
|
|
*/
|
|
|
|
public static function parseEscapeSequences($s) {
|
|
|
|
// TODO: parse hex and oct escape sequences
|
|
|
|
|
|
|
|
return str_replace(
|
|
|
|
array('\\\\', '\"', '\$', '\n', '\r', '\t', '\f', '\v'),
|
|
|
|
array( '\\', '"', '$', "\n", "\r", "\t", "\f", "\v"),
|
|
|
|
$s
|
|
|
|
);
|
2011-05-28 00:21:12 +02:00
|
|
|
}
|
2011-05-27 18:20:44 +02:00
|
|
|
}
|