Documentation ¶
Index ¶
- func NewParser(filename string, r io.Reader) (*Parser, TokenList, error)
- func VisitDepthFirst(node ASTNode, fn func(node ASTNode, parents []ASTNode))
- type ASTNode
- type BaseNode
- type BlockItem
- type CompoundStatement
- type Declaration
- type DoWhileStatement
- type ExpressionStatement
- type ExternalDeclaration
- type ForStatement
- type FunctionDefinition
- type IterationStatement
- type JumpStatement
- type LabeledStatement
- type Parser
- type SelectionStatement
- type Statement
- type TokenList
- func (tl *TokenList) Advance(n int)
- func (tl *TokenList) Next() *lexer.Token
- func (tl *TokenList) Peek() *lexer.Token
- func (tl *TokenList) PeekN(n int) *lexer.Token
- func (tl *TokenList) PeekReverseSearch(fn func(i int, t *lexer.Token) bool) int
- func (tl *TokenList) PeekSearch(fn func(i int, t *lexer.Token) bool) int
- func (tl *TokenList) PeekTail() *lexer.Token
- func (tl *TokenList) Sub() TokenList
- func (tl *TokenList) SubN(n int) TokenList
- type TranslationUnit
- type UnexpectedTokenError
- type WhileStatement
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func VisitDepthFirst ¶
VisitDepthFirst facilitates a depth first traversal of the AST. `fn` is called for every node in the sub-tree. `parents` contains a slice of parents of the current node, len(parents)-1 being the direct parent and 0 being root.
Types ¶
type BaseNode ¶
type BaseNode struct { // Head is the position of the starting character of a node Head lexer.Position // Tail is the position of the last character of a node Tail lexer.Position }
BaseNode contains basic shared features and is embedded by all nodes
func (*BaseNode) SetHead ¶
BaseNode sets the positions of the first character, it takes the position from the given token
type BlockItem ¶
type BlockItem struct { BaseNode Declaration *Declaration Statement *Statement }
BlockItem is a variable declaration or "something else" aka a statement ISO/IEC 9899:TC2 - 6.8.2 Compound statement
type CompoundStatement ¶
CompoundStatement is a scope { } and all of the items within that scope ISO/IEC 9899:TC2 - 6.8.2 Compound statement
func (*CompoundStatement) Children ¶
func (n *CompoundStatement) Children() []ASTNode
Children returns the child nodes
type Declaration ¶
type Declaration struct {
BaseNode
}
Declaration is a variable declaration ISO/IEC 9899:TC2 - 6.7 Declarations
func (*Declaration) Children ¶
func (n *Declaration) Children() []ASTNode
Children returns the child nodes
type DoWhileStatement ¶
DoWhileStatement is a do-while loop ISO/IEC 9899:TC2 - 6.8.5 Iteration statements
func (*DoWhileStatement) Children ¶
func (n *DoWhileStatement) Children() []ASTNode
Children returns the child nodes
type ExpressionStatement ¶
ExpressionStatement is anything other than control flow, e.g ('abc++;' or 'def[abc] = ++somefunc();') ISO/IEC 9899:TC2 - 6.8.3 Expression and null statements
func (*ExpressionStatement) Children ¶
func (n *ExpressionStatement) Children() []ASTNode
Children returns the child nodes
type ExternalDeclaration ¶
type ExternalDeclaration struct { BaseNode FuncDef *FunctionDefinition Decl *Declaration }
ExternalDeclaration declares a type, global variable, or function ISO/IEC 9899:TC2 - 6.9 External definitions
func (*ExternalDeclaration) Children ¶
func (n *ExternalDeclaration) Children() []ASTNode
Children returns the child nodes
type ForStatement ¶
type ForStatement struct { BaseNode ClosingBracket lexer.Position InitExpression TokenList GuardExpression TokenList IterationExpression TokenList Body *Statement }
ForStatement is a for loop ISO/IEC 9899:TC2 - 6.8.5 Iteration statements
func (*ForStatement) Children ¶
func (n *ForStatement) Children() []ASTNode
Children returns the child nodes
type FunctionDefinition ¶
type FunctionDefinition struct { BaseNode // The keywords, return value, name and paramenters of the function. DeclaratorAndSpec TokenList // The function body CompoundStatement *CompoundStatement }
FunctionDefinition contains the function of a function ISO/IEC 9899:TC2 - 6.9.1 Function definitions
func (*FunctionDefinition) Children ¶
func (n *FunctionDefinition) Children() []ASTNode
Children returns the child nodes
type IterationStatement ¶
type IterationStatement struct { BaseNode While *WhileStatement For *ForStatement DoWhile *DoWhileStatement }
IterationStatement is a while, for, or do-while loop ISO/IEC 9899:TC2 - 6.8.5 Iteration statements
func (*IterationStatement) Children ¶
func (n *IterationStatement) Children() []ASTNode
Children returns the child nodes
type JumpStatement ¶
type JumpStatement struct { BaseNode // "continue" or "break", empty if goto ContinueBreak string // The name of the goto label, empty if not goto GotoIdent string // The expression, the value of which is returned. ReturnExpression TokenList }
JumpStatement is a continue, break, goto, or return statement. ISO/IEC 9899:TC2 - 6.8.6 Jump statements
func (*JumpStatement) Children ¶
func (n *JumpStatement) Children() []ASTNode
Children returns the child nodes
type LabeledStatement ¶
type LabeledStatement struct { BaseNode // Name of the label, ("case" or "default" in switch cases) Label string // The value of a switch case, nil otherwise ConstantExpression TokenList // The statement after the label Statement *Statement }
LabeledStatement is a goto label, switch case or switch default case. ISO/IEC 9899:TC2 - 6.8.1 Labeled statements
func (*LabeledStatement) Children ¶
func (n *LabeledStatement) Children() []ASTNode
Children returns the child nodes
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is a C file parser
type SelectionStatement ¶
type SelectionStatement struct { BaseNode // Additional position information which is particularly useful for code coverage annotation. ClosingBracket lexer.Position ElseToken *lexer.Position Expression TokenList IfBody *Statement ElseBody *Statement SwitchBody *Statement }
SelectionStatement is a if, if-else, or switch case ISO/IEC 9899:TC2 - 6.8.4 Selection statements
func (*SelectionStatement) Children ¶
func (n *SelectionStatement) Children() []ASTNode
Children returns the child nodes
type Statement ¶
type Statement struct { BaseNode // A goto label, switch case or default case. LabeledStatement *LabeledStatement // A scope, { } CompoundStatement *CompoundStatement // Anything other than control flow, e.g ('abc++;' or 'def[abc] = ++somefunc();') ExpressionStatement *ExpressionStatement // A if or switch statement SelectionStatement *SelectionStatement // A loop (for, while, do-while) IterationStatement *IterationStatement // A jump (goto, continue, break) JumpStatement *JumpStatement }
Statement is code other than a variable declaration. ISO/IEC 9899:TC2 - 6.8 Statements and blocks
type TokenList ¶
TokenList is a list of tokens, the type contains a number of methods to make it easier to consume tokens.
func (*TokenList) PeekReverseSearch ¶
PeekReverseSearch peeks backward and searches all remaining tokens. If `fn` return true, we stop and return the matching index.
func (*TokenList) PeekSearch ¶
PeekSearch peeks ahead and searches all remaining tokens. If `fn` return true, we stop and return the matching index.
type TranslationUnit ¶
type TranslationUnit struct { BaseNode ExternalDeclarations []*ExternalDeclaration }
TranslationUnit is a full file containing C code. ISO/IEC 9899:TC2 - 6.9 External definitions
func ParseFile ¶
func ParseFile(path string) (*TranslationUnit, error)
ParseFile attempts to parse a C file.
func (*TranslationUnit) Children ¶
func (n *TranslationUnit) Children() []ASTNode
Children returns the child nodes
type UnexpectedTokenError ¶
UnexpectedTokenError also known as a syntax error
func (*UnexpectedTokenError) Error ¶
func (ute *UnexpectedTokenError) Error() string
type WhileStatement ¶
type WhileStatement struct { BaseNode // Closing bracket location, which is useful for coloring coverage reports. ClosingBracket lexer.Position GuardExpression TokenList Body *Statement }
WhileStatement is a while loop ISO/IEC 9899:TC2 - 6.8.5 Iteration statements
func (*WhileStatement) Children ¶
func (n *WhileStatement) Children() []ASTNode
Children returns the child nodes