Commit f9e78994 by Jordi Boggiano

Fixed parsing of function/constants that start with an underscore, thanks to…

Fixed parsing of function/constants that start with an underscore, thanks to Dominik del Bondio for the patch Fixes #68
parent b8ab7d48
...@@ -29,6 +29,8 @@ ...@@ -29,6 +29,8 @@
to use them even as string. to use them even as string.
* Fixed parsing bug with method calls used as arguments with a comma * Fixed parsing bug with method calls used as arguments with a comma
following. following.
* Fixed parsing of function/constants that start with an underscore,
thanks to Dominik del Bondio for the patch
[2010-02-07] 1.1.1 [2010-02-07] 1.1.1
+ Added {optional} plugin that just prints an optional var without any + Added {optional} plugin that just prints an optional var without any
......
...@@ -1354,13 +1354,13 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -1354,13 +1354,13 @@ class Dwoo_Compiler implements Dwoo_ICompiler
// var // var
$out = $this->parseVar($in, $from, $to, $parsingParams, $curBlock, $pointer); $out = $this->parseVar($in, $from, $to, $parsingParams, $curBlock, $pointer);
$parsed = 'var'; $parsed = 'var';
} elseif ($first==='%' && preg_match('#^%[a-z]#i', $substr)) { } elseif ($first==='%' && preg_match('#^%[a-z_]#i', $substr)) {
// const // const
$out = $this->parseConst($in, $from, $to, $parsingParams, $curBlock, $pointer); $out = $this->parseConst($in, $from, $to, $parsingParams, $curBlock, $pointer);
} elseif (($first==='"' || $first==="'") && !(is_array($parsingParams) && preg_match('#^([\'"])[a-z0-9_]+\1\s*=>?(?:\s+|[^=])#i', $substr))) { } elseif (($first==='"' || $first==="'") && !(is_array($parsingParams) && preg_match('#^([\'"])[a-z0-9_]+\1\s*=>?(?:\s+|[^=])#i', $substr))) {
// string // string
$out = $this->parseString($in, $from, $to, $parsingParams, $curBlock, $pointer); $out = $this->parseString($in, $from, $to, $parsingParams, $curBlock, $pointer);
} elseif (preg_match('/^[a-z][a-z0-9_]*(?:::[a-z][a-z0-9_]*)?('.(is_array($parsingParams)||$curBlock!='root'?'':'\s+[^(]|').'\s*\(|\s*'.$this->rdr.'|\s*;)/i', $substr)) { } elseif (preg_match('/^[a-z_][a-z0-9_]*(?:::[a-z_][a-z0-9_]*)?('.(is_array($parsingParams)||$curBlock!='root'?'':'\s+[^(]|').'\s*\(|\s*'.$this->rdr.'|\s*;)/i', $substr)) {
// func // func
$out = $this->parseFunction($in, $from, $to, $parsingParams, $curBlock, $pointer); $out = $this->parseFunction($in, $from, $to, $parsingParams, $curBlock, $pointer);
$parsed = 'func'; $parsed = 'func';
...@@ -1371,7 +1371,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -1371,7 +1371,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
$pointer++; $pointer++;
} }
return $this->parse($in, $from+1, $to, false, 'root', $pointer); return $this->parse($in, $from+1, $to, false, 'root', $pointer);
} elseif ($curBlock === 'root' && preg_match('#^/([a-z][a-z0-9_]*)?#i', $substr, $match)) { } elseif ($curBlock === 'root' && preg_match('#^/([a-z_][a-z0-9_]*)?#i', $substr, $match)) {
// close block // close block
if (!empty($match[1]) && $match[1] == 'else') { if (!empty($match[1]) && $match[1] == 'else') {
throw new Dwoo_Compilation_Exception($this, 'Else blocks must not be closed explicitly, they are automatically closed when their parent block is closed'); throw new Dwoo_Compilation_Exception($this, 'Else blocks must not be closed explicitly, they are automatically closed when their parent block is closed');
...@@ -1536,7 +1536,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -1536,7 +1536,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
} }
// func parsed, check if any func-extension applies // func parsed, check if any func-extension applies
if ($parsed==='func' && preg_match('#^->[a-z0-9_]+(\s*\(.+|->[a-z].*)?#is', $substr, $match)) { if ($parsed==='func' && preg_match('#^->[a-z0-9_]+(\s*\(.+|->[a-z_].*)?#is', $substr, $match)) {
// parse method call or property read // parse method call or property read
$ptr = 0; $ptr = 0;
...@@ -1573,7 +1573,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -1573,7 +1573,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
protected function parseFunction($in, $from, $to, $parsingParams = false, $curBlock='', &$pointer = null) protected function parseFunction($in, $from, $to, $parsingParams = false, $curBlock='', &$pointer = null)
{ {
$cmdstr = substr($in, $from, $to-$from); $cmdstr = substr($in, $from, $to-$from);
preg_match('/^([a-z][a-z0-9_]*(?:::[a-z][a-z0-9_]*)?)(\s*'.$this->rdr.'|\s*;)?/i', $cmdstr, $match); preg_match('/^([a-z_][a-z0-9_]*(?:::[a-z_][a-z0-9_]*)?)(\s*'.$this->rdr.'|\s*;)?/i', $cmdstr, $match);
if (empty($match[1])) { if (empty($match[1])) {
throw new Dwoo_Compilation_Exception($this, 'Parse error, invalid function name : '.substr($cmdstr, 0, 15)); throw new Dwoo_Compilation_Exception($this, 'Parse error, invalid function name : '.substr($cmdstr, 0, 15));
...@@ -2687,7 +2687,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -2687,7 +2687,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
} }
$cmdstr = $cmdstrsrc; $cmdstr = $cmdstrsrc;
$paramsep = ':'; $paramsep = ':';
if (!preg_match('/^(@{0,2}[a-z][a-z0-9_]*)(:)?/i', $cmdstr, $match)) { if (!preg_match('/^(@{0,2}[a-z_][a-z0-9_]*)(:)?/i', $cmdstr, $match)) {
throw new Dwoo_Compilation_Exception($this, 'Invalid modifier name, started with : '.substr($cmdstr, 0, 10)); throw new Dwoo_Compilation_Exception($this, 'Invalid modifier name, started with : '.substr($cmdstr, 0, 10));
} }
$paramspos = !empty($match[2]) ? strlen($match[1]) : false; $paramspos = !empty($match[2]) ? strlen($match[1]) : false;
......
...@@ -317,11 +317,11 @@ replace="BAR" ...@@ -317,11 +317,11 @@ replace="BAR"
public function testMethodCalls() public function testMethodCalls()
{ {
$tpl = new Dwoo_Template_String('{$a} {$a->foo()} {$b[$c]->foo()} {$a->bar()+$a->bar()} {$a->baz(5, $foo)} {$a->make(5)->getInt()} {$a->make(5)->getInt()/2}'); $tpl = new Dwoo_Template_String('{$a} {$a->foo()} {$b[$c]->foo()} {$a->bar()+$a->bar()} {$a->baz(5, $foo)} {$a->make(5)->getInt()} {$a->make(5)->getInt()/2} {$a->_foo($foo, 5)} {$a->_fooChain()->_foo(5, $foo)}');
$tpl->forceCompilation(); $tpl->forceCompilation();
$a = new MethodCallsHelper(); $a = new MethodCallsHelper();
$this->assertEquals('obj 0 1 7 10bar 5 2.5', $this->dwoo->get($tpl, array('a'=>$a, 'b'=>array('test'=>$a), 'c'=>'test', 'foo'=>'bar'), $this->compiler)); $this->assertEquals('obj 0 1 7 10bar 5 2.5 -5bar- -bar5-', $this->dwoo->get($tpl, array('a'=>$a, 'b'=>array('test'=>$a), 'c'=>'test', 'foo'=>'bar'), $this->compiler));
} }
public function testLooseTagHandling() public function testLooseTagHandling()
...@@ -711,6 +711,14 @@ fail ...@@ -711,6 +711,14 @@ fail
$tpl->forceCompilation(); $tpl->forceCompilation();
$this->assertEquals('testtest', $this->dwoo->get($tpl, array('obj'=>new PluginHelper()), $this->compiler)); $this->assertEquals('testtest', $this->dwoo->get($tpl, array('obj'=>new PluginHelper()), $this->compiler));
} }
public function testFunctionCanStartWithUnderscore()
{
$tpl = new Dwoo_Template_String('{_underscoreHelper("test", _underscoreHelper("bar", 10))|_underscoreModifierHelper}');
$tpl->forceCompilation();
$this->assertEquals('_--10bar-test-_', $this->dwoo->get($tpl, array(), $this->compiler));
}
} }
function excessArgsHelper($a) { function excessArgsHelper($a) {
...@@ -718,6 +726,14 @@ function excessArgsHelper($a) { ...@@ -718,6 +726,14 @@ function excessArgsHelper($a) {
return implode(':', $args); return implode(':', $args);
} }
function _underscoreHelper($foo, $bar) {
return "-$bar$foo-";
}
function _underscoreModifierHelper($value) {
return "_${value}_";
}
class StaticHelper { class StaticHelper {
static $foo = 33; static $foo = 33;
} }
...@@ -821,4 +837,12 @@ class MethodCallsHelper ...@@ -821,4 +837,12 @@ class MethodCallsHelper
public static function staticFoo($bar, $baz) { public static function staticFoo($bar, $baz) {
return "-$baz$bar-"; return "-$baz$bar-";
} }
public function _foo($bar, $baz) {
return "-$baz$bar-";
}
public function _fooChain() {
return $this;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment