Files
llgo/chore/_xtool/llcppsigfetch/parse/parse.go

99 lines
1.9 KiB
Go
Raw Normal View History

package parse
import (
"errors"
"fmt"
"os"
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-10-21 20:51:08 +08:00
type dbgFlags = int
const (
DbgParse dbgFlags = 1 << iota
DbgFlagAll = DbgParse
)
var (
debugParse bool
)
func SetDebug(dbgFlags dbgFlags) {
debugParse = (dbgFlags & DbgParse) != 0
}
type Context struct {
2024-09-19 16:49:05 +08:00
Files []*FileEntry
IsCpp bool
}
func NewContext(isCpp bool) *Context {
return &Context{
2024-09-19 16:49:05 +08:00
Files: make([]*FileEntry, 0),
IsCpp: isCpp,
}
}
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
}
// ProcessFiles processes the given files and adds them to the context
func (p *Context) ProcessFiles(files []string) error {
2024-10-21 20:51:08 +08:00
if debugParse {
fmt.Fprintln(os.Stderr, "ProcessFiles: files", files, "isCpp", p.IsCpp)
2024-10-21 20:51:08 +08:00
}
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-10-21 20:51:08 +08:00
if debugParse {
fmt.Fprintln(os.Stderr, "processFile: path", path)
2024-10-21 20:51:08 +08:00
}
2024-09-19 16:49:05 +08:00
for _, entry := range p.Files {
if entry.Path == path {
2024-10-21 20:51:08 +08:00
if debugParse {
fmt.Fprintln(os.Stderr, "processFile: already parsed", path)
2024-10-21 20:51:08 +08:00
}
2024-09-19 16:49:05 +08:00
return nil
}
}
2024-09-19 16:49:05 +08:00
parsedFiles, err := p.parseFile(path)
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...)
return nil
}
2024-09-19 16:49:05 +08:00
func (p *Context) parseFile(path string) ([]*FileEntry, error) {
2024-10-21 20:51:08 +08:00
if debugParse {
fmt.Fprintln(os.Stderr, "parseFile: path", path)
2024-10-21 20:51:08 +08:00
}
2024-09-24 14:43:33 +08:00
converter, err := NewConverter(&clangutils.Config{
File: path,
Temp: false,
IsCpp: p.IsCpp,
2024-08-19 18:01:46 +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
}