📄 parser.php
字号:
} public function parseConcatExpression() { $lineno = $this->stream->current->lineno; $left = $this->parseMulExpression(); while ($this->stream->test(Twig_Token::OPERATOR_TYPE, '~')) { $this->stream->next(); $right = $this->parseMulExpression(); $left = new Twig_ConcatExpression($left, $right, $lineno); $lineno = $this->stream->current->lineno; } return $left; } public function parseMulExpression() { $lineno = $this->stream->current->lineno; $left = $this->parseDivExpression(); while ($this->stream->test(Twig_Token::OPERATOR_TYPE, '*')) { $this->stream->next(); $right = $this->parseDivExpression(); $left = new Twig_MulExpression($left, $right, $lineno); $lineno = $this->stream->current->lineno; } return $left; } public function parseDivExpression() { $lineno = $this->stream->current->lineno; $left = $this->parseModExpression(); while ($this->stream->test(Twig_Token::OPERATOR_TYPE, '/')) { $this->stream->next(); $right = $this->parseModExpression(); $left = new Twig_DivExpression($left, $right, $lineno); $lineno = $this->stream->current->lineno; } return $left; } public function parseModExpression() { $lineno = $this->stream->current->lineno; $left = $this->parseUnaryExpression(); while ($this->stream->test(Twig_Token::OPERATOR_TYPE, '%')) { $this->stream->next(); $right = $this->parseUnaryExpression(); $left = new Twig_ModExpression($left, $right, $lineno); $lineno = $this->stream->current->lineno; } return $left; } public function parseUnaryExpression() { if ($this->stream->test('not')) return $this->parseNotExpression(); if ($this->stream->current->type == Twig_Token::OPERATOR_TYPE) { switch ($this->stream->current->value) { case '-': return $this->parseNegExpression(); case '+': return $this->parsePosExpression(); } } return $this->parsePrimaryExpression(); } public function parseNotExpression() { $token = $this->stream->next(); $node = $this->parseUnaryExpression(); return new Twig_NotExpression($node, $token->lineno); } public function parseNegExpression() { $token = $this->stream->next(); $node = $this->parseUnaryExpression(); return new Twig_NegExpression($node, $token->lineno); } public function parsePosExpression() { $token = $this->stream->next(); $node = $this->parseUnaryExpression(); return new Twig_PosExpression($node, $token->lineno); } public function parsePrimaryExpression($assignment=false) { $token = $this->stream->current; switch ($token->type) { case Twig_Token::NAME_TYPE: $this->stream->next(); switch ($token->value) { case 'true': $node = new Twig_Constant(true, $token->lineno); break; case 'false': $node = new Twig_Constant(false, $token->lineno); break; case 'none': $node = new Twig_Constant(NULL, $token->lineno); break; default: $cls = $assignment ? 'Twig_AssignNameExpression' : 'Twig_NameExpression'; $node = new $cls($token->value, $token->lineno); } break; case Twig_Token::NUMBER_TYPE: case Twig_Token::STRING_TYPE: $this->stream->next(); $node = new Twig_Constant($token->value, $token->lineno); break; default: if ($token->test(Twig_Token::OPERATOR_TYPE, '(')) { $this->stream->next(); $node = $this->parseExpression(); $this->stream->expect(Twig_Token::OPERATOR_TYPE, ')'); } else throw new Twig_SyntaxError('unexpected token', $token->lineno); } if (!$assignment) $node = $this->parsePostfixExpression($node); return $node; } public function parsePostfixExpression($node) { $stop = false; while (!$stop && $this->stream->current->type == Twig_Token::OPERATOR_TYPE) switch ($this->stream->current->value) { case '.': case '[': $node = $this->parseSubscriptExpression($node); break; case '|': $node = $this->parseFilterExpression($node); break; default: $stop = true; break; } return $node; } public function parseSubscriptExpression($node) { $token = $this->stream->next(); $lineno = $token->lineno; if ($token->value == '.') { $token = $this->stream->next(); if ($token->type == Twig_Token::NAME_TYPE || $token->type == Twig_Token::NUMBER_TYPE) $arg = new Twig_Constant($token->value, $lineno); else throw new Twig_SyntaxError('expected name or number', $lineno); } else { $arg = $this->parseExpression(); $this->stream->expect(Twig_Token::OPERATOR_TYPE, ']'); } if (!$this->stream->test(Twig_Token::OPERATOR_TYPE, '(')) return new Twig_GetAttrExpression($node, $arg, $lineno, $token->value); /* sounds like something wants to call a member with some arguments. Let's parse the parameters */ $this->stream->next(); $arguments = array(); while (!$this->stream->test(Twig_Token::OPERATOR_TYPE, ')')) { if (count($arguments)) $this->stream->expect(Twig_Token::OPERATOR_TYPE, ','); $arguments[] = $this->parseExpression(); } $this->stream->expect(Twig_Token::OPERATOR_TYPE, ')'); return new Twig_MethodCallExpression($node, $arg, $arguments, $lineno); } public function parseFilterExpression($node) { $lineno = $this->stream->current->lineno; $filters = array(); while ($this->stream->test(Twig_Token::OPERATOR_TYPE, '|')) { $this->stream->next(); $token = $this->stream->expect(Twig_Token::NAME_TYPE); $args = array(); if ($this->stream->test( Twig_Token::OPERATOR_TYPE, '(')) { $this->stream->next(); while (!$this->stream->test( Twig_Token::OPERATOR_TYPE, ')')) { if (!empty($args)) $this->stream->expect( Twig_Token::OPERATOR_TYPE, ','); $args[] = $this->parseExpression(); } $this->stream->expect(Twig_Token::OPERATOR_TYPE, ')'); } $filters[] = array($token->value, $args); } return new Twig_FilterExpression($node, $filters, $lineno); } public function parseAssignmentExpression() { $lineno = $this->stream->current->lineno; $targets = array(); $is_multitarget = false; while (true) { if (!empty($targets)) $this->stream->expect(Twig_Token::OPERATOR_TYPE, ','); if ($this->stream->test(Twig_Token::OPERATOR_TYPE, ')') || $this->stream->test(Twig_Token::VAR_END_TYPE) || $this->stream->test(Twig_Token::BLOCK_END_TYPE) || $this->stream->test('in')) break; $targets[] = $this->parsePrimaryExpression(true); if (!$this->stream->test(Twig_Token::OPERATOR_TYPE, ',')) break; $is_multitarget = true; } if (!$is_multitarget && count($targets) == 1) return array(false, $targets[0]); return array(true, $targets); } public function subparse($test, $drop_needle=false) { $lineno = $this->stream->current->lineno; $rv = array(); while (!$this->stream->eof) { switch ($this->stream->current->type) { case Twig_Token::TEXT_TYPE: $token = $this->stream->next(); $rv[] = new Twig_Text($token->value, $token->lineno); break; case Twig_Token::VAR_START_TYPE: $token = $this->stream->next(); $expr = $this->parseExpression(); $this->stream->expect(Twig_Token::VAR_END_TYPE); $rv[] = new Twig_Print($expr, $token->lineno); break; case Twig_Token::BLOCK_START_TYPE: $this->stream->next(); $token = $this->stream->current; if ($token->type !== Twig_Token::NAME_TYPE) throw new Twig_SyntaxError('expected directive', $token->lineno); if (!is_null($test) && call_user_func($test, $token)) { if ($drop_needle) $this->stream->next(); return Twig_NodeList::fromArray($rv, $lineno); } if (!isset($this->handlers[$token->value])) throw new Twig_SyntaxError('unknown directive', $token->lineno); $this->stream->next(); $node = call_user_func($this->handlers[$token->value], $token); if (!is_null($node)) $rv[] = $node; break; default: assert(false, 'Lexer or parser ended up in ' . 'unsupported state.'); } } return Twig_NodeList::fromArray($rv, $lineno); } public function parse() { try { $body = $this->subparse(NULL); } catch (Twig_SyntaxError $e) { if (is_null($e->filename)) $e->filename = $this->stream->filename; throw $e; } if (!is_null($this->extends)) foreach ($this->blocks as $block) $block->parent = $this->extends; return new Twig_Module($body, $this->extends, $this->blocks, $this->stream->filename); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -