ast

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: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LhsAst

func LhsAst[T internal.TokenTyper, N interface {
	Child() iter.Seq[N]
	BackwardChild() iter.Seq[N]

	uttr.Noder
}](root *gr.Token[T], lhs T, f func(children []*gr.Token[T]) ([]N, error)) ([]N, error)

LhsAst is a helper function for building ASTs with a given LHS according to the rule LHS -> RHS LHS?

Parameters:

  • root: The root token.
  • lhs: The LHS token type.
  • f: The function that builds the AST.

Returns:

  • []N: The extracted nodes.
  • error: An error if any.

Types

type AstBuilder

type AstBuilder[T internal.TokenTyper, N interface {
	Child() iter.Seq[N]
	BackwardChild() iter.Seq[N]
	Cleanup() []N
	Copy() N
	LinkChildren(children []N)

	uttr.Noder
}] struct {
	// contains filtered or unexported fields
}

AstBuilder is an AST builder.

func NewAstBuilder

func NewAstBuilder[T internal.TokenTyper, N interface {
	Child() iter.Seq[N]
	BackwardChild() iter.Seq[N]
	Cleanup() []N
	Copy() N
	LinkChildren(children []N)

	uttr.Noder
}]() *AstBuilder[T, N]

NewAstBuilder creates a new AST builder.

Returns:

  • *AstBuilder[T, N]: The new AST builder. Never returns nil.

func (*AstBuilder[T, N]) Build

func (b *AstBuilder[T, N]) Build(root *gr.Token[T]) (N, error)

Build builds an AST from a token. This function is an helper function that is used within the registered functions.

Parameters:

  • root: The root of the parse tree.

Returns:

  • N: The AST node.
  • error: An error if the function failed.

func (*AstBuilder[T, N]) Make

func (b *AstBuilder[T, N]) Make(tree *uttr.Tree[*gr.Token[T]]) (*uttr.Tree[N], error)

Make creates an AST from a tree.

Parameters:

  • tree: The tree to create the AST from.

Returns:

  • *tree.Tree[N]: The AST.
  • error: An error if the function failed.

func (*AstBuilder[T, N]) Register

func (b *AstBuilder[T, N]) Register(type_ T, fn ToAstFunc[T, N])

Register registers a function to convert a token to an AST node.

Parameters:

  • type_: The type of the token.
  • fn: The function to convert a token to an AST node.

It ignores the function if it is nil and when multiple types are registered the previous one will be overwritten.

type ErrIn

type ErrIn[T internal.TokenTyper] struct {
	// Type is the type in which the error occurred.
	Type T

	// Reason is the reason for the error.
	Reason error
}

ErrIn is an error that occurs when an error is encountered in a rule.

func NewErrIn

func NewErrIn[T internal.TokenTyper](type_ T, reason error) *ErrIn[T]

NewErrIn creates a new error that occurs when an error is encountered in a rule.

Parameters:

  • type_: The type in which the error occurred.
  • reason: The reason for the error.

Returns:

  • *ErrIn[T]: An error if the type is invalid. Never returns nil.

func (*ErrIn[T]) ChangeReason

func (e *ErrIn[T]) ChangeReason(reason error)

ChangeReason changes the reason of the error.

Parameters:

  • reason: The new reason of the error.

func (ErrIn[T]) Error

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

Error implements the error interface.

Message: "in <type>: <reason>"

func (ErrIn[T]) Unwrap

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

Unwrap returns the reason for the error.

Returns:

  • error: The reason for the error.

type ToAstFunc

type ToAstFunc[T internal.TokenTyper, N interface {
	BackwardChild() iter.Seq[N]
	Child() iter.Seq[N]
	Cleanup() []N
	Copy() N
	LinkChildren(children []N)

	uttr.Noder
}] func(tk *gr.Token[T]) (N, error)

ToAstFunc is a function that converts a token to an AST node.

Parameters:

  • tk: The token. Assume tk is not nil.

Returns:

  • N: The AST node.
  • error: An error if the function failed.

Jump to

Keyboard shortcuts

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