예제 #1
0
 public function testSkip()
 {
     $text = "1 foo: bar ( 3 + double ) ";
     $tokens = new Tokenizer($text);
     $tokens->skip()->skip(T_STRING)->skip(':');
     try {
         $tokens->skip(T_STRING)->skip('(')->skip(':');
     } catch (\Exception $e) {
         $this->assertInstanceOf('Fenom\\Error\\UnexpectedTokenException', $e);
         $this->assertStringStartsWith("Unexpected token '3' in expression, expect ':'", $e->getMessage());
     }
     $this->assertTrue($tokens->valid());
     $this->assertSame("3", $tokens->current());
     $this->assertSame(T_LNUMBER, $tokens->key());
     $this->assertSame($tokens, $tokens->next());
     $tokens->next();
     $this->assertSame("double", $tokens->getAndNext());
     $this->assertSame(")", $tokens->current());
     $this->assertTrue($tokens->isLast());
     $this->assertSame($tokens, $tokens->next());
     $tokens->p = 1000;
     $this->assertSame($tokens, $tokens->next());
     $tokens->p = -1000;
     $this->assertSame($tokens, $tokens->back());
     $this->assertNull($tokens->undef);
 }
예제 #2
0
파일: Accessor.php 프로젝트: simuta/new_rep
 /**
  * Accessor {$.fetch(...)}
  * @param Tokenizer $tokens
  * @param Template $tpl
  * @return string
  */
 public static function fetch(Tokenizer $tokens, Template $tpl)
 {
     $tokens->skip('(');
     $name = $tpl->parsePlainArg($tokens, $static);
     if ($static) {
         if (!$tpl->getStorage()->templateExists($static)) {
             throw new \RuntimeException("Template {$static} not found");
         }
     }
     if ($tokens->is(',')) {
         $tokens->skip()->need('[');
         $vars = $tpl->parseArray($tokens) . ' + $var';
     } else {
         $vars = '$var';
     }
     $tokens->skip(')');
     return '$tpl->getStorage()->fetch(' . $name . ', ' . $vars . ')';
 }
예제 #3
0
파일: Template.php 프로젝트: simuta/new_rep
 /**
  * Parse string with or without variable
  *
  * @param Tokenizer $tokens
  * @throws UnexpectedTokenException
  * @return string
  */
 public function parseQuote(Tokenizer $tokens)
 {
     if ($tokens->is('"')) {
         $stop = $tokens->current();
         $_str = '"';
         $tokens->next();
         while ($t = $tokens->key()) {
             if ($t === T_ENCAPSED_AND_WHITESPACE) {
                 $_str .= $tokens->current();
                 $tokens->next();
             } elseif ($t === T_VARIABLE) {
                 if (strlen($_str) > 1) {
                     $_str .= '".';
                 } else {
                     $_str = "";
                 }
                 $_str .= '$var["' . substr($tokens->current(), 1) . '"]';
                 $tokens->next();
                 if ($tokens->is($stop)) {
                     $tokens->skip();
                     return $_str;
                 } else {
                     $_str .= '."';
                 }
             } elseif ($t === T_CURLY_OPEN) {
                 if (strlen($_str) > 1) {
                     $_str .= '".';
                 } else {
                     $_str = "";
                 }
                 $tokens->getNext(T_VARIABLE);
                 $_str .= '(' . $this->parseExpr($tokens) . ')';
                 if ($tokens->is($stop)) {
                     $tokens->next();
                     return $_str;
                 } else {
                     $_str .= '."';
                 }
             } elseif ($t === "}") {
                 $tokens->next();
             } elseif ($t === $stop) {
                 $tokens->next();
                 return $_str . '"';
             } else {
                 break;
             }
         }
         throw new UnexpectedTokenException($tokens);
     } elseif ($tokens->is(T_CONSTANT_ENCAPSED_STRING)) {
         return $tokens->getAndNext();
     } elseif ($tokens->is(T_ENCAPSED_AND_WHITESPACE)) {
         throw new UnexpectedTokenException($tokens);
     } else {
         return "";
     }
 }