mirror of
https://github.com/safedep/vet.git
synced 2025-12-10 00:22:08 -06:00
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>
48 lines
969 B
Go
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
|
|
}
|