You are here

public function Twig_Parser::parse in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/twig/twig/lib/Twig/Parser.php \Twig_Parser::parse()

Converts a token stream to a node tree.

Parameters

Twig_TokenStream $stream A token stream instance:

Return value

Twig_Node_Module A node tree

Throws

Twig_Error_Syntax When the token stream is syntactically or semantically wrong

Overrides Twig_ParserInterface::parse

File

vendor/twig/twig/lib/Twig/Parser.php, line 63

Class

Twig_Parser
Default parser implementation.

Code

public function parse(Twig_TokenStream $stream, $test = null, $dropNeedle = false) {

  // push all variables into the stack to keep the current state of the parser
  $vars = get_object_vars($this);
  unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser'], $vars['reservedMacroNames']);
  $this->stack[] = $vars;

  // tag handlers
  if (null === $this->handlers) {
    $this->handlers = $this->env
      ->getTokenParsers();
    $this->handlers
      ->setParser($this);
  }

  // node visitors
  if (null === $this->visitors) {
    $this->visitors = $this->env
      ->getNodeVisitors();
  }
  if (null === $this->expressionParser) {
    $this->expressionParser = new Twig_ExpressionParser($this, $this->env
      ->getUnaryOperators(), $this->env
      ->getBinaryOperators());
  }
  $this->stream = $stream;
  $this->parent = null;
  $this->blocks = array();
  $this->macros = array();
  $this->traits = array();
  $this->blockStack = array();
  $this->importedSymbols = array(
    array(),
  );
  $this->embeddedTemplates = array();
  try {
    $body = $this
      ->subparse($test, $dropNeedle);
    if (null !== $this->parent && null === ($body = $this
      ->filterBodyNodes($body))) {
      $body = new Twig_Node();
    }
  } catch (Twig_Error_Syntax $e) {
    if (!$e
      ->getTemplateFile()) {
      $e
        ->setTemplateFile($this
        ->getFilename());
    }
    if (!$e
      ->getTemplateLine()) {
      $e
        ->setTemplateLine($this->stream
        ->getCurrent()
        ->getLine());
    }
    throw $e;
  }
  $node = new Twig_Node_Module(new Twig_Node_Body(array(
    $body,
  )), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->embeddedTemplates, $this
    ->getFilename());
  $traverser = new Twig_NodeTraverser($this->env, $this->visitors);
  $node = $traverser
    ->traverse($node);

  // restore previous stack so previous parse() call can resume working
  foreach (array_pop($this->stack) as $key => $val) {
    $this->{$key} = $val;
  }
  return $node;
}