Pidgin

Struct ParseState<TToken>

Represents the state of a parsing process. Includes functionality managing and buffering the input stream, reporting errors, and computing source positions.

For efficiency, this object is implemented as a mutable struct and is intended to be passed by reference.

WARNING: This API is unstable and subject to change in future versions of the library.

Inherited Members
Declaration
public ref struct ParseState<TToken>
Type Parameters
Name Description

TToken

The type of tokens consumed by the parser.

Properties

Configuration

Gets the parser configuration.

Declaration
public readonly IConfiguration<TToken> Configuration { get; }
Property Value
Type Description

IConfiguration<TToken>

Current

Returns the current token.

Declaration
public TToken Current { get; }
Property Value
Type Description

TToken

HasCurrent

Returns true if the parser has not reached the end of the input.

Declaration
public bool HasCurrent { get; }
Property Value
Type Description

bool

Location

Returns the total number of tokens which have been consumed. In other words, the current absolute offset of the input stream.

Declaration
public int Location { get; }
Property Value
Type Description

int

Methods

Advance(int)

Advance the input stream by count tokens.

Declaration
public void Advance(int count = 1)
Parameters
Type Name Description

int

count

The number of tokens to advance.

Bookmark()

Start buffering the input.

Declaration
public int Bookmark()
Returns
Type Description

int

The location of the bookmark.

DiscardBookmark(int)

Stop buffering the input.

Declaration
public void DiscardBookmark(int bookmark)
Parameters
Type Name Description

int

bookmark

The location of the bookmark.

LookAhead(int)

Returns a Span<T> containing the next count tokens.

This method may return a span shorter than count, if the parser reaches the end of the input stream.

Declaration
public ReadOnlySpan<TToken> LookAhead(int count)
Parameters
Type Name Description

int

count

The number of tokens to advance.

Returns
Type Description

ReadOnlySpan<TToken>

A ReadOnlySpan<T> containing the tokens.

Rewind(int)

Return to a bookmark previously obtained from Bookmark() and discard it.

Declaration
public void Rewind(int bookmark)
Parameters
Type Name Description

int

bookmark

The location of the bookmark.

SetError(Maybe<TToken>, bool, int, string?)

Sets the error. Call this when your parser fails.

Declaration
public void SetError(Maybe<TToken> unexpected, bool eof, int errorLocation, string? message)
Parameters
Type Name Description

Maybe<TToken>

unexpected

The token that wasn't expected.

bool

eof

Whether the parser unexpectedly reached the end of the input.

int

errorLocation

The location at which the parse error was encountered.

string

message

An error message.