/** {@inheritDoc} */
 public function parse(\Twig_Token $token)
 {
     $stream = $this->parser->getStream();
     $lineno = $stream->getCurrent()->getLine();
     // Export type: "js" or "css"
     $export_type = $stream->expect(\Twig_Token::NAME_TYPE)->getValue();
     if (!in_array($export_type, ['js', 'css', 'inline'])) {
         // This exception will include the template filename by itself.
         throw new \Twig_Error_Syntax(sprintf('Expected export type "inline", "js" or "css", got "%s" at line %d.', $export_type, $lineno));
     }
     if ($export_type === "inline") {
         return $this->parseInline($stream, $lineno);
     }
     return $this->parseType($stream, $lineno, $export_type);
 }
 public function parseTestExpression(Twig_Parser $parser, $node)
 {
     $stream = $parser->getStream();
     $name = $stream->expect(Twig_Token::NAME_TYPE);
     $arguments = null;
     if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
         $arguments = $parser->getExpressionParser()->parseArguments();
     }
     return new Twig_Node_Expression_Test($node, $name->getValue(), $arguments, $parser->getCurrentToken()->getLine());
 }
Example #3
0
 public function parseTestExpression(Twig_Parser $parser, $node)
 {
     $stream = $parser->getStream();
     $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
     $arguments = null;
     if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
         $arguments = $parser->getExpressionParser()->parseArguments();
     }
     $class = $this->getTestNodeClass($parser->getEnvironment(), $name);
     return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine());
 }
Example #4
0
 protected function getTestName(Twig_Parser $parser, $line)
 {
     $stream = $parser->getStream();
     $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
     $env = $parser->getEnvironment();
     $testMap = $env->getTests();
     if (isset($testMap[$name])) {
         return $name;
     }
     if ($stream->test(Twig_Token::NAME_TYPE)) {
         // try 2-words tests
         $name = $name . ' ' . $parser->getCurrentToken()->getValue();
         if (isset($testMap[$name])) {
             $parser->getStream()->next();
             return $name;
         }
     }
     $message = sprintf('The test "%s" does not exist', $name);
     if ($alternatives = $env->computeAlternatives($name, array_keys($testMap))) {
         $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
     }
     throw new Twig_Error_Syntax($message, $line, $parser->getFilename());
 }
Example #5
0
File: Core.php Project: TiGR/Twig
 protected function getTestNodeClass(Twig_Parser $parser, $name, $line)
 {
     $env = $parser->getEnvironment();
     $testMap = $env->getTests();
     $testName = null;
     if (isset($testMap[$name])) {
         $testName = $name;
     } elseif ($parser->getStream()->test(Twig_Token::NAME_TYPE)) {
         // try 2-words tests
         $name = $name . ' ' . $parser->getCurrentToken()->getValue();
         if (isset($testMap[$name])) {
             $parser->getStream()->next();
             $testName = $name;
         }
     }
     if (null === $testName) {
         $message = sprintf('The test "%s" does not exist', $name);
         if ($alternatives = $env->computeAlternatives($name, array_keys($env->getTests()))) {
             $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
         }
         throw new Twig_Error_Syntax($message, $line, $parser->getFilename());
     }
     if ($testMap[$name] instanceof Twig_SimpleTest) {
         return $testMap[$name]->getNodeClass();
     }
     return $testMap[$name] instanceof Twig_Test_Node ? $testMap[$name]->getClass() : 'Twig_Node_Expression_Test';
 }
Example #6
0
 protected function getTest(Twig_Parser $parser, $line)
 {
     $stream = $parser->getStream();
     $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
     $env = $parser->getEnvironment();
     if ($test = $env->getTest($name)) {
         return array($name, $test);
     }
     if ($stream->test(Twig_Token::NAME_TYPE)) {
         // try 2-words tests
         $name = $name . ' ' . $parser->getCurrentToken()->getValue();
         if ($test = $env->getTest($name)) {
             $parser->getStream()->next();
             return array($name, $test);
         }
     }
     $e = new Twig_Error_Syntax(sprintf('Unknown "%s" test.', $name), $line, $parser->getFilename());
     $e->addSuggestions($name, array_keys($env->getTests()));
     throw $e;
 }
 public function moveToNextNode(\Twig_Parser $parser)
 {
     $parser->getStream()->next();
     $parser->getStream()->expect(\Twig_Token::BLOCK_START_TYPE);
 }