- Added while/endwhile and do/while syntaxes
- Fix the abstractclass requirement of parse() function
This commit is contained in:
1 parent
33298ff2e4
commit
bb29a56077
36 files changed
+115
-73
No files matched your search
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\ControlStructure;
|
||||
|
||||
abstract class AbstractLoop implements ControlStructure {
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
switch($token) {
|
||||
case "while":
|
||||
case "foreach":
|
||||
$name = "$".uniqid("{$token}_");
|
||||
|
||||
$stack = array_filter($context->iterationStack ?? [], function($item) {
|
||||
return ! $item['or'];
|
||||
});
|
||||
|
||||
$count = count($stack);
|
||||
|
||||
if ( $count > 0 ) {
|
||||
$name .= "[" . end($stack)['uid'] . "]";
|
||||
}
|
||||
|
||||
$context->iterationStack[] = [
|
||||
'or' => false,
|
||||
'uid' => $name,
|
||||
'token' => "end{$token}",
|
||||
];
|
||||
|
||||
return "<?php $token ($arguments): $name = ( $name ?? 0 ) + 1; ?>";
|
||||
|
||||
case "endwhile":
|
||||
case "endforeach":
|
||||
$last = end($context->iterationStack);
|
||||
|
||||
if ( $last['or'] === false ) {
|
||||
$output = "<?php $token; ?>";
|
||||
}
|
||||
else {
|
||||
$output = "<?php endif; if ( isset({$last['uid']}) ) unset({$last['uid']}); ?>";
|
||||
}
|
||||
|
||||
array_pop($context->iterationStack);
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ class BlockToken implements ControlStructure {
|
||||
|
||||
public array $token = [ "arguments", "block", "endblock", "define", "slot", "endslot", "using" ];
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
static $slotDefinitions = [];
|
||||
|
||||
switch($token) {
|
||||
|
||||
@@ -6,7 +6,7 @@ class BreakToken implements ControlStructure {
|
||||
|
||||
public string $token = "break";
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
return "<?php break; ?>";
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ class CaseToken implements ControlStructure {
|
||||
|
||||
public string $token = "case";
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
$output = "";
|
||||
|
||||
if ( $context->switchStack ) {
|
||||
|
||||
@@ -6,7 +6,7 @@ class ContinueToken implements ControlStructure {
|
||||
|
||||
public string $token = "continue";
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
return "<?php continue; ?>";
|
||||
}
|
||||
|
||||
|
||||
@@ -3,5 +3,5 @@
|
||||
namespace Picea\ControlStructure;
|
||||
|
||||
interface ControlStructure {
|
||||
public function parse(\Picae\Compiler\Context &$context, string $sourceCode, string $token);
|
||||
public function parse(\Picea\Compiler\Context &$context, string $sourceCode, string $token);
|
||||
}
|
||||
@@ -6,7 +6,7 @@ class DefaultToken implements ControlStructure {
|
||||
|
||||
public string $token = "default";
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
$output = "";
|
||||
|
||||
if ( $context->switchStack ) {
|
||||
|
||||
@@ -6,7 +6,7 @@ class EndRawToken implements ControlStructure {
|
||||
|
||||
public string $token = "endraw";
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,7 +6,7 @@ class ExtendsToken implements ControlStructure {
|
||||
|
||||
public string $token = "extends";
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $path, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $path, string $token) {
|
||||
# Triming string's quotes
|
||||
$path = trim($path, "\"\' \t");
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ class ForToken implements ControlStructure {
|
||||
|
||||
public array $token = [ "for", "endfor" ];
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
switch($token) {
|
||||
case "for":
|
||||
$uid = "$".uniqid("for_");
|
||||
|
||||
@@ -2,46 +2,8 @@
|
||||
|
||||
namespace Picea\ControlStructure;
|
||||
|
||||
class ForeachToken implements ControlStructure {
|
||||
use DI\Definition\Source\AnnotationBasedAutowiring;
|
||||
|
||||
class ForeachToken extends AbstractLoop {
|
||||
public array $token = [ "foreach", "endforeach" ];
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
switch($token) {
|
||||
case "foreach":
|
||||
$name = "$".uniqid("foreach_");
|
||||
|
||||
$stack = array_filter($context->iterationStack ?? [], function($item) {
|
||||
return ! $item['or'];
|
||||
});
|
||||
|
||||
$count = count($stack);
|
||||
|
||||
if ( $count > 0 ) {
|
||||
$name .= "[" . end($stack)['uid'] . "]";
|
||||
}
|
||||
|
||||
$context->iterationStack[] = [
|
||||
'or' => false,
|
||||
'uid' => $name,
|
||||
'token' => 'endforeach',
|
||||
];
|
||||
|
||||
return "<?php foreach ($arguments): $name = ( $name ?? 0 ) + 1; ; ?>";
|
||||
|
||||
case "endforeach":
|
||||
$last = end($context->iterationStack);
|
||||
|
||||
if ( $last['or'] === false ) {
|
||||
$output = "<?php endforeach; ?>";
|
||||
}
|
||||
else {
|
||||
$output = "<?php endif; unset({$last['uid']}) ?>";
|
||||
}
|
||||
|
||||
array_pop($context->iterationStack);
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ class FunctionToken implements ControlStructure {
|
||||
|
||||
public array $token = [ "function", "endfunction", "return" ];
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
switch($token) {
|
||||
case "function":
|
||||
return $this->printFunction($context, $arguments);
|
||||
|
||||
@@ -6,7 +6,7 @@ class IfToken implements ControlStructure {
|
||||
|
||||
public array $token = [ "if", "else", "elseif", "endif" ];
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
switch($token) {
|
||||
case "if":
|
||||
return "<?php if ($arguments): ?>";
|
||||
|
||||
@@ -6,7 +6,7 @@ class IncludeToken implements ControlStructure {
|
||||
|
||||
public string $token = "include";
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
return "<?php echo \$___class__template->picea->inlineContent($arguments); ?>";
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ class NamespaceToken implements ControlStructure {
|
||||
|
||||
public string $token = "namespace";
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
$context->namespace = $arguments;
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ class OrToken implements ControlStructure {
|
||||
|
||||
public string $token = "or";
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
if ( empty($context->iterationStack) ) {
|
||||
throw new \LogicException("Token `or` was used outside of iterator. Make sure your `for` or `foreach` declaration are properly made.");
|
||||
throw new \LogicException("Token `or` was used outside of iterator. Make sure your `for`, `foreach`, `while`, `do/until` declaration are properly made.");
|
||||
}
|
||||
|
||||
$key = count( $context->iterationStack ) - 1;
|
||||
|
||||
@@ -6,7 +6,7 @@ class RawToken implements ControlStructure {
|
||||
|
||||
public string $token = "raw";
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,7 +6,7 @@ class SectionToken implements ControlStructure {
|
||||
|
||||
public array $token = [ "section", "endsection" ];
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
switch($token) {
|
||||
case "section":
|
||||
return $this->printSection($context, $arguments);
|
||||
|
||||
@@ -6,7 +6,7 @@ class SwitchToken implements ControlStructure {
|
||||
|
||||
public array $token = [ "switch", "case", "endswitch" ];
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
switch($token) {
|
||||
case "switch":
|
||||
$context->switchStack[] = true;
|
||||
|
||||
@@ -6,7 +6,7 @@ class UseToken implements ControlStructure {
|
||||
|
||||
public string $token = "use";
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
$context->useStack[] = $arguments;
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ class ViewToken implements ControlStructure {
|
||||
|
||||
public string $token = "view";
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
# The way this is ordered, if you provide a second arguments, being an array of variables, get_defined_vars() will not be pushed inside the view
|
||||
return "<?php echo \$___class__template->picea->inlineHtml(\$this, $arguments, get_defined_vars()); ?>";
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\ControlStructure;
|
||||
|
||||
class WhileToken extends AbstractLoop {
|
||||
public array $token = [ "do", "while", "endwhile", ];
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
switch($token) {
|
||||
case "do":
|
||||
$context->iterationStack[] = [
|
||||
'or' => false,
|
||||
'token' => "do",
|
||||
];
|
||||
|
||||
return "<?php do { ?>";
|
||||
|
||||
case "while":
|
||||
if ( $context->iterationStack ?? false ) {
|
||||
if ( end($context->iterationStack)['token'] === 'do' ) {
|
||||
array_pop($context->iterationStack);
|
||||
|
||||
return "<?php } while($arguments); ?>";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return parent::parse($context, $arguments, $token);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user