vet/pkg/code/nodes/cst.go
abhisek e6f6288701
feat: Code analysis framework infra
feat: Building code graph

Refactor to support import processing

Handle relative import name fixup

Add docs for code analysis framework

Update docs to include additional examples

feat: Function call graph

Update code graph to link function decl and calls

Include call node in function calls

feat: Flatten vulnerabilities in CSV reporter

refactor: Maintain separation of concerns for code analysis framework

refactor: Separate storage entities in its own package

feat: Add callback support in code graph builder

docs: Fix code analysis framework docs
Signed-off-by: abhisek <abhisek.datta@gmail.com>
2024-07-11 15:09:11 +05:30

48 lines
969 B
Go

package nodes
import (
"context"
sitter "github.com/smacker/go-tree-sitter"
)
// Represents a Concreate Syntax Tree (CST) of a *single* source file
// We will use TreeSitter as the only supported parser.
// However, we can have language specific wrappers over TreeSitter CST
// to make it easy for high level modules to operate
type CST struct {
tree *sitter.Tree
lang *sitter.Language
code []byte
}
func NewCST(tree *sitter.Tree, lang *sitter.Language, code []byte) *CST {
return &CST{tree: tree, lang: lang, code: code}
}
func (n *CST) Close() {
n.tree.Close()
}
func (n *CST) Root() *sitter.Node {
return n.tree.RootNode()
}
func (n *CST) Code() []byte {
return n.code
}
func (n *CST) SubTree(node *sitter.Node) (*CST, error) {
p := sitter.NewParser()
p.SetLanguage(n.lang)
data := []byte(node.Content(n.code))
t, err := p.ParseCtx(context.Background(), nil, data)
if err != nil {
return nil, err
}
return NewCST(t, n.lang, data), nil
}