Changeset 315

Show
Ignore:
Timestamp:
11/27/06 17:13:09 (2 years ago)
Author:
laurentj
Message:

preprocesseur : ajout de la prise en charge de #if expression

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/build/lib/preprocessor.lib.php

    r297 r315  
    2020        '#ifxx statement is missing', 
    2121        '#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' 
    2325    ); 
    2426 
    25     public function __construct($sourceFilename, $sourceLine, $code=0) { 
     27    public function __construct($sourceFilename, $sourceLine, $code=0, $param=null) { 
    2628        $this->sourceFilename = $sourceFilename; 
    2729        $this->sourceLine = $sourceLine+1; 
    2830        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); 
    3037    } 
    3138 
     
    6370    const ERR_ENDIF_MISSING = 3; 
    6471    const ERR_INVALID_FILENAME = 4; 
     72    const ERR_EXPR_SYNTAX = 5; 
     73    const ERR_EXPR_SYNTAX_TOK = 6; 
    6574 
    6675    public function __construct(){ 
     
    171180                    $source[$nb]=false; 
    172181                } 
     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; 
    173194 
    174195            }elseif(preg_match('/^\#(endif|else)\s*$/m',$line,$m)){ 
     
    204225                        $path = realpath(dirname($filename).'/'.$path); 
    205226                        if($path == ''){ 
    206                             throw new jExceptionPreProc($filename,$nb,self::ERR_INVALID_FILENAME); 
     227                            throw new jExceptionPreProc($filename,$nb,self::ERR_INVALID_FILENAME, $m[2]); 
    207228                        } 
    208229                    } 
     
    214235                        $this->_variables = $preproc->_variables; 
    215236                    }else{ 
    216                         throw new jExceptionPreProc($filename,$nb,self::ERR_INVALID_FILENAME); 
     237                        throw new jExceptionPreProc($filename,$nb,self::ERR_INVALID_FILENAME,$m[2] ); 
    217238                    } 
    218239                    if($m[1] == 'php'){ 
     
    255276        return $result; 
    256277    } 
     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    } 
    257323} 
    258324 
  • trunk/build/tests/testpreprocess.php

    r297 r315  
    105105           'result7_5.txt'=>array('BAZ'=>true, 'BAR'=>true), 
    106106          ), 
     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        ), 
    107123    ); 
    108124 
     
    154170        'source_err5.txt'=>array(4,'source_err5.txt',7), // err invalid filename 
    155171        '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 
    156174 
    157175    ); 
Download in other formats: Unified Diff Zip Archive