55use JsPhpize \JsPhpize ;
66use JsPhpize \Lexer \Lexer ;
77use JsPhpize \Nodes \Main ;
8+ use JsPhpize \Nodes \NodeEnd ;
89
910class Parser extends Visitor
1011{
@@ -33,6 +34,11 @@ class Parser extends Visitor
3334 */
3435 protected $ stack ;
3536
37+ /**
38+ * @var Token
39+ */
40+ protected $ previousToken ;
41+
3642 public function __construct (JsPhpize $ engine , $ input , $ filename )
3743 {
3844 $ input = str_replace (array ("\r\n" , "\r" ), array ("\n" , '' ), $ input );
@@ -68,6 +74,15 @@ protected function getHelper($helper)
6874 return '$GLOBALS[" ' . $ this ->engine ->getOption ('varPrefix ' , JsPhpize::VAR_PREFIX ) . 'h_ ' . $ helper . '"] ' ;
6975 }
7076
77+ protected function helperWrap ($ helper , $ argumments )
78+ {
79+ if (!is_array ($ argumments )) {
80+ $ argumments = array_slice (func_get_args (), 1 );
81+ }
82+
83+ return 'call_user_func( ' . $ this ->getHelper ($ helper ) . ', ' . implode (', ' , $ argumments ) . ') ' ;
84+ }
85+
7186 protected function exceptionInfos ()
7287 {
7388 return $ this ->lexer ->exceptionInfos ();
@@ -78,9 +93,14 @@ protected function prepend($token)
7893 return array_unshift ($ this ->tokens , $ token );
7994 }
8095
96+ protected function getNextUnread ()
97+ {
98+ return $ this ->lexer ->next ();
99+ }
100+
81101 protected function next ()
82102 {
83- return array_shift ($ this ->tokens ) ?: $ this ->lexer -> next ();
103+ return array_shift ($ this ->tokens ) ?: $ this ->getNextUnread ();
84104 }
85105
86106 protected function skip ($ index = 1 )
@@ -95,7 +115,7 @@ protected function advance($index)
95115 $ token = null ;
96116
97117 while ($ index --) {
98- $ token = $ this ->lexer -> next ();
118+ $ token = $ this ->getNextUnread ();
99119 $ this ->tokens [] = $ token ;
100120 }
101121
@@ -104,6 +124,10 @@ protected function advance($index)
104124
105125 protected function get ($ index )
106126 {
127+ if ($ index === -1 ) {
128+ return $ this ->previous ();
129+ }
130+
107131 return isset ($ this ->tokens [$ index ])
108132 ? $ this ->tokens [$ index ]
109133 : $ this ->advance ($ index + 1 - count ($ this ->tokens ));
@@ -114,6 +138,11 @@ protected function current()
114138 return $ this ->get (0 );
115139 }
116140
141+ protected function previous ()
142+ {
143+ return $ this ->previousToken ;
144+ }
145+
117146 protected function unexpected ($ token )
118147 {
119148 throw new Exception ('Unexpected ' . $ token ->type . rtrim (' ' . ($ token ->value ?: '' )) . $ this ->exceptionInfos (), 8 );
@@ -136,7 +165,6 @@ protected function getCurrentBlock()
136165
137166 protected function visitToken ($ token )
138167 {
139- $ block = $ this ->getCurrentBlock ();
140168 $ method = 'visit ' . ucfirst ($ token ->type );
141169 $ token = method_exists ($ this , $ method )
142170 ? $ this ->$ method ($ token )
@@ -151,15 +179,16 @@ protected function visitToken($token)
151179 public function parseBlock ($ block )
152180 {
153181 $ this ->stack [] = $ block ;
154- $ prev = null ;
182+ $ this -> previousToken = null ;
155183 while ($ token = $ this ->next ()) {
156- if ($ token === $ prev ) {
184+ if ($ token === $ this -> previousToken ) {
157185 $ this ->unexpected ($ token );
158186 }
159- $ prev = $ token ;
160187 if ($ token ->type === '; ' ) {
188+ $ block ->addNode (new NodeEnd ($ token ));
161189 continue ;
162190 }
191+ $ this ->previousToken = $ token ;
163192 if ($ token ->type === '} ' ) {
164193 return ;
165194 }
0 commit comments