final class BisonTokenStream
Restricted visibility: declared "@visibility root". Code outside that scope must not name this declaration.

Adds lookahead to a Bison lexer.

A recursive-descent parser has to decide what it is reading before it commits to reading it, which means looking at tokens it does not want to consume yet. Buffering those here keeps the lexer a plain forward scan: nothing in it has to know that a token may be read twice.

Methods§

public function __construct(BisonLexer $lexer)

Parameters

$lexerBisonLexerSource of tokens
Test cases 101
public static function over(string $source): self

Builds a stream over grammar source text.

Parameters

$sourcestringBison grammar text

Returns

self A stream reading that source with the default lexeme rules
Test cases 178
Called from 1
Calls 2
public function next(): BisonToken

Consumes the next token.

Returns

BisonToken The token, taken from the lookahead buffer when one is waiting

Throws

GrammarParseException When the source cannot be tokenized
Test cases 96
Called from 15
Calls 2
public function peek(): BisonToken

Reads the next token without consuming it.

Returns

BisonToken The token that the next call to next() will return

Throws

GrammarParseException When the source cannot be tokenized
Test cases 8
Called from 14
Calls 1
public function peekN(int $distance): BisonToken

Reads a token further ahead without consuming anything.

Parameters

$distanceintHow many tokens ahead to look, counting from one

Returns

BisonToken The token at that distance

Throws

InvalidArgumentException When the distance is less than one
GrammarParseException When the source cannot be tokenized
Test cases 13
Called from 3
Calls 3
public function nextIf(BisonLexeme ...$accepted): BisonToken|null

Consumes the next token only when it is one of the given kinds.

Reading a grammar is mostly "take this if it is there": a type tag, an explicit token code, an alias. Asking the stream keeps that one step, so a reader cannot look at one token and then consume another.

Parameters

$acceptedBisonLexemeKinds the caller is willing to take

Returns

BisonToken|null The consumed token, or null with the stream unmoved

Throws

GrammarParseException When the source cannot be tokenized
Test cases 4
Called from 15
Calls 3
public function nextString(): string

Consumes the next token and reads its value as a string.

Returns

string The token value, with a numeric value rendered as digits

Throws

GrammarParseException When the source cannot be tokenized
Called from 9
Calls 1
public function nextInt(): int

Consumes the next token and reads its value as an integer.

Returns

int The token value, with a non-numeric value read as zero

Throws

GrammarParseException When the source cannot be tokenized
Test cases 2
Calls 1
public function consumeRemaining(): string

Consumes the rest of the source as raw text.

Buffered lookahead is discarded rather than prepended: the cursor has already moved past those tokens, so the text they came from is behind the point the epilogue starts at.

Returns

string Everything left after the tokens already scanned
Test cases 2
Calls 1

Private surface 2§

Implementation details, listed for orientation only.

private BisonLexer $lexer
private list<BisonToken> $lookahead = []

Test cases 179§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 102
Other tests reaching this symbol 77

Relations§

Static calls 1
Method calls 51
Type declarations 21