Exemplo n.º 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());
 }
Exemplo n.º 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());
 }
Exemplo n.º 3
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);
 }
Exemplo n.º 4
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());
     }
 }
Exemplo n.º 5
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);
 }
Exemplo n.º 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, '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());
 }
Exemplo n.º 7
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);
 }
Exemplo n.º 8
0
 public function testToken()
 {
     $token = new Token(null, '"Masoud Zohrabi"', TokenTypes::T_STR, 0, 0);
     $this->assertTrue($token->test(TokenTypes::T_STR));
     $this->assertTrue($token->test(TokenTypes::T_STR, '"Masoud Zohrabi"'));
 }
Exemplo n.º 9
0
 /**
  * @param Token  $token
  * @param Parser $parser
  * @return bool
  */
 public function canParse(Token $token, Parser $parser)
 {
     return $token->is(TokenTypes::T_SECTION_START);
 }
Exemplo n.º 10
0
 /**
  * @param Token $token
  * @return int
  */
 protected function unaryPrecedence(Token $token)
 {
     return $this->getUnaryOperator($token->getValue())->getPrecedence() ?: 10;
 }
Exemplo n.º 11
0
 /**
  * @param Token $token
  * @param Parser $parser
  * @return bool
  */
 public function canParse(Token $token, Parser $parser)
 {
     return $token->test($this->getStartTokenType(), [$this->getSectionName()]);
 }
Exemplo n.º 12
0
 /**
  * @param Token  $token
  * @param Parser $parser
  * @return bool
  */
 public function canParse(Token $token, Parser $parser)
 {
     return $token->is(TokenTypes::T_RAW);
 }