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"; $before = "if(!isset($var)) {\n";
$after = "\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(); $scope->close();
$tokens->next();
if ($tokens->is("[")) { if ($tokens->is("[")) {
return $before.$var . '=' . $scope->tpl->parseArray($tokens) . ';'.$after; return $before.$var . $equal . $scope->tpl->parseArray($tokens) . ';'.$after;
} else { } else {
return $before.$var . '=' . $scope->tpl->parseExpr($tokens) . ';'.$after; return $before.$var . $equal . $scope->tpl->parseExpr($tokens) . ';'.$after;
} }
} else { } else {
$scope["name"] = $var; $scope["name"] = $var;

View File

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

View File

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

View File

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