示例#1
0
 public function setArgs($args)
 {
     $this->args = (string) $args;
     $this->tokenizer->tokenize($this->args);
 }
示例#2
0
 /**
  * Preprocessor for tokens.
  * @return MacroTokenizer
  */
 public function preprocess(MacroTokenizer $tokenizer = NULL)
 {
     $tokenizer = $tokenizer === NULL ? $this->argsTokenizer : $tokenizer;
     $inTernary = $prev = NULL;
     $tokens = $arrays = array();
     while ($token = $tokenizer->fetchToken()) {
         $token['depth'] = $depth = count($arrays);
         if ($token['type'] === MacroTokenizer::T_COMMENT) {
             continue;
             // remove comments
         } elseif ($token['type'] === MacroTokenizer::T_WHITESPACE) {
             $tokens[] = $token;
             continue;
         }
         if ($token['value'] === '?') {
             // short ternary operators without :
             $inTernary = $depth;
         } elseif ($token['value'] === ':') {
             $inTernary = NULL;
         } elseif ($inTernary === $depth && ($token['value'] === ',' || $token['value'] === ')' || $token['value'] === ']')) {
             // close ternary
             $tokens[] = MacroTokenizer::createToken(':') + array('depth' => $depth);
             $tokens[] = MacroTokenizer::createToken('null') + array('depth' => $depth);
             $inTernary = NULL;
         }
         if ($token['value'] === '[') {
             // simplified array syntax [...]
             if ($arrays[] = $prev['value'] !== ']' && $prev['value'] !== ')' && $prev['type'] !== MacroTokenizer::T_SYMBOL && $prev['type'] !== MacroTokenizer::T_VARIABLE && $prev['type'] !== MacroTokenizer::T_KEYWORD) {
                 $tokens[] = MacroTokenizer::createToken('array') + array('depth' => $depth);
                 $token = MacroTokenizer::createToken('(');
             }
         } elseif ($token['value'] === ']') {
             if (array_pop($arrays) === TRUE) {
                 $token = MacroTokenizer::createToken(')');
             }
         } elseif ($token['value'] === '(') {
             // only count
             $arrays[] = '(';
         } elseif ($token['value'] === ')') {
             // only count
             array_pop($arrays);
         }
         $tokens[] = $prev = $token;
     }
     if ($inTernary !== NULL) {
         // close ternary
         $tokens[] = MacroTokenizer::createToken(':') + array('depth' => count($arrays));
         $tokens[] = MacroTokenizer::createToken('null') + array('depth' => count($arrays));
     }
     $tokenizer = clone $tokenizer;
     $tokenizer->reset();
     $tokenizer->tokens = $tokens;
     return $tokenizer;
 }