extractor

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: Apache-2.0 Imports: 8 Imported by: 1

Documentation

Index

Constants

View Source
const (
	KindGolangMethodDecl      core.KindRepr = "method_declaration"
	KindGolangFuncDecl        core.KindRepr = "function_declaration"
	KindGolangIdentifier      core.KindRepr = "identifier"
	KindGolangFieldIdentifier core.KindRepr = "field_identifier"
	KindGolangTypeIdentifier  core.KindRepr = "type_identifier"
	KindGolangParameterList   core.KindRepr = "parameter_list"
	KindGolangParameterDecl   core.KindRepr = "parameter_declaration"
	KindGolangCallExpression  core.KindRepr = "call_expression"
	FieldGolangType           core.KindRepr = "type"
	FieldGolangName           core.KindRepr = "name"
	FieldGolangParameters     core.KindRepr = "parameters"
	FieldGolangFunction       core.KindRepr = "function"
	FieldGolangArguments      core.KindRepr = "arguments"
	FieldGolangResult         core.KindRepr = "result"
)

https://github.com/tree-sitter/tree-sitter-go/blob/master/src/node-types.json

View Source
const (
	KindJavaProgram              core.KindRepr = "program"
	KindJavaProgramDeclaration   core.KindRepr = "package_declaration"
	KindJavaScopeIdentifier      core.KindRepr = "scoped_identifier"
	KindJavaIdentifier           core.KindRepr = "identifier"
	KindJavaClassDeclaration     core.KindRepr = "class_declaration"
	KindJavaEnumDeclaration      core.KindRepr = "enum_declaration"
	KindJavaInterfaceDeclaration core.KindRepr = "interface_declaration"
	KindJavaMethodDeclaration    core.KindRepr = "method_declaration"
	KindJavaFormalParameters     core.KindRepr = "formal_parameters"
	KindJavaFormalParameter      core.KindRepr = "formal_parameter"
	KindJavaMethodInvocation     core.KindRepr = "method_invocation"
	KindJavaModifiers            core.KindRepr = "modifiers"
	KindJavaAnnotation           core.KindRepr = "annotation"
	KindJavaMarkerAnnotation     core.KindRepr = "marker_annotation"
	KindJavaBlock                core.KindRepr = "block"
	FieldJavaType                core.KindRepr = "type"
	FieldJavaDimensions          core.KindRepr = "dimensions"
	FieldJavaObject              core.KindRepr = "object"
	FieldJavaName                core.KindRepr = "name"
	FieldJavaArguments           core.KindRepr = "arguments"
)

https://github.com/tree-sitter/tree-sitter-java/tree/master/src/node-types.json

View Source
const (
	KindPythonFunctionDefinition  core.KindRepr = "function_definition"
	KindPythonIdentifier          core.KindRepr = "identifier"
	KindPythonDecoratedDefinition core.KindRepr = "decorated_definition"
	KindPythonDecorator           core.KindRepr = "decorator"
	KindPythonBlock               core.KindRepr = "block"
)

https://github.com/tree-sitter/tree-sitter-python/blob/master/src/node-types.json

Variables

This section is empty.

Functions

func PathStandardize

func PathStandardize(results []*FileResult, basedir string) error

Types

type BaseFileResult

type BaseFileResult[T DataType] struct {
	Path     string        `json:"path"`
	Language core.LangType `json:"language"`
	Type     string        `json:"type"`
	Units    []T           `json:"units"`
}

type Call

type Call struct {
	Src       string    `json:"src"`
	Caller    string    `json:"caller"`
	Arguments []string  `json:"arguments"`
	Span      core.Span `json:"span"`
}

Call NON-PRECISE

func aFunc() {
	a.b(c, d)
}

aFunc  == Src
a.b    == caller
[c, d] == arguments

func (*Call) GetDesc

func (c *Call) GetDesc() string

func (*Call) GetIndexName

func (c *Call) GetIndexName() string

func (*Call) GetSpan

func (c *Call) GetSpan() *core.Span

type CallFileResult

type CallFileResult = BaseFileResult[*Call]

type CallSupport

type CallSupport interface {
	IsCall(unit *core.Unit) bool
	ExtractCalls([]*core.Unit) ([]*Call, error)
}

type DataType

type DataType interface {
	GetIndexName() string // for query and locate from outside
	GetDesc() string      // easy to understand what it actually contains
	GetSpan() *core.Span
}

func DataTypeOf

func DataTypeOf[T DataType](dataList []T) []DataType

type DataTypes

type DataTypes = []DataType

type ExtractType

type ExtractType = string
const (
	TypeExtractFunction ExtractType = "func"
	TypeExtractSymbol   ExtractType = "symbol"
	TypeExtractCall     ExtractType = "call"
)

these extractors are independent with each other

type Extractor

type Extractor interface {
	GetLang() core.LangType
	SymbolSupport
	FunctionSupport
	CallSupport
}

Extractor

in tree-sitter, a specific language is ruled by grammar.js. https://github.com/tree-sitter/tree-sitter-java/blob/master/grammar.js

unlike other projects, we will only keep the necessary parts, not the whole grammar rule

func GetExtractor

func GetExtractor(lang core.LangType) Extractor

type FileResult

type FileResult = BaseFileResult[DataType]

type FuncSignature

type FuncSignature = string

type Function

type Function struct {
	Name       string       `json:"name"`
	Receiver   string       `json:"receiver"`
	Parameters []*ValueUnit `json:"parameters"`
	Returns    []*ValueUnit `json:"returns"`
	// this span will include header and content
	Span core.Span `json:"span"`
	// which includes only body
	BodySpan core.Span `json:"bodySpan"`

	// which contains language-specific contents
	Extras interface{} `json:"extras"`
	// contains filtered or unexported fields
}

func Json2Func added in v0.8.0

func Json2Func(exported []byte) (*Function, error)

func Map2Func added in v0.8.0

func Map2Func(exported map[string]any) (*Function, error)

Map2Func reverse ToMap

func NewFunction

func NewFunction() *Function

func (*Function) GetDesc

func (f *Function) GetDesc() string

func (*Function) GetIndexName

func (f *Function) GetIndexName() string

func (*Function) GetSignature

func (f *Function) GetSignature() FuncSignature

func (*Function) GetSpan

func (f *Function) GetSpan() *core.Span

func (*Function) GetUnit

func (f *Function) GetUnit() *core.Unit

func (*Function) ToJson

func (f *Function) ToJson() ([]byte, error)

func (*Function) ToMap

func (f *Function) ToMap() (map[string]any, error)

ToMap export a very simple map without any custom structs. It will lose ptr to origin unit.

type FunctionFileResult

type FunctionFileResult = BaseFileResult[*Function]

type FunctionSupport

type FunctionSupport interface {
	IsFunction(*core.Unit) bool
	ExtractFunctions([]*core.Unit) ([]*Function, error)
	ExtractFunction(*core.Unit) (*Function, error)
}

type GolangExtractor

type GolangExtractor struct {
}

func (*GolangExtractor) ExtractCalls

func (extractor *GolangExtractor) ExtractCalls(units []*core.Unit) ([]*Call, error)

func (*GolangExtractor) ExtractFunction

func (extractor *GolangExtractor) ExtractFunction(unit *core.Unit) (*Function, error)

func (*GolangExtractor) ExtractFunctions

func (extractor *GolangExtractor) ExtractFunctions(units []*core.Unit) ([]*Function, error)

func (*GolangExtractor) ExtractSymbols

func (extractor *GolangExtractor) ExtractSymbols(unit []*core.Unit) ([]*Symbol, error)

func (*GolangExtractor) GetLang

func (extractor *GolangExtractor) GetLang() core.LangType

func (*GolangExtractor) IsCall

func (extractor *GolangExtractor) IsCall(unit *core.Unit) bool

func (*GolangExtractor) IsFunction

func (extractor *GolangExtractor) IsFunction(unit *core.Unit) bool

func (*GolangExtractor) IsSymbol

func (extractor *GolangExtractor) IsSymbol(unit *core.Unit) bool

type GolangFuncExtras

type GolangFuncExtras struct {
}

type JavaClassInfo

type JavaClassInfo struct {
	PackageName string   `json:"packageName"`
	ClassName   string   `json:"className"`
	Annotations []string `json:"annotations"`
}

type JavaExtractor

type JavaExtractor struct {
}

func (*JavaExtractor) ExtractCalls

func (extractor *JavaExtractor) ExtractCalls(units []*core.Unit) ([]*Call, error)

func (*JavaExtractor) ExtractFunction

func (extractor *JavaExtractor) ExtractFunction(unit *core.Unit) (*Function, error)

func (*JavaExtractor) ExtractFunctions

func (extractor *JavaExtractor) ExtractFunctions(units []*core.Unit) ([]*Function, error)

func (*JavaExtractor) ExtractSymbols

func (extractor *JavaExtractor) ExtractSymbols(units []*core.Unit) ([]*Symbol, error)

func (*JavaExtractor) GetLang

func (extractor *JavaExtractor) GetLang() core.LangType

func (*JavaExtractor) IsCall

func (extractor *JavaExtractor) IsCall(unit *core.Unit) bool

func (*JavaExtractor) IsFunction

func (extractor *JavaExtractor) IsFunction(unit *core.Unit) bool

func (*JavaExtractor) IsSymbol

func (extractor *JavaExtractor) IsSymbol(unit *core.Unit) bool

type JavaFunctionExtras

type JavaFunctionExtras struct {
	Annotations []string       `json:"annotations"`
	ClassInfo   *JavaClassInfo `json:"classInfo"`
}

JavaFunctionExtras

type PythonExtractor

type PythonExtractor struct {
}

func (*PythonExtractor) ExtractCalls

func (extractor *PythonExtractor) ExtractCalls(units []*core.Unit) ([]*Call, error)

func (*PythonExtractor) ExtractFunction

func (extractor *PythonExtractor) ExtractFunction(unit *core.Unit) (*Function, error)

func (*PythonExtractor) ExtractFunctions

func (extractor *PythonExtractor) ExtractFunctions(units []*core.Unit) ([]*Function, error)

func (*PythonExtractor) ExtractSymbols

func (extractor *PythonExtractor) ExtractSymbols(units []*core.Unit) ([]*Symbol, error)

func (*PythonExtractor) GetLang

func (extractor *PythonExtractor) GetLang() core.LangType

func (*PythonExtractor) IsCall

func (extractor *PythonExtractor) IsCall(unit *core.Unit) bool

func (*PythonExtractor) IsFunction

func (extractor *PythonExtractor) IsFunction(unit *core.Unit) bool

func (*PythonExtractor) IsSymbol

func (extractor *PythonExtractor) IsSymbol(unit *core.Unit) bool

type PythonFunctionExtras

type PythonFunctionExtras struct {
	Decorators []string `json:"decorators"`
}

type Symbol

type Symbol struct {
	Symbol    string    `json:"symbol"`
	Kind      string    `json:"kind"`
	Span      core.Span `json:"span"`
	FieldName string    `json:"fieldName"`
	// contains filtered or unexported fields
}

Symbol Units are named identifiers driven by the ASTs

https://github.com/github/semantic/blob/main/docs/examples.md#symbols https://github.com/github/semantic/blob/main/proto/semantic.proto

message Unit {
  string symbol = 1;
  string kind = 2;
  Span span = 4;
  NodeType node_type = 6;
  SyntaxType syntax_type = 7;
}

enum NodeType {
  ROOT_SCOPE = 0;
  JUMP_TO_SCOPE = 1;
  EXPORTED_SCOPE = 2;
  DEFINITION = 3;
  REFERENCE = 4;
}

enum SyntaxType {
  FUNCTION = 0;
  METHOD = 1;
  CLASS = 2;
  MODULE = 3;
  CALL = 4;
  TYPE = 5;
  INTERFACE = 6;
  IMPLEMENTATION = 7;
}

func (*Symbol) GetDesc

func (s *Symbol) GetDesc() string

func (*Symbol) GetIndexName

func (s *Symbol) GetIndexName() string

func (*Symbol) GetSpan

func (s *Symbol) GetSpan() *core.Span

func (*Symbol) GetUnit

func (s *Symbol) GetUnit() *core.Unit

type SymbolFileResult

type SymbolFileResult = BaseFileResult[*Symbol]

type SymbolSupport

type SymbolSupport interface {
	IsSymbol(*core.Unit) bool
	ExtractSymbols([]*core.Unit) ([]*Symbol, error)
}

type ValueUnit

type ValueUnit struct {
	Type string `json:"type"`
	Name string `json:"name"`
}

Jump to

Keyboard shortcuts

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