Changeset 315
- Timestamp:
- 11/27/06 17:13:09 (2 years ago)
- Files:
-
- trunk/build/lib/preprocessor.lib.php (modified) (6 diffs)
- trunk/build/tests/ppdatas/source_if1.txt (added)
- trunk/build/tests/ppdatas/source_if2.txt (added)
- trunk/build/tests/ppdatas/source_if3.txt (added)
- trunk/build/tests/ppdatas/source_if4.txt (added)
- trunk/build/tests/ppdatas/source_if_err1.txt (added)
- trunk/build/tests/ppdatas/source_if_err2.txt (added)
- trunk/build/tests/testpreprocess.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/build/lib/preprocessor.lib.php
r297 r315 20 20 '#ifxx statement is missing', 21 21 '#endif statement is missing', 22 'cannot include file', 22 'Cannot include file %s', 23 'Syntax error in the expression : %s', 24 'Syntax error in an expression : "%s" is not allowed' 23 25 ); 24 26 25 public function __construct($sourceFilename, $sourceLine, $code=0 ) {27 public function __construct($sourceFilename, $sourceLine, $code=0, $param=null) { 26 28 $this->sourceFilename = $sourceFilename; 27 29 $this->sourceLine = $sourceLine+1; 28 30 if($code > count($this->errmessages)) $code = 0; 29 parent::__construct($this->errmessages[$code], $code); 31 if($param != null){ 32 $err = sprintf($this->errmessages[$code], $param); 33 }else{ 34 $err = $this->errmessages[$code]; 35 } 36 parent::__construct($err, $code); 30 37 } 31 38 … … 63 70 const ERR_ENDIF_MISSING = 3; 64 71 const ERR_INVALID_FILENAME = 4; 72 const ERR_EXPR_SYNTAX = 5; 73 const ERR_EXPR_SYNTAX_TOK = 6; 65 74 66 75 public function __construct(){ … … 171 180 $source[$nb]=false; 172 181 } 182 }elseif(preg_match('/^\#if\s(.*)$/m',$line,$m)){ 183 if( !$isOpen ){ 184 array_push($this->_blockstack, self::BLOCK_IF_NO); 185 }else{ 186 $val = $this->evalExpression($m[1], $filename,$nb); 187 if($val){ 188 array_push($this->_blockstack, self::BLOCK_IF_YES); 189 }else{ 190 array_push($this->_blockstack, self::BLOCK_IF_NO); 191 } 192 } 193 $source[$nb]=false; 173 194 174 195 }elseif(preg_match('/^\#(endif|else)\s*$/m',$line,$m)){ … … 204 225 $path = realpath(dirname($filename).'/'.$path); 205 226 if($path == ''){ 206 throw new jExceptionPreProc($filename,$nb,self::ERR_INVALID_FILENAME );227 throw new jExceptionPreProc($filename,$nb,self::ERR_INVALID_FILENAME, $m[2]); 207 228 } 208 229 } … … 214 235 $this->_variables = $preproc->_variables; 215 236 }else{ 216 throw new jExceptionPreProc($filename,$nb,self::ERR_INVALID_FILENAME );237 throw new jExceptionPreProc($filename,$nb,self::ERR_INVALID_FILENAME,$m[2] ); 217 238 } 218 239 if($m[1] == 'php'){ … … 255 276 return $result; 256 277 } 278 279 protected $authorizedToken=array(T_DNUMBER, T_BOOLEAN_AND, T_BOOLEAN_OR, T_CHARACTER, 280 T_IS_EQUAL,T_IS_GREATER_OR_EQUAL,T_IS_IDENTICAL,T_IS_NOT_EQUAL,T_IS_NOT_IDENTICAL, 281 T_IS_SMALLER_OR_EQUAL, T_LOGICAL_AND, T_LOGICAL_OR, T_LOGICAL_XOR, T_LNUMBER, 282 T_CONSTANT_ENCAPSED_STRING, T_WHITESPACE); 283 284 protected $authorizedChar = array('.', '+', '-', '/', '*','<','>', '!'); 285 286 protected function evalExpression($expression, $filename,$nb){ 287 288 $arr = token_get_all('<?php '.$expression.' ?>'); 289 $expr =''; 290 foreach($arr as $k=>$c){ 291 if(is_array($c)){ 292 if($c[0] == T_OPEN_TAG){ 293 if($k != 0) throw new jExceptionPreProc($filename,$nb,self::ERR_EXPR_SYNTAX_TOK, $c[1]); 294 }elseif($c[0] == T_CLOSE_TAG){ 295 if($k != count($arr) -1) throw new jExceptionPreProc($filename,$nb,self::ERR_EXPR_SYNTAX_TOK, $c[1]); 296 }elseif($c[0] == T_STRING){ 297 if(isset($this->_variables[$c[1]])) 298 $expr.='$this->_variables[\''.$c[1].'\']'; 299 else 300 $expr.='""'; 301 }elseif(in_array($c[0], $this->authorizedToken)){ 302 $expr .= $c[1]; 303 }else{ 304 throw new jExceptionPreProc($filename,$nb,self::ERR_EXPR_SYNTAX_TOK, $c[1]); 305 } 306 }else{ 307 if(in_array($c, $this->authorizedChar)){ 308 $expr .= $c; 309 }else{ 310 throw new jExceptionPreProc($filename,$nb,self::ERR_EXPR_SYNTAX_TOK, $c); 311 } 312 } 313 } 314 315 $val = null; 316 317 if(false === @eval('$val='.$expr.';')){ 318 throw new jExceptionPreProc($filename,$nb,self::ERR_EXPR_SYNTAX, $expression); 319 }else{ 320 return $val; 321 } 322 } 257 323 } 258 324 trunk/build/tests/testpreprocess.php
r297 r315 105 105 'result7_5.txt'=>array('BAZ'=>true, 'BAR'=>true), 106 106 ), 107 'source_if1.txt'=>array( 108 'result2_1.txt'=>array('FOO'=>''), 109 'result2_2.txt'=>array('FOO'=>true), 110 ), 111 'source_if2.txt'=>array( 112 'result2_1.txt'=>array('FOO'=>''), 113 'result2_2.txt'=>array('FOO'=>14), 114 ), 115 'source_if3.txt'=>array( 116 'result2_1.txt'=>array('FOO'=>'', 'BAR'=>'toto'), 117 'result2_2.txt'=>array('FOO'=>'toto', 'BAR'=>'toto'), 118 ), 119 'source_if4.txt'=>array( 120 'result2_1.txt'=>array('FOO'=>true), 121 'result2_2.txt'=>array('FOO'=>false), 122 ), 107 123 ); 108 124 … … 154 170 'source_err5.txt'=>array(4,'source_err5.txt',7), // err invalid filename 155 171 'source_err6.txt'=>array(4,'subdir/inc_err.txt',11), // err invalid filename 172 'source_if_err1.txt'=>array(5,'source_if_err1.txt',5), // err syntax err expression 173 'source_if_err2.txt'=>array(6,'source_if_err2.txt',5), // err syntax err expression tok 156 174 157 175 );
