test

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

Documentation

Overview

Code generated by vartan-go. DO NOT EDIT.

Code generated by vartan-go. DO NOT EDIT.

Code generated by vartan-go. DO NOT EDIT.

Index

Constants

View Source
const (
	ModeNameNil               = ""
	ModeNameDefault           = "default"
	ModeNameRawString         = "raw_string"
	ModeNameInterpretedString = "interpreted_string"
)
View Source
const (
	KindNameNil                    = ""
	KindNameWs                     = "ws"
	KindNameLParen                 = "l_paren"
	KindNameRParen                 = "r_paren"
	KindNameIdentifier             = "identifier"
	KindNameRawStringOpen          = "raw_string_open"
	KindNameInterpretedStringOpen  = "interpreted_string_open"
	KindNameRawStringBody          = "raw_string_body"
	KindNameRawStringClose         = "raw_string_close"
	KindNameInterpretedSeq         = "interpreted_seq"
	KindNameCodepointPrefix        = "codepoint_prefix"
	KindNameLBrace                 = "l_brace"
	KindNameRBrace                 = "r_brace"
	KindNameHexDigits              = "hex_digits"
	KindNameEscapedSeq             = "escaped_seq"
	KindNameEscapeChar             = "escape_char"
	KindNameInterpretedStringClose = "interpreted_string_close"
)
View Source
const (
	NodeTypeError       = 0
	NodeTypeTerminal    = 1
	NodeTypeNonTerminal = 2
)

Variables

This section is empty.

Functions

func KindIDToName

func KindIDToName(id KindID) string

KindIDToName converts a kind ID to a name.

func ModeIDToName

func ModeIDToName(id ModeID) string

ModeIDToName converts a mode ID to a name.

func NewGrammar

func NewGrammar() *grammarImpl

func NewLexSpec

func NewLexSpec() *lexSpec

func NewTokenStream

func NewTokenStream(src io.Reader) (*tokenStream, error)

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 KindID

type KindID int
const (
	KindIDNil                    KindID = 0
	KindIDWs                     KindID = 1
	KindIDLParen                 KindID = 2
	KindIDRParen                 KindID = 3
	KindIDIdentifier             KindID = 4
	KindIDRawStringOpen          KindID = 5
	KindIDInterpretedStringOpen  KindID = 6
	KindIDRawStringBody          KindID = 7
	KindIDRawStringClose         KindID = 8
	KindIDInterpretedSeq         KindID = 9
	KindIDCodepointPrefix        KindID = 10
	KindIDLBrace                 KindID = 11
	KindIDRBrace                 KindID = 12
	KindIDHexDigits              KindID = 13
	KindIDEscapedSeq             KindID = 14
	KindIDEscapeChar             KindID = 15
	KindIDInterpretedStringClose KindID = 16
)

func (KindID) Int

func (id KindID) Int() int

type LexSpec

type LexSpec interface {
	InitialMode() ModeID
	Pop(mode ModeID, modeKind ModeKindID) bool
	Push(mode ModeID, modeKind ModeKindID) (ModeID, bool)
	ModeName(mode ModeID) string
	InitialState(mode ModeID) StateID
	NextState(mode ModeID, state StateID, v int) (StateID, bool)
	Accept(mode ModeID, state StateID) (ModeKindID, bool)
	KindIDAndName(mode ModeID, modeKind ModeKindID) (KindID, string)
}

type Lexer

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

func NewLexer

func NewLexer(spec LexSpec, src io.Reader, opts ...LexerOption) (*Lexer, error)

NewLexer returns a new lexer.

func (*Lexer) Mode

func (l *Lexer) Mode() ModeID

Mode returns the current lex mode.

func (*Lexer) Next

func (l *Lexer) Next() (*Token, error)

Next returns a next token.

func (*Lexer) PopMode

func (l *Lexer) PopMode() error

PopMode removes a lex mode from the top of the mode stack.

func (*Lexer) PushMode

func (l *Lexer) PushMode(mode ModeID)

PushMode adds a lex mode onto the mode stack.

type LexerOption

type LexerOption func(l *Lexer) error

func DisableModeTransition

func DisableModeTransition() LexerOption

DisableModeTransition disables the active mode transition. Thus, even if the lexical specification has the push and pop operations, the lexer doesn't perform these operations. When the lexical specification has multiple modes, and this option is enabled, you need to call the Lexer.Push and Lexer.Pop methods to perform the mode transition. You can use the Lexer.Mode method to know the current lex mode.

type ModeID

type ModeID int
const (
	ModeIDNil               ModeID = 0
	ModeIDDefault           ModeID = 1
	ModeIDRawString         ModeID = 2
	ModeIDInterpretedString ModeID = 3
)

func (ModeID) Int

func (id ModeID) Int() int

type ModeKindID

type ModeKindID int

func (ModeKindID) Int

func (id ModeKindID) 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 StateID

type StateID int

func (StateID) Int

func (id StateID) Int() int

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 TestCase

type TestCase struct {
	Description string
	Source      []byte
	Output      *Tree
}

func ParseTestCase

func ParseTestCase(r io.Reader) (*TestCase, error)

type Token

type Token struct {
	// ModeID is an ID of a lex mode.
	ModeID ModeID

	// KindID is an ID of a kind. This is unique among all modes.
	KindID KindID

	// ModeKindID is an ID of a lexical kind. This is unique only within a mode.
	// Note that you need to use KindID field if you want to identify a kind across all modes.
	ModeKindID ModeKindID

	// Row is a row number where a lexeme appears.
	Row int

	// Col is a column number where a lexeme appears.
	// Note that Col is counted in code points, not bytes.
	Col int

	// Lexeme is a byte sequence matched a pattern of a lexical specification.
	Lexeme []byte

	// When this field is true, it means the token is the EOF token.
	EOF bool

	// When this field is true, it means the token is an error token.
	Invalid bool
}

Token representes a token.

type TokenStream

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

type Tree

type Tree struct {
	Parent   *Tree
	Offset   int
	Kind     string
	Children []*Tree
	Lexeme   string
}

func NewNonTerminalTree

func NewNonTerminalTree(kind string, children ...*Tree) *Tree

func NewTerminalNode

func NewTerminalNode(kind string, lexeme string) *Tree

func (*Tree) Fill

func (t *Tree) Fill() *Tree

func (*Tree) Format

func (t *Tree) Format() []byte

type TreeDiff

type TreeDiff struct {
	ExpectedPath string
	ActualPath   string
	Message      string
}

func DiffTree

func DiffTree(expected, actual *Tree) []*TreeDiff

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