WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 01dc663

Browse files
committed
2 parents 7561bd4 + 35b1a79 commit 01dc663

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

parser-Go/main.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type Output struct {
5353
PackageInfo *uast.PackagePathInfo `json:"packageInfo"`
5454
ModuleName string `json:"moduleName"`
5555
GoModPath string `json:"goModPath"`
56+
NumOfGoMod int `json:"numOfGoMod"`
5657
}
5758

5859
func main() {
@@ -88,7 +89,7 @@ func parseSingleFile(file string, output string) {
8889
packages := make(map[string]*ast.Package)
8990
packages["__single__"] = pkg
9091

91-
buildAndPrint("__single_module__", packages, fset, output, "")
92+
buildAndPrint("__single_module__", packages, fset, output, "", 0)
9293
}
9394

9495
func parseGoModule(rootDir string, output string) {
@@ -97,59 +98,70 @@ func parseGoModule(rootDir string, output string) {
9798
var moduleName string
9899
// Read the module name from go.mod
99100
// Find the go.mod file
100-
goModPath, err := findGoMod(rootDir)
101+
goModPaths, err := findAllGoMod(rootDir)
101102
if err != nil {
102103
moduleName = "__unknown_module__"
103104
} else {
104-
name, _ := readModuleName(goModPath)
105+
name, _ := readModuleName(goModPaths[0])
105106
moduleName = name
106107
}
107108

108109
packages, _ := preparePackage(moduleName, rootDir, fset)
109110
//if err != nil {
110111
// panic(err)
111112
//}
112-
buildAndPrint(moduleName, packages, fset, output, goModPath)
113+
//默认取找到的第一个go.mod
114+
firstGoModPath := ""
115+
if goModPaths != nil {
116+
firstGoModPath = goModPaths[0]
117+
}
118+
buildAndPrint(moduleName, packages, fset, output, firstGoModPath, len(goModPaths))
113119
}
114120

115121
// findGoMod searches for a go.mod file starting from dir and recursing into subdirectories if not found
116-
func findGoMod(dir string) (string, error) {
122+
func findAllGoMod(dir string) ([]string, error) {
117123
if strings.Contains(dir, "/vendor") {
118-
return "", fmt.Errorf("find vendor")
124+
return nil, fmt.Errorf("find vendor")
119125
}
120126
const goModFileName = "go.mod"
121127

128+
var paths []string
129+
122130
// Check if go.mod exists in the current directory
123131
goModPath := filepath.Join(dir, goModFileName)
124-
if _, err := os.Stat(goModPath); !os.IsNotExist(err) {
125-
return goModPath, nil
132+
if _, err := os.Stat(goModPath); err == nil {
133+
paths = append(paths, goModPath)
126134
}
127135

128136
// If not found, recurse into subdirectories
129137
entries, err := os.ReadDir(dir)
130138
if err != nil {
131-
return "", err
139+
return nil, err
132140
}
133141

134142
for _, entry := range entries {
135143
if entry.IsDir() {
136144
subDir := filepath.Join(dir, entry.Name())
137-
goModPath, err := findGoMod(subDir)
138-
if err == nil {
139-
return goModPath, nil
145+
subPaths, err := findAllGoMod(subDir)
146+
if err == nil && len(subPaths) > 0 {
147+
paths = append(paths, subPaths...)
140148
}
141149
}
142150
}
143-
144-
return "", fmt.Errorf("go.mod not found in directory or subdirectories: %s", dir)
151+
if paths != nil {
152+
return paths, nil
153+
} else {
154+
return nil, fmt.Errorf("not found go.mod")
155+
}
145156
}
146157

147-
func buildAndPrint(moduleName string, packages map[string]*ast.Package, fset *token.FileSet, outputPath string, goModPath string) {
158+
func buildAndPrint(moduleName string, packages map[string]*ast.Package, fset *token.FileSet, outputPath string, goModPath string, numOfGoMod int) {
148159
packageInfo := buildPackage(moduleName, packages, fset)
149160
output := &Output{
150161
PackageInfo: packageInfo,
151162
ModuleName: moduleName,
152163
GoModPath: goModPath,
164+
NumOfGoMod: numOfGoMod,
153165
}
154166
//jsonBytes, err := json.MarshalIndent(output, "", " ")
155167
//if err != nil {

parser-Python/uast/visitor.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,31 @@ def visit_If(self, node):
217217

218218
def visit_While(self, node):
219219
bodys = []
220-
for body in node.body:
221-
bodys.append(self.packPos(body, self.visit(body)))
222-
return self.packPos(node, UNode.WhileStatement(UNode.SourceLocation(), UNode.Meta(),
223-
self.packPos(node.test, self.visit(node.test)), bodys))
220+
if node.body:
221+
col_offsets = [body.col_offset for body in node.body]
222+
end_col_offsets = [body.end_col_offset for body in node.body]
223+
min_col = min(col_offsets)
224+
max_col = max(end_col_offsets)
225+
body_loc = UNode.SourceLocation(
226+
UNode.Position(node.body[0].lineno, min_col),
227+
UNode.Position(node.body[-1].end_lineno, max_col),
228+
self.sourcefile
229+
)
230+
# 处理 body
231+
for body in node.body:
232+
bodys.append(self.packPos(body, self.visit(body)))
233+
else:
234+
body_loc = UNode.SourceLocation()
235+
236+
return self.packPos(
237+
node,
238+
UNode.WhileStatement(
239+
UNode.SourceLocation(),
240+
UNode.Meta(),
241+
self.packPos(node.test, self.visit(node.test)),
242+
UNode.ScopedStatement(body_loc, UNode.Meta(), bodys)
243+
)
244+
)
224245

225246
def visit_Gt(self, node):
226247
return ">"

0 commit comments

Comments
 (0)