- Token name and options are now passed through parse() method

This commit is contained in:
Dave M. 2023-02-02 19:38:40 +00:00
parent e9b2f0ece3
commit b3a8e3aca2
29 changed files with 70 additions and 59 deletions

View File

@ -50,14 +50,15 @@ class Compiler
list($token, $arguments) = array_pad(array_filter(explode(' ', $matches[2], 2), 'strlen'), 2, null); list($token, $arguments) = array_pad(array_filter(explode(' ', $matches[2], 2), 'strlen'), 2, null);
$token = strtolower(trim($token)); $token = strtolower(trim($token));
$tokenName = explode('.', $token, 2)[0]; $tokenName = $context->tokenName($token);
$tokenOptions = $context->tokenOptions($token);
# @TODO Refractor this parts to allows registration to the tag's name # @TODO Refractor this parts to allows registration to the tag's name
if ( $this->tagList[$tokenName] ?? false ) { if ( $this->tagList[$tokenName] ?? false ) {
return $this->tagList[$tokenName]->parse($context, $arguments, $token); return $this->tagList[$tokenName]->parse($context, $arguments, $tokenName, $tokenOptions);
} }
elseif ( $this->extensionList[$tokenName] ?? false ) { elseif ( $this->extensionList[$tokenName] ?? false ) {
return $this->extensionList[$tokenName]->parse($context, $arguments, $token); return $this->extensionList[$tokenName]->parse($context, $arguments, $tokenName, $tokenOptions);
} }
else { else {
throw new \LogicException("Impossible to find token `$tokenName` declared in `{$matches[2]}`. Perhapse you forgot to add a custom token to Picea's engine ?"); throw new \LogicException("Impossible to find token `$tokenName` declared in `{$matches[2]}`. Perhapse you forgot to add a custom token to Picea's engine ?");

View File

@ -95,8 +95,7 @@ abstract class Context {
public function tokenOptions(string $token, bool $export = false) : array|string public function tokenOptions(string $token, bool $export = false) : array|string
{ {
$options = explode('.', strtolower($token)); $options = array_slice(explode('.', strtolower($token)), 1);
array_shift($options);
return $export ? var_export($options, true) : $options; return $export ? var_export($options, true) : $options;
} }

View File

@ -4,7 +4,8 @@ namespace Picea\ControlStructure;
abstract class AbstractLoop implements ControlStructure { abstract class AbstractLoop implements ControlStructure {
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
switch($token) { switch($token) {
case "while": case "while":
case "foreach": case "foreach":

View File

@ -6,7 +6,8 @@ class BlockToken implements ControlStructure {
public array $token = [ "arguments", "block", "endblock", "define", "slot", "endslot", "using" ]; public array $token = [ "arguments", "block", "endblock", "define", "slot", "endslot", "using" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
static $slotDefinitions = []; static $slotDefinitions = [];
switch($token) { switch($token) {

View File

@ -6,7 +6,8 @@ class BreakToken implements ControlStructure {
public string $token = "break"; public string $token = "break";
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
return "<?php break; ?>"; return "<?php break; ?>";
} }

View File

@ -6,7 +6,8 @@ class CaseToken implements ControlStructure {
public string $token = "case"; public string $token = "case";
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
$output = ""; $output = "";
if ( $context->switchStack ) { if ( $context->switchStack ) {

View File

@ -6,7 +6,7 @@ class ContinueToken implements ControlStructure {
public string $token = "continue"; public string $token = "continue";
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
return "<?php continue; ?>"; return "<?php continue; ?>";
} }

View File

@ -6,7 +6,7 @@ class DefaultToken implements ControlStructure {
public string $token = "default"; public string $token = "default";
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
$output = ""; $output = "";
if ( $context->switchStack ) { if ( $context->switchStack ) {

View File

@ -6,7 +6,7 @@ class EchoToken implements ControlStructure {
public array $token = [ "echo", "echo.raw" ]; public array $token = [ "echo", "echo.raw" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
switch($token) { switch($token) {
case "echo": case "echo":
return "<?php \\Picea\\ControlStructure\\EchoToken::echoSafe($arguments) ?>"; return "<?php \\Picea\\ControlStructure\\EchoToken::echoSafe($arguments) ?>";

View File

@ -6,7 +6,7 @@ class ForToken implements ControlStructure {
public array $token = [ "for", "endfor" ]; public array $token = [ "for", "endfor" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
switch($token) { switch($token) {
case "for": case "for":
$uid = "$".uniqid("for_"); $uid = "$".uniqid("for_");

View File

@ -6,7 +6,7 @@ class FunctionToken implements ControlStructure {
public array $token = [ "function", "endfunction", "return" ]; public array $token = [ "function", "endfunction", "return" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
switch($token) { switch($token) {
case "function": case "function":
$context->functions++; $context->functions++;

View File

@ -6,7 +6,7 @@ class IfToken implements ControlStructure {
public array $token = [ "if", "else", "elseif", "endif" ]; public array $token = [ "if", "else", "elseif", "endif" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
switch($token) { switch($token) {
case "if": case "if":
return "<?php if ($arguments): ?>"; return "<?php if ($arguments): ?>";

View File

@ -6,7 +6,7 @@ class NamespaceToken implements ControlStructure {
public string $token = "namespace"; public string $token = "namespace";
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
$context->namespace = $arguments; $context->namespace = $arguments;
return ""; return "";
} }

View File

@ -6,7 +6,7 @@ class OrToken implements ControlStructure {
public string $token = "or"; public string $token = "or";
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
if ( empty($context->iterationStack) ) { if ( empty($context->iterationStack) ) {
throw new \LogicException("Token `or` was used outside of iterator. Make sure your `for`, `foreach`, `while`, `do/until` 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.");
} }

View File

@ -2,19 +2,26 @@
namespace Picea\ControlStructure; namespace Picea\ControlStructure;
enum PrintActionEnum : string {
case prepend = "prepend";
case default = "default";
case append = "append";
# case both = "both";
}
class SectionToken implements ControlStructure { class SectionToken implements ControlStructure {
public array $token = [ "section", "endsection" ]; public array $token = [ "section", "endsection" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) protected PrintActionEnum $action = PrintActionEnum::default;
{
$opt = $context->tokenOptions($token);
if (in_array('prepend', $opt)) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
$context->sectionAction = "prepend"; {
if (in_array('prepend', $options)) {
$this->action = PrintActionEnum::prepend;
} }
elseif (in_array('append', $opt)) { elseif (in_array('append', $options)) {
$context->sectionAction = "append"; $this->action = PrintActionEnum::append;
} }
switch($token) { switch($token) {
@ -47,9 +54,7 @@ class SectionToken implements ControlStructure {
'options' => $options, 'options' => $options,
]; ];
$action = $options['action'] ?? $context->sectionAction ?? "default"; $action = $options['action'] ?? $this->action->value;
unset($context->sectionAction);
if (! in_array($action, ['prepend', 'append', 'default'])) { if (! in_array($action, ['prepend', 'append', 'default'])) {
throw new \RuntimeException("An unsupported action `$action` was given as an option of a {% section %} tag"); throw new \RuntimeException("An unsupported action `$action` was given as an option of a {% section %} tag");

View File

@ -6,7 +6,7 @@ class SwitchToken implements ControlStructure {
public array $token = [ "switch", "case", "endswitch" ]; public array $token = [ "switch", "case", "endswitch" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
switch($token) { switch($token) {
case "switch": case "switch":
$context->switchStack[] = true; $context->switchStack[] = true;

View File

@ -6,7 +6,7 @@ class UseToken implements ControlStructure {
public string $token = "use"; public string $token = "use";
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
$context->useStack[] = $arguments; $context->useStack[] = $arguments;
return ""; return "";
} }

View File

@ -6,7 +6,7 @@ class ViewToken implements ControlStructure {
public string $token = "view"; public string $token = "view";
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
# 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 # 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()); ?>"; return "<?php echo \$___class__template->picea->inlineHtml(\$this, $arguments, get_defined_vars()); ?>";
} }

View File

@ -5,7 +5,7 @@ namespace Picea\ControlStructure;
class WhileToken extends AbstractLoop { class WhileToken extends AbstractLoop {
public array $token = [ "do", "while", "endwhile", ]; public array $token = [ "do", "while", "endwhile", ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
switch($token) { switch($token) {
case "do": case "do":
$context->iterationStack[] = [ $context->iterationStack[] = [

View File

@ -0,0 +1,7 @@
<?php
namespace Picea\Exception;
use Picea\Picea;
class ParseExtensionException extends \Exception {}

View File

@ -3,5 +3,5 @@
namespace Picea\Extension; namespace Picea\Extension;
interface Extension { interface Extension {
public function parse(\Picea\Compiler\Context &$context, string $sourceCode, string $token); public function parse(\Picea\Compiler\Context &$context, string $sourceCode, string $token, array $options = []) : string;
} }

View File

@ -10,17 +10,15 @@ class JsonExtension implements Extension, FunctionExtension {
public int $flags = JSON_HEX_TAG | \JSON_HEX_QUOT | \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_UNICODE; public int $flags = JSON_HEX_TAG | \JSON_HEX_QUOT | \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_UNICODE;
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
$opt = $context->tokenOptions($token);
$flag = $this->flags; $flag = $this->flags;
if ( in_array('pretty', $opt) ) { if ( in_array('pretty', $options) ) {
$flag |= \JSON_PRETTY_PRINT; $flag |= \JSON_PRETTY_PRINT;
} }
if ( in_array('html', $opt) ) { if ( in_array('html', $options) ) {
return "<?php echo htmlentities(json_encode($arguments, $flag), ENT_QUOTES, 'UTF-8') ?>"; return "<?php echo htmlentities(json_encode($arguments, $flag), ENT_QUOTES, 'UTF-8') ?>";
} }

View File

@ -25,10 +25,8 @@ class LanguageExtension implements Extension, FunctionExtension {
]; ];
} }
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
$opt = $context->tokenOptions($token);
switch(explode('.', $token)[0]) { switch(explode('.', $token)[0]) {
case "language": case "language":
$cls = $this::class; $cls = $this::class;
@ -49,14 +47,14 @@ class LanguageExtension implements Extension, FunctionExtension {
PHP; PHP;
case "lang": case "lang":
if ( in_array('raw', $opt) ) { if ( in_array('raw', $options) ) {
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments) ?>"; return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments) ?>";
} }
return "<?php echo htmlspecialchars(\$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>"; return "<?php echo htmlspecialchars(\$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>";
case "_": case "_":
if ( in_array('raw', $opt) ) { if ( in_array('raw', $options) ) {
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments) ?>"; return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments) ?>";
} }

View File

@ -26,7 +26,8 @@ class MoneyExtension implements Extension, FunctionExtension {
]; ];
} }
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
return "<?php echo money($arguments) ?>"; return "<?php echo money($arguments) ?>";
} }

View File

@ -6,7 +6,8 @@ class PhpExtension implements Extension {
public array $token = [ "php" ]; public array $token = [ "php" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
return "<?php $arguments ?>"; return "<?php $arguments ?>";
} }
} }

View File

@ -18,21 +18,19 @@ class PrintExtension implements Extension {
$this->encoding = ini_get("default_charset"); $this->encoding = ini_get("default_charset");
} }
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
$opt = $context->tokenOptions($token); switch($token) {
switch($context->tokenName($token)) {
case 'echo': case 'echo':
case 'print': case 'print':
if (in_array('raw', $opt)) { if (in_array('raw', $options)) {
return "<?php echo $arguments ?>"; return "<?php echo $arguments ?>";
} }
return "<?php echo htmlspecialchars((string) $arguments, {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>"; return "<?php echo htmlspecialchars((string) $arguments, {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
case 'printf': case 'printf':
if (in_array('raw', $opt)) { if (in_array('raw', $options)) {
return "<?php printf((string) $arguments) ?>"; return "<?php printf((string) $arguments) ?>";
} }

View File

@ -15,7 +15,8 @@ class TitleExtension implements Extension, FunctionExtension {
]; ];
} }
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
return <<<PHP return <<<PHP
<?php <?php
if ( null !== \$title = title($arguments) ) { if ( null !== \$title = title($arguments) ) {

View File

@ -29,10 +29,8 @@ PATTERN;
$this->assetToken = $assetToken; $this->assetToken = $assetToken;
} }
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : ?string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
$opt = $context->tokenOptions($token);
switch($token) { switch($token) {
case "asset": case "asset":
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildAssetUrl($arguments) ?>"; return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildAssetUrl($arguments) ?>";
@ -41,10 +39,10 @@ PATTERN;
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildRouteUrl($arguments) ?>"; return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildRouteUrl($arguments) ?>";
case "url": case "url":
if ( in_array('parameters', $opt) ) { if ( in_array('parameters', $options) ) {
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->setUrlParameters($arguments) ?>"; return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->setUrlParameters($arguments) ?>";
} }
elseif ( in_array('current', $opt) ) { elseif ( in_array('current', $options) ) {
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->currentUrl($arguments) ?>"; return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->currentUrl($arguments) ?>";
} }
@ -54,7 +52,7 @@ PATTERN;
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->slug($arguments) ?>"; return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->slug($arguments) ?>";
} }
return null; throw new \ParseExtensionException("Unknown token given $token in UrlExtension.");
} }
public function exportFunctions(): array public function exportFunctions(): array

View File

@ -24,7 +24,7 @@ class Request implements Extension, FunctionExtension {
$this->request = $request; $this->request = $request;
} }
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string { } public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { }
public function exportFunctions(): array public function exportFunctions(): array
{ {