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 ¶
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 ¶
ChangeReason changes the reason of the error.
Parameters:
- reason: The new reason of 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.