parser

package
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2024 License: GPL-3.0 Imports: 13 Imported by: 1

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

func CheckTop[T gr.TokenTyper](parser *Parser[T], allowed ...T) (*gr.Token[T], bool)

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]

func (Builder[T]) Build

func (b Builder[T]) Build() Parser[T]

func (*Builder[T]) Register

func (b *Builder[T]) Register(rhs T, fn ParseFn[T])

func (*Builder[T]) Reset

func (b *Builder[T]) Reset()

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

func NewItem[T gr.TokenTyper](rule *Rule[T], pos int) (*Item[T], error)

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.

func (Item[T]) Pos added in v0.1.8

func (i Item[T]) Pos() int

Pos returns the position of the item in the rule.

Returns:

  • int: the position of the item in the rule.

func (Item[T]) RhsAt added in v0.1.8

func (i Item[T]) RhsAt(idx int) (T, bool)

RhsAt returns the rhs at the given index.

Returns:

  • T: the rhs at the given index.
  • bool: true if the index is valid, false otherwise.

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]) Forest

func (p Parser[T]) Forest() []*gr.Token[T]

Forest returns the forest.

Returns:

  • []*gr.Token[T]: the forest.

func (*Parser[T]) Parse

func (p *Parser[T]) Parse() error

Parse parses the list of tokens.

Parameters:

  • tokens: the list of tokens.

Returns:

  • error: if an error occurred.

func (Parser[T]) Pop

func (p Parser[T]) Pop() (*gr.Token[T], bool)

Pop pops a token from the stack.

Returns:

  • grammar.Token[T]: the popped token.
  • bool: true if the token was found, false otherwise.

func (*Parser[T]) Reset

func (p *Parser[T]) Reset()

Reset resets the parser; allowing it to be reused.

func (*Parser[T]) SetTokens

func (p *Parser[T]) SetTokens(tokens []*gr.Token[T])

SetTokens sets the list of tokens.

Parameters:

  • tokens: the list of tokens.

Does nothing if the receiver is nil.

type Rule

type Rule[T gr.TokenTyper] struct {
	// contains filtered or unexported fields
}

A rule is a production of a grammar.

func NewRule

func NewRule[T gr.TokenTyper](lhs T, rhss ...T) (*Rule[T], error)

NewRule creates a new rule.

Parameters:

  • lhs: the left hand side of the rule.
  • rhss: the right hand sides of the rule.

Returns:

  • *Rule: the new rule.
  • error: if the rule does not have at least one right hand side.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL