From b703e05d9154845b6e5c6f1bf20b0a42df7c3613 Mon Sep 17 00:00:00 2001 From: xue <> Date: Sun, 29 Jan 2006 00:46:56 +0000 Subject: Enhanced error report for template parser. --- framework/Web/UI/TTemplateManager.php | 296 ++++++++++++++-------------------- 1 file changed, 122 insertions(+), 174 deletions(-) (limited to 'framework/Web') diff --git a/framework/Web/UI/TTemplateManager.php b/framework/Web/UI/TTemplateManager.php index 5bf0d4bb..3c522897 100644 --- a/framework/Web/UI/TTemplateManager.php +++ b/framework/Web/UI/TTemplateManager.php @@ -430,208 +430,156 @@ class TTemplate extends TComponent implements ITemplate $textStart=0; $stack=array(); $container=-1; + $matchEnd=0; $c=0; - for($i=0;$i<$n;++$i) + try { - $match=&$matches[$i]; - $str=$match[0][0]; - $matchStart=$match[0][1]; - $matchEnd=$matchStart+strlen($str)-1; - if(strpos($str,'$textStart) - $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart)); - $textStart=$matchEnd+1; - $type=$match[1][0]; - $attributes=$this->parseAttributes($match[2][0]); - try + $match=&$matches[$i]; + $str=$match[0][0]; + $matchStart=$match[0][1]; + $matchEnd=$matchStart+strlen($str)-1; + if(strpos($str,'$textStart) + $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart)); + $textStart=$matchEnd+1; + $type=$match[1][0]; + $attributes=$this->parseAttributes($match[2][0]); $this->validateAttributes($type,$attributes); + $tpl[$c++]=array($container,$type,$attributes); + if($str[strlen($str)-2]!=='/') // open tag + { + array_push($stack,$type); + $container=$c-1; + } } - catch(Exception $e) - { - $line=count(explode("\n",substr($input,0,$matchEnd+1))); - if($this->_tplFile===null) - throw new TConfigurationException('template_componenttag_invalid',"Line $line",$type,$e->getMessage()); - else - throw new TConfigurationException('template_componenttag_invalid',"{$this->_tplFile} (Line $line)",$type,$e->getMessage()); - } - $tpl[$c++]=array($container,$type,$attributes); - if($str[strlen($str)-2]!=='/') // open tag + else if(strpos($str,'$textStart) - $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart)); - $textStart=$matchEnd+1; - $type=$match[1][0]; + if($expectPropEnd) + continue; + if($matchStart>$textStart) + $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart)); + $textStart=$matchEnd+1; + $type=$match[1][0]; - if(empty($stack)) - { - $line=count(explode("\n",substr($input,0,$matchEnd+1))); - if($this->_tplFile===null) - throw new TConfigurationException('template_closingtag_unexpected',"Line $line",""); - else - throw new TConfigurationException('template_closingtag_unexpected',"{$this->_tplFile} (Line $line)",""); - } + if(empty($stack)) + throw new TConfigurationException('template_closingtag_unexpected',""); - $name=array_pop($stack); - if($name!==$type) - { - if($name[0]==='@') - $tag=''; - else - $tag=''; - $line=count(explode("\n",substr($input,0,$matchEnd+1))); - if($this->_tplFile===null) - throw new TConfigurationException('template_closingtag_expected',"Line $line",$tag); - else - throw new TConfigurationException('template_closingtag_expected',"{$this->_tplFile} (Line $line)",$tag); - } - $container=$tpl[$container][0]; - } - else if(strpos($str,'<%@')===0) // directive - { - if($expectPropEnd) - continue; - if($matchStart>$textStart) - $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart)); - $textStart=$matchEnd+1; - if(isset($tpl[0])) - { - $line=count(explode("\n",substr($input,0,$matchEnd+1))); - if($this->_tplFile===null) - throw new TConfigurationException('template_directive_nonunique',"Line $line"); - else - throw new TConfigurationException('template_directive_nonunique',"{$this->_tplFile} (Line $line)"); + $name=array_pop($stack); + if($name!==$type) + { + $tag=$name[0]==='@' ? '' : ""; + throw new TConfigurationException('template_closingtag_expected',$tag); + } + $container=$tpl[$container][0]; } - $this->_directive=$this->parseAttributes($match[4][0]); - } - else if(strpos($str,'<%')===0) // expression - { - if($expectPropEnd) - continue; - if($matchStart>$textStart) - $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart)); - $textStart=$matchEnd+1; - if($str[2]==='=') // expression - $tpl[$c++]=array($container,'TExpression',array('Expression'=>THttpUtility::htmlDecode($match[5][0]))); - else if($str[2]==='%') // statements - $tpl[$c++]=array($container,'TStatements',array('Statements'=>THttpUtility::htmlDecode($match[5][0]))); - else - $tpl[$c++]=array($container,'TLiteral',array('Text'=>$this->parseAttribute($str))); - } - else if(strpos($str,'$textStart) $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart)); $textStart=$matchEnd+1; - $expectPropEnd=true; + if(isset($tpl[0])) + throw new TConfigurationException('template_directive_nonunique'); + $this->_directive=$this->parseAttributes($match[4][0]); } - } - else if(strpos($str,'_tplFile===null) - throw new TConfigurationException('template_closingtag_unexpected',"Line $line",""); + if($expectPropEnd) + continue; + if($matchStart>$textStart) + $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart)); + $textStart=$matchEnd+1; + if($str[2]==='=') // expression + $tpl[$c++]=array($container,'TExpression',array('Expression'=>THttpUtility::htmlDecode($match[5][0]))); + else if($str[2]==='%') // statements + $tpl[$c++]=array($container,'TStatements',array('Statements'=>THttpUtility::htmlDecode($match[5][0]))); else - throw new TConfigurationException('template_closingtag_unexpected',"{$this->_tplFile} (Line $line)",""); + $tpl[$c++]=array($container,'TLiteral',array('Text'=>$this->parseAttribute($str))); } - $name=array_pop($stack); - if($name!=='@'.$prop) + else if(strpos($str,''; - else - $tag=''; - $line=count(explode("\n",substr($input,0,$matchEnd+1))); - if($this->_tplFile===null) - throw new TConfigurationException('template_closingtag_expected',"Line $line",$tag); - else - throw new TConfigurationException('template_closingtag_expected',"{$this->_tplFile} (Line $line)",$tag); + $prop=strtolower($match[3][0]); + array_push($stack,'@'.$prop); + if(!$expectPropEnd) + { + if($matchStart>$textStart) + $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart)); + $textStart=$matchEnd+1; + $expectPropEnd=true; + } } - if(($last=count($stack))<1 || $stack[$last-1][0]!=='@') + else if(strpos($str,'$textStart && $container>=0) + $prop=strtolower($match[3][0]); + if(empty($stack)) + { + $line=count(explode("\n",substr($input,0,$matchEnd+1))); + if($this->_tplFile===null) + throw new TConfigurationException('template_closingtag_unexpected',"Line $line",""); + else + throw new TConfigurationException('template_closingtag_unexpected',"{$this->_tplFile} (Line $line)",""); + } + $name=array_pop($stack); + if($name!=='@'.$prop) { - $value=substr($input,$textStart,$matchStart-$textStart); - $value=$this->parseAttribute($value); - $type=$tpl[$container][1]; - try + $tag=$name[0]==='@' ? '' : ""; + throw new TConfigurationException('template_closingtag_expected',$tag); + } + if(($last=count($stack))<1 || $stack[$last-1][0]!=='@') + { + if($matchStart>$textStart && $container>=0) { + $value=substr($input,$textStart,$matchStart-$textStart); + $value=$this->parseAttribute($value); + $type=$tpl[$container][1]; $this->validateAttributes($type,array($prop=>$value)); + $tpl[$container][2][$prop]=$value; + $textStart=$matchEnd+1; } - catch(Exception $e) - { - $line=count(explode("\n",substr($input,0,$matchEnd+1))); - if($this->_tplFile===null) - throw new TConfigurationException('template_componenttag_invalid',"Line $line",$type,$e->getMessage()); - else - throw new TConfigurationException('template_componenttag_invalid',"{$this->_tplFile} (Line $line)",$type,$e->getMessage()); - } - $tpl[$container][2][$prop]=$value; - $textStart=$matchEnd+1; + $expectPropEnd=false; } - $expectPropEnd=false; } - } - else if(strpos($str,'