Skip to content

Commit f7fdc17

Browse files
committed
refactor: unify all parser into lang
1 parent f409e94 commit f7fdc17

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+257
-272
lines changed

script/make_parser.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
root=$(dirname $(realpath $(dirname $0)))
1717

18-
# make go_ast
19-
cd $root/src/compress/golang/plugin
20-
go build -o ../../../../tools/parser/go_ast .
18+
# # make go_ast
19+
# cd $root/src/compress/golang/plugin
20+
# go build -o ../../../../tools/parser/go_ast .
2121

2222

2323
# make lang

src/bin/cmd.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
use std::{env, panic, process, thread, time::Duration};
1616

1717
use ABCoder::{
18-
compress::{compress::compress_all, types::types::CodeCache},
19-
config::CONFIG,
20-
repo,
18+
compress::compress::compress_all,
19+
repo::{self, CompressOptions},
2120
};
2221

2322
#[derive(Clone, Debug)]
@@ -36,6 +35,17 @@ struct CompressAction {
3635
export_compress: bool,
3736
force_update_ast: bool,
3837
not_load_external_symbol: bool,
38+
no_need_comment: bool,
39+
}
40+
41+
impl CompressAction {
42+
fn to_compress_options(&self) -> CompressOptions {
43+
CompressOptions {
44+
export_compress: self.export_compress,
45+
not_load_external_symbol: self.not_load_external_symbol,
46+
no_need_comment: self.no_need_comment,
47+
}
48+
}
3949
}
4050

4151
fn main() {
@@ -61,7 +71,9 @@ Action: compress
6171
compress: compress the repo. Including flags:
6272
--export-compress: export the compress result
6373
--force-update-ast: force parsing repo and merge the previous result
64-
--not-load-external-symbol: not load external external symbols to speed up parsing";
74+
--not-load-external-symbol: not load external external symbols to speed up parsing
75+
--no-need-comment: not need comment in symbol content (only works for Go now)
76+
";
6577

6678
fn parse_options() -> Options {
6779
let args: Vec<String> = env::args().collect();
@@ -85,6 +97,9 @@ fn parse_options() -> Options {
8597
"--not-load-external-symbol" => {
8698
compress_action.not_load_external_symbol = true;
8799
}
100+
"--no-need-comment" => {
101+
compress_action.no_need_comment = true;
102+
}
88103
_ => {}
89104
}
90105
}
@@ -109,7 +124,7 @@ fn compress(repo_path: &String, cmp: &CompressAction) {
109124
// recoverable logic
110125
let run = || {
111126
// get the repo
112-
let repo = repo::get_repo(repo_path, !cmp.not_load_external_symbol);
127+
let repo = repo::get_repo(repo_path, &cmp.to_compress_options());
113128
if let Err(err) = repo {
114129
println!("get repo error: {:?}", err);
115130
process::exit(1);
@@ -144,7 +159,7 @@ fn compress(repo_path: &String, cmp: &CompressAction) {
144159

145160
fn export_compress(repo_path: &String, cmp: &CompressAction) {
146161
// get the repo
147-
let repo = repo::get_repo(repo_path, !cmp.not_load_external_symbol);
162+
let repo = repo::get_repo(repo_path, &cmp.to_compress_options());
148163
if let Err(err) = repo {
149164
println!("get repo error: {:?}", err);
150165

@@ -162,17 +177,17 @@ fn export_compress(repo_path: &String, cmp: &CompressAction) {
162177
println!("successfully compressed repo: {}", repo.id);
163178
}
164179

165-
fn merge_compress(repo_path: &String, opts: &CompressAction) {
180+
fn merge_compress(repo_path: &String, cmp: &CompressAction) {
166181
// get old repo
167-
let repo = repo::get_repo(repo_path, !opts.not_load_external_symbol);
182+
let repo = repo::get_repo(repo_path, &cmp.to_compress_options());
168183
if let Err(err) = repo {
169184
println!("get repo error: {:?}", err);
170185
process::exit(1);
171186
}
172187
let mut repo = repo.unwrap();
173188

174189
// parse new repo
175-
let nrepo = repo::force_parse_repo(repo_path, !opts.not_load_external_symbol);
190+
let nrepo = repo::force_parse_repo(repo_path, &cmp.to_compress_options());
176191
if let Err(err) = nrepo {
177192
println!("parse repo error: {:?}", err);
178193
process::exit(1);

src/compress/golang/mod.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/compress/golang/parser.rs

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/compress/golang/plugin/go.mod

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/compress/golang/plugin/go.sum

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/compress/golang/plugin/main.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/compress/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
pub mod compress;
16-
pub mod golang;
1716
mod llm;
1817
pub mod parser;
1918
pub mod rust;

src/config/mod.rs

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::path::{Path, PathBuf};
1717
use lazy_static::lazy_static;
1818
use serde::Deserialize;
1919

20-
use crate::repo::{self};
20+
use crate::repo::{self, CompressOptions};
2121

2222
#[derive(Debug)]
2323
pub enum Language {
@@ -185,6 +185,16 @@ pub enum ProgramLanguage {
185185
Unknown(String),
186186
}
187187

188+
impl ProgramLanguage {
189+
pub fn to_string(&self) -> String {
190+
match self {
191+
ProgramLanguage::Rust => "rust".to_string(),
192+
ProgramLanguage::Go => "go".to_string(),
193+
ProgramLanguage::Unknown(s) => s.to_string(),
194+
}
195+
}
196+
}
197+
188198
fn decide_language(path: &str) -> ProgramLanguage {
189199
// scan root directory
190200
walkdir::WalkDir::new(path)
@@ -208,40 +218,22 @@ fn decide_language(path: &str) -> ProgramLanguage {
208218
.unwrap_or(ProgramLanguage::Unknown(path.to_string()))
209219
}
210220

211-
pub fn parser_and_args<'a>(repo_path: &'a str, load_extern: bool) -> (String, Vec<String>) {
221+
pub fn parser_and_args<'a>(repo_path: &'a str, opts: &CompressOptions) -> (String, Vec<String>) {
212222
let lang = decide_language(repo_path);
213-
let path = match lang {
214-
ProgramLanguage::Go => go_ast_path(),
215-
ProgramLanguage::Rust => rust_ast_path(),
216-
_ => panic!("unsupported language"),
217-
};
218-
let args = match lang {
219-
ProgramLanguage::Go => {
220-
let mut args = vec!["--collect_comment".to_string()];
221-
if load_extern {
222-
args.push("--refer_code_depth=1".to_string());
223-
}
224-
if CONFIG.exclude_dirs.len() > 0 {
225-
args.push(format!("--excludes={}", &CONFIG.exclude_dirs.join(",")));
226-
}
227-
args.push(repo_path.to_string());
228-
args
229-
}
230-
ProgramLanguage::Rust => {
231-
let mut args = vec![
232-
"collect".to_string(),
233-
"rust".to_string(),
234-
repo_path.to_string(),
235-
];
236-
for exclude in &CONFIG.exclude_dirs {
237-
args.push(format!("--exclude={exclude}"));
238-
}
239-
if load_extern {
240-
args.push("--load-external-symbol".to_string());
241-
}
242-
args
243-
}
244-
_ => panic!("unsupported language"),
245-
};
223+
let path = rust_ast_path();
224+
let mut args = vec![
225+
"collect".to_string(),
226+
lang.to_string(),
227+
repo_path.to_string(),
228+
];
229+
for exclude in &CONFIG.exclude_dirs {
230+
args.push(format!("--exclude={exclude}"));
231+
}
232+
if !opts.not_load_external_symbol {
233+
args.push("--load-external-symbol".to_string());
234+
}
235+
if opts.no_need_comment {
236+
args.push("--no-need-comment".to_string());
237+
}
246238
(path, args)
247239
}

src/lang/collect/collect.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
type CollectOption struct {
3232
LoadExternalSymbol bool
3333
NeedStdSymbol bool
34+
NoNeedComment bool
35+
Language lsp.Language
3436
Excludes []string
3537
}
3638

0 commit comments

Comments
 (0)