Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckLookahead ¶ added in v0.1.8
func CheckLookahead[T gr.TokenTyper](lookahead *gr.Token[T], allowed ...T) bool
CheckLookahead is a function that checks if the lookahead is in the allowed list.
Parameters:
- lookahead: The lookahead to check.
- allowed: The list of allowed tokens.
Returns:
- bool: True if the lookahead is in the allowed list, false otherwise.
If the receiver is nil or no allowed tokens are provided, then it returns false.
func CheckTop ¶ added in v0.1.8
CheckTop is a function that checks if the top of the stack is in the allowed list.
Parameters:
- parser: The parser to check.
- allowed: The list of allowed tokens.
Returns:
- *gr.Token[T]: The top of the stack.
- bool: True if the top of the stack is in the allowed list, false otherwise.
If the receiver is nil, then it returns nil and false.
If no allowed tokens are provided, then it returns the top of the stack and false.
Types ¶
type Builder ¶
type Builder[T gr.TokenTyper] struct { // contains filtered or unexported fields }
func NewBuilder ¶
func NewBuilder[T gr.TokenTyper]() Builder[T]
type ErrUnexpectedToken ¶
type ErrUnexpectedToken[T gr.TokenTyper] struct { // Expecteds is the expected tokens. Expecteds []T // After is the token after the expected token. After *T // Got is the token that was found. Got *T }
ErrUnexpectedToken is an error that is returned when an unexpected token is found.
func NewErrUnexpectedToken ¶
func NewErrUnexpectedToken[T gr.TokenTyper](expecteds []T, after, got *T) *ErrUnexpectedToken[T]
NewErrUnexpectedToken creates a new ErrUnexpectedToken error.
Parameters:
- expecteds: the expected tokens.
- after: the token after the expected token.
- got: the token that was found.
Returns:
- *ErrUnexpectedToken[T]: the error. Never returns nil.
func (ErrUnexpectedToken[T]) Error ¶
func (e ErrUnexpectedToken[T]) Error() string
Error implements the error interface.
Message:
"expected {expected} before {after}, got {got} instead".
type Item ¶
type Item[T gr.TokenTyper] struct { // contains filtered or unexported fields }
Item is an item in the parsing table.
func MustNewItem ¶ added in v0.1.8
func MustNewItem[T gr.TokenTyper](rule *Rule[T], pos int) *Item[T]
Same as NewItem, but panics instead of returning an error.
Never returns nil.
func NewItem ¶
NewItem creates a new item.
Parameters:
- rule: the rule of the item.
- pos: the position of the item in the rule.
Returns:
- *Item: the new item.
- error: if the position is out of range.
type ParseFn ¶
type ParseFn[T gr.TokenTyper] func(parser *Parser[T], top1 *gr.Token[T], lookahead *gr.Token[T]) ([]*Item[T], error)
func UnambiguousRule ¶ added in v0.1.8
func UnambiguousRule[T gr.TokenTyper](items ...*Item[T]) ParseFn[T]
UnambiguousRule is a function that creates a function that parses an unambiguous rule.
An unambiguous rule is one that has only one possible outcome or if it can be determined by only popping values from the stack.
Parameters:
- items: The items to parse.
Returns:
- ParseFn: The parse function.
Will panic if items is empty or all items are nil.
type Parser ¶
type Parser[T gr.TokenTyper] struct { // contains filtered or unexported fields }
Parser is a parser.
func (*Parser[T]) Parse ¶
Parse parses the list of tokens.
Parameters:
- tokens: the list of tokens.
Returns:
- error: if an error occurred.
func (Parser[T]) Pop ¶
Pop pops a token from the stack.
Returns:
- grammar.Token[T]: the popped token.
- bool: true if the token was found, false otherwise.
type Rule ¶
type Rule[T gr.TokenTyper] struct { // contains filtered or unexported fields }
A rule is a production of a grammar.