parser

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2024 License: GPL-3.0 Imports: 15 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 *ActiveParser[T], allowed ...T) (*gr.ParseTree[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 ActiveParser added in v0.1.9

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

ActiveParser is a parser.

func NewActiveParser added in v0.1.9

func NewActiveParser[T gr.TokenTyper](global *Parser[T]) (*ActiveParser[T], error)

func (*ActiveParser[T]) Align added in v0.1.9

func (ap *ActiveParser[T]) Align(history *bck.History[*internal.Item[T]]) bool

func (*ActiveParser[T]) ApplyEvent added in v0.1.9

func (ap *ActiveParser[T]) ApplyEvent(item *internal.Item[T]) bool

ApplyEvent applies an event to the active parser. Does nothing if item or the receiver are nil.

Parameters:

  • item: The item to apply the event to.

Returns:

  • bool: True if the active parser has accepted, false otherwise.

func (*ActiveParser[T]) DetermineNextEvents added in v0.1.9

func (ap *ActiveParser[T]) DetermineNextEvents() []*internal.Item[T]

func (ActiveParser[T]) Error added in v0.1.9

func (ap ActiveParser[T]) Error() error

func (ActiveParser[T]) Forest added in v0.1.9

func (p ActiveParser[T]) Forest() []*gr.ParseTree[T]

Forest returns the forest.

Returns:

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

func (ActiveParser[T]) HasError added in v0.1.9

func (ap ActiveParser[T]) HasError() bool

func (ActiveParser[T]) Pop added in v0.1.9

func (p ActiveParser[T]) Pop() (*gr.ParseTree[T], bool)

Pop pops a token from the stack.

Returns:

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

func (*ActiveParser[T]) Reset added in v0.1.9

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

Reset resets the parser; allowing it to be reused.

func (*ActiveParser[T]) SetTokens added in v0.1.9

func (p *ActiveParser[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 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 ItemSet added in v0.1.9

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

ItemSet is an item set.

func NewItemSet added in v0.1.9

func NewItemSet[T gr.TokenTyper]() ItemSet[T]

NewItemSet creates a new item set.

Returns:

  • ItemSet: the new item set.

func (*ItemSet[T]) AddRule added in v0.1.9

func (is *ItemSet[T]) AddRule(lhs T, rhss ...T) error

AddRule adds a new rule to the item set.

Parameters:

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

Returns:

  • error: if the rule could not be added.

Errors:

  • gcers.NilReceiver: if the receiver is nil.
  • gcers.ErrInvalidParameter: if the rule does not have at least one right hand side.

func (ItemSet[T]) Build added in v0.1.9

func (b ItemSet[T]) Build() *Parser[T]

Build builds the parser.

Returns:

  • Parser: the parser. Never returns nil.

func (ItemSet[T]) ItemsWithLhsOf added in v0.1.9

func (is ItemSet[T]) ItemsWithLhsOf(lhs T) []*internal.Item[T]

ItemsWithLhsOf returns the items with the given left-hand side.

Parameters:

  • lhs: The left-hand side to search.

Returns:

  • []*Item[T]: The items with the given left-hand side. Nil if there are no items with the given left-hand side.

func (ItemSet[T]) PrintTable added in v0.1.9

func (is ItemSet[T]) PrintTable() []string

PrintTable prints the item table of the item set.

Each item is printed on a new line, with the format:

<item_string>

The strings are separated by an empty line.

Returns:

  • []string: the lines of the item set.

type ParseFn

type ParseFn[T gr.TokenTyper] func(parser *ActiveParser[T], top1 *gr.ParseTree[T], lookahead *gr.Token[T]) ([]*internal.Item[T], error)

ParseFn is a function that parses a production.

Parameters:

  • parser: The parser. Assumed to be non-nil.
  • top1: The top token of the production. Assumed to be non-nil.
  • lookahead: The lookahead token.

Returns:

  • []*Item: The list of items.
  • error: if an error occurred.

type Parser

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

Parser is a parser.

func Build added in v0.1.9

func Build[T gr.TokenTyper](is *ItemSet[T]) *Parser[T]

NewBuilder creates a new parser builder.

Returns:

  • Builder: the builder.

func (*Parser[T]) Parse

func (p *Parser[T]) Parse() ([]*gr.ParseTree[T], error)

Parse parses the list of tokens. Successive calls to this function yields different parsing results.

The results are valid and then, as soon as this returns an error, the next results are all invalid.

Returns:

  • *ActiveParser[T]: the active parser. Nil the parser has done.
  • error: if an error occurred.

func (Parser[T]) ParseFnOf added in v0.1.9

func (p Parser[T]) ParseFnOf(symbol T) (ParseFn[T], bool)

ParseFnOf returns the parse function for the given symbol.

Parameters:

  • symbol: the symbol.

Returns:

  • ParseFn[T]: the parse function.
  • bool: true if the symbol 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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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