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); }
/** * 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 ""; } }