- First commit of first draft

This commit is contained in:
mcndave committed 2019-08-21 16:11:39 -04:00
commit 66c7b349d1
36 files changed
+820

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
<?php
namespace Picea\ControlStructure;
class BreakToken implements ControlStructure {
public string $token = "break";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
return "<?php break; ?>";
}
}
+21
View File
@@ -0,0 +1,21 @@
<?php
namespace Picea\ControlStructure;
class CaseToken implements ControlStructure {
public string $token = "case";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
$output = "";
if ( $context->switchStack ) {
array_pop($context->switchStack);
}
else {
$output = "<?php ";
}
return ( $output ?? "" ) . "case $arguments: ?>";
}
}
@@ -0,0 +1,7 @@
<?php
namespace Picea\ControlStructure;
interface ControlStructure {
public function parse(\Picae\Compiler\Context &$context, string $sourceCode);
}
+21
View File
@@ -0,0 +1,21 @@
<?php
namespace Picea\ControlStructure;
class DefaultToken implements ControlStructure {
public string $token = "default";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
$output = "";
if ( $context->switchStack ) {
array_pop($context->switchStack);
}
else {
$output = "<?php ";
}
return ( $output ?? "" ) . "default: ?>";
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace Picea\ControlStructure;
class ElseIfToken implements ControlStructure {
public string $token = "elseif";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
return "<?php elseif ($arguments): ?>";
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace Picea\ControlStructure;
class ElseToken implements ControlStructure {
public string $token = "else";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
return "<?php else: ?>";
}
}
+7
View File
@@ -0,0 +1,7 @@
<?php
namespace Picea\ControlStructure;
class EndCaseToken extends BreakToken {
public string $token = "endcase";
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace Picea\ControlStructure;
class EndIfToken implements ControlStructure {
public string $token = "endif";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
return "<?php endif ?>";
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Picea\ControlStructure;
class EndSectionToken implements ControlStructure {
public string $token = "endsection";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
if ( empty($context->sections) ) {
throw new \RuntimeException("A section closing tag {% endsection %} was found without an opening {% section %} tag");
}
$section = array_pop($context->sections);
$build = $context->extendFrom ? "" : "\$this->__renderSection({$section['name']});";
return "<?php }]; $build?>";
}
}
+12
View File
@@ -0,0 +1,12 @@
<?php
namespace Picea\ControlStructure;
class EndSwitchToken implements ControlStructure {
public string $token = "endswitch";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
return "<?php endswitch; ?>";
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace Picea\ControlStructure;
class EndforToken implements ControlStructure {
public string $token = "endfor";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
if ( end($context->iterationStack)['or'] === false ) {
$output = "<?php endfor; ?>";
}
else {
$output = "<?php endif; ?>";
}
array_pop($context->iterationStack);
return $output;
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace Picea\ControlStructure;
class EndforeachToken implements ControlStructure {
public string $token = "endforeach";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
if ( end($context->iterationStack)['or'] === false ) {
$output = "<?php endforeach; ?>";
}
else {
$output = "<?php endif; ?>";
}
array_pop($context->iterationStack);
return $output;
}
}
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace Picea\ControlStructure;
class ExtendsToken implements ControlStructure {
public string $token = "extends";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $path) {
# Triming string's quotes
$path = trim($path, "\"\' \t");
$context->extendFrom = $path;
return "";
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace Picea\ControlStructure;
class ForToken implements ControlStructure {
public string $token = "for";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
$uid = "$".uniqid("for_");
$context->iterationStack[] = [
'or' => false,
'uid' => $uid,
'token' => 'endfor',
];
return "<?php for ($arguments): {$uid} = 1; ?>";
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace Picea\ControlStructure;
class ForeachToken implements ControlStructure {
public string $token = "foreach";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
$uid = "$".uniqid("foreach_");
$context->iterationStack[] = [
'or' => false,
'uid' => $uid,
'token' => 'endforeach',
];
return "<?php foreach ($arguments): {$uid} = 1; ?>";
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace Picea\ControlStructure;
class IfToken implements ControlStructure {
public string $token = "if";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
return "<?php if ($arguments): ?>";
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace Picea\ControlStructure;
class NamespaceToken implements ControlStructure {
public string $token = "namespace";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
$context->namespace = $arguments;
return "";
}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace Picea\ControlStructure;
class OrToken implements ControlStructure {
public string $token = "or";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
if ( empty($context->iterationStack) ) {
throw new \LogicException("Token `or` was used outside of iterator. Make sure your `for` or `foreach` declaration are properly made.");
}
$key = count( $context->iterationStack ) - 1;
$context->iterationStack[$key]['or'] = true;
return "<?php {$context->iterationStack[$key]['token']}; if(false === {$context->iterationStack[$key]['uid']} ?? false): ?>";
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace Picea\ControlStructure;
class SectionToken implements ControlStructure {
public string $token = "section";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
list($name, $options) = array_pad(explode(',', $arguments), 2, null);
if ( $options ?? false ) {
$options = eval($options);
}
if ( ! ctype_alnum(str_replace([".", "\"", "'"], "", $name)) ) {
throw new \RuntimeException("Your section named `{$name}` contains invalid character. Allowed are only letters, numbers and dots");
}
$context->sections[] = [
'name' => $name,
'options' => $options,
];
$action = $options['action'] ?? "default";
if (! in_array($action, ['prepend', 'append', 'default'])) {
throw new \RuntimeException("An unsupported action `$action` was given as an option of a {% section %} tag");
}
$order = $options['order'] ?? "count(\$this->__section_list[$name]['$action'])";
return "<?php \$this->__section_list[$name] ??= [ 'prepend' => [], 'append' => [], 'default' => [] ];
\$this->__section_list[$name]['$action'][] = [ 'order' => $order, 'callback' => function() {
extract(\$this->__variables_list); ?>";
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace Picea\ControlStructure;
class SwitchToken implements ControlStructure {
public string $token = "switch";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
$context->switchStack[] = true;
return "<?php switch($arguments):";
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace Picea\ControlStructure;
class UseToken implements ControlStructure {
public string $token = "use";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
$context->useStack[] = $arguments;
return "";
}
}