Parse token.

@param \Twig\Token $token Token.

@return \WyriHaximus\TwigView\Lib\Twig\Node\Element
187722
public function parse(Token $token): ElementNode
    {
        $stream = $this->parser->getStream();
        $name = $this->parser->getExpressionParser()->parseExpression();

        $data = null;
        if (!$stream->test(Token::BLOCK_END_TYPE)) {
            $data = $this->parser->getExpressionParser()->parseExpression();
        }

        $options = null;
        if (!$stream->test(Token::BLOCK_END_TYPE)) {
            $options = $this->parser->getExpressionParser()->parseExpression();
        }

        $stream->expect(Token::BLOCK_END_TYPE);

        return new ElementNode($name, $data, $options, $token->getLine(), $this->getTag());
    }
935478
public function parse(Token $token): ValueInterface
    {
        parent::parse($token);

        $value = $token->getValue();
        try {
            $elements = substr($value, 1, strlen($value) - 2);

            return new ArrayValue($this->parseElements($this->parser, $elements));
        } catch (\TypeError $error) {
            throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token, 0, $error);
        }
    }
78135
public function parse(\Twig_Token $token)
    {
        $lineno = $token->getLine();
        $stream = $this->parser->getStream();

        $table = $this->parser->getExpressionParser()->parseExpression();

        if ($this->parser->getStream()->test(\Twig_Token::NAME_TYPE, 'with')) {
            $this->parser->getStream()->next();
            $resources = $this->parser->getExpressionParser()->parseExpression();
        } else {
            $resources = new \Twig_Node_Expression_Array(array(), $stream->getCurrent()->getLine());
            do {
                $resources->addElement($this->parser->getExpressionParser()->parseExpression());
            } while (!$stream->test(\Twig_Token::BLOCK_END_TYPE));
        }

        $stream->expect(\Twig_Token::BLOCK_END_TYPE);

        return new TableThemeNode($table, $resources, $lineno, $this->getTag());
    }
343872
public function parse(\Twig_Token $token)
    {
        list($type, $options, $variables, $skip, $loop) = $this->parseArguments();

        $stream = $this->parser->getStream();
        $lineno = $stream->getCurrent()->getLine();

        $superblock = new Superblock($type, $options, $loop, $lineno, $this->getTag());
        $name = $superblock->getAttribute('name');
        $reference = new SuperblockReference($name, $variables, $loop, $lineno, $this->getTag());
        $reference->setAttribute('parent_name', $name);

        $this->parser->setBlock($name, $superblock);
        $this->parser->pushLocalScope();
        $this->parser->pushBlockStack($name);

        if ($skip) {
            $this->parser->popBlockStack();
            $this->parser->popLocalScope();

            return $reference;
        }

        // body content
        $sBlocks = new \Twig_Node([], [], $lineno);
        $body = $this->parser->subparse([$this, 'decideBlockEnd'], true);
        $previousTwigNode = null;

        if (0 === \count($body) || null !== $body->getNodeTag()) {
            $body = new \Twig_Node([$body], [], $lineno);
        }

        if (null === $body->getNodeTag()) {
            /* @var \Twig_Node $node */
            foreach ($body->getIterator() as $node) {
                if (!$node instanceof \Twig_Node) {
                    continue;
                }

                if ($node instanceof SuperblockReference) {
                    $this->pushClosureNode($sBlocks, $variables, $name, $previousTwigNode);
                    $previousTwigNode = null;

                    $node->setAttribute('is_root', false);
                    $node->setAttribute('parent_name', $name);
                    $sBlocks->setNode(\count($sBlocks), $node);
                } elseif ($node instanceof \Twig_Node_Set) {
                    $this->pushClosureNode($sBlocks, $variables, $name, $previousTwigNode);
                    $previousTwigNode = null;

                    $sBlocks->setNode(\count($sBlocks), $node);
                } elseif (!$node instanceof \Twig_Node_Text || ($node instanceof \Twig_Node_Text && '' !== trim($node->getAttribute('data')))) {
                    if (null === $previousTwigNode) {
                        $previousTwigNode = new SuperblockClosure(new \Twig_Node([], [], $lineno), $node->getTemplateLine());
                    }

                    $previousTwigNode->getNode('body')->setNode(\count($previousTwigNode->getNode('body')), $node);
                }
            }
        }

        $this->pushClosureNode($sBlocks, $variables, $name, $previousTwigNode);

        $superblock->setNode('sblocks', $sBlocks);

        $stream->expect(\Twig_Token::BLOCK_END_TYPE);

        $this->parser->popBlockStack();
        $this->parser->popLocalScope();

        return $reference;
    }
1004684
public function setToken($token)
    {
        $this->_tokenString = (string) $token;
        $this->_token       = $token;
        return $this;
    }
57220
protected static function parseArgument($token)
    {
        $description = null;

        if (mb_strpos($token, ' : ') !== false) {
            list($token, $description) = explode(' : ', $token, 2);

            $token = trim($token);

            $description = trim($description);
        }

        switch (true) {
            case Text::endsWith($token, '?*'):
                return new InputArgument(trim($token, '?*'), InputArgument::IS_ARRAY, $description);
            case Text::endsWith($token, '*'):
                return new InputArgument(trim($token, '*'), InputArgument::IS_ARRAY | InputArgument::REQUIRED, $description);
            case Text::endsWith($token, '?'):
                return new InputArgument(trim($token, '?'), InputArgument::OPTIONAL, $description);
            case preg_match('/(.+)\=(.+)/', $token, $matches):
                return new InputArgument($matches[1], InputArgument::OPTIONAL, $description, $matches[2]);
            default:
                return new InputArgument($token, InputArgument::REQUIRED, $description);
        }
    }
219506
public function parse(\Twig_Token $token)
    {
        $lineno = $token->getLine();
        $stream = $this->parser->getStream();

        $crud = $this->parser->getExpressionParser()->parseExpression();
        $resources = array();
        do {
            $resources[] = $this->parser->getExpressionParser()->parseExpression();
        } while (!$stream->test(\Twig_Token::BLOCK_END_TYPE));

        $stream->expect(\Twig_Token::BLOCK_END_TYPE);

        return new CrudThemeNode($crud, new \Twig_Node($resources), $lineno, $this->getTag());
    }
374569
public function parse(\Twig_Token $token)
    {
        $this->parseCodeBlock();

        return new CodeBlockNode(
            $this->highlighterName,
            $this->highlighterArgs,
            $this->getAttributes(),
            $this->getBody(),
            $token->getLine(),
            $this->getTag()
        );
    }
1155457
public function parse(Twig_Token $token)
    {
        $parser = $this->parser;
        $stream = $parser->getStream();
        $subunitExpression = $parser->getExpressionParser()->parseExpression();
        $stream->expect(Twig_Token::BLOCK_END_TYPE);

        return new RenderNode($subunitExpression, $token->getLine(), $this->getTag());
    }
832925
public function parse(Token $token)
    {
        $lineno = $token->getLine();
        $stream = $this->parser->getStream();

        $code = $stream->expect(Token::NUMBER_TYPE)->getValue();
        $message = $this->parser->getExpressionParser()->parseExpression();
        $stream->expect(Token::BLOCK_END_TYPE);

        return new TwigNodeThrow($code, $message, $lineno, $this->getTag());
    }
1356030
protected function processToken($token)
    {
        switch ($token) {
            case ')':
                $this->processTokenClosingParenthesis();
                break;
            case '(':
                $this->processTokenOpeningParenthesis();
                break;
            case self::LOGICAL_OR:
            case self::LOGICAL_AND:
                $this->processTokenLogicalOperator($token);
                break;
            default:
                $this->processTokenCondition($token);
                break;
        }

        return $this;
    }
828314
public function parse(Twig_Token $token)
    {
        $lineno = $token->getLine();
        $stream = $this->parser->getStream();

        $name = null;
        if ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) {
            $name = $token->getValue();
        }

        $stream->expect(Twig_Token::BLOCK_END_TYPE);
        return new FrameworkNode($name, $lineno, $this->getTag());
    }
36017
public function parse(\Twig_Token $token)
    {
        $lineNo = $token->getLine();
        $stream = $this->parser->getStream();

        $pygmentizeLexers = array_keys(Pygmentize::lexers());

        $acceptedLexers = array();
        foreach($pygmentizeLexers as $lexer) {
            $k = str_replace('+', '', $lexer);
            $k = str_replace('-', '', $k);
            $k = str_replace(' ', '', $k);
            $acceptedLexers[$k] = $lexer;
        }

        $additionalLexers = array(
            'twig' => 'jinja',
            'twightml' => 'html+jinja',
            'htmltwig' => 'html+jinja',
            'composer' => 'json',
            'objcpp' => 'obj-c++',
            'objectivecpp' => 'objective-c++'
        );

        $lexer = null;

        if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {

            if ($stream->test(\Twig_Token::BLOCK_END_TYPE)) {
                throw new \Twig_Error_Syntax(
                    'A lexer is needed for highlighting. ',
                    $stream->getCurrent()->getLine(),
                    $stream->getFilename()
                );
            }

            $lexer = $this->parser->getExpressionParser()->parseExpression();
            if(array_key_exists($lexer->getAttribute('name'), $acceptedLexers)) {
                $lexer->setAttribute(
                    'name',
                    $acceptedLexers[$lexer->getAttribute('name')]
                );
            }
            else {
                if (array_key_exists($lexer->getAttribute('name'), $additionalLexers)) {
                    $lexer->setAttribute('name', $additionalLexers[$lexer->getAttribute('name')]);
                } else {
                    throw new \Twig_Error_Syntax(
                        'Unexpected lexer: ' . $lexer->getAttribute('name'),
                        $stream->getCurrent()->getLine(),
                        $stream->getFilename()
                    );
                }
            }
        }

        $stream->expect(\Twig_Token::BLOCK_END_TYPE);

        $source = $this->parser->subparse(array($this, 'decideBlockEnd'), true);

        if (!$source instanceof \Twig_Node &&
            !$source instanceof \Twig_Node_Text &&
            !$source instanceof \Twig_Node_Expression
        ) {
            throw new \Twig_Error_Syntax(
                'A message inside a highlight tag must be a simple text.',
                $source->getLine(),
                $stream->getFilename()
            );
        }

        $stream->expect(\Twig_Token::BLOCK_END_TYPE);

        return new HighlightNode($source, $lexer, $lineNo, $this->getTag());
    }
429547
public function parse(Token $token)
    {
        $lineno = $token->getLine();
        $stream = $this->parser->getStream();

        $stream->expect(Token::BLOCK_END_TYPE);
        $try = $this->parser->subparse([$this, 'decideCatch']);
        $stream->next();
        $stream->expect(Token::BLOCK_END_TYPE);
        $catch = $this->parser->subparse([$this, 'decideEnd']);
        $stream->next();
        $stream->expect(Token::BLOCK_END_TYPE);

        return new TwigNodeTryCatch($try, $catch, $lineno, $this->getTag());
    }
969693
public function parse(Twig_Token $token)
    {
        $stream = $this->parser->getStream();

        $stream->expect(Twig_Token::BLOCK_END_TYPE);
        $body = $this->parser->subparse(array($this, 'decideEndAnnotations'));
        $stream->expect('endannotations');
        $stream->expect(Twig_Token::BLOCK_END_TYPE);

        return new AnnotationsNode(array('body' => $body));
    }
249346
public function parse(Token $token)
    {
        parent::parse($token);

        $split = preg_split(self::REGEX, $token->getValue(), 2, PREG_SPLIT_DELIM_CAPTURE + PREG_SPLIT_NO_EMPTY);
        $splitSize = count($split);
        if (1 === $splitSize) {
            return $this->functionTokenParser->parse($token);
        }

        if (3 !== count($split) && 4 !== count($split)) {
            throw ExpressionLanguageExceptionFactory::createForCouldNotLexValue($token->getValue());
        }

        $values = [
            $this->parser->parse($split[0].$split[1]),
        ];
        if (3 === $splitSize) {
            $values[] = $this->parser->parse('<'.$split[2]);
        }

        if (4 === $splitSize) {
            $values[] = $this->parser->parse($split[2]);
            $values[] = $this->parser->parse('<'.$split[3]);
        }

        return $this->mergeValues($values);
    }
32769
protected function renderToken(Token $token)
    {
        $type = $token->getType();

        // handle edge cases
        switch ($type) {
            case "open":
                return "(";
            case "close":
                return ")";
            case "group":
                return $this->renderGroupBy();
            case "sort":
                return $this->renderSort();
            case "function":
                /** @var Value $token */
                return $this->renderFunction($token);
        }

        if ($token instanceof Entity) {
            return $this->renderEntity($token);
        }
        if ($token instanceof Reference) {
            return $this->renderReference($token);
        }
        if ($token instanceof Value) {
            return $this->renderValue($token);
        }

        return strtoupper($type);
    }