Parses the given TokenStream into a RootNode (AST)

@param TokenStream $stream
@return RootNode
@throws AegisError
179718
public function parse(TokenStream $stream) : RootNode
    {
        if (! $this->runtime) {
            throw new AegisError('Runtime needs to be set before parsing');
        }

        $this->prepare($stream);
        $this->parseOutsideTag();

        if ($this->cursor !== $this->lastTokenIndex) {
            $this->syntaxError('Unexpected token ' . $this->getCurrentToken());
        }

        return $this->getRoot();
    }
1002334
private function parseImport(Stream $stream)
    {
        $stream->expects(Token::TYPE_KEYWORD, 'import');

        if ($stream->current()->equals(Token::TYPE_SYMBOL, '{')) {
            $stream->expects(Token::TYPE_SYMBOL, '{');

            $map = $this->parseImportList($stream);

            $stream->expects(Token::TYPE_SYMBOL, '}');
        } else {
            $stream->expects(Token::TYPE_SYMBOL, '*');

            $map = null;
        }

        $stream->expects(Token::TYPE_KEYWORD, 'from');

        $path = $stream->expects(Token::TYPE_STRING | Token::TYPE_SINGLE_QUOTED)->getValue();

        $stream->expects(Token::TYPE_SYMBOL, ';');

        return [$path, $map];
    }
840749
public function parse(AbstractStream $stream)
    {
        $value = $this->format($this->read($stream));
        $this->validate($value);
        return $value;
    }
651158
public function parseCondition(Stream $stream)
    {
        $expr = $this->parseExpr($stream);

        $stream->expects(Token::TYPE_KEYWORD, 'is');

        if ($stream->current()->equals(Token::TYPE_KEYWORD, 'not')) {
            $stream->expects(Token::TYPE_KEYWORD, 'not');

            $not = true;
        } else {
            $not = false;
        }

        if ($stream->current()->equals(Token::TYPE_KEYWORD, 'like')) {
            $stream->expects(Token::TYPE_KEYWORD, 'like');
        }

        $expr = new EqualsNode($expr, $this->parseExpr($stream));

        if ($not === true) {
            $expr = new UnaryPrefixOperatorNode('!', new GroupNode($expr));
        }

        if ($stream->current()->equals(Token::TYPE_KEYWORD, 'and')) {
            $stream->expects(Token::TYPE_KEYWORD, 'and');

            $expr = new BinaryInfixOperatorNode('&&', $expr, $this->parseCondition($stream));
        }

        if ($stream->current()->equals(Token::TYPE_KEYWORD, 'or')) {
            $stream->expects(Token::TYPE_KEYWORD, 'or');

            $expr = new BinaryInfixOperatorNode('||', $expr, $this->parseCondition($stream));
        }

        return $expr;
    }
1306012
function parse(array $tokens, &$i) {

    if (T_OPEN_TAG !== $tokens[$i][0]) {
      return FALSE;
    }

    ++$i;

    $nodes = array();
    while (TRUE) {
      $node = $this->parser->parse($tokens, $i);
      if (FALSE === $node) {
        $id = ParserUtil::nextSubstantialIncl($tokens, $i);
        if ('#' === $id) {
          // End of file/stream.
          break;
        }
        return FALSE;
      }
      if (NULL !== $node) {
        $nodes[] = $node;
      }
    }

    return new AstFile($nodes);
  }
903214
protected function parseStartOption(\Twig_Token $token, \Twig_TokenStream $stream)
    {
        $this->testToken('start', $token, $stream);

        return $this->getNextExpectedStringValueFromStream($stream, \Twig_Token::NUMBER_TYPE);
    }
1006582
public function parse(AbstractStream $stream)
    {
        try {
            return parent::parse($stream);
        } catch (\OutOfBoundsException $e) {
            if ($this->isUntilEof()) {
                return $this->dataSet->getData();
            }

            throw $e;
        }
    }
317423
public function prepare(TokenStream $stream) :void
    {
        $this->root = $this->scope = new RootNode();
        $this->stream = $stream;
        $this->lastTokenIndex = max(0, count($this->stream->getTokens()) - 1);
        $this->cursor = 0;
    }
501098
private function parseTypeAlias(Stream $stream)
    {
        $alias = $this->parseTypeIdentifier($stream);

        while ($stream->current()->equals(Token::TYPE_SYMBOL, '[')) {
            $stream->expects(Token::TYPE_SYMBOL, '[');
            $stream->expects(Token::TYPE_SYMBOL, ']');

            $alias = sprintf('%s[]', $alias);
        }

        return $alias;
    }
1275831
protected function parseBody(TokenStream $stream)
    {
        $visibility = $this->skip($stream)->expect(array(Lexer::TOKEN_PUBL, Lexer::TOKEN_PROT, Lexer::TOKEN_PRIV));
        $name = $this->skip($stream)->expect(array(Lexer::TOKEN_NODE));

        if ($this->skip($stream)->is(Lexer::TOKEN_METO)) {
            $this->parseMethod($stream, $visibility, $name);
        } else {
            $this->parseProperty($stream, $visibility, $name);
        }
    }
1202416
public function parse(Stream $stream)
    {
        $this->operatorStack = new \SplStack();
        $this->operandStack  = new \SplStack();
        $this->stream        = $stream;

        return $this->parseExpression(true);
    }
1059855
public function supportsExpr(Stream $stream)
    {
        $token = $stream->current();

        return !$token->is(Token::TYPE_KEYWORD) && ($token->is(Token::TYPE_STRING) || $token->is(Token::TYPE_NUMBER) || $token->is(Token::TYPE_CONSTANT) || $token->is(Token::TYPE_IDENTIFIER) || $token->equals(Token::TYPE_SYMBOL, '{') || $token->equals(Token::TYPE_SYMBOL, '['));
    }
1014470
private function parseClass(Stream $stream)
    {
        $stream->expects(Token::TYPE_KEYWORD, 'new');

        $class = [$stream->expects(Token::TYPE_IDENTIFIER)->getValue()];

        while ($stream->current()->equals(Token::TYPE_SYMBOL, '\\')) {
            $stream->expects(Token::TYPE_SYMBOL, '\\');

            $class[] = $stream->expects(Token::TYPE_IDENTIFIER)->getValue();
        }

        $class = implode('\\', $class);

        $arguments = [];

        if ($stream->current()->equals(Token::TYPE_SYMBOL, '(')) {
            $stream->expects(Token::TYPE_SYMBOL, '(');

            if (!$stream->current()->equals(Token::TYPE_SYMBOL, ')')) {
                $arguments = $this->parseList($stream);
            }

            $stream->expects(Token::TYPE_SYMBOL, ')');
        }

        return new ClassNode($class, $arguments);
    }
1091714
public function parse(Token $token): CellNode
    {
        $stream = $this->parser->getStream();

        $variable = null;
        if ($stream->test(Token::NAME_TYPE)) {
            $variable = $stream->expect(Token::NAME_TYPE)->getValue();
        }
        $assign = false;
        if ($stream->test(Token::OPERATOR_TYPE)) {
            $stream->expect(Token::OPERATOR_TYPE, '=');
            $assign = true;
        }

        $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 CellNode($assign, $variable, $name, $data, $options, $token->getLine(), $this->getTag());
    }
244919
private function parseSimpleSelector(TokenStream $stream, bool $insideNegation = false): array
    {
        $stream->skipWhitespace();

        $selectorStart = \count($stream->getUsed());
        $result = $this->parseElementNode($stream);
        $pseudoElement = null;

        while (true) {
            $peek = $stream->getPeek();
            if ($peek->isWhitespace()
                || $peek->isFileEnd()
                || $peek->isDelimiter([',', '+', '>', '~'])
                || ($insideNegation && $peek->isDelimiter([')']))
            ) {
                break;
            }

            if (null !== $pseudoElement) {
                throw SyntaxErrorException::pseudoElementFound($pseudoElement, 'not at the end of a selector');
            }

            if ($peek->isHash()) {
                $result = new Node\HashNode($result, $stream->getNext()->getValue());
            } elseif ($peek->isDelimiter(['.'])) {
                $stream->getNext();
                $result = new Node\ClassNode($result, $stream->getNextIdentifier());
            } elseif ($peek->isDelimiter(['['])) {
                $stream->getNext();
                $result = $this->parseAttributeNode($result, $stream);
            } elseif ($peek->isDelimiter([':'])) {
                $stream->getNext();

                if ($stream->getPeek()->isDelimiter([':'])) {
                    $stream->getNext();
                    $pseudoElement = $stream->getNextIdentifier();

                    continue;
                }

                $identifier = $stream->getNextIdentifier();
                if (\in_array(strtolower($identifier), ['first-line', 'first-letter', 'before', 'after'])) {
                    // Special case: CSS 2.1 pseudo-elements can have a single ':'.
                    // Any new pseudo-element must have two.
                    $pseudoElement = $identifier;

                    continue;
                }

                if (!$stream->getPeek()->isDelimiter(['('])) {
                    $result = new Node\PseudoNode($result, $identifier);

                    continue;
                }

                $stream->getNext();
                $stream->skipWhitespace();

                if ('not' === strtolower($identifier)) {
                    if ($insideNegation) {
                        throw SyntaxErrorException::nestedNot();
                    }

                    list($argument, $argumentPseudoElement) = $this->parseSimpleSelector($stream, true);
                    $next = $stream->getNext();

                    if (null !== $argumentPseudoElement) {
                        throw SyntaxErrorException::pseudoElementFound($argumentPseudoElement, 'inside ::not()');
                    }

                    if (!$next->isDelimiter([')'])) {
                        throw SyntaxErrorException::unexpectedToken('")"', $next);
                    }

                    $result = new Node\NegationNode($result, $argument);
                } else {
                    $arguments = [];
                    $next = null;

                    while (true) {
                        $stream->skipWhitespace();
                        $next = $stream->getNext();

                        if ($next->isIdentifier()
                            || $next->isString()
                            || $next->isNumber()
                            || $next->isDelimiter(['+', '-'])
                        ) {
                            $arguments[] = $next;
                        } elseif ($next->isDelimiter([')'])) {
                            break;
                        } else {
                            throw SyntaxErrorException::unexpectedToken('an argument', $next);
                        }
                    }

                    if (empty($arguments)) {
                        throw SyntaxErrorException::unexpectedToken('at least one argument', $next);
                    }

                    $result = new Node\FunctionNode($result, $identifier, $arguments);
                }
            } else {
                throw SyntaxErrorException::unexpectedToken('selector', $peek);
            }
        }

        if (\count($stream->getUsed()) === $selectorStart) {
            throw SyntaxErrorException::unexpectedToken('selector', $stream->getPeek());
        }

        return [$result, $pseudoElement];
    }
1112034
public function parse(Token $token)
    {
        $parser = $this->parser;
        $stream = $parser->getStream();

        $default = null;
        $cases = [];
        $end = false;

        $name = $parser->getExpressionParser()->parseExpression();
        $stream->expect(Token::BLOCK_END_TYPE);
        $stream->expect(Token::TEXT_TYPE);
        $stream->expect(Token::BLOCK_START_TYPE);
        while (!$end) {
            /** @var Token $v */
            $v = $stream->next();
            switch ($v->getValue()) {
                case 'default':
                    $stream->expect(Token::BLOCK_END_TYPE);
                    $default = $parser->subparse([$this, 'decideIfEnd']);
                    break;

                case 'case':
                    $expr = $parser->getExpressionParser()->parseExpression();
                    $stream->expect(Token::BLOCK_END_TYPE);
                    $body = $parser->subparse([$this, 'decideIfFork']);
                    $cases[] = $expr;
                    $cases[] = $body;
                    break;

                case 'endswitch':
                    $end = true;
                    break;

                default:
                    $message = sprintf('Unexpected end of template. Twig was looking for the following tags "case", "default", or "endswitch" to close the "switch" block started at line %d)', $v->getLine());

                    throw new SyntaxError($message, $v->getLine());
            }
        }

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

        return new SwitchNode($name, new Node($cases), $default, $token->getLine(), $this->getTag());
    }
134157
private function subscribeStreamEvent($stream, $flag)
    {
        $key = (int) $stream;

        if (isset($this->streamEvents[$key])) {
            $event = $this->streamEvents[$key];
            $flags = $this->streamFlags[$key] |= $flag;

            event_del($event);
            event_set($event, $stream, EV_PERSIST | $flags, $this->streamCallback);
        } else {
            $event = event_new();

            event_set($event, $stream, EV_PERSIST | $flag, $this->streamCallback);
            event_base_set($event, $this->eventBase);

            $this->streamEvents[$key] = $event;
            $this->streamFlags[$key] = $flag;
        }

        event_add($event);
    }