Changeset 228

Show
Ignore:
Timestamp:
08/02/06 12:19:44 (2 years ago)
Author:
laurentj
Message:

Amélioration du préprocesseur : ajout de #define et #undef, run() est changé en parseFile(), fix bug : #expand mot ne renvoyait rien

Files:

Legend:

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

    r165 r228  
    9898 
    9999        $preproc->setVars($_SERVER); 
    100         $contents = $preproc->run(file_get_contents($sourcedir.$currentsrcdir.$m[2])); 
     100        $contents = $preproc->parseFile($sourcedir.$currentsrcdir.$m[2]); 
    101101        if($contents===false){ 
    102102            echo "$ficlist : line $nbline, cannot process file ".$m[2]." (error code=".$preproc->errorCode."  line=".$preproc->errorLine.")\n"; 
  • trunk/build/preprocess.php

    r165 r228  
    5555 
    5656 
    57 $source = file_get_contents($sourcefile); 
    58  
    5957$proc = new jPreProcessor(); 
    6058$proc->setVars($_SERVER); 
    61 $dist = $proc->run($source); 
     59$dist = $proc->parseFile($sourcefile); 
    6260 
    6361 
  • trunk/build/preprocessor.lib.php

    r82 r228  
    3232  const ERR_SYNTAX     = 1; 
    3333  const ERR_IF_MISSING = 2; 
     34  const ERR_ENDIF_MISSING = 2; 
    3435 
    3536 
     
    5051  } 
    5152 
    52   public function run($sourceContent){ 
     53  public function parseFile($filename){ 
    5354   $this->errorCode=0; 
    5455   $this->errorLine=0; 
     56   $this->_blockstack = array(); 
     57 
     58   // on sauve les variables pour les retrouver intact apr�le parsing 
     59   // de fa� �ouvoir r�ecuter plusieurs fois run sur des contenus diff�nts 
    5560   $this->_savedVariables= $this->_variables; 
    56    $source =explode("\n",$sourceContent); 
     61 
     62   $source =explode("\n",file_get_contents($filename)); 
    5763 
    5864   $result=''; 
     65   // on parcours chaque ligne du source 
    5966   foreach($source as $nb=>$line){ 
    60      if(preg_match('/^\#(ifdef|expand|define|ifndef|elifdef)\s+([^\s]*)\s*$/m',$line,$m)){ 
     67     $isOpen = !(end($this->_blockstack) & self::BLOCK_NO); 
     68 
     69     if(preg_match('/^\#(ifdef|define|ifndef|elifdef|undef)\s+(\w+)\s*$/m',$line,$m)){ 
    6170       switch($m[1]){ 
    6271           case 'ifdef': 
    63               $end = end($this->_blockstack); 
    64               if( $end &  self::BLOCK_NO ){ 
     72              if( !$isOpen ){ 
    6573                   array_push($this->_blockstack, self::BLOCK_IF_NO); 
    6674              }else{ 
     
    7381              $source[$nb]=false; 
    7482              break; 
    75            case 'define': 
    76               /*if(preg_match('/^(\w+)(?:\s+(\w+))?$/m',$m[2],$m2)){ 
    77  
    78               }else{ 
    79  
    80  
    81               }*/ 
     83           case 'define': // define avec un seul argument. 
     84              if($isOpen ){ 
     85                $this->_variables[$m[2]] = ''; 
     86              } 
     87              $source[$nb]=false; 
     88              break; 
     89           case 'undef': 
     90              if($isOpen ){ 
     91                unset($this->_variables[$m[2]]); 
     92              } 
    8293              $source[$nb]=false; 
    8394              break; 
    8495           case 'ifndef': 
    85               if(end($this->_blockstack) &  self::BLOCK_NO){ 
     96              if(!$isOpen){ 
    8697                   array_push($this->_blockstack, self::BLOCK_IF_NO); 
    8798              }else{ 
     
    116127       var_dump($this->_blockstack); 
    117128       echo "\n";*/ 
    118         
     129 
     130     }elseif(preg_match('/^\#(define)\s+(\w+)\s+(.+)$/m',$line,$m)){ 
     131        // define avec deux arguments 
     132        if($isOpen){ 
     133            $this->_variables[$m[2]] = trim($m[3]); 
     134        } 
     135        $source[$nb]=false; 
     136 
    119137     }elseif(preg_match('/^\#(expand)\s(.*)$/m',$line,$m)){ 
    120         if(! (end($this->_blockstack) & self::BLOCK_NO)){ 
     138        if($isOpen){ 
    121139            $source[$nb]=preg_replace('/\_\_(\w*)\_\_/e', '(isset($this->_variables["\\1"])?$this->_variables["\\1"]:"__\\1__")',$m[2]); 
    122140        }else{ 
    123141            $source[$nb]=false; 
    124142        } 
    125  
    126143      
    127144     }elseif(preg_match('/^\#(endif|else)\s*$/m',$line,$m)){ 
     
    151168        } 
    152169     }else{ 
    153          if(end($this->_blockstack) &  self::BLOCK_NO){ 
     170         if(!$isOpen){ 
    154171            $source[$nb]=false; 
    155172         } 
     
    163180   } 
    164181 
     182   if(count($this->_blockstack)) 
     183     return $this->doError($nb,self::ERR_ENDIF_MISSING); 
     184 
    165185   $this->_variables = $this->_savedVariables; 
    166186    
    167187   return $result; 
    168    //return implode("\n",$source); 
    169188 } 
    170189 
  • trunk/build/tests/ppdatas/result5_1.txt

    r82 r228  
    66 
    77 voici le contenu de FOO : "__FOO__" et celui de BAR : __BAR__ 
    8  
     8toto 
    99ffffff 
  • trunk/build/tests/ppdatas/result5_2.txt

    r82 r228  
    66 
    77 voici le contenu de FOO : "une variable foo" et celui de BAR : le bar est ouvert 
    8  
     8toto 
    99ffffff 
  • trunk/build/tests/ppdatas/source5.txt

    r82 r228  
    1010 
    1111#expand  voici le contenu de FOO : "__FOO__" et celui de BAR : __BAR__ 
    12  
     12#expand toto 
    1313ffffff 
  • trunk/build/tests/testpreprocess.php

    r82 r228  
    5656           'result7_5.txt'=>array('BAZ'=>true, 'BAR'=>true), 
    5757          ), 
     58      'source_define.txt'=>array( 
     59            'result_define.txt'=>array('FOO'=>true), 
     60          ), 
     61      'source_define2.txt'=>array( 
     62            'result_define2.txt'=>array('FOO'=>'ok'), 
     63          ), 
    5864    ); 
    5965     
     
    7278      $proc = new jPreProcessor(); 
    7379      foreach($this->testcase as $source=>$datas){ 
    74          $contents = file_get_contents(PP_DATA_DIR.$source); 
    7580         foreach($datas as $result=>$vars){           
    7681           $proc->setVars($vars); 
    77            $res = $proc->run($contents); 
     82           $res = $proc->parseFile(PP_DATA_DIR.$source); 
    7883           $this->assertEqual($res, file_get_contents(PP_DATA_DIR.$result)); 
    7984         } 
Download in other formats: Unified Diff Zip Archive