mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Refectory term parser (if ->switch)
This commit is contained in:
parent
ec890cc819
commit
30921d3500
@ -782,85 +782,102 @@ class Template extends Render
|
|||||||
} else {
|
} else {
|
||||||
$unary = "";
|
$unary = "";
|
||||||
}
|
}
|
||||||
if ($tokens->is(T_LNUMBER, T_DNUMBER)) {
|
switch($tokens->key()) {
|
||||||
$code = $unary . $this->parseScalar($tokens, true);
|
case T_LNUMBER:
|
||||||
} elseif ($tokens->is(T_CONSTANT_ENCAPSED_STRING, '"', T_ENCAPSED_AND_WHITESPACE)) {
|
case T_DNUMBER:
|
||||||
if ($unary) {
|
$code = $unary . $this->parseScalar($tokens, true);
|
||||||
throw new UnexpectedTokenException($tokens->back());
|
break;
|
||||||
}
|
case T_CONSTANT_ENCAPSED_STRING:
|
||||||
$code = $this->parseScalar($tokens, true);
|
case '"':
|
||||||
} elseif ($tokens->is(T_VARIABLE)) {
|
case T_ENCAPSED_AND_WHITESPACE:
|
||||||
$code = $this->parseVariable($tokens);
|
if ($unary) {
|
||||||
if ($tokens->is("(") && $tokens->hasBackList(T_STRING, T_OBJECT_OPERATOR)) {
|
throw new UnexpectedTokenException($tokens->back());
|
||||||
if ($this->_options & Fenom::DENY_METHODS) {
|
|
||||||
throw new \LogicException("Forbidden to call methods");
|
|
||||||
}
|
}
|
||||||
$code = $unary . $this->parseChain($tokens, $code);
|
$code = $this->parseScalar($tokens, true);
|
||||||
} elseif ($tokens->is(Tokenizer::MACRO_INCDEC)) {
|
break;
|
||||||
|
case T_VARIABLE:
|
||||||
|
$code = $this->parseVariable($tokens);
|
||||||
|
if ($tokens->is("(") && $tokens->hasBackList(T_STRING, T_OBJECT_OPERATOR)) {
|
||||||
|
if ($this->_options & Fenom::DENY_METHODS) {
|
||||||
|
throw new \LogicException("Forbidden to call methods");
|
||||||
|
}
|
||||||
|
$code = $unary . $this->parseChain($tokens, $code);
|
||||||
|
} elseif ($tokens->is(Tokenizer::MACRO_INCDEC)) {
|
||||||
|
if($this->_options & Fenom::FORCE_VERIFY) {
|
||||||
|
$code = $unary . '(isset(' . $code . ') ? ' . $code . $tokens->getAndNext() . ' : null)';
|
||||||
|
} else {
|
||||||
|
$code = $unary . $code . $tokens->getAndNext();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if($this->_options & Fenom::FORCE_VERIFY) {
|
||||||
|
$code = $unary . '(isset(' . $code . ') ? ' . $code . ' : null)';
|
||||||
|
} else {
|
||||||
|
$is_var = true;
|
||||||
|
$code = $unary . $code;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '$':
|
||||||
|
$code = $unary . $this->parseAccessor($tokens, $is_var);
|
||||||
|
break;
|
||||||
|
case T_DEC:
|
||||||
|
case T_INC:
|
||||||
if($this->_options & Fenom::FORCE_VERIFY) {
|
if($this->_options & Fenom::FORCE_VERIFY) {
|
||||||
$code = $unary . '(isset(' . $code . ') ? ' . $code . $tokens->getAndNext() . ' : null)';
|
$var = $this->parseVariable($tokens);
|
||||||
|
$code = $unary . '(isset(' . $var . ') ? ' . $tokens->getAndNext() . $this->parseVariable($tokens).' : null)';
|
||||||
} else {
|
} else {
|
||||||
$code = $unary . $code . $tokens->getAndNext();
|
$code = $unary . $tokens->getAndNext() . $this->parseVariable($tokens);
|
||||||
}
|
}
|
||||||
} else {
|
break;
|
||||||
if($this->_options & Fenom::FORCE_VERIFY) {
|
case '(':
|
||||||
$code = $unary . '(isset(' . $code . ') ? ' . $code . ' : null)';
|
$tokens->next();
|
||||||
|
$code = $unary . "(" . $this->parseExpr($tokens) . ")";
|
||||||
|
$tokens->need(")")->next();
|
||||||
|
break;
|
||||||
|
case T_STRING:
|
||||||
|
if ($tokens->isSpecialVal()) {
|
||||||
|
$code = $unary . $tokens->getAndNext();
|
||||||
|
} elseif ($tokens->isNext("(") && !$tokens->getWhitespace()) {
|
||||||
|
$func = $this->_fenom->getModifier($modifier = $tokens->current(), $this);
|
||||||
|
if (!$func) {
|
||||||
|
throw new \Exception("Function " . $tokens->getAndNext() . " not found");
|
||||||
|
}
|
||||||
|
if (!is_string($func)) { // dynamic modifier
|
||||||
|
$call = 'call_user_func_array($tpl->getStorage()->getModifier("' . $modifier . '"), array'.$this->parseArgs($tokens->next()).')'; // @todo optimize
|
||||||
|
} else {
|
||||||
|
$call = $func . $this->parseArgs($tokens->next());
|
||||||
|
}
|
||||||
|
$code = $unary . $this->parseChain($tokens, $call);
|
||||||
|
} elseif ($tokens->isNext(T_NS_SEPARATOR, T_DOUBLE_COLON)) {
|
||||||
|
$method = $this->parseStatic($tokens);
|
||||||
|
$args = $this->parseArgs($tokens);
|
||||||
|
$code = $unary . $this->parseChain($tokens, $method . $args);
|
||||||
} else {
|
} else {
|
||||||
$is_var = true;
|
return false;
|
||||||
$code = $unary . $code;
|
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
} elseif ($tokens->is('$')) {
|
case T_ISSET:
|
||||||
$code = $unary . $this->parseAccessor($tokens, $is_var);
|
case T_EMPTY:
|
||||||
} elseif ($tokens->is(Tokenizer::MACRO_INCDEC)) {
|
$func = $tokens->getAndNext();
|
||||||
if($this->_options & Fenom::FORCE_VERIFY) {
|
if ($tokens->is("(") && $tokens->isNext(T_VARIABLE)) {
|
||||||
$var = $this->parseVariable($tokens);
|
$code = $unary . $func . "(" . $this->parseVariable($tokens->next()) . ")";
|
||||||
$code = $unary . '(isset(' . $var . ') ? ' . $tokens->getAndNext() . $this->parseVariable($tokens).' : null)';
|
$tokens->need(')')->next();
|
||||||
} else {
|
|
||||||
$code = $unary . $tokens->getAndNext() . $this->parseVariable($tokens);
|
|
||||||
}
|
|
||||||
} elseif ($tokens->is("(")) {
|
|
||||||
$tokens->next();
|
|
||||||
$code = $unary . "(" . $this->parseExpr($tokens) . ")";
|
|
||||||
$tokens->need(")")->next();
|
|
||||||
} elseif ($tokens->is(T_STRING)) {
|
|
||||||
if ($tokens->isSpecialVal()) {
|
|
||||||
$code = $unary . $tokens->getAndNext();
|
|
||||||
} elseif ($tokens->isNext("(") && !$tokens->getWhitespace()) {
|
|
||||||
$func = $this->_fenom->getModifier($modifier = $tokens->current(), $this);
|
|
||||||
if (!$func) {
|
|
||||||
throw new \Exception("Function " . $tokens->getAndNext() . " not found");
|
|
||||||
}
|
|
||||||
if (!is_string($func)) { // dynamic modifier
|
|
||||||
$call = 'call_user_func_array($tpl->getStorage()->getModifier("' . $modifier . '"), array'.$this->parseArgs($tokens->next()).')'; // @todo optimize
|
|
||||||
} else {
|
} else {
|
||||||
$call = $func . $this->parseArgs($tokens->next());
|
throw new TokenizeException("Unexpected token " . $tokens->getNext() . ", isset() and empty() accept only variables");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '[':
|
||||||
|
if ($unary) {
|
||||||
|
throw new UnexpectedTokenException($tokens->back());
|
||||||
|
}
|
||||||
|
$code = $this->parseArray($tokens);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if ($unary) {
|
||||||
|
throw new UnexpectedTokenException($tokens->back());
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
$code = $unary . $this->parseChain($tokens, $call);
|
|
||||||
} elseif ($tokens->isNext(T_NS_SEPARATOR, T_DOUBLE_COLON)) {
|
|
||||||
$method = $this->parseStatic($tokens);
|
|
||||||
$args = $this->parseArgs($tokens);
|
|
||||||
$code = $unary . $this->parseChain($tokens, $method . $args);
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} elseif ($tokens->is(T_ISSET, T_EMPTY)) {
|
|
||||||
$func = $tokens->getAndNext();
|
|
||||||
if ($tokens->is("(") && $tokens->isNext(T_VARIABLE)) {
|
|
||||||
$code = $unary . $func . "(" . $this->parseVariable($tokens->next()) . ")";
|
|
||||||
$tokens->need(')')->next();
|
|
||||||
} else {
|
|
||||||
throw new TokenizeException("Unexpected token " . $tokens->getNext() . ", isset() and empty() accept only variables");
|
|
||||||
}
|
|
||||||
} elseif ($tokens->is('[')) {
|
|
||||||
if ($unary) {
|
|
||||||
throw new UnexpectedTokenException($tokens->back());
|
|
||||||
}
|
|
||||||
$code = $this->parseArray($tokens);
|
|
||||||
} elseif ($unary) {
|
|
||||||
throw new UnexpectedTokenException($tokens->back());
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
if (($allows & self::TERM_MODS) && $tokens->is('|')) {
|
if (($allows & self::TERM_MODS) && $tokens->is('|')) {
|
||||||
$code = $this->parseModifier($tokens, $code);
|
$code = $this->parseModifier($tokens, $code);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user