-
Notifications
You must be signed in to change notification settings - Fork 43
/
language_parser.go
216 lines (188 loc) · 6.02 KB
/
language_parser.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package ssaapi
import (
"fmt"
"io"
"time"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/utils"
"github.com/yaklang/yaklang/common/utils/memedit"
js2ssa "github.com/yaklang/yaklang/common/yak/JS2ssa"
"github.com/yaklang/yaklang/common/yak/java/java2ssa"
"github.com/yaklang/yaklang/common/yak/php/php2ssa"
"github.com/yaklang/yaklang/common/yak/ssa"
"github.com/yaklang/yaklang/common/yak/ssa4analyze"
"github.com/yaklang/yaklang/common/yak/ssaapi/ssareducer"
"github.com/yaklang/yaklang/common/yak/yak2ssa"
)
type Language string
const (
Yak Language = "yak"
JS Language = "js"
PHP Language = "php"
JAVA Language = "java"
)
type Builder interface {
Build(string, bool, *ssa.FunctionBuilder) error
FilterFile(string) bool
}
var LanguageBuilders = map[Language]Builder{
Yak: yak2ssa.Builder,
JS: js2ssa.Builder,
PHP: php2ssa.Builder,
JAVA: java2ssa.Builder,
}
func (c *config) parseProject() ([]*Program, error) {
ret := make([]*Program, 0)
programPath := c.programPath
log.Infof("parse project in fs: %T, localpath: %v", c.fs, programPath)
// parse project
err := ssareducer.ReducerCompile(
programPath, // base
ssareducer.WithFileSystem(c.fs),
ssareducer.WithEntryFiles(c.entryFile...),
ssareducer.WithCompileMethod(func(path string, f io.Reader) (includeFiles []string, err error) {
log.Debugf("start to compile from: %v", path)
startTime := time.Now()
raw, err := io.ReadAll(f)
if err != nil {
return nil, err
}
prog, err := c.parseSimple(path, memedit.NewMemEditor(string(raw)))
endTime := time.Now()
if err != nil {
log.Debugf("parse %#v failed: %v", path, err)
return nil, utils.Wrapf(err, "parse file %s error", path)
}
log.Infof("compile %s cost: %v", path, endTime.Sub(startTime))
ret = append(ret, NewProgram(prog, c))
exclude := prog.GetIncludeFiles()
if len(exclude) > 0 {
log.Infof("program include files: %v will not be as the entry from project", len(exclude))
}
return exclude, nil
}),
)
if err != nil {
return nil, utils.Wrap(err, "parse project error")
}
return ret, nil
}
func (c *config) parseFile() (ret *Program, err error) {
prog, err := c.parseSimple("", c.originEditor)
if err != nil {
return nil, err
}
return NewProgram(prog, c), nil
}
func (c *config) feed(prog *ssa.Program, code *memedit.MemEditor) error {
builder := prog.GetAndCreateFunctionBuilder("main", "main")
if err := prog.Build("", code, builder); err != nil {
return err
}
builder.Finish()
ssa4analyze.RunAnalyzer(prog)
return nil
}
func (c *config) parseSimple(path string, r *memedit.MemEditor) (ret *ssa.Program, err error) {
defer func() {
if r := recover(); r != nil {
ret = nil
err = utils.Errorf("parse error with panic : %v", r)
log.Errorf("parse error with panic : %v", err)
utils.PrintCurrentGoroutineRuntimeStack()
}
}()
prog, builder, err := c.init(path, r)
if err != nil {
return nil, err
}
// parse code
if err := prog.Build(path, r, builder); err != nil {
return nil, err
}
builder.Finish()
ssa4analyze.RunAnalyzer(prog)
prog.Finish()
return prog, nil
}
var SkippedError = ssareducer.SkippedError
func (c *config) init(path string, editor *memedit.MemEditor) (*ssa.Program, *ssa.FunctionBuilder, error) {
LanguageBuilder := c.Builder
language := c.language
programName := c.DatabaseProgramName
if path != "" {
// TODO: whether to use the same programName for all program ?? when call ParseProject
// programName += "-" + path
// auto select language by path filter, if not set language
if LanguageBuilder != nil {
if !LanguageBuilder.FilterFile(path) {
return nil, nil, utils.Wrapf(ssareducer.SkippedError, "file[%s] is filtered by language [%s], skip this file", path, language)
}
} else {
for lang, languageBuilder := range LanguageBuilders {
if languageBuilder.FilterFile(path) {
language = lang
LanguageBuilder = languageBuilder
break
}
}
if LanguageBuilder == nil {
return nil, nil, utils.Errorf("file[%s] is not supported by any language builder, skip this file", path)
}
log.Infof("file[%s] is supported by language [%s], use this language", path, language)
}
} else {
// path is empty, use language or YakLang as default
if LanguageBuilder == nil {
LanguageBuilder = LanguageBuilders[Yak]
// log.Infof("use default language [%s] for empty path", Yak)
}
}
prog := ssa.NewProgram(programName, c.fs, c.programPath)
prog.Build = func(filePath string, src *memedit.MemEditor, fb *ssa.FunctionBuilder) error {
// check builder
if LanguageBuilder == nil {
return utils.Errorf("not support language %s", c.language)
}
// get source code
if src == nil {
return fmt.Errorf("origin source code (MemEditor) is nil")
}
// backup old editor (source code)
originEditor := fb.GetEditor()
// include source code will change the context of the origin editor
newCodeEditor := src
newCodeEditor.SetUrl(filePath)
fb.SetEditor(newCodeEditor) // set for current builder
if originEditor != nil {
originEditor.PushSourceCodeContext(newCodeEditor.SourceCodeMd5())
}
// push into program for recording what code is compiling
prog.PushEditor(newCodeEditor)
defer func() {
// recover source code context
fb.SetEditor(originEditor)
prog.PopEditor()
}()
if ret := fb.GetEditor(); ret != nil {
prog := fb.GetProgram()
cache := prog.Cache
progName, hash := prog.GetProgramName(), ret.SourceCodeMd5()
if cache.IsExistedSourceCodeHash(progName, hash) {
c.DatabaseProgramCacheHitter(fb)
}
} else {
log.Warnf("(BUG or in DEBUG Mode)Range not found for %s", fb.GetName())
}
return LanguageBuilder.Build(src.GetSourceCode(), c.ignoreSyntaxErr, fb)
}
prog.PushEditor(editor)
builder := prog.GetAndCreateFunctionBuilder("main", "main")
// TODO: this extern info should be set in program
builder.WithExternLib(c.externLib)
builder.WithExternValue(c.externValue)
builder.WithExternMethod(c.externMethod)
builder.WithDefineFunction(c.defineFunc)
builder.SetRangeInit(editor)
return prog, builder, nil
}