Done #97 + tests

This commit is contained in:
bzick 2014-08-06 01:17:21 +04:00
parent 469833376d
commit 7d59b0e642
4 changed files with 21 additions and 8 deletions

View File

@ -748,13 +748,17 @@ class Compiler
$before = "if(!isset($var)) {\n";
$after = "\n}";
}
if ($tokens->is('=')) { // inline tag {var ...}
if ($tokens->is(Tokenizer::MACRO_EQUALS, '[')) { // inline tag {var ...}
$equal = $tokens->getAndNext();
if($equal == '[') {
$tokens->need(']')->next()->need('=')->next();
$equal = '[]=';
}
$scope->close();
$tokens->next();
if ($tokens->is("[")) {
return $before.$var . '=' . $scope->tpl->parseArray($tokens) . ';'.$after;
return $before.$var . $equal . $scope->tpl->parseArray($tokens) . ';'.$after;
} else {
return $before.$var . '=' . $scope->tpl->parseExpr($tokens) . ';'.$after;
return $before.$var . $equal . $scope->tpl->parseExpr($tokens) . ';'.$after;
}
} else {
$scope["name"] = $var;

View File

@ -693,11 +693,15 @@ class Template extends Render
$cond = false;
}
$op = $tokens->getAndNext();
} elseif ($tokens->is(Tokenizer::MACRO_EQUALS)) { // assignment operator: $a = 4, $a += 3, ...
} elseif ($tokens->is(Tokenizer::MACRO_EQUALS, '[')) { // assignment operator: $a = 4, $a += 3, ...
if (!$var) {
break;
}
$op = $tokens->getAndNext();
if($op == '[') {
$tokens->need(']')->next()->need('=')->next();
$op = '[]=';
}
} elseif ($tokens->is(T_STRING)) { // test or containment operator: $a in $b, $a is set, ...
if (!$exp) {
break;
@ -866,6 +870,9 @@ class Template extends Render
}
$var .= $key;
} elseif ($t === "[") {
if($tokens->isNext(']')) {
break;
}
$tokens->next();
if ($tokens->is(Tokenizer::MACRO_STRING)) {
if ($tokens->isNext("(")) {

View File

@ -241,7 +241,6 @@ class Tokenizer
\T_SR_EQUAL => 1,
\T_XOR_EQUAL => 1,
'=' => 1,
// \T_CONCAT_EQUAL => 1,
),
self::MACRO_SCALAR => array(
\T_LNUMBER => 1,

View File

@ -122,9 +122,11 @@ class TemplateTest extends TestCase
{
return array(
array('hello, {$a.}!', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('hello, {$b[}!', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('hello, {$b.}!', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('hello, {$b[c}!', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('hello, {$b.c]}!', 'Fenom\Error\CompileException', "Unexpected token ']'"),
array('hello, {$b[ ]}!', 'Fenom\Error\CompileException', "Unexpected token ']'"),
array('hello, {$b[ ]}!', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('hello, {$b[9/].c}!', 'Fenom\Error\CompileException', "Unexpected token ']'"),
array('hello, {$b[3]$c}!', 'Fenom\Error\CompileException', "Unexpected token '\$c'"),
array('hello, {$b[3]c}!', 'Fenom\Error\CompileException', "Unexpected token 'c'"),
@ -579,6 +581,7 @@ class TemplateTest extends TestCase
public static function providerArrays()
{
return array(
array('{set $arr[] = 4}', array(4)),
array('{set $arr = []}', array()),
array('{set $arr = [1]}', array(1)),
array('{set $arr = [1,]}', array(1)),
@ -1315,7 +1318,7 @@ class TemplateTest extends TestCase
try {
var_dump(
$this->fenom->compileCode(
'{add $x = 3} {add $x = 9}'
'{add $a[] = 5}'
)->getBody()
);
} catch (\Exception $e) {