parser

package
v0.1.12 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrExhausted occurs when an iterator is exhausted.
	// This can be checked with the == operator.
	//
	// Format:
	//
	// 	"iterator is exhausted"
	ErrExhausted error
)

Functions

func NewUnsupportedValue added in v0.1.11

func NewUnsupportedValue(kind, expected string) error

NewUnsupportedValue creates a new errors.Err error with the code InvalidOperation and the message "value of <expected> is not a supported <kind> type".

Parameters:

  • kind: The kind of the value. Ignored if not provided.
  • expected: The expected value. Ignored if not provided.

Returns:

  • *errors.Err[ErrorCode]: The new error. Never returns nil.

Types

type Active added in v0.1.11

type Active struct {
	// contains filtered or unexported fields
}

Active is the active parser.

func NewActive added in v0.1.11

func NewActive(global *baseParser, tokens []*slgr.Token) (*Active, error)

NewActive creates a new parser based on the given parser and tokens.

Parameters:

  • global: The parser to use. Must not be nil.
  • tokens: The tokens to parse. Must not be nil.

Returns:

  • *Active[T]: The new parser.
  • error: An error if the initial shift failed.

func (*Active) ApplyEvent added in v0.1.11

func (active *Active) ApplyEvent(event *internal.Event) error

ApplyEvent implements the history.Subject interface.

func (Active) DebugInputStreamString added in v0.1.11

func (active Active) DebugInputStreamString() string

DebugInputStreamString returns a string representation of the input stream in a human-readable format. It constructs a slice of strings by iterating over the input stream and calling the String() method on each token. The resulting strings are reversed to reflect the original order of the input stream and concatenated with " <- " separators.

Returns:

  • string: A human-readable string representation of the input stream.

func (Active) DebugStackString added in v0.1.11

func (active Active) DebugStackString() string

DebugStackString returns a string representation of the parse stack in a human-readable format. It constructs a parse forest by popping all tokens from the stack, creating a parse tree for each token, and then converting each parse tree to a string. The resulting strings are reversed to reflect the original order of the parse stack and concatenated with newline separators.

Returns:

  • string: A human-readable string representation of the parse stack.

func (*Active) Forest added in v0.1.11

func (active *Active) Forest() []*ParseTree

Forest returns the parse forest of the parser. The parse forest is a slice of parse trees, where each parse tree is a tree of tokens. The parse forest is constructed by popping all the tokens from the parse stack and constructing a parse tree for each one. The parse forest is useful for debugging and for visualizing the parse tree.

The function never returns an error. If the parse stack is empty, the function returns a slice with a single element, which is a parse tree with a single token, which is the end of file token.

Returns:

  • []*ParseTree[T]: A slice of parse trees. The slice is never empty, since the parser always has a parse stack.

func (Active) GetError added in v0.1.11

func (active Active) GetError() error

GetError implements the history.Subject interface.

func (Active) HasError added in v0.1.11

func (active Active) HasError() bool

HasError implements the history.Subject interface.

func (*Active) NextEvents added in v0.1.11

func (active *Active) NextEvents() ([]*internal.Event, error)

NextEvents implements the history.Subject interface.

func (Active) Shadow added in v0.1.11

func (active Active) Shadow() (*Active, error)

Shadow creates a shadow of the active parser. The shadow is a new parser that is a copy of the original parser. The shadow is useful for debugging and for testing the parser. The function never returns an error. If the function fails, it panics.

The function returns a pointer to the shadow parser. The pointer is never nil.

Returns:

  • *Active[T]: A pointer to the shadow parser.
  • error: An error if the function failed.

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

func (*Builder) AddRule added in v0.1.11

func (b *Builder) AddRule(lhs string, rhss ...string) error

func (Builder) Build

func (b Builder) Build() map[string][]*internal.Item

func (*Builder) Reset

func (b *Builder) Reset()

type Iterator added in v0.1.11

type Iterator struct {
	// contains filtered or unexported fields
}

Iterator is an iterator over the results of an active parser.

func NewIterator added in v0.1.11

func NewIterator(init_fn func() (*Active, error)) (*Iterator, error)

NewIterator creates a new iterator over the results of an active parser.

Parameters:

  • init_fn: A function that creates the initial active parser.

Returns:

  • *Iterator: An iterator over the results of the active parser.
  • error: An error if the init_fn returns an error.

func (Iterator) Next added in v0.1.11

func (itr Iterator) Next() (*Result, error)

Next returns the next result of the iterator.

Returns:

  • *Result: The next result.
  • error: An error if the iterator is exhausted or if the evaluation fails.

Errors:

  • ErrExhausted: The iterator is exhausted.

func (Iterator) Stop added in v0.1.11

func (itr Iterator) Stop()

Stop stops the iterator.

After calling Stop, the iterator is no longer valid.

type ParseTree added in v0.1.11

type ParseTree struct {
	// contains filtered or unexported fields
}

ParseTree the parse tree output by the parser.

func NewParseTree added in v0.1.11

func NewParseTree(tk *slgr.Token) (*ParseTree, error)

NewParseTree creates a new parse tree from a token.

Parameters:

  • tk: The root token of the parse tree. Must not be nil.

Returns:

  • *ParseTree: The new parse tree.
  • error: An error if the token is nil.

func (ParseTree) Equals added in v0.1.11

func (pt ParseTree) Equals(other *ParseTree) bool

Equals checks whether the given parse tree is equal to the current parse tree.

Parameters:

  • other: The parse tree to compare with. May be nil.

Returns:

  • bool: True if the parse trees are equal, false otherwise.

func (ParseTree) Root added in v0.1.11

func (pt ParseTree) Root() *slgr.Token

Root returns the root token of the parse tree.

Returns:

  • *slgr.Token: The root token of the parse tree.

func (ParseTree) Slice added in v0.1.11

func (pt ParseTree) Slice() []*slgr.Token

Slice returns a slice of all the tokens in the parse tree in a pre-order view.

Returns:

  • []*slgr.Token: A slice of all the tokens in the parse tree.

func (ParseTree) String added in v0.1.11

func (pt ParseTree) String() string

String implements the fmt.Stringer interface.

type Parser

type Parser interface {
	Parse(tokens []*slgr.Token) *Iterator
	ItemsOf(type_ string) ([]*internal.Item, bool)
}

func NewParser added in v0.1.11

func NewParser(table map[string][]*internal.Item) Parser

type PhaseType added in v0.1.11

type PhaseType int
const (
	PhaseReduction PhaseType = iota
	PhasePrediction
	PhaseShifting
	PhaseCheckBranch
)

type Result added in v0.1.11

type Result struct {
	// contains filtered or unexported fields
}

Result is the result of an evaluation.

func (Result) Emulate added in v0.1.11

func (r Result) Emulate() iter.Seq[*Active]

Emulate emulates the parse process. It returns a sequence of all the active trees that were generated during the parse process.

Returns:

  • iter.Seq[*Active]: The sequence of active trees. Never returns nil.

func (Result) Forest added in v0.1.11

func (r Result) Forest() []*ParseTree

Forest returns the parse forest of the parser. The parse forest is a slice of parse trees, where each parse tree is a tree of tokens. The parse forest is constructed by popping all the tokens from the parse stack and constructing a parse tree for each one. The parse forest is useful for debugging and for visualizing the parse tree.

Returns:

  • []*ParseTree: A slice of parse trees. The slice is never empty, since the parser always has a parse stack.

func (Result) GetError added in v0.1.11

func (r Result) GetError() error

GetError returns the error that occurred during the parse process.

Returns:

  • error: An error if the parse process failed. Otherwise, nil.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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