/** * @param $file * @return array * @throws \Nette\InvalidArgumentException */ public function getElementsByFile($file) { if (!file_exists($file)) { throw new InvalidArgumentException("File '{$file}' does not exist."); } $ret = array(); $tokenizer = new MacroTokenizer(file_get_contents($file)); while (($word = $tokenizer->fetchWord()) !== FALSE) { if ($word === '{element') { $name = trim($tokenizer->fetchWord(), '}\'"'); $id = trim($tokenizer->fetchWord(), '}\'"'); $ret[$id] = $name; } } return $ret; }
/** * @return bool */ public function canQuote(MacroTokenizer $tokenizer) { return $tokenizer->isCurrent(MacroTokenizer::T_SYMBOL) && (!$tokenizer->hasPrev() || $tokenizer->isPrev(',', '(', '[', '=', '=>', ':', '?')) && (!$tokenizer->hasNext() || $tokenizer->isNext(',', ')', ']', '=', '=>', ':', '|')); }
public function setArgs($args) { $this->args = (string) $args; $this->tokenizer->tokenize($this->args); }
/** * 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; }
canQuote(MacroTokenizer$tokenizer){return$tokenizer->isCurrent(MacroTokenizer::T_SYMBOL)&&(!$tokenizer->hasPrev()||$tokenizer->isPrev(',','(','[','=','=>',':','?'))&&(!$tokenizer->hasNext()||$tokenizer->isNext(',',')',']','=','=>',':','|'));}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;}elseif($token['type']===MacroTokenizer::T_WHITESPACE){$tokens[]=$token;continue;}if($token['value']==='?'){$inTernary=$depth;}elseif($token['value']===':'){$inTernary=NULL;}elseif($inTernary===$depth&&($token['value']===','||$token['value']===')'||$token['value']===']')){$tokens[]=MacroTokenizer::createToken(':')+array('depth'=>$depth);$tokens[]=MacroTokenizer::createToken('null')+array('depth'=>$depth);$inTernary=NULL;}if($token['value']==='['){if($arrays[]=$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']==='('){$arrays[]='(';}elseif($token['value']===')'){array_pop($arrays);}$tokens[]=$prev=$token;}if($inTernary!==NULL){$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;}function
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; } elseif ($token['type'] === MacroTokenizer::T_WHITESPACE) { $tokens[] = $token; continue; } if ($token['value'] === '?') { $inTernary = $depth; } elseif ($token['value'] === ':') { $inTernary = NULL; } elseif ($inTernary === $depth && ($token['value'] === ',' || $token['value'] === ')' || $token['value'] === ']')) { $tokens[] = MacroTokenizer::createToken(':') + array('depth' => $depth); $tokens[] = MacroTokenizer::createToken('null') + array('depth' => $depth); $inTernary = NULL; } if ($token['value'] === '[') { if ($arrays[] = $prev['value'] !== ']' && $prev['type'] !== MacroTokenizer::T_SYMBOL && $prev['type'] !== MacroTokenizer::T_VARIABLE) { $tokens[] = MacroTokenizer::createToken('array') + array('depth' => $depth); $token = MacroTokenizer::createToken('('); } } elseif ($token['value'] === ']') { if (array_pop($arrays) === TRUE) { $token = MacroTokenizer::createToken(')'); } } elseif ($token['value'] === '(') { $arrays[] = '('; } elseif ($token['value'] === ')') { array_pop($arrays); } $tokens[] = $prev = $token; } if ($inTernary !== NULL) { $tokens[] = MacroTokenizer::createToken(':') + array('depth' => count($arrays)); $tokens[] = MacroTokenizer::createToken('null') + array('depth' => count($arrays)); } $tokenizer = clone $tokenizer; $tokenizer->position = 0; $tokenizer->tokens = $tokens; return $tokenizer; }