parser

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NodeTypeError       = 0
	NodeTypeTerminal    = 1
	NodeTypeNonTerminal = 2
)

Variables

This section is empty.

Functions

func GenParser

func GenParser(cgram *spec.CompiledGrammar, pkgName string) ([]byte, error)

func GenSemanticAction

func GenSemanticAction(pkgName string) ([]byte, error)

func NewGrammar

func NewGrammar(g *spec.CompiledGrammar) *grammarImpl

func PrintTree

func PrintTree(w io.Writer, node *Node)

PrintTree prints a syntax tree whose root is `node`.

Types

type DefaulSyntaxTreeBuilder

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

DefaulSyntaxTreeBuilder is a implementation of SyntaxTreeBuilder.

func NewDefaultSyntaxTreeBuilder

func NewDefaultSyntaxTreeBuilder() *DefaulSyntaxTreeBuilder

NewDefaultSyntaxTreeBuilder returns a new DefaultSyntaxTreeBuilder.

func (*DefaulSyntaxTreeBuilder) Accept

Accept is a implementation of SyntaxTreeBuilder.Accept.

func (*DefaulSyntaxTreeBuilder) Reduce

func (b *DefaulSyntaxTreeBuilder) Reduce(kindName string, children []SyntaxTreeNode) SyntaxTreeNode

Reduce is a implementation of SyntaxTreeBuilder.Reduce.

func (*DefaulSyntaxTreeBuilder) Shift

func (b *DefaulSyntaxTreeBuilder) Shift(kindName string, text string, row, col int) SyntaxTreeNode

Shift is a implementation of SyntaxTreeBuilder.Shift.

func (*DefaulSyntaxTreeBuilder) ShiftError

func (b *DefaulSyntaxTreeBuilder) ShiftError(kindName string) SyntaxTreeNode

ShiftError is a implementation of SyntaxTreeBuilder.ShiftError.

func (*DefaulSyntaxTreeBuilder) Tree

func (b *DefaulSyntaxTreeBuilder) Tree() *Node

Tree returns a syntax tree when the parser has accepted an input. If a syntax error occurs, the return value is nil.

type Grammar

type Grammar interface {
	// InitialState returns the initial state of a parser.
	InitialState() int

	// StartProduction returns the start production of grammar.
	StartProduction() int

	// Action returns an ACTION entry corresponding to a (state, terminal symbol) pair.
	Action(state int, terminal int) int

	// GoTo returns a GOTO entry corresponding to a (state, non-terminal symbol) pair.
	GoTo(state int, lhs int) int

	// ErrorTrapperState returns true when a state can shift the error symbol.
	ErrorTrapperState(state int) bool

	// LHS returns a LHS symbol of a production.
	LHS(prod int) int

	// AlternativeSymbolCount returns a symbol count of p production.
	AlternativeSymbolCount(prod int) int

	// RecoverProduction returns true when a production has the recover directive.
	RecoverProduction(prod int) bool

	// NonTerminal retuns a string representaion of a non-terminal symbol.
	NonTerminal(nonTerminal int) string

	// TerminalCount returns a terminal symbol count of grammar.
	TerminalCount() int

	// SkipTerminal returns true when a terminal symbol must be skipped on syntax analysis.
	SkipTerminal(terminal int) bool

	// EOF returns the EOF symbol.
	EOF() int

	// Error returns the error symbol.
	Error() int

	// Terminal retuns a string representaion of a terminal symbol.
	Terminal(terminal int) string

	// ASTAction returns an AST action entries.
	ASTAction(prod int) []int
}

type Node

type Node struct {
	Type     NodeType
	KindName string
	Text     string
	Row      int
	Col      int
	Children []*Node
}

Node is a implementation of SyntaxTreeNode interface.

func (*Node) ChildCount

func (n *Node) ChildCount() int

ChildCount is a implementation of SyntaxTreeNode.ChildCount.

func (*Node) ExpandChildren

func (n *Node) ExpandChildren() []SyntaxTreeNode

ExpandChildren is a implementation of SyntaxTreeNode.ExpandChildren.

func (*Node) MarshalJSON

func (n *Node) MarshalJSON() ([]byte, error)

type NodeType

type NodeType int

type Parser

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

func NewParser

func NewParser(toks TokenStream, gram Grammar, opts ...ParserOption) (*Parser, error)

func (*Parser) Parse

func (p *Parser) Parse() error

func (*Parser) SyntaxErrors

func (p *Parser) SyntaxErrors() []*SyntaxError

type ParserOption

type ParserOption func(p *Parser) error

func DisableLAC

func DisableLAC() ParserOption

DisableLAC disables LAC (lookahead correction). LAC is enabled by default.

func SemanticAction

func SemanticAction(semAct SemanticActionSet) ParserOption

type SemanticActionSet

type SemanticActionSet interface {
	// Shift runs when the parser shifts a symbol onto a state stack. `tok` is a token corresponding to the symbol.
	// When the parser recovered from an error state by shifting the token, `recovered` is true.
	Shift(tok VToken, recovered bool)

	// Reduce runs when the parser reduces an RHS of a production to its LHS. `prodNum` is a number of the production.
	// When the parser recovered from an error state by reducing the production, `recovered` is true.
	Reduce(prodNum int, recovered bool)

	// Accept runs when the parser accepts an input.
	Accept()

	// TrapAndShiftError runs when the parser traps a syntax error and shifts a error symbol onto the state stack.
	// `cause` is a token that caused a syntax error. `popped` is the number of frames that the parser discards
	// from the state stack.
	// Unlike `Shift` function, this function doesn't take a token to be shifted as an argument because a token
	// corresponding to the error symbol doesn't exist.
	TrapAndShiftError(cause VToken, popped int)

	// MissError runs when the parser fails to trap a syntax error. `cause` is a token that caused a syntax error.
	MissError(cause VToken)
}

SemanticActionSet is a set of semantic actions a parser calls.

type SyntaxError

type SyntaxError struct {
	Row               int
	Col               int
	Message           string
	Token             VToken
	ExpectedTerminals []string
}

type SyntaxTreeActionSet

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

SyntaxTreeActionSet is a implementation of SemanticActionSet interface and constructs a syntax tree.

func NewASTActionSet

func NewASTActionSet(gram Grammar, builder SyntaxTreeBuilder) *SyntaxTreeActionSet

NewASTActionSet returns a new SyntaxTreeActionSet that constructs an AST (Abstract Syntax Tree). When grammar `gram` contains `#ast` directives, the new SyntaxTreeActionSet this function returns interprets them.

func NewCSTActionSet

func NewCSTActionSet(gram Grammar, builder SyntaxTreeBuilder) *SyntaxTreeActionSet

NewCSTTActionSet returns a new SyntaxTreeActionSet that constructs a CST (Concrete Syntax Tree). Even if grammar `gram` contains `#ast` directives, the new SyntaxTreeActionSet this function returns ignores them.

func (*SyntaxTreeActionSet) Accept

func (a *SyntaxTreeActionSet) Accept()

Accept is a implementation of SemanticActionSet.Accept method.

func (*SyntaxTreeActionSet) MissError

func (a *SyntaxTreeActionSet) MissError(cause VToken)

MissError is a implementation of SemanticActionSet.MissError method.

func (*SyntaxTreeActionSet) Reduce

func (a *SyntaxTreeActionSet) Reduce(prodNum int, recovered bool)

Reduce is a implementation of SemanticActionSet.Reduce method.

func (*SyntaxTreeActionSet) Shift

func (a *SyntaxTreeActionSet) Shift(tok VToken, recovered bool)

Shift is a implementation of SemanticActionSet.Shift method.

func (*SyntaxTreeActionSet) TrapAndShiftError

func (a *SyntaxTreeActionSet) TrapAndShiftError(cause VToken, popped int)

TrapAndShiftError is a implementation of SemanticActionSet.TrapAndShiftError method.

type SyntaxTreeBuilder

type SyntaxTreeBuilder interface {
	Shift(kindName string, text string, row, col int) SyntaxTreeNode
	ShiftError(kindName string) SyntaxTreeNode
	Reduce(kindName string, children []SyntaxTreeNode) SyntaxTreeNode
	Accept(f SyntaxTreeNode)
}

SyntaxTreeBuilder allows you to construct a syntax tree containing arbitrary user-defined node types. The parser uses SyntaxTreeBuilder interface as a part of semantic actions via SyntaxTreeActionSet interface.

type SyntaxTreeNode

type SyntaxTreeNode interface {
	// ChildCount returns a child count of a node. A parser calls this method to know the child count to be expanded by an `#ast`
	// directive with `...` operator.
	ChildCount() int

	// ExpandChildren returns children of a node. A parser calls this method to fetch the children to be expanded by an `#ast`
	// directive with `...` operator.
	ExpandChildren() []SyntaxTreeNode
}

SyntaxTreeNode is a node of a syntax tree. A node type used in SyntaxTreeActionSet must implement SyntaxTreeNode interface.

type TokenStream

type TokenStream interface {
	Next() (VToken, error)
}

func NewTokenStream

func NewTokenStream(g *spec.CompiledGrammar, src io.Reader) (TokenStream, error)

type VToken

type VToken interface {
	// TerminalID returns a terminal ID.
	TerminalID() int

	// Lexeme returns a lexeme.
	Lexeme() []byte

	// EOF returns true when a token represents EOF.
	EOF() bool

	// Invalid returns true when a token is invalid.
	Invalid() bool

	// Position returns (row, column) pair.
	Position() (int, int)
}

Jump to

Keyboard shortcuts

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