22
33namespace Phug ;
44
5- use Phug \Reader \LineOffsetTrait ;
6- use Phug \Reader \PregUtil ;
7-
85/**
96 * A string reading utility that searches strings byte by byte.
107 */
118class Reader
129{
1310
11+ /**
12+ * An array of PREG errors with a good error message.
13+ *
14+ * @var array
15+ */
1416 private static $ pregErrors = [
1517 PREG_NO_ERROR => 'No error occured ' ,
1618 PREG_INTERNAL_ERROR => 'An internal error occured ' ,
@@ -335,7 +337,7 @@ public function match($pattern, $modifiers = null, $ignoredSuffixes = null)
335337
336338 if ($ result === false ) {
337339 $ this ->throwException (
338- 'Failed to match pattern: ' .$ this ->getLastPregErrorText ()
340+ 'Failed to match pattern: ' .$ this ->getPregErrorText ()
339341 );
340342 }
341343
@@ -750,7 +752,7 @@ public function readIdentifier($prefix = null, $allowedChars = null)
750752 return null ;
751753 }
752754
753- return $ this ->readWhile (function ($ char ) use ($ allowedChars ) {
755+ return $ this ->readWhile (function () use ($ allowedChars ) {
754756
755757 return $ this ->peekIdentifier ($ allowedChars );
756758 });
@@ -780,11 +782,10 @@ public function readString(array $escapeSequences = null, $raw = false)
780782 $ quoteStyle = $ this ->consume ();
781783 $ escapeSequences [$ quoteStyle ] = $ quoteStyle ;
782784
783- $ last = null ;
784785 $ char = null ;
785786 $ string = '' ;
787+ $ closed = false ;
786788 while ($ this ->hasLength ()) {
787- $ last = $ char ;
788789 $ char = $ this ->peek ();
789790 $ this ->consume ();
790791
@@ -806,21 +807,25 @@ public function readString(array $escapeSequences = null, $raw = false)
806807
807808 //End the string (Escaped quotes have already been handled)
808809 if ($ char === $ quoteStyle ) {
809- if ($ raw ) {
810- $ string = $ quoteStyle .$ string .$ quoteStyle ;
811- }
810+ $ closed = true ;
812811
813- return $ string ;
812+ break ;
814813 }
815814
816815 $ string .= $ char ;
817816 }
818817
819- $ this ->throwException (
820- "Unclosed string ( $ quoteStyle) encountered "
821- );
818+ if (!$ closed ) {
819+ $ this ->throwException (
820+ "Unclosed string ( $ quoteStyle) encountered "
821+ );
822+ }
823+
824+ if ($ raw ) {
825+ $ string = $ quoteStyle .$ string .$ quoteStyle ;
826+ }
822827
823- return '' ;
828+ return $ string ;
824829 }
825830
826831 /**
@@ -903,10 +908,16 @@ public function readExpression(array $breaks = null, array $brackets = null)
903908 return trim ($ expression );
904909 }
905910
906- protected function getLastPregErrorText ()
911+ /**
912+ * Returns a describing text for the last PREG error that happened.
913+ *
914+ * @param null $code
915+ * @return string
916+ */
917+ protected function getPregErrorText ($ code = null )
907918 {
908919
909- $ code = preg_last_error ();
920+ $ code = $ code ?: preg_last_error ();
910921
911922 if (!isset (self ::$ pregErrors [$ code ])) {
912923 $ code = PREG_NO_ERROR ;
0 commit comments