示例#1
0
 /**
  * @param Token  $token
  * @param Parser $parser
  * @return Node
  */
 public function parse(Token $token, Parser $parser)
 {
     $stream = $parser->getStream();
     $stream->expect(TokenTypes::T_BLOCK_PRINT_OPEN);
     $expr = $parser->parseExpression();
     $stream->expect(TokenTypes::T_BLOCK_PRINT_CLOSE);
     return new Node\PrintNode($expr, $token->getLine());
 }
示例#2
0
 /**
  * @param Token  $token
  * @param Parser $parser
  * @return Node
  */
 public function parse(Token $token, Parser $parser)
 {
     $stream = $parser->getStream();
     $stream->expect(TokenTypes::T_COMMENT_START);
     $comment = $stream->expect(TokenTypes::T_COMMENT);
     $stream->expect(TokenTypes::T_COMMENT_END);
     return new CommentNode($comment->getValue(), $token->getLine());
 }
示例#3
0
 /**
  * @dataProvider dataCompile
  * @param $template
  * @param $compiled
  * @throws \Azera\Fry\Exception\Exception
  * @throws \Azera\Fry\Exception\LexerException
  */
 public function testCompiles($template, $compiled)
 {
     $env = $this->getEnvironment()->addExtension(new Core());
     $lexer = new Lexer(new Reader($template), $env);
     $parser = new Parser($lexer->tokenize()->getStream(), $env);
     $bodyNode = $parser->parse()->getBody();
     $compiler = new Compiler($this->getEnvironment());
     $output = $compiler->compile($bodyNode);
     $this->assertEquals(trim($compiled), trim($output));
 }
示例#4
0
 /**
  * @param Token  $token
  * @param Parser $parser
  * @return Node
  * @throws Exception
  * @throws \Azera\Fry\Exception\SyntaxException
  */
 public function parse(Token $token, Parser $parser)
 {
     $stream = $parser->getStream();
     $stream->expect(TokenTypes::T_SECTION_TYPE, self::EXTENDS_TAG);
     $parent = $parser->parseExpression();
     if (!$parent instanceof Node\Expression\Constant || $parent->getType() !== Node\Expression\Constant::TYPE_STRING) {
         throw new Exception('Invalid view layout value, layout must be a string. in line %d', $token->getLine());
     }
     $parser->setParent($parent);
     $stream->expect(TokenTypes::T_SECTION_OPEN);
 }
示例#5
0
 /**
  * @param Token  $token
  * @param Parser $parser
  * @return Node
  * @throws SyntaxException
  */
 public function parse(Token $token, Parser $parser)
 {
     $stream = $parser->getStream();
     $stream->expect(TokenTypes::T_SECTION_START);
     $token = $stream->getCurrent();
     foreach ($this->sections as $section) {
         if ($section->canParse($token, $parser)) {
             return $section->parse($token, $parser);
         }
     }
     throw new SyntaxException('Unexpected section, "%s" not defined.', $token->getValue());
 }
示例#6
0
 /**
  * @param Token  $token
  * @param Parser $parser
  * @return Node
  */
 public function parse(Token $token, Parser $parser)
 {
     $stream = $parser->getStream();
     $stream->expect(TokenTypes::T_SECTION_TYPE, 'set');
     $var = $stream->expect(TokenTypes::T_NAME, null, 'Variable name is empty');
     if ($stream->test(TokenTypes::T_SET)) {
         $stream->expect(TokenTypes::T_SET);
         $exp = $parser->parseExpression();
         $stream->expect(TokenTypes::T_SECTION_OPEN);
         return new Node\SetNode($var->getValue(), $exp, false, $token->getLine());
     } else {
         $stream->expect(TokenTypes::T_SECTION_OPEN, '{');
         $value = $parser->subparse([$this, 'decideIfEnd']);
         $stream->expect(TokenTypes::T_SECTION_CLOSE, '}');
         return new Node\SetNode($var->getValue(), $value, true, $token->getLine());
     }
 }
示例#7
0
 /**
  * @param Token  $token
  * @param Parser $parser
  * @return Node
  */
 public function parse(Token $token, Parser $parser)
 {
     $stream = $parser->getStream();
     $stream->expect(TokenTypes::T_SECTION_TYPE, 'for');
     $key = $value = null;
     $value = $stream->expect(TokenTypes::T_NAME, null, 'Loop value variable not defined.');
     if ($stream->test(TokenTypes::T_COMMA)) {
         $stream->expect(TokenTypes::T_COMMA);
         $key = $value;
         $value = $stream->expect(TokenTypes::T_NAME, null, 'Loop value variable not defined.');
     }
     $stream->expect(TokenTypes::T_KEYWORD, 'in');
     $repo = $parser->parseExpression();
     $stream->expect(TokenTypes::T_SECTION_OPEN, '{');
     $body = $parser->subparse([$this, 'decideIfEnd']);
     $stream->expect(TokenTypes::T_SECTION_CLOSE, '}');
     return new Node\LoopNode($key ? $key->getValue() : null, $value->getValue(), $repo, $body, $token->getLine());
 }
 /**
  * @param Parser $parser
  * @return \Azera\Fry\Node
  * @throws \Azera\Fry\Exception\Exception
  */
 public function parseBody(Parser $parser)
 {
     $stream = $parser->getStream();
     $isBrace = false;
     if ($this->endType() == self::END_TYPE_BRACE) {
         $isBrace = !!$stream->expect(TokenTypes::T_SECTION_OPEN, '{');
     } elseif ($this->endType() == self::END_TYPE_BOTH && $stream->test(TokenTypes::T_SECTION_OPEN, '{')) {
         $isBrace = !!$stream->next();
     } else {
         $stream->expect(TokenTypes::T_SECTION_OPEN);
     }
     $body = $parser->subparse([$this, $isBrace ? 'testEndBrace' : 'testEnd']);
     if ($isBrace) {
         $stream->expect(TokenTypes::T_SECTION_CLOSE, '}');
     } else {
         $stream->expect(TokenTypes::T_SECTION_TYPE, $this->getSectionEnd());
         $stream->expect(TokenTypes::T_SECTION_OPEN);
     }
     return $body;
 }
示例#9
0
 /**
  * @param Token  $token
  * @param Parser $parser
  * @return Node
  */
 public function parse(Token $token, Parser $parser)
 {
     $line = $token->getLine();
     $stream = $parser->getStream();
     $stream->expect(null, [self::BLOCK_START]);
     $blockName = $stream->expect(TokenTypes::T_NAME, null, 'Missing block name');
     $endDecider = 'decideIfEnd';
     if ($stream->test(TokenTypes::T_SECTION_OPEN, '{')) {
         $endDecider = 'decideIfBraceEnd';
     }
     $stream->expect(TokenTypes::T_SECTION_OPEN);
     $body = $parser->subparse([$this, $endDecider]);
     if ($endDecider == 'decideIfEnd') {
         $stream->expect(TokenTypes::T_SECTION_TYPE, 'endblock');
         $stream->expect(TokenTypes::T_SECTION_OPEN);
     } else {
         $stream->expect(TokenTypes::T_SECTION_CLOSE, '}');
     }
     $parser->setBlock($blockName->getValue(), new Node\Block($body, $blockName->getValue(), $line));
     return new Node\PrintNode(new Node\RenderBlock($blockName->getValue(), new Node\Expression\Arguments(), $line), $line);
 }
示例#10
0
 /**
  * @param Token  $token
  * @param Parser $parser
  * @return Node
  * @throws SyntaxException
  * @throws \Azera\Fry\Exception\Exception
  */
 public function parse(Token $token, Parser $parser)
 {
     $line = $token->getLine();
     $stream = $parser->getStream();
     $stream->expect(TokenTypes::T_SECTION_TYPE, 'if');
     $expr = $parser->parseExpression();
     if ($expr == null) {
         throw new SyntaxException("If statement must have an expression, on line %d column %d", $token->getLine(), $token->getColumn());
     }
     $openToken = $stream->expect(TokenTypes::T_SECTION_OPEN);
     $body = $parser->subparse([$this, $openToken->getValue() == '{' ? 'decideIfBraceEnd' : 'decideIfFork']);
     $else = null;
     $tests = [$expr, $body];
     if ($openToken->getValue() == '{') {
         $stream->expect(TokenTypes::T_SECTION_CLOSE, '}');
     } else {
         $end = false;
         while (!$end) {
             $token = $stream->getToken();
             if (!$stream->isEOF()) {
                 $stream->next();
             }
             switch ($token->getValue()) {
                 case 'else':
                     $stream->expect(TokenTypes::T_SECTION_OPEN);
                     $else = $parser->subparse([$this, 'decideIfEnd']);
                     break;
                 case 'elseif':
                     $expr = $parser->parseExpression();
                     $stream->expect(TokenTypes::T_SECTION_OPEN);
                     $body = $parser->subparse([$this, 'decideIfFork']);
                     $tests[] = $expr;
                     $tests[] = $body;
                     break;
                 case 'endif':
                     $stream->expect(TokenTypes::T_SECTION_OPEN);
                     $end = true;
                     break;
                 default:
                     throw new SyntaxException('Unexpected end of template, if statement end not found');
             }
         }
     }
     return new Node\IfSection($tests, $else, $line);
 }
示例#11
0
 public function hasUnaryOperator($name)
 {
     return $this->parser->getEnvironment()->hasUnaryOperator($name);
 }
示例#12
0
 public function compileSource($source, $fileName)
 {
     $lexer = new Lexer(new Reader($source, $fileName), $this);
     $parser = new Parser($lexer->tokenize()->getStream(), $this);
     $compiler = new Compiler($this);
     $compiled = $compiler->compile($parser->parse());
     return $compiled;
 }
示例#13
0
 /**
  * @param Token  $token
  * @param Parser $parser
  * @return Node
  */
 public function parse(Token $token, Parser $parser)
 {
     $token = $parser->getStream()->expect(TokenTypes::T_RAW);
     return new Node\Text($token->getName(), $token->getLine());
 }