grammar

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: 11 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// EtEOF is a special token type used to indicate the end of the input (or file).
	EtEOF string = "EtEOF"

	// EtToSkip is a special token type used to indicate the token must be skipped.
	EtToSkip string = "EtToSkip"
)

Variables

View Source
var (
	// AST is the namespace that allows for the creation of AST nodes.
	AST astT
)
View Source
var (
	ErrNotAsExpected faults.Descriptor
)
View Source
var (
	// WriteTree is a function that writes a tree to the given writer.
	//
	// Parameters:
	//   - w: The writer to write the tree to.
	//   - node: The root node of the tree to write.
	//
	// Returns:
	//   - int: The number of bytes written to the writer.
	//   - error: Returns an error if the operation fails.
	WriteTree func(w io.Writer, node *Node) (int, error)
)

Functions

func CheckNode added in v0.1.12

func CheckNode(idx int, kind string, node *Node, types ...string) faults.Fault

CheckNode checks that the given node's type is one of the given types. If the node is nil, an error is returned. If the node is not nil and the type of the node is not one of the given types, an error is also returned.

Parameters:

  • kind: The kind of AST node to check.
  • node: The node to check.
  • types: The types to check against.

Returns:

  • error: Returns an error if the type of the node is not as expected.

func CheckToken added in v0.1.12

func CheckToken(idx int, kind string, token *Token, types ...string) error

CheckToken checks that the given token's type is one of the given types. If the token is nil, an error is returned. If the token is not nil and the type of the token is not one of the given types, an error is also returned.

Parameters:

  • kind: The kind of token to check.
  • token: The token to check.
  • types: The types to check against.

Returns:

  • error: Returns an error if the type of the token is not as expected.

func IsTerminal added in v0.1.11

func IsTerminal(rhs string) bool

IsTerminal checks if the token type is a terminal. Terminal token types start with "T" or "E".

Parameters:

  • rhs: The token type.

Returns:

  • bool: True if the token type is a terminal, false otherwise.

func NewErrUnsupportedType added in v0.1.12

func NewErrUnsupportedType(quote bool, type_ string) error

NewErrUnsupportedType creates a new instance of ErrUnsupportedType.

Parameters:

  • quote: A flag that indicates whether the type is quoted or not.
  • type_: The type that is not supported.

Returns:

  • error: The new instance of ErrUnsupportedType. Never returns nil.

Types

type ErrUnsupportedType added in v0.1.12

type ErrUnsupportedType struct {
	// Quote is a flag that indicates whether the type is quoted or not.
	Quote bool

	// Type is the type that is not supported.
	Type string
}

ErrUnsupportedType is an error that occurs when a token type is not supported.

func (ErrUnsupportedType) Error added in v0.1.12

func (e ErrUnsupportedType) Error() string

Error implements the error interface.

type FaultCode added in v0.1.12

type FaultCode int
const (
	NotAsExpected FaultCode = iota
)

func (FaultCode) String added in v0.1.12

func (code FaultCode) String() string

type Node added in v0.1.12

type Node struct {
	// Parent, FirstChild, LastChild, NextSibling, and PrevSibling are pointers of
	// the node.
	Parent, FirstChild, LastChild, NextSibling, PrevSibling *Node

	// Pos is the position in the source code.
	Pos int

	// Type is the type of the node.
	Type string

	// Data is the data of the node.
	Data string
}

Node is the node in the tree.

func NewNode added in v0.1.12

func NewNode(pos int, t string, data string) *Node

NewNode creates a new node with the given position, type, and data.

Parameters:

  • pos: The position of the node.
  • t: The type of the node.
  • data: The data of the node.

Returns:

  • *Node: The new node. Never returns nil.

func (*Node) AppendChildren added in v0.1.12

func (n *Node) AppendChildren(children ...*Node) error

AppendChildren adds the given children nodes to the end of the current node's children list.

Parameters:

  • children: Variadic parameter of type Node representing the children to be appended.

Returns:

  • error: Returns an error if the operation fails or if the receiver is nil, otherwise returns nil.

func (Node) BackwardChild added in v0.1.12

func (n Node) BackwardChild() iter.Seq[*Node]

BackwardChild is an iterator over the children of the node that goes from the last child to the first child.

Returns:

  • iter.Seq[*Node]: An iterator over the children of the node. Never returns nil.

func (Node) Child added in v0.1.12

func (n Node) Child() iter.Seq[*Node]

Child iterates over the children of the node from the first child to the last child.

Returns:

  • iter.Seq[*Node]: An iterator over the children of the node. Never returns nil.

func (Node) IsLeaf added in v0.1.12

func (n Node) IsLeaf() bool

IsLeaf implements the TreeNoder interface.

func (*Node) IsNil added in v0.1.12

func (n *Node) IsNil() bool

IsNil implements the TreeNoder interface.

func (*Node) PrependChildren added in v0.1.12

func (n *Node) PrependChildren(children ...*Node) error

PrependChildren adds the given children nodes to the beginning of the current node's children list.

Parameters:

  • children: Variadic parameter of type Node representing the children to be added.

Returns:

  • error: Returns an error if the operation fails or if the receiver is nil, otherwise returns nil.

func (Node) String added in v0.1.12

func (n Node) String() string

String implements the TreeNoder interface.

type ToASTFn added in v0.1.12

type ToASTFn func(token *Token) ([]*Node, error)

ToASTFn is a function that converts a token to a node.

Parameters:

  • token: the token to convert. Assumed to not be nil.

Returns:

  • []*Node: the nodes created by the token.
  • error: an error if the token cannot be converted.

type Token

type Token struct {
	// Parent, FirstChild, LastChild, NextSibling, PrevSibling are all the
	// pointers required to traverse the tree.
	Parent, FirstChild, LastChild, NextSibling, PrevSibling *Token

	// Type is the type of the token.
	Type string

	// Data is the data of the token.
	Data string

	// Pos is the position of the token (in bytes).
	Pos int

	// Lookahead is the lookahead token.
	Lookahead *Token
}

Token is a token.

var (
	// EOFToken is the end of file token.
	EOFToken *Token
)

func NewToken added in v0.1.9

func NewToken(pos int, type_, data string) *Token

NewToken is a convenience function to create a new token.

Parameters:

  • pos: The position of the token (in bytes).
  • type_: The type of the token.
  • data: The data of the token.

Returns:

  • T: The new token. Never returns nil.

func (*Token) AppendChildren added in v0.1.11

func (t *Token) AppendChildren(children ...*Token) error

AppendChildren adds the given children tokens to the current token.

Parameters:

  • children: Variadic parameter of type Token representing the children to be added.

Returns:

  • error: Returns an error if the receiver is nil.

func (Token) BackwardChild

func (t Token) BackwardChild() iter.Seq[*Token]

BackwardChild is an iterator over the children of the token that goes from the last child to the first child.

Returns:

  • iter.Seq[*Token[T]]: An iterator over the children of the token. Never returns nil.

func (Token) Child

func (t Token) Child() iter.Seq[*Token]

Child is an iterator over the children of the token that goes from the first child to the last child.

Returns:

  • iter.Seq[*Token[T]]: An iterator over the children of the token. Never returns nil.

func (Token) Equals added in v0.1.11

func (t Token) Equals(other *Token) bool

Equals checks whether the given token is equal to the current token.

Parameters:

  • other: The token to compare with.

Returns:

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

func (Token) IsLeaf

func (t Token) IsLeaf() bool

IsLeaf implements the tree.TreeNode interface.

func (*Token) IsNil added in v0.1.11

func (t *Token) IsNil() bool

IsNil implements the tree.TreeNode interface.

func (Token) String

func (t Token) String() string

String implements the tree.TreeNode interface.

Jump to

Keyboard shortcuts

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