-
Notifications
You must be signed in to change notification settings - Fork 0
/
language.go
137 lines (111 loc) · 3.01 KB
/
language.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package errgoengine
import (
"context"
"fmt"
"io/fs"
"regexp"
"strings"
sitter "github.com/smacker/go-tree-sitter"
)
type NodeValueAnalyzer interface {
FindSymbol(name string, pos int) Symbol
AnalyzeValue(n SyntaxNode) Symbol
}
type LanguageAnalyzer interface {
FallbackSymbol() Symbol
FindSymbol(name string) Symbol
AnalyzeNode(context.Context, SyntaxNode) Symbol
AnalyzeImport(ImportParams) ResolvedImport
}
type LocationConverterContext struct {
Path string
Pos string
ContextData *ContextData
}
type LocationConverterFunc func(ctx LocationConverterContext) Position
type Language struct {
isCompiled bool
stackTraceRegex *regexp.Regexp
externSymbols map[string]*SymbolTree
Name string
FilePatterns []string
SitterLanguage *sitter.Language
StackTracePattern string
ErrorPattern string
SymbolsToCapture string
LocationConverter func(ctx LocationConverterContext) Location
AnalyzerFactory func(cd *ContextData) LanguageAnalyzer
ExternFS fs.ReadFileFS
}
func (lang *Language) MatchPath(path string) bool {
for _, ext := range lang.FilePatterns {
if strings.HasSuffix(path, ext) {
return true
}
}
return false
}
func (lang *Language) Compile() {
if lang.isCompiled {
return
}
if lang.LocationConverter == nil {
lang.LocationConverter = DefaultLocationConverter
}
if len(lang.StackTracePattern) != 0 {
lang.stackTraceRegex = regexp.MustCompile("(?m)" + lang.StackTracePattern)
}
if lang.AnalyzerFactory == nil {
panic(fmt.Sprintf("[Language -> %s] AnalyzerFactory must not be nil", lang.Name))
}
// if err := lang.compileExternSymbols(); err != nil {
// panic(err)
// }
lang.isCompiled = true
}
// func (lang *Language) compileExternSymbols() error {
// if lang.isCompiled || lang.ExternFS == nil {
// return nil
// }
// lang.externSymbols = make(map[string]*SymbolTree)
// matches, err := fs.Glob(lang.ExternFS, "**/*.json")
// if err != nil {
// return err
// }
// for _, match := range matches {
// if err := lang.compileExternSymbol(match); err != nil {
// return err
// }
// }
// }
// func (lang *Language) compileExternSymbol(path string) error {
// if lang.isCompiled || lang.ExternFS == nil {
// return nil
// }
// file, err := lang.ExternFS.Open(path)
// if err != nil {
// return err
// }
// defer file.Close()
// var symTree SymbolTree
// if err := json.NewDecoder(file).Decode(&symTree); err != nil {
// return err
// }
// lang.externSymbols[path] = &symTree
// return nil
// }
// SetTemplateStackTraceRegex sets the language's regex pattern directly. for testing purposes only
func SetTemplateStackTraceRegex(lang *Language, pattern *regexp.Regexp) {
lang.stackTraceRegex = pattern
}
func DefaultLocationConverter(ctx LocationConverterContext) Location {
var trueLine int
if _, err := fmt.Sscanf(ctx.Pos, "%d", &trueLine); err != nil {
panic(err)
}
return Location{
DocumentPath: ctx.Path,
StartPos: Position{Line: trueLine},
EndPos: Position{Line: trueLine},
}
}