Bond 0.9
C++ Bond Runtime Library API Documentation
Loading...
Searching...
No Matches
Bond::TokenStream Class Reference

An iterator over the contents of a TokenCollection. More...

#include <bond/compiler/tokenstream.h>

Public Member Functions

 TokenStream ()
 Constructs an empty TokenStream object. A buffer must be set with SetBuffer() before any Tokens are accessed.
 TokenStream (const Token *buffer, int length)
 Constructs a TokenStream object.
void SetBuffer (const Token *buffer, int length)
 Sets the underlying buffer of Token objects and resets the position of the stream.
void Reset ()
 Resets the position of the stream.
void Advance ()
 Increments the position of the stream by one, unless it is at the last Token.
const Token * Next ()
 Returns the next token in the stream and advances the position.
const Token * NextIf (Token::TokenType type)
 Returns the next token in the stream if it matches the given type, otherwise returns nullptr.
const Token * NextIf (const TokenTypeSet &typeSet)
 Returns the next token in the stream if it matches any of the given types, otherwise returns nullptr.
const Token * Peek () const
 Returns the next token in the stream without advancing the position.
const Token * PeekIf (Token::TokenType type) const
 Returns the next token in the stream if it matches the given type, otherwise returns nullptr.
const Token * PeekIf (const TokenTypeSet &typeSet) const
 Returns the next token in the stream if it matches any of the given types, otherwise returns nullptr.
void SkipTo (Token::TokenType type)
 Advances the position of the stream until a Token of the given type is encountered. If no such Token is encountered, the stream is advanced to the last Token.
void SkipTo (const TokenTypeSet &typeSet)
 Advances the position of the stream until a Token of one of the given types is encountered. If no such Token is encountered, the stream is advanced to the last Token.
int GetPosition () const
 Returns the current position of the stream.
void SetPosition (int index)
 Sets the current position of the stream.
int GetLength () const
 Returns the total number of tokens in the stream.

Detailed Description

An iterator over the contents of a TokenCollection.

TokenStream has methods to return the next available Token, test whether the next Token is of a certain type, peek at the next Token and manipulate the position in the stream.

The position of the stream never advances past the last Token, which for a TokenCollection generated by a Lexer is an END Token. Once the last Token is reached, Next() keeps returning it.

Constructor & Destructor Documentation

◆ TokenStream() [1/2]

Bond::TokenStream::TokenStream ( )
inline

Constructs an empty TokenStream object. A buffer must be set with SetBuffer() before any Tokens are accessed.

◆ TokenStream() [2/2]

Bond::TokenStream::TokenStream ( const Token * buffer,
int length )
inline

Constructs a TokenStream object.

Parameters
bufferA pointer to a buffer of Token objects over which the stream iterates. It is not copied, so it must outlive the TokenStream.
lengthThe number of Tokens contained in the buffer. It must be at least 1.

Member Function Documentation

◆ Advance()

void Bond::TokenStream::Advance ( )
inline

Increments the position of the stream by one, unless it is at the last Token.

◆ GetLength()

int Bond::TokenStream::GetLength ( ) const
inline

Returns the total number of tokens in the stream.

◆ GetPosition()

int Bond::TokenStream::GetPosition ( ) const
inline

Returns the current position of the stream.

◆ Next()

const Token * Bond::TokenStream::Next ( )

Returns the next token in the stream and advances the position.

◆ NextIf() [1/2]

const Token * Bond::TokenStream::NextIf ( const TokenTypeSet & typeSet)

Returns the next token in the stream if it matches any of the given types, otherwise returns nullptr.

Parameters
typeSetThe set of types against which the next Token is compared.

◆ NextIf() [2/2]

const Token * Bond::TokenStream::NextIf ( Token::TokenType type)

Returns the next token in the stream if it matches the given type, otherwise returns nullptr.

Parameters
typeThe type against which the next Token is compared.

◆ Peek()

const Token * Bond::TokenStream::Peek ( ) const
inline

Returns the next token in the stream without advancing the position.

◆ PeekIf() [1/2]

const Token * Bond::TokenStream::PeekIf ( const TokenTypeSet & typeSet) const

Returns the next token in the stream if it matches any of the given types, otherwise returns nullptr.

Parameters
typeSetThe set of types against which the next Token is compared.

◆ PeekIf() [2/2]

const Token * Bond::TokenStream::PeekIf ( Token::TokenType type) const

Returns the next token in the stream if it matches the given type, otherwise returns nullptr.

Parameters
typeThe type against which the next Token is compared.

◆ Reset()

void Bond::TokenStream::Reset ( )
inline

Resets the position of the stream.

◆ SetBuffer()

void Bond::TokenStream::SetBuffer ( const Token * buffer,
int length )

Sets the underlying buffer of Token objects and resets the position of the stream.

Parameters
bufferA pointer to a buffer of Token objects that the stream iterates over.
lengthThe number of Tokens contained in the buffer.

◆ SetPosition()

void Bond::TokenStream::SetPosition ( int index)
inline

Sets the current position of the stream.

Parameters
indexThe position that the stream is set to. It is clamped to the range [0, GetLength() - 1].

◆ SkipTo() [1/2]

void Bond::TokenStream::SkipTo ( const TokenTypeSet & typeSet)

Advances the position of the stream until a Token of one of the given types is encountered. If no such Token is encountered, the stream is advanced to the last Token.

Parameters
typeSetThe set of types against which the Tokens are compared.

◆ SkipTo() [2/2]

void Bond::TokenStream::SkipTo ( Token::TokenType type)

Advances the position of the stream until a Token of the given type is encountered. If no such Token is encountered, the stream is advanced to the last Token.

Parameters
typeThe type against which the Tokens are compared.

The documentation for this class was generated from the following file:
  • include/bond/compiler/tokenstream.h