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
- func KindIDToName(id KindID) string
- func ModeIDToName(id ModeID) string
- func NewGrammar() *grammarImpl
- func NewLexSpec() *lexSpec
- func NewTokenStream(src io.Reader) (*tokenStream, error)
- func PrintTree(w io.Writer, node *Node)
- type DefaulSyntaxTreeBuilder
- func (b *DefaulSyntaxTreeBuilder) Accept(f SyntaxTreeNode)
- func (b *DefaulSyntaxTreeBuilder) Reduce(kindName string, children []SyntaxTreeNode) SyntaxTreeNode
- func (b *DefaulSyntaxTreeBuilder) Shift(kindName string, text string, row, col int) SyntaxTreeNode
- func (b *DefaulSyntaxTreeBuilder) ShiftError(kindName string) SyntaxTreeNode
- func (b *DefaulSyntaxTreeBuilder) Tree() *Node
- type Grammar
- type KindID
- type LexSpec
- type Lexer
- type LexerOption
- type ModeID
- type ModeKindID
- type Node
- type NodeType
- type Parser
- type ParserOption
- type SemanticActionSet
- type StateID
- type SyntaxError
- type SyntaxTreeActionSet
- type SyntaxTreeBuilder
- type SyntaxTreeNode
- type TestCase
- type Token
- type TokenStream
- type Tree
- type TreeDiff
- type VToken
Constants ¶
const ( ModeNameNil = "" ModeNameDefault = "default" ModeNameRawString = "raw_string" ModeNameInterpretedString = "interpreted_string" )
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" )
const ( NodeTypeError = 0 NodeTypeTerminal = 1 NodeTypeNonTerminal = 2 )
Variables ¶
This section is empty.
Functions ¶
func NewGrammar ¶
func NewGrammar() *grammarImpl
func NewLexSpec ¶
func NewLexSpec() *lexSpec
func NewTokenStream ¶
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 ¶
func (b *DefaulSyntaxTreeBuilder) Accept(f SyntaxTreeNode)
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 )
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
}
type LexerOption ¶
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 Node ¶
Node is a implementation of SyntaxTreeNode interface.
func (*Node) ChildCount ¶
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 ¶
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
func NewParser ¶
func NewParser(toks TokenStream, gram Grammar, opts ...ParserOption) (*Parser, error)
func (*Parser) SyntaxErrors ¶
func (p *Parser) SyntaxErrors() []*SyntaxError
type ParserOption ¶
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 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 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 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) }