@@ -28,6 +28,11 @@ class Parser extends Visitor
2828 */
2929 protected $ dependencies ;
3030
31+ /**
32+ * @var array
33+ */
34+ protected $ stack ;
35+
3136 public function __construct (JsPhpize $ engine , $ input , $ filename )
3237 {
3338 $ input = str_replace (array ("\r\n" , "\r" ), array ("\n" , '' ), $ input );
@@ -60,7 +65,7 @@ protected function getHelper($helper)
6065 ));
6166 }
6267
63- return '$GLOBALS["__jp_ ' . $ helper . '"] ' ;
68+ return '$GLOBALS[" ' . $ this -> engine -> getOption ( ' varPrefix ' , JsPhpize:: VAR_PREFIX ) . ' h_ ' . $ helper . '"] ' ;
6469 }
6570
6671 protected function exceptionInfos ()
@@ -124,13 +129,26 @@ protected function expect($type)
124129 return $ token ;
125130 }
126131
127- public function parse ()
132+ protected function getCurrentBlock ()
133+ {
134+ return end ($ this ->stack );
135+ }
136+
137+ public function parseBlock ($ block )
128138 {
129- $ block = new Main ();
139+ $ this ->stack [] = $ block ;
140+ $ prev = null ;
130141 while ($ token = $ this ->next ()) {
142+ if ($ token === $ prev ) {
143+ $ this ->unexpected ($ token );
144+ }
145+ $ prev = $ token ;
131146 if ($ token ->type === '; ' ) {
132147 continue ;
133148 }
149+ if ($ token ->type === '} ' ) {
150+ return ;
151+ }
134152 $ method = 'visit ' . ucfirst ($ token ->type );
135153 $ token = method_exists ($ this , $ method )
136154 ? $ this ->$ method ($ token )
@@ -139,10 +157,17 @@ public function parse()
139157 $ token = array ($ token );
140158 }
141159 $ block ->addNodes ($ token );
142- // if (!method_exists($this, $method)) {
143- // $this->unexpected($token);
144- // }
145- // $block->addNodes((array) $this->$method($token));
160+ }
161+ array_pop ($ this ->stack );
162+ }
163+
164+ public function parse ()
165+ {
166+ $ block = new Main ($ this ->engine ->getOption ('varPrefix ' , JsPhpize::VAR_PREFIX ) . 'h_ ' );
167+ $ this ->stack = array ();
168+ $ this ->parseBlock ($ block );
169+ if ($ this ->next ()) {
170+ $ this ->unexpected ('} ' );
146171 }
147172 $ block ->addDependencies ($ this ->dependencies );
148173
0 commit comments