protected function parseError(TokenStream $ts, Parser $parser, $msg) { $current = $ts->current(); $result = $parser->onCommit(function ($commit) use($parser) { $this->fail("Unexpected commit on {$parser->type()}()."); })->withErrorLevel(Error::ENABLED)->parse($ts); $this->assertSame($current, $ts->current()); $this->assertInstanceOf(Error::class, $result); $this->assertSame(implode(PHP_EOL, (array) $msg), $result->message()); }
function concat(TokenStream $ts) : TokenStream { $buffer = []; while ($t = $ts->current()) { $str = (string) $t; if (!preg_match('/^\\w+$/', $str)) { throw new YayException("Only valid identifiers are mergeable, '{$t->dump()}' given."); } $buffer[] = $str; $ts->next(); } return TokenStream::fromSequence(new Token(T_STRING, implode('', $buffer))); }
protected function parseDictKey(TokenStream $stream) { $stream->expect(array(Tokens::T_IDENTIFIER, Tokens::T_STRING)); $token = $stream->current(); switch ($token->name) { case Tokens::T_STRING: $key = $this->cleanString($token->value); break; case Tokens::T_IDENTIFIER: default: $key = $token->value; break; } $stream->next(); return $key; }
private function parseIdentifier(TokenStream $tokens) { $cur = $tokens->current(); $node = new Node\IdentifierNode($cur->getValue(), $cur); $tokens->next(); return $node; }