Refectory term parser (if ->switch)

This commit is contained in:
bzick
2015-06-01 23:36:30 +03:00
parent ec890cc819
commit 30921d3500

View File

@@ -782,14 +782,20 @@ class Template extends Render
} else { } else {
$unary = ""; $unary = "";
} }
if ($tokens->is(T_LNUMBER, T_DNUMBER)) { switch($tokens->key()) {
case T_LNUMBER:
case T_DNUMBER:
$code = $unary . $this->parseScalar($tokens, true); $code = $unary . $this->parseScalar($tokens, true);
} elseif ($tokens->is(T_CONSTANT_ENCAPSED_STRING, '"', T_ENCAPSED_AND_WHITESPACE)) { break;
case T_CONSTANT_ENCAPSED_STRING:
case '"':
case T_ENCAPSED_AND_WHITESPACE:
if ($unary) { if ($unary) {
throw new UnexpectedTokenException($tokens->back()); throw new UnexpectedTokenException($tokens->back());
} }
$code = $this->parseScalar($tokens, true); $code = $this->parseScalar($tokens, true);
} elseif ($tokens->is(T_VARIABLE)) { break;
case T_VARIABLE:
$code = $this->parseVariable($tokens); $code = $this->parseVariable($tokens);
if ($tokens->is("(") && $tokens->hasBackList(T_STRING, T_OBJECT_OPERATOR)) { if ($tokens->is("(") && $tokens->hasBackList(T_STRING, T_OBJECT_OPERATOR)) {
if ($this->_options & Fenom::DENY_METHODS) { if ($this->_options & Fenom::DENY_METHODS) {
@@ -810,20 +816,25 @@ class Template extends Render
$code = $unary . $code; $code = $unary . $code;
} }
} }
} elseif ($tokens->is('$')) { break;
case '$':
$code = $unary . $this->parseAccessor($tokens, $is_var); $code = $unary . $this->parseAccessor($tokens, $is_var);
} elseif ($tokens->is(Tokenizer::MACRO_INCDEC)) { break;
case T_DEC:
case T_INC:
if($this->_options & Fenom::FORCE_VERIFY) { if($this->_options & Fenom::FORCE_VERIFY) {
$var = $this->parseVariable($tokens); $var = $this->parseVariable($tokens);
$code = $unary . '(isset(' . $var . ') ? ' . $tokens->getAndNext() . $this->parseVariable($tokens).' : null)'; $code = $unary . '(isset(' . $var . ') ? ' . $tokens->getAndNext() . $this->parseVariable($tokens).' : null)';
} else { } else {
$code = $unary . $tokens->getAndNext() . $this->parseVariable($tokens); $code = $unary . $tokens->getAndNext() . $this->parseVariable($tokens);
} }
} elseif ($tokens->is("(")) { break;
case '(':
$tokens->next(); $tokens->next();
$code = $unary . "(" . $this->parseExpr($tokens) . ")"; $code = $unary . "(" . $this->parseExpr($tokens) . ")";
$tokens->need(")")->next(); $tokens->need(")")->next();
} elseif ($tokens->is(T_STRING)) { break;
case T_STRING:
if ($tokens->isSpecialVal()) { if ($tokens->isSpecialVal()) {
$code = $unary . $tokens->getAndNext(); $code = $unary . $tokens->getAndNext();
} elseif ($tokens->isNext("(") && !$tokens->getWhitespace()) { } elseif ($tokens->isNext("(") && !$tokens->getWhitespace()) {
@@ -844,7 +855,9 @@ class Template extends Render
} else { } else {
return false; return false;
} }
} elseif ($tokens->is(T_ISSET, T_EMPTY)) { break;
case T_ISSET:
case T_EMPTY:
$func = $tokens->getAndNext(); $func = $tokens->getAndNext();
if ($tokens->is("(") && $tokens->isNext(T_VARIABLE)) { if ($tokens->is("(") && $tokens->isNext(T_VARIABLE)) {
$code = $unary . $func . "(" . $this->parseVariable($tokens->next()) . ")"; $code = $unary . $func . "(" . $this->parseVariable($tokens->next()) . ")";
@@ -852,16 +865,20 @@ class Template extends Render
} else { } else {
throw new TokenizeException("Unexpected token " . $tokens->getNext() . ", isset() and empty() accept only variables"); throw new TokenizeException("Unexpected token " . $tokens->getNext() . ", isset() and empty() accept only variables");
} }
} elseif ($tokens->is('[')) { break;
case '[':
if ($unary) { if ($unary) {
throw new UnexpectedTokenException($tokens->back()); throw new UnexpectedTokenException($tokens->back());
} }
$code = $this->parseArray($tokens); $code = $this->parseArray($tokens);
} elseif ($unary) { break;
default:
if ($unary) {
throw new UnexpectedTokenException($tokens->back()); throw new UnexpectedTokenException($tokens->back());
} else { } else {
return false; 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);
$is_var = false; $is_var = false;