1313use JsPhpize \Nodes \FunctionCall ;
1414use JsPhpize \Nodes \HooksArray ;
1515use JsPhpize \Nodes \Main ;
16+ use JsPhpize \Nodes \Node ;
1617use JsPhpize \Nodes \Parenthesis ;
18+ use JsPhpize \Nodes \Ternary ;
1719use JsPhpize \Nodes \Value ;
1820use JsPhpize \Nodes \Variable ;
1921
@@ -296,6 +298,28 @@ protected function expectValue($next, $exception = null)
296298 }
297299 throw new Exception ('Value expected before ' . $ this ->exceptionInfos (), 13 );
298300 }
301+ if ($ next ->is ('function ' )) {
302+ $ function = new Block ('function ' );
303+ $ next = $ this ->get (0 );
304+ if ($ next ->is ('variable ' )) {
305+ $ this ->skip ();
306+ $ next = $ this ->get (0 );
307+ }
308+ if (!$ next ->is ('( ' )) {
309+ $ this ->unexpected ($ next );
310+ }
311+ $ this ->skip ();
312+ $ function ->setValue ($ this ->parseParentheses ());
313+ $ next = $ this ->get (0 );
314+ if (!$ next ->is ('{ ' )) {
315+ $ this ->unexpected ($ next );
316+ }
317+ $ this ->skip ();
318+ $ this ->parseBlock ($ function );
319+ $ this ->skip ();
320+
321+ return $ function ;
322+ }
299323 $ value = $ this ->getValueFromToken ($ next );
300324 if (!$ value ) {
301325 $ this ->unexpected ($ next );
@@ -304,6 +328,26 @@ protected function expectValue($next, $exception = null)
304328 return $ value ;
305329 }
306330
331+ protected function parseTernary (Node $ condition )
332+ {
333+ $ trueValue = $ this ->expectValue ($ this ->next ());
334+ $ next = $ this ->next ();
335+ if (!$ next ) {
336+ throw new Exception ("Ternary expression not properly closed after '?' " . $ this ->exceptionInfos (), 14 );
337+ }
338+ if (!$ next ->is (': ' )) {
339+ throw new Exception ("':' expected but $ next given " . $ this ->exceptionInfos (), 15 );
340+ }
341+ $ next = $ this ->next ();
342+ if (!$ next ) {
343+ throw new Exception ("Ternary expression not properly closed after ':' " . $ this ->exceptionInfos (), 16 );
344+ }
345+ $ falseValue = $ this ->expectValue ($ next );
346+ $ next = $ this ->get (0 );
347+
348+ return new Ternary ($ condition , $ trueValue , $ falseValue );
349+ }
350+
307351 protected function parseValue ($ token )
308352 {
309353 $ debug = ($ token ->value === 'array_slice ' );
@@ -341,27 +385,19 @@ protected function parseValue($token)
341385 }
342386 if ($ token ->is ('? ' )) {
343387 $ this ->skip ();
344- $ trueValue = $ this ->expectValue ($ this ->next ());
345- $ next = $ this ->next ();
346- if (!$ next ) {
347- throw new Exception ("Ternary expression not properly closed after '?' " . $ this ->exceptionInfos (), 14 );
348- }
349- if (!$ next ->is (': ' )) {
350- throw new Exception ("':' expected but $ next given " . $ this ->exceptionInfos (), 15 );
351- }
352- $ next = $ this ->next ();
353- if (!$ next ) {
354- throw new Exception ("Ternary expression not properly closed after ':' " . $ this ->exceptionInfos (), 16 );
355- }
356- $ falseValue = $ this ->expectValue ($ this ->next ());
357- $ value = new Ternary ($ value , $ trueValue , $ falseValue );
388+ $ value = $ this ->parseTernary ($ value );
358389
359390 continue ;
360391 }
361392
362393 $ this ->skip ();
363394 $ nextValue = $ this ->expectValue ($ this ->next ());
364395 $ value = new Dyiade ($ token ->type , $ value , $ nextValue );
396+ $ token = $ this ->get (0 );
397+ if ($ token && $ token ->is ('? ' )) {
398+ $ this ->skip ();
399+ $ value = $ this ->parseTernary ($ value );
400+ }
365401
366402 continue ;
367403 }
0 commit comments