2024-08-08 15:59:08 +08:00
|
|
|
package parse
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
|
2024-08-16 11:52:22 +08:00
|
|
|
"github.com/goplus/llgo/c/cjson"
|
2024-09-24 14:43:33 +08:00
|
|
|
"github.com/goplus/llgo/chore/_xtool/llcppsymg/clangutils"
|
2024-08-08 15:59:08 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Context struct {
|
2024-09-19 16:49:05 +08:00
|
|
|
Files []*FileEntry
|
2024-09-06 11:44:56 +08:00
|
|
|
IsCpp bool
|
2024-08-08 15:59:08 +08:00
|
|
|
}
|
|
|
|
|
|
2024-09-06 11:44:56 +08:00
|
|
|
func NewContext(isCpp bool) *Context {
|
2024-08-08 15:59:08 +08:00
|
|
|
return &Context{
|
2024-09-19 16:49:05 +08:00
|
|
|
Files: make([]*FileEntry, 0),
|
2024-09-06 11:44:56 +08:00
|
|
|
IsCpp: isCpp,
|
2024-08-08 15:59:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-16 11:52:22 +08:00
|
|
|
func (p *Context) Output() *cjson.JSON {
|
2024-09-19 15:35:42 +08:00
|
|
|
return MarshalOutputASTFiles(p.Files)
|
2024-08-16 11:52:22 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-08 15:59:08 +08:00
|
|
|
// ProcessFiles processes the given files and adds them to the context
|
|
|
|
|
func (p *Context) ProcessFiles(files []string) error {
|
|
|
|
|
for _, file := range files {
|
|
|
|
|
if err := p.processFile(file); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parse file and add it to the context,avoid duplicate parsing
|
|
|
|
|
func (p *Context) processFile(path string) error {
|
2024-09-19 16:49:05 +08:00
|
|
|
for _, entry := range p.Files {
|
|
|
|
|
if entry.Path == path {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-08-08 15:59:08 +08:00
|
|
|
}
|
2024-09-19 16:49:05 +08:00
|
|
|
parsedFiles, err := p.parseFile(path)
|
2024-08-08 15:59:08 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return errors.New("failed to parse file: " + path)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 16:49:05 +08:00
|
|
|
p.Files = append(p.Files, parsedFiles...)
|
2024-08-08 15:59:08 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 16:49:05 +08:00
|
|
|
func (p *Context) parseFile(path string) ([]*FileEntry, error) {
|
2024-09-24 14:43:33 +08:00
|
|
|
converter, err := NewConverter(&clangutils.Config{
|
2024-09-06 11:44:56 +08:00
|
|
|
File: path,
|
|
|
|
|
Temp: false,
|
|
|
|
|
IsCpp: p.IsCpp,
|
2024-08-19 18:01:46 +08:00
|
|
|
})
|
2024-08-08 15:59:08 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.New("failed to create converter " + path)
|
|
|
|
|
}
|
|
|
|
|
defer converter.Dispose()
|
|
|
|
|
|
|
|
|
|
files, err := converter.Convert()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
|
}
|