class TokenStream

internal  
 

The tokens of one statement, read in runs and never past its terminator.

A token is its source offset and upper-case source text; its type is derived from the text when first needed unless the lexer recorded it. Tokens that were read are kept, so a parser can look behind the current one or rewind. Source skipped by skipGroup() or skipRest() is never read, so it cannot be replayed.

Constants

private OPERATORS

Keyword operators that take an operand after them, in the expressions that clause keywords can end.

Methods

__construct(Lexer $lexer, int $start)

Begin reading a statement at its first token.

void
next()

Consume the current token.

TokenType
type(int $ahead = 0)

Get the type of the current token or one after it.

int
offset(int $ahead = 0)

Get the source offset of the current token or one after it.

string|null
keyword(int $ahead = 0)

Get the upper-case keyword of the current token or one after it, or null when it is not a word.

string
text(int $ahead = 0)

Get the upper-case source text of the current token or one after it, or an empty string for the terminator.

bool
is(string $keyword, string ...$following)

Determine whether the next tokens are the given sequence of keywords, without consuming them.

bool
isSymbol(string $symbol, int $ahead = 0)

Determine whether the current token, or one before or after it, is the given punctuation.

bool
accept(string $keyword, string ...$following)

Consume a sequence of keywords when all of them are next.

string|null
acceptName()

Consume the current token and get its decoded value when it is a word or quoted identifier.

bool
acceptSymbol(string $symbol)

Consume the given punctuation when it is next.

void
expectSymbol(string $symbol, string $description)

Consume required punctuation.

void
skipGroup()

Consume a group from its opening parenthesis, the current token, through the matching parenthesis.

bool
skipExpression(array $keywords, bool $afterOperand = false)

Consume an expression that ends before a top-level comma, an unmatched closing bracket or one of the given keywords.

int
mark()

Get a mark of the current position to return to.

void
rewind(int $mark)

Return to a marked position within the statement.

bool
atEnd()

Determine whether the statement has no more tokens.

string
value(int $ahead = 0)

Get the decoded value of the current token or one after it.

string
spanFrom(int $start)

Get the source text from an offset through the most recently consumed token.

void
skipRest()

Skip the unread remainder of the statement without reading its tokens.

error(string $message, int $ahead = 0)

Create a parse error located at the current token or one after it.

Details

at line 58
__construct(Lexer $lexer, int $start)

Begin reading a statement at its first token.

Parameters

Lexer $lexer
int $start

at line 68
void next()

Consume the current token.

Return Value

void

at line 80
TokenType type(int $ahead = 0)

Get the type of the current token or one after it.

Parameters

int $ahead

Return Value

TokenType

at line 92
int offset(int $ahead = 0)

Get the source offset of the current token or one after it.

Parameters

int $ahead

Return Value

int

at line 102
string|null keyword(int $ahead = 0)

Get the upper-case keyword of the current token or one after it, or null when it is not a word.

Parameters

int $ahead

Return Value

string|null

at line 114
string text(int $ahead = 0)

Get the upper-case source text of the current token or one after it, or an empty string for the terminator.

Parameters

int $ahead

Return Value

string

at line 124
bool is(string $keyword, string ...$following)

Determine whether the next tokens are the given sequence of keywords, without consuming them.

Parameters

string $keyword
string ...$following

Return Value

bool

at line 134
bool isSymbol(string $symbol, int $ahead = 0)

Determine whether the current token, or one before or after it, is the given punctuation.

Parameters

string $symbol
int $ahead

Return Value

bool

at line 144
bool accept(string $keyword, string ...$following)

Consume a sequence of keywords when all of them are next.

Parameters

string $keyword
string ...$following

Return Value

bool

at line 161
string|null acceptName()

Consume the current token and get its decoded value when it is a word or quoted identifier.

Return Value

string|null

at line 181
bool acceptSymbol(string $symbol)

Consume the given punctuation when it is next.

Parameters

string $symbol

Return Value

bool

at line 195
void expectSymbol(string $symbol, string $description)

Consume required punctuation.

Parameters

string $symbol
string $description

Return Value

void

at line 207
void skipGroup()

Consume a group from its opening parenthesis, the current token, through the matching parenthesis.

Return Value

void

at line 249
bool skipExpression(array $keywords, bool $afterOperand = false)

Consume an expression that ends before a top-level comma, an unmatched closing bracket or one of the given keywords.

Brackets and CASE ... END nest. A keyword ends the expression only after a complete operand, so DEFAULT NULL keeps its NULL and 1 + NULL keeps both operands, and a word before a qualification dot is a name. Returns whether anything was consumed.

Parameters

array $keywords
bool $afterOperand

whether the caller has already consumed an operand of this expression

Return Value

bool

at line 348
int mark()

Get a mark of the current position to return to.

Return Value

int

at line 358
void rewind(int $mark)

Return to a marked position within the statement.

A group consumed by skipGroup() keeps only its brackets as tokens, so a mark cannot be reused across one.

Parameters

int $mark

Return Value

void

at line 366
bool atEnd()

Determine whether the statement has no more tokens.

Return Value

bool

at line 376
string value(int $ahead = 0)

Get the decoded value of the current token or one after it.

Parameters

int $ahead

Return Value

string

at line 388
string spanFrom(int $start)

Get the source text from an offset through the most recently consumed token.

Parameters

int $start

Return Value

string

at line 398
void skipRest()

Skip the unread remainder of the statement without reading its tokens.

Return Value

void

at line 415
ParseException error(string $message, int $ahead = 0)

Create a parse error located at the current token or one after it.

Parameters

string $message
int $ahead

Return Value

ParseException