-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathparse.go
More file actions
198 lines (182 loc) · 5.08 KB
/
parse.go
File metadata and controls
198 lines (182 loc) · 5.08 KB
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
/**
* Copyright 2025 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package lang
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/cloudwego/abcoder/lang/collect"
"github.com/cloudwego/abcoder/lang/cxx"
"github.com/cloudwego/abcoder/lang/golang/parser"
"github.com/cloudwego/abcoder/lang/log"
"github.com/cloudwego/abcoder/lang/lsp"
"github.com/cloudwego/abcoder/lang/rust"
"github.com/cloudwego/abcoder/lang/uniast"
)
// ParseOptions is the options for parsing the repo.
type ParseOptions struct {
// LSP sever executable path
LSP string
// Language of the repo
Verbose bool
collect.CollectOption
// specify the repo id
RepoID string
}
func Parse(ctx context.Context, uri string, args ParseOptions) ([]byte, error) {
if !filepath.IsAbs(uri) {
uri, _ = filepath.Abs(uri)
}
l, lspPath, err := checkLSP(args.Language, args.LSP)
if err != nil {
return nil, err
}
openfile, opentime, err := checkRepoPath(uri, l)
if err != nil {
return nil, err
}
var client *lsp.LSPClient
if lspPath != "" {
// Initialize the LSP client
log.Info("start initialize LSP server %s...\n", lspPath)
var err error
client, err = lsp.NewLSPClient(uri, openfile, opentime, lsp.ClientOptions{
Server: lspPath,
Language: l,
Verbose: args.Verbose,
})
if err != nil {
log.Error("failed to initialize LSP server: %v\n", err)
return nil, err
}
log.Info("end initialize LSP server")
}
repo, err := collectSymbol(ctx, client, uri, args.CollectOption)
if err != nil {
log.Error("Failed to collect symbols: %v\n", err)
return nil, err
}
log.Info("all symbols collected, start writing to stdout...\n")
if args.RepoID != "" {
repo.Name = args.RepoID
}
out, err := json.Marshal(repo)
if err != nil {
log.Error("Failed to marshal repository: %v\n", err)
return nil, err
}
return out, nil
}
func checkRepoPath(repoPath string, language uniast.Language) (openfile string, wait time.Duration, err error) {
if _, err := os.Stat(repoPath); os.IsNotExist(err) {
return "", 0, fmt.Errorf("repository not found: %s", repoPath)
}
switch language {
case uniast.Rust:
// NOTICE: open the Cargo.toml file is required for Rust projects
openfile, wait = rust.CheckRepo(repoPath)
case uniast.Cxx:
openfile, wait = cxx.CheckRepo(repoPath)
default:
openfile = ""
wait = 0
}
log.Info("open file '%s' and wait for %d seconds for initialize workspace\n", openfile, wait/time.Second)
return
}
func checkLSP(language uniast.Language, lspPath string) (l uniast.Language, s string, err error) {
switch language {
case uniast.Rust:
l, s = rust.GetDefaultLSP()
case uniast.Cxx:
l, s = cxx.GetDefaultLSP()
case uniast.Golang:
l = uniast.Golang
s = ""
if _, err := exec.LookPath("go"); err != nil {
if _, err := os.Stat(lspPath); os.IsNotExist(err) {
log.Error("Go compiler not found, please make it excutable!\n", lspPath)
return uniast.Unknown, "", err
}
}
return
default:
return uniast.Unknown, "", fmt.Errorf("unsupported language: %s", language)
}
// check if lsp excutable
if lspPath != "" {
if _, err := exec.LookPath(lspPath); err != nil {
if _, err := os.Stat(lspPath); os.IsNotExist(err) {
log.Error("Language server %s not found, please make it excutable!\n", lspPath)
return uniast.Unknown, "", err
}
}
s = lspPath
}
return
}
func collectSymbol(ctx context.Context, cli *lsp.LSPClient, repoPath string, opts collect.CollectOption) (repo *uniast.Repository, err error) {
if opts.Language == uniast.Golang {
repo, err = callGoParser(ctx, repoPath, opts)
if err != nil {
return nil, err
}
} else {
collector := collect.NewCollector(repoPath, cli)
collector.CollectOption = opts
log.Info("start collecting symbols...\n")
err = collector.Collect(ctx)
if err != nil {
return nil, err
}
log.Info("all symbols collected.\n")
log.Info("start exporting symbols...\n")
repo, err = collector.Export(ctx)
if err != nil {
return nil, err
}
}
if err := repo.BuildGraph(); err != nil {
return nil, err
}
return repo, nil
}
func callGoParser(ctx context.Context, repoPath string, opts collect.CollectOption) (*uniast.Repository, error) {
goopts := parser.Options{}
if opts.LoadExternalSymbol {
goopts.ReferCodeDepth = 1
}
if !opts.NoNeedComment {
goopts.CollectComment = true
}
if opts.NeedTest {
goopts.NeedTest = true
}
if opts.LoadByPackages {
goopts.LoadByPackages = true
}
goopts.Excludes = opts.Excludes
p := parser.NewParser(repoPath, repoPath, goopts)
repo, err := p.ParseRepo()
if err != nil {
return nil, err
}
return &repo, nil
}