parse

package
v0.0.0-...-a5d0cb6 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewEofResult

func NewEofResult(expected []lex.TokenType) core.Result

func NewUnexpectedTokenResult

func NewUnexpectedTokenResult(
	expected []lex.TokenType,
	actual lex.Token,
) core.Result

func ParseMany

func ParseMany[Node any](p Parser[Node], v *TokenView) (nodes []Node)

func ParseManyIgnoreSeparators

func ParseManyIgnoreSeparators[Node any](
	p Parser[Node],
	v *TokenView,
) (nodes []Node, err core.Result)

func SortNodesBySourceOrder

func SortNodesBySourceOrder(nodes []Node)

This function sorts the nodes according to their source order.

Types

type ArgumentNode

type ArgumentNode Node

type ArgumentParser

type ArgumentParser struct {
	RegisterParser  Parser[RegisterNode]
	ImmediateParser *ImmediateParser
	LabelParser     Parser[LabelNode]
	GlobalParser    Parser[GlobalNode]
}

func (ArgumentParser) Parse

func (p ArgumentParser) Parse(v *TokenView) (node ArgumentNode, err core.Result)

type BlockNode

type BlockNode[NodeT Node] struct {
	core.UnmanagedSourceView
	Nodes []NodeT
}

func (BlockNode[NodeT]) String

func (n BlockNode[NodeT]) String(ctx *StringContext) (s string)

func (BlockNode[NodeT]) View

func (n BlockNode[NodeT]) View() core.UnmanagedSourceView

type BlockParser

type BlockParser[NodeT Node] struct {
	Parser Parser[NodeT]
}

func (BlockParser[NodeT]) Parse

func (p BlockParser[NodeT]) Parse(v *TokenView) (block BlockNode[NodeT], err core.Result)

type ConstDeclarationNode

type ConstDeclarationNode struct {
	Declaration GlobalDeclarationNode
}

func (ConstDeclarationNode) String

func (n ConstDeclarationNode) String(ctx *StringContext) string

func (ConstDeclarationNode) View

type ConstDeclarationParser

type ConstDeclarationParser struct {
	ConstParser             Parser[ConstNode]
	GlobalDeclarationParser GlobalDeclarationParser
}

func (ConstDeclarationParser) Parse

type ConstNode

type ConstNode struct{ TokenNode }

func ConstNodeCreator

func ConstNodeCreator(tkn lex.Token) ConstNode

type ConstParser

type ConstParser struct{ TokenParser[ConstNode] }

type FileNode

type FileNode struct {
	Functions []FunctionNode
	Types     []TypeDeclarationNode
	Constants []ConstDeclarationNode
	Variables []VarDeclarationNode
}

func (FileNode) String

func (n FileNode) String(ctx *StringContext) (s string)

func (FileNode) View

type FileParser

type FileParser struct {
	FunctionParser         FunctionParser
	TypeDeclarationParser  TypeDeclarationParser
	ConstDeclarationParser Parser[ConstDeclarationNode]
	VarDeclarationParser   Parser[VarDeclarationNode]
}

func NewFileParser

func NewFileParser() FileParser

func (FileParser) Parse

func (p FileParser) Parse(v *TokenView) (node FileNode, err core.Result)

type FunctionNode

type FunctionNode struct {
	core.UnmanagedSourceView
	Signature    FunctionSignatureNode
	Instructions *BlockNode[InstructionNode]
}

func (FunctionNode) String

func (n FunctionNode) String(ctx *StringContext) string

func (FunctionNode) View

type FunctionParser

type FunctionParser struct {
	FunctionSignatureParser FunctionSignatureParser
	InstructionBlockParser  BlockParser[InstructionNode]
}

func NewFunctionParser

func NewFunctionParser() FunctionParser

func (FunctionParser) Parse

func (p FunctionParser) Parse(v *TokenView) (node FunctionNode, err core.Result)

type FunctionSignatureNode

type FunctionSignatureNode struct {
	core.UnmanagedSourceView
	Identifier core.UnmanagedSourceView
	Parameters []ParameterNode
	Returns    []TypeNode
}

func (FunctionSignatureNode) String

func (n FunctionSignatureNode) String(ctx *StringContext) (s string)

func (FunctionSignatureNode) View

type FunctionSignatureParser

type FunctionSignatureParser struct {
	ParameterParser ParameterParser
	TypeParser      TypeParser
}

func NewFunctionSignatureParser

func NewFunctionSignatureParser() FunctionSignatureParser

func (FunctionSignatureParser) Parse

type GlobalDeclarationNode

type GlobalDeclarationNode struct {
	Identifier GlobalNode
	Type       TypeNode
	Immediate  *ImmediateValueNode
}

func (GlobalDeclarationNode) String

func (GlobalDeclarationNode) View

type GlobalDeclarationParser

type GlobalDeclarationParser struct {
	GlobalParser         Parser[GlobalNode]
	TypeParser           TypeParser
	ImmediateValueParser *ImmediateValueParser
}

func NewGlobalDeclarationParser

func NewGlobalDeclarationParser() GlobalDeclarationParser

func (*GlobalDeclarationParser) Parse

type GlobalNode

type GlobalNode struct{ TokenNode }

func GlobalNodeCreator

func GlobalNodeCreator(tkn lex.Token) GlobalNode

type GlobalParser

type GlobalParser struct{ TokenParser[GlobalNode] }

type ImmediateBlockNode

type ImmediateBlockNode = BlockNode[ImmediateFieldNode]

type ImmediateBlockParser

type ImmediateBlockParser = BlockParser[ImmediateFieldNode]

func NewImmediateBlockParser

func NewImmediateBlockParser(valueParser *ImmediateValueParser) *ImmediateBlockParser

type ImmediateFieldNode

type ImmediateFieldNode struct {
	// At most one (possible, zero) field labels can be specified for each filed
	// Field is nil if a label is not specified.
	Label *LabelNode
	Value ImmediateValueNode
}

ImmediateFieldNode represents a single field (entry) in the initialization of an immediate custom type (struct).

func (ImmediateFieldNode) String

func (n ImmediateFieldNode) String(ctx *StringContext) string

func (ImmediateFieldNode) View

type ImmediateFieldParser

type ImmediateFieldParser struct {
	LabelParser          Parser[LabelNode]
	ImmediateValueParser *ImmediateValueParser
}

func NewImmediateFieldParser

func NewImmediateFieldParser(valueParser *ImmediateValueParser) *ImmediateFieldParser

func (ImmediateFieldParser) Parse

type ImmediateFinalValueNode

type ImmediateFinalValueNode struct {
	core.UnmanagedSourceView
}

func (ImmediateFinalValueNode) String

func (ImmediateFinalValueNode) View

type ImmediateFinalValueParser

type ImmediateFinalValueParser struct{}

func (ImmediateFinalValueParser) Parse

type ImmediateNode

type ImmediateNode struct {
	Type  TypeNode
	Value ImmediateValueNode
}

func (ImmediateNode) String

func (n ImmediateNode) String(ctx *StringContext) string

func (ImmediateNode) View

type ImmediateParser

type ImmediateParser struct {
	TypeParser
	ImmediateValueParser *ImmediateValueParser
}

func NewImmediateParser

func NewImmediateParser() *ImmediateParser

func (ImmediateParser) Parse

func (p ImmediateParser) Parse(v *TokenView) (node ImmediateNode, err core.Result)

type ImmediateValueNode

type ImmediateValueNode interface {
	Node
}

This is an interface of the type that appear as a value in a field of a custom type initialization. It can be either (1.) an ImmediateFinalValueNode (#1234), or (2.) an ImmediateBlockNode ({ ... }).

type ImmediateValueParser

type ImmediateValueParser struct {
	ImmediateFinalValueParser *ImmediateFinalValueParser
	ImmediateBlockParser      *ImmediateBlockParser
}

func NewImmediateValueParser

func NewImmediateValueParser() *ImmediateValueParser

func (ImmediateValueParser) Parse

func (p ImmediateValueParser) Parse(v *TokenView) (
	node ImmediateValueNode,
	err core.Result,
)

type InstructionNode

type InstructionNode struct {
	Operator  core.UnmanagedSourceView
	Arguments []ArgumentNode
	Targets   []TargetNode
	Labels    []LabelNode
}

func (InstructionNode) String

func (n InstructionNode) String(ctx *StringContext) string

func (InstructionNode) View

type InstructionParser

type InstructionParser struct {
	LabelParser    Parser[LabelNode]
	TargetParser   Parser[TargetNode]
	ArgumentParser Parser[ArgumentNode]
}

func NewInstructionParser

func NewInstructionParser() InstructionParser

func (InstructionParser) Parse

func (p InstructionParser) Parse(v *TokenView) (node InstructionNode, err core.Result)

Parsing of the following regular expression:

> Lbl* ((Type? Reg)+ Eql)? Opr? Arg+ !Arg \n+

type LabelNode

type LabelNode struct{ TokenNode }

func LabelNodeCreator

func LabelNodeCreator(tkn lex.Token) LabelNode

type LabelParser

type LabelParser struct{ TokenParser[LabelNode] }

type Node

type Node interface {
	// Return a reference to the node substring in the source code
	View() core.UnmanagedSourceView

	// Regenerate ("format") the code to a unique, single representation.
	String(ctx *StringContext) string
}

type ParameterNode

type ParameterNode struct {
	Type     TypeNode
	Register RegisterNode
}

func (ParameterNode) String

func (n ParameterNode) String(ctx *StringContext) string

func (ParameterNode) View

type ParameterParser

type ParameterParser struct {
	TypeParser     TypeParser
	RegisterParser Parser[RegisterNode]
}

func NewParameterParser

func NewParameterParser() ParameterParser

func (ParameterParser) Parse

func (p ParameterParser) Parse(v *TokenView) (node ParameterNode, err core.Result)

type Parser

type Parser[Node any] interface {
	Parse(v *TokenView) (Node, core.Result)
}

func NewArgumentParser

func NewArgumentParser() Parser[ArgumentNode]

func NewConstDeclarationParser

func NewConstDeclarationParser() Parser[ConstDeclarationNode]

func NewConstParser

func NewConstParser() Parser[ConstNode]

func NewGlobalParser

func NewGlobalParser() Parser[GlobalNode]

func NewLabelParser

func NewLabelParser() Parser[LabelNode]

func NewRegisterParser

func NewRegisterParser() Parser[RegisterNode]

func NewTargetParser

func NewTargetParser() Parser[TargetNode]

func NewTypeFieldParser

func NewTypeFieldParser() Parser[TypeFieldNode]

func NewVarDeclarationParser

func NewVarDeclarationParser() Parser[VarDeclarationNode]

func NewVarParser

func NewVarParser() Parser[VarNode]

type RegisterNode

type RegisterNode struct{ TokenNode }

func RegisterNodeCreator

func RegisterNodeCreator(tkn lex.Token) RegisterNode

type RegisterParser

type RegisterParser struct{ TokenParser[RegisterNode] }

type StringContext

type StringContext struct {
	core.SourceContext
	Indent int
}

type TargetNode

type TargetNode struct {
	// Optional type declaration. Depending on the instruction, the type may be
	// inferred and does not need to be provided explicitly.
	Type *TypeNode

	Register RegisterNode
}

func (TargetNode) String

func (n TargetNode) String(ctx *StringContext) (s string)

func (TargetNode) View

type TargetParser

type TargetParser struct {
	TypeParser
	RegisterParser Parser[RegisterNode]
}

func (TargetParser) Parse

func (p TargetParser) Parse(v *TokenView) (node TargetNode, err core.Result)

type TokenNode

type TokenNode struct {
	core.UnmanagedSourceView
}

func (TokenNode) String

func (n TokenNode) String(ctx *StringContext) string

func (TokenNode) View

type TokenParser

type TokenParser[NodeT Node] struct {
	Token       lex.TokenType
	NodeCreator func(lex.Token) NodeT
}

func (TokenParser[NodeT]) Parse

func (p TokenParser[NodeT]) Parse(v *TokenView) (node NodeT, err core.Result)

type TokenView

type TokenView struct{ view.View[lex.Token, uint32] }

func NewTokenView

func NewTokenView(tkns []lex.Token) TokenView

func (*TokenView) ConsumeAtLeastTokens

func (v *TokenView) ConsumeAtLeastTokens(
	atLeast int, expectedTypes ...lex.TokenType,
) (err core.Result)

Consume as many tokens as possible greedily, until we receive an error.

If the number of tokens consumed is strictly less than the provided number, returns the underlying error. Otherwise, returns nil.

func (*TokenView) ConsumeManyTokens

func (v *TokenView) ConsumeManyTokens(
	expectedTypes ...lex.TokenType,
) (count int, err core.Result)

Consume as many tokens as possible greedily, until we receive an error. The error and token count are returned.

func (*TokenView) ConsumeToken

func (v *TokenView) ConsumeToken(expectedTypes ...lex.TokenType) (tkn lex.Token, result core.Result)

func (*TokenView) ConsumeTokenIgnoreSeparator

func (v *TokenView) ConsumeTokenIgnoreSeparator(
	expectedTypes ...lex.TokenType,
) (lex.Token, core.Result)

Consume a token, but ignore any separator tokens that come before it.

func (*TokenView) PeekToken

func (v *TokenView) PeekToken(expectedTypes ...lex.TokenType) (tkn lex.Token, result core.Result)

type TypeDeclarationNode

type TypeDeclarationNode struct {
	core.UnmanagedSourceView
	Identifier core.UnmanagedSourceView
	Fields     BlockNode[TypeFieldNode]
}

func (TypeDeclarationNode) String

func (n TypeDeclarationNode) String(ctx *StringContext) string

func (TypeDeclarationNode) View

type TypeDeclarationParser

type TypeDeclarationParser struct {
	FieldsParser BlockParser[TypeFieldNode]
}

func NewTypeDeclarationParser

func NewTypeDeclarationParser() TypeDeclarationParser

func (TypeDeclarationParser) Parse

type TypeDecoratorNode

type TypeDecoratorNode struct {
	core.UnmanagedSourceView
	Type TypeDecoratorType
}

func (TypeDecoratorNode) String

func (n TypeDecoratorNode) String(ctx *StringContext) string

func (TypeDecoratorNode) View

type TypeDecoratorParser

type TypeDecoratorParser struct{}

func NewTypeDecoratorParser

func NewTypeDecoratorParser() TypeDecoratorParser

func (TypeDecoratorParser) Parse

func (p TypeDecoratorParser) Parse(v *TokenView) (node TypeDecoratorNode, err core.Result)

type TypeDecoratorType

type TypeDecoratorType uint8
const (
	PointerTypeDecorator TypeDecoratorType = iota
	RepeatTypeDecorator
)

type TypeFieldNode

type TypeFieldNode struct {
	Type   TypeNode
	Labels []LabelNode
}

func (TypeFieldNode) String

func (n TypeFieldNode) String(ctx *StringContext) (s string)

func (TypeFieldNode) View

type TypeFieldParser

type TypeFieldParser struct {
	LabelParser Parser[LabelNode]
	TypeParser  TypeParser
}

func (TypeFieldParser) Parse

func (p TypeFieldParser) Parse(v *TokenView) (node TypeFieldNode, err core.Result)

type TypeNode

type TypeNode struct {
	Identifier core.UnmanagedSourceView
	Decorators []TypeDecoratorNode
}

func (TypeNode) String

func (n TypeNode) String(ctx *StringContext) string

func (TypeNode) View

type TypeParser

type TypeParser struct {
	TypeDecoratorParser
}

func NewTypeParser

func NewTypeParser() TypeParser

func (TypeParser) Parse

func (p TypeParser) Parse(v *TokenView) (node TypeNode, err core.Result)

type VarDeclarationNode

type VarDeclarationNode struct {
	Declaration GlobalDeclarationNode
}

func (VarDeclarationNode) String

func (n VarDeclarationNode) String(ctx *StringContext) string

func (VarDeclarationNode) View

type VarDeclarationParser

type VarDeclarationParser struct {
	VarParser               Parser[VarNode]
	GlobalDeclarationParser GlobalDeclarationParser
}

func (VarDeclarationParser) Parse

func (p VarDeclarationParser) Parse(v *TokenView) (
	node VarDeclarationNode,
	err core.Result,
)

type VarNode

type VarNode struct{ TokenNode }

func VarNodeCreator

func VarNodeCreator(tkn lex.Token) VarNode

type VarParser

type VarParser struct{ TokenParser[VarNode] }

Jump to

Keyboard shortcuts

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