cparser

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewParser

func NewParser(filename string, r io.Reader) (*Parser, TokenList, error)

NewParser creates a new parser for the given filename and contents.

func VisitDepthFirst

func VisitDepthFirst(node ASTNode, fn func(node ASTNode, parents []ASTNode))

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 ASTNode

type ASTNode interface {
	Children() []ASTNode
	GetHead() lexer.Position
	GetTail() lexer.Position
}

ASTNode is implemented by all AST nodes in this package

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) GetHead

func (n *BaseNode) GetHead() lexer.Position

GetHead returns the position of the starting character of a node

func (*BaseNode) GetTail

func (n *BaseNode) GetTail() lexer.Position

GetTail returns the position of the last character of a node

func (*BaseNode) SetHead

func (n *BaseNode) SetHead(token *lexer.Token)

BaseNode sets the positions of the first character, it takes the position from the given token

func (*BaseNode) SetSpan

func (n *BaseNode) SetSpan(head, tail *lexer.Token)

SetSpan sets both the head and tail position

func (*BaseNode) SetTail

func (n *BaseNode) SetTail(token *lexer.Token)

BaseNode sets the positions of the last character, it takes the position from the given token and adds its length

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

func (*BlockItem) Children

func (n *BlockItem) Children() []ASTNode

Children returns the child nodes

type CompoundStatement

type CompoundStatement struct {
	BaseNode

	BlockItems []*BlockItem
}

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

type DoWhileStatement struct {
	BaseNode

	Body            *Statement
	GuardExpression TokenList
}

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

type ExpressionStatement struct {
	BaseNode

	Tokens TokenList
}

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

func (*Parser) EOL

func (p *Parser) EOL() lexer.TokenType

EOL returns the lexer token type for a end of line token

func (*Parser) Ident

func (p *Parser) Ident() lexer.TokenType

Ident returns the lexer token type for a identifier

func (*Parser) ParseTU

func (p *Parser) ParseTU(tokens TokenList) (*TranslationUnit, error)

ParseTU attempts to parse all tokens within the parser as a translation unit

func (*Parser) Punct

func (p *Parser) Punct() lexer.TokenType

Punct returns the lexer token type for a punctuation

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

func (*Statement) Children

func (n *Statement) Children() []ASTNode

Children returns the child nodes

type TokenList

type TokenList []lexer.Token

TokenList is a list of tokens, the type contains a number of methods to make it easier to consume tokens.

func (*TokenList) Advance

func (tl *TokenList) Advance(n int)

func (*TokenList) Next

func (tl *TokenList) Next() *lexer.Token

Next returns the next token in the list and advances the internal cursor

func (*TokenList) Peek

func (tl *TokenList) Peek() *lexer.Token

Peek the next token without advancing the internal cursor

func (*TokenList) PeekN

func (tl *TokenList) PeekN(n int) *lexer.Token

Peek a token `n` positions forward without advancing the internal cursor

func (*TokenList) PeekReverseSearch

func (tl *TokenList) PeekReverseSearch(fn func(i int, t *lexer.Token) bool) int

PeekReverseSearch peeks backward and searches all remaining tokens. If `fn` return true, we stop and return the matching index.

func (*TokenList) PeekSearch

func (tl *TokenList) PeekSearch(fn func(i int, t *lexer.Token) bool) int

PeekSearch peeks ahead and searches all remaining tokens. If `fn` return true, we stop and return the matching index.

func (*TokenList) PeekTail

func (tl *TokenList) PeekTail() *lexer.Token

Peek the last token contained in the list

func (*TokenList) Sub

func (tl *TokenList) Sub() TokenList

Sub returns a new token list starting from the internal cursor

func (*TokenList) SubN

func (tl *TokenList) SubN(n int) TokenList

SubN returns a new token list starting from the internal cursor and ending at `n`

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

type UnexpectedTokenError struct {
	UnexpectedToken lexer.Token
	Expected        string
}

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

Jump to

Keyboard shortcuts

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