From 950ada4cbaed40cab7ff90a45aac5682298e22ea Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 19 Sep 2015 22:05:23 +0800 Subject: [PATCH] Fix issue #227 Use \z instead of $. --- grammar/rebuildParser.php | 2 +- lib/PhpParser/Node/Scalar/String_.php | 2 +- lib/PhpParser/Parser.php | 2 +- .../code/parser/scalar/docStringNewlines.test | 58 +++++++++++++++++++ 4 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 test/code/parser/scalar/docStringNewlines.test diff --git a/grammar/rebuildParser.php b/grammar/rebuildParser.php index 600eccf..5d21a1e 100644 --- a/grammar/rebuildParser.php +++ b/grammar/rebuildParser.php @@ -145,7 +145,7 @@ function resolveMacros($code) { if ('parseEncapsedDoc' == $name) { assertArgs(1, $args, $name); - return 'foreach (' . $args[0] . ' as &$s) { if (is_string($s)) { $s = Node\Scalar\String_::parseEscapeSequences($s, null); } } $s = preg_replace(\'~(\r\n|\n|\r)$~\', \'\', $s); if (\'\' === $s) array_pop(' . $args[0] . ');'; + return 'foreach (' . $args[0] . ' as &$s) { if (is_string($s)) { $s = Node\Scalar\String_::parseEscapeSequences($s, null); } } $s = preg_replace(\'~(\r\n|\n|\r)\z~\', \'\', $s); if (\'\' === $s) array_pop(' . $args[0] . ');'; } return $matches[0]; diff --git a/lib/PhpParser/Node/Scalar/String_.php b/lib/PhpParser/Node/Scalar/String_.php index e6dcafa..f7c10c3 100644 --- a/lib/PhpParser/Node/Scalar/String_.php +++ b/lib/PhpParser/Node/Scalar/String_.php @@ -107,7 +107,7 @@ class String_ extends Scalar */ 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); + $str = preg_replace('~(\r\n|\n|\r)\z~', '', $str); // nowdoc string if (false !== strpos($startToken, '\'')) { diff --git a/lib/PhpParser/Parser.php b/lib/PhpParser/Parser.php index 70a67b7..6408fbe 100644 --- a/lib/PhpParser/Parser.php +++ b/lib/PhpParser/Parser.php @@ -2573,7 +2573,7 @@ class Parser extends ParserAbstract } protected function reduceRule379() { - foreach ($this->semStack[$this->stackPos-(3-2)] as &$s) { if (is_string($s)) { $s = Node\Scalar\String_::parseEscapeSequences($s, null); } } $s = preg_replace('~(\r\n|\n|\r)$~', '', $s); if ('' === $s) array_pop($this->semStack[$this->stackPos-(3-2)]);; $this->semValue = new Scalar\Encapsed($this->semStack[$this->stackPos-(3-2)], $this->startAttributeStack[$this->stackPos-(3-1)] + $this->endAttributes); + foreach ($this->semStack[$this->stackPos-(3-2)] as &$s) { if (is_string($s)) { $s = Node\Scalar\String_::parseEscapeSequences($s, null); } } $s = preg_replace('~(\r\n|\n|\r)\z~', '', $s); if ('' === $s) array_pop($this->semStack[$this->stackPos-(3-2)]);; $this->semValue = new Scalar\Encapsed($this->semStack[$this->stackPos-(3-2)], $this->startAttributeStack[$this->stackPos-(3-1)] + $this->endAttributes); } protected function reduceRule380() { diff --git a/test/code/parser/scalar/docStringNewlines.test b/test/code/parser/scalar/docStringNewlines.test new file mode 100644 index 0000000..479c995 --- /dev/null +++ b/test/code/parser/scalar/docStringNewlines.test @@ -0,0 +1,58 @@ +Trailing newlines in doc strings +----- +