Arbit - project tracking

Dwoo

History

Diff

b8ab7d f9e789 a/CHANGELOG
30 30 to use them even as string.
31 31 * Fixed parsing bug with method calls used as arguments with a comma
32 32 following.
33 +* Fixed parsing of function/constants that start with an underscore,
34 + thanks to Dominik del Bondio for the patch
33 35
34 36 [2010-02-07] 1.1.1
35 37 + Added {optional} plugin that just prints an optional var without any
538 540 although the API to register them is already in.
539 541
540 542 [2008-02-08] 0.1.0
541 -Initial release
543 +Initial release
b8ab7d f9e789 a/lib/Dwoo/Compiler.php
1355 1355 // var
1356 1356 $out = $this->parseVar($in, $from, $to, $parsingParams, $curBlock, $pointer);
1357 1357 $parsed = 'var';
1358 - } elseif ($first==='%' && preg_match('#^%[a-z]#i', $substr)) {
1358 + } elseif ($first==='%' && preg_match('#^%[a-z_]#i', $substr)) {
1359 1359 // const
1360 1360 $out = $this->parseConst($in, $from, $to, $parsingParams, $curBlock, $pointer);
1361 1361 } elseif (($first==='"' || $first==="'") && !(is_array($parsingParams) && preg_match('#^([\'"])[a-z0-9_]+\1\s*=>?(?:\s+|[^=])#i', $substr))) {
1362 1362 // string
1363 1363 $out = $this->parseString($in, $from, $to, $parsingParams, $curBlock, $pointer);
1364 - } 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)) {
1364 + } 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)) {
1365 1365 // func
1366 1366 $out = $this->parseFunction($in, $from, $to, $parsingParams, $curBlock, $pointer);
1367 1367 $parsed = 'func';
1372 1372 $pointer++;
1373 1373 }
1374 1374 return $this->parse($in, $from+1, $to, false, 'root', $pointer);
1375 - } elseif ($curBlock === 'root' && preg_match('#^/([a-z][a-z0-9_]*)?#i', $substr, $match)) {
1375 + } elseif ($curBlock === 'root' && preg_match('#^/([a-z_][a-z0-9_]*)?#i', $substr, $match)) {
1376 1376 // close block
1377 1377 if (!empty($match[1]) && $match[1] == 'else') {
1378 1378 throw new Dwoo_Compilation_Exception($this, 'Else blocks must not be closed explicitly, they are automatically closed when their parent block is closed');
1537 1537 }
1538 1538
1539 1539 // func parsed, check if any func-extension applies
1540 - if ($parsed==='func' && preg_match('#^->[a-z0-9_]+(\s*\(.+|->[a-z].*)?#is', $substr, $match)) {
1540 + if ($parsed==='func' && preg_match('#^->[a-z0-9_]+(\s*\(.+|->[a-z_].*)?#is', $substr, $match)) {
1541 1541 // parse method call or property read
1542 1542 $ptr = 0;
1543 1543
1574 1574 protected function parseFunction($in, $from, $to, $parsingParams = false, $curBlock='', &$pointer = null)
1575 1575 {
1576 1576 $cmdstr = substr($in, $from, $to-$from);
1577 - preg_match('/^([a-z][a-z0-9_]*(?:::[a-z][a-z0-9_]*)?)(\s*'.$this->rdr.'|\s*;)?/i', $cmdstr, $match);
1577 + preg_match('/^([a-z_][a-z0-9_]*(?:::[a-z_][a-z0-9_]*)?)(\s*'.$this->rdr.'|\s*;)?/i', $cmdstr, $match);
1578 1578
1579 1579 if (empty($match[1])) {
1580 1580 throw new Dwoo_Compilation_Exception($this, 'Parse error, invalid function name : '.substr($cmdstr, 0, 15));
2688 2688 }
2689 2689 $cmdstr = $cmdstrsrc;
2690 2690 $paramsep = ':';
2691 - if (!preg_match('/^(@{0,2}[a-z][a-z0-9_]*)(:)?/i', $cmdstr, $match)) {
2691 + if (!preg_match('/^(@{0,2}[a-z_][a-z0-9_]*)(:)?/i', $cmdstr, $match)) {
2692 2692 throw new Dwoo_Compilation_Exception($this, 'Invalid modifier name, started with : '.substr($cmdstr, 0, 10));
2693 2693 }
2694 2694 $paramspos = !empty($match[2]) ? strlen($match[1]) : false;
b8ab7d f9e789 a/tests/CompilerTests.php
318 318
319 319 public function testMethodCalls()
320 320 {
321 - $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}');
321 + $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)}');
322 322 $tpl->forceCompilation();
323 323
324 324 $a = new MethodCallsHelper();
325 - $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));
325 + $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));
326 326 }
327 327
328 328 public function testLooseTagHandling()
712 712 $tpl->forceCompilation();
713 713 $this->assertEquals('testtest', $this->dwoo->get($tpl, array('obj'=>new PluginHelper()), $this->compiler));
714 714 }
715 +
716 + public function testFunctionCanStartWithUnderscore()
717 + {
718 + $tpl = new Dwoo_Template_String('{_underscoreHelper("test", _underscoreHelper("bar", 10))|_underscoreModifierHelper}');
719 + $tpl->forceCompilation();
720 + $this->assertEquals('_--10bar-test-_', $this->dwoo->get($tpl, array(), $this->compiler));
721 + }
722 +
715 723 }
716 724
717 725 function excessArgsHelper($a) {
719 727 return implode(':', $args);
720 728 }
721 729
730 +function _underscoreHelper($foo, $bar) {
731 + return "-$bar$foo-";
732 +}
733 +
734 +function _underscoreModifierHelper($value) {
735 + return "_${value}_";
736 +}
737 +
722 738 class StaticHelper {
723 739 static $foo = 33;
724 740 }
822 838 public static function staticFoo($bar, $baz) {
823 839 return "-$baz$bar-";
824 840 }
841 +
842 + public function _foo($bar, $baz) {
843 + return "-$baz$bar-";
844 + }
845 +
846 + public function _fooChain() {
847 + return $this;
848 + }
825 849 }