parser

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AcceptAct added in v0.1.20

type AcceptAct[T gr.Enumer] struct {
	// contains filtered or unexported fields
}

AcceptAct is an accept action.

func NewAcceptAct added in v0.1.20

func NewAcceptAct[T gr.Enumer](rule *Rule[T]) (*AcceptAct[T], error)

NewAcceptAct creates a new accept action.

Parameters:

  • rule: The rule that is being accepted.

Returns:

  • *AcceptAct: The new accept action.
  • error: An error if rule is nil.

func (AcceptAct[T]) Rule added in v0.1.20

func (a AcceptAct[T]) Rule() *Rule[T]

Rule returns the rule that is being accepted.

Returns:

  • *Rule: The rule that is being accepted. Never returns nil.

type Actioner

type Actioner interface {
}

Actioner is an interface for actions.

type Builder added in v0.1.20

type Builder[T gr.Enumer] struct {
	// contains filtered or unexported fields
}

Builder is a parser builder.

func NewBuilder added in v0.1.20

func NewBuilder[T gr.Enumer]() Builder[T]

NewBuilder creates a new parser builder.

Returns:

  • Builder: The new parser builder.

func (Builder[T]) Build added in v0.1.20

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

Build builds a parser.

Returns:

  • *Parser: The new parser. Never returns nil.

func (*Builder[T]) Register added in v0.1.20

func (b *Builder[T]) Register(type_ T, fn ParseFunc[T])

Register registers a rule.

Parameters:

  • type_: The type of the rule.
  • fn: The parse function of the rule.

If fn is nil, the rule will not be registered. Previously registered rules with the same type will be overwritten.

func (*Builder[T]) Reset added in v0.1.20

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

Reset resets the builder.

type ErrAfter added in v0.1.20

type ErrAfter[T gr.Enumer] struct {
	// Type is the type before the one that caused the error.
	Type T

	// Err is the underlying error.
	Err error
}

ErrAfter is an error that occurs after a certain type.

func NewErrAfter added in v0.1.20

func NewErrAfter[T gr.Enumer](type_ T, err error) *ErrAfter[T]

NewErrAfter creates a new ErrAfter error.

Parameters:

  • type_: The type before the one that caused the error.
  • err: The underlying error.

Returns:

  • *ErrAfter: The new error. Never returns nil.

func (ErrAfter[T]) Error added in v0.1.20

func (e ErrAfter[T]) Error() string

Error implements the error interface.

Message: "after <type>: <error>" or "something went wrong after <type>"

func (ErrAfter[T]) Unwrap added in v0.1.20

func (e ErrAfter[T]) Unwrap() error

Unwrap implements the error interface.

type ErrBefore added in v0.1.20

type ErrBefore[T gr.Enumer] struct {
	// Type is the type after the one that caused the error.
	Type T

	// Err is the underlying error.
	Err error
}

ErrBefore is an error that occurs before a certain type.

func NewErrBefore added in v0.1.20

func NewErrBefore[T gr.Enumer](type_ T, err error) *ErrBefore[T]

NewErrBefore creates a new ErrBefore error.

Parameters:

  • type_: The type after the one that caused the error.
  • err: The underlying error.

Returns:

  • *ErrBefore: The new error. Never returns nil.

func (ErrBefore[T]) Error added in v0.1.20

func (e ErrBefore[T]) Error() string

Error implements the error interface.

Message: "before <type>: <error>" or "something went wrong before <type>"

func (ErrBefore[T]) Unwrap added in v0.1.20

func (e ErrBefore[T]) Unwrap() error

Unwrap implements the error interface.

type ErrUnexpectedToken

type ErrUnexpectedToken[T gr.Enumer] struct {
	// Left is the expected type.
	Left T

	// Right is the unexpected type.
	Right T

	// Got is the token that was found.
	Got *T
}

ErrUnexpectedToken is an error that occurs when an unexpected token is found.

func NewErrUnexpectedToken

func NewErrUnexpectedToken[T gr.Enumer](left, right T, got *T) *ErrUnexpectedToken[T]

NewErrUnexpectedToken creates a new ErrUnexpectedToken error.

Parameters:

  • left: The expected type.
  • right: The unexpected type.
  • got: The token that was found.

Returns:

  • *ErrUnexpectedToken: The new error. Never returns nil.

func (ErrUnexpectedToken[T]) Error

func (e ErrUnexpectedToken[T]) Error() string

Error implements the error interface.

Message: "expected either <left> or <right> but got <got> instead"

type ParseFunc added in v0.1.20

type ParseFunc[T gr.Enumer] func(parser *Parser[T], top1 *gr.Token[T], la *gr.Token[T]) (Actioner, error)

ParseFunc is a function that parses a token.

Parameters:

  • parser: The parser. Assumed to be non-nil.
  • top1: The first token. Assumed to be non-nil.
  • la: The lookahead token.

Returns:

  • Actioner: The action to perform.
  • error: An error if the decision is invalid.

type Parser

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

Parser is a parser.

func (*Parser[T]) Parse added in v0.1.19

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

Parse parses a list of tokens.

Parameters:

  • tokens: The list of tokens to parse.

Returns:

  • *gr.Token[T]: The root token of the parse tree.
  • error: An error if the parse failed.

func (*Parser[T]) Pop

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

Pop pops a token from the stack.

Returns:

  • *gr.Token[T]: The popped token.
  • bool: True if the token was popped, false otherwise.

type ReduceAct added in v0.1.20

type ReduceAct[T gr.Enumer] struct {
	// contains filtered or unexported fields
}

ReduceAct is a reduce action.

func NewReduceAct added in v0.1.20

func NewReduceAct[T gr.Enumer](rule *Rule[T]) (*ReduceAct[T], error)

NewReduceAct creates a new reduce action.

Parameters:

  • rule: The rule that is being reduced.

Returns:

  • *ReduceAct: The new reduce action.
  • error: An error if rule is nil.

func (ReduceAct[T]) Rule added in v0.1.20

func (a ReduceAct[T]) Rule() *Rule[T]

Rule returns the rule that is being reduced.

Returns:

  • *Rule: The rule that is being reduced. Never returns nil.

type Rule

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

Rule represents a rule in the grammar.

func NewRule

func NewRule[T gr.Enumer](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 side of the rule.

Returns:

  • *Rule: The new rule.
  • error: An error if rhss is empty.

func (Rule[T]) BackwardRhs added in v0.1.20

func (r Rule[T]) BackwardRhs() iter.Seq[T]

BackwardRhs returns the right hand side of the rule in reverse order.

Returns:

  • iter.Seq[T]: The right hand side of the rule in reverse order.

func (Rule[T]) Lhs added in v0.1.19

func (r Rule[T]) Lhs() T

Lhs returns the left hand side of the rule.

Returns:

  • T: The left hand side of the rule.

type ShiftAct added in v0.1.20

type ShiftAct struct {
}

ShiftAct is a shift action.

func NewShiftAct added in v0.1.20

func NewShiftAct() *ShiftAct

NewShiftAct creates a new shift action.

Returns:

  • *ShiftAct: The new shift action. Never returns nil.

Jump to

Keyboard shortcuts

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