Skip to content

Commit 499ef36

Browse files
authored
Merge pull request #360 from Pivot-Studio/griffin/refactor/docs-and-functions
refactor: add documents and rename some functions
2 parents c5f6b23 + ca2ed1d commit 499ef36

File tree

6 files changed

+138
-87
lines changed

6 files changed

+138
-87
lines changed

src/ast/compiler.rs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ pub fn pl_link(llvmmod: Module, oxbjs: Vec<PathBuf>, out: String, op: Options) {
208208
}
209209
}
210210

211+
/// compile_dry compiles the source code of pivot-lang into the pivot-lang AST with a wrapper.
212+
/// the `dry` refers the function ends up parsing at pivot-lang AST and won't interact with llvm.
211213
#[salsa::tracked]
212214
pub fn compile_dry(db: &dyn Db, docs: MemDocsInput) -> Option<ModWrapper> {
213215
let path = search_config_file(docs.file(db).to_string());
@@ -216,16 +218,18 @@ pub fn compile_dry(db: &dyn Db, docs: MemDocsInput) -> Option<ModWrapper> {
216218
return None;
217219
}
218220

219-
let input = docs
220-
.get_file_params(db, docs.file(db).clone(), true)
221+
let parser_entry = docs
222+
.finalize_parser_input(db, docs.file(db).clone(), true)
221223
.unwrap();
224+
222225
log::trace!("entering compile_dry_file");
226+
let re = compile_dry_file(db, parser_entry);
223227

224-
let re = compile_dry_file(db, input);
225-
// calculate find references results
228+
// calculate find references results for lsp
226229
if docs.action(db) != ActionType::FindReferences {
227230
return re;
228231
}
232+
229233
if let Some(res) = db.get_ref_str() {
230234
if let Some(plmod) = re {
231235
plmod
@@ -238,33 +242,39 @@ pub fn compile_dry(db: &dyn Db, docs: MemDocsInput) -> Option<ModWrapper> {
238242
}
239243

240244
#[salsa::tracked(recovery_fn=cycle_deps_recover)]
241-
pub fn compile_dry_file(db: &dyn Db, docs: FileCompileInput) -> Option<ModWrapper> {
242-
if docs.file(db).ends_with(".toml") {
243-
log::error!("lsp error: toml file {}", docs.file(db));
245+
pub fn compile_dry_file(db: &dyn Db, parser_entry: FileCompileInput) -> Option<ModWrapper> {
246+
if parser_entry.file(db).ends_with(".toml") {
247+
log::error!("lsp error: toml file {}", parser_entry.file(db));
244248
// skip toml
245249
return None;
246250
}
247-
// eprintln!("compile_dry_file: {:#?}", docs.debug_all(db));
248-
let re = docs.get_file_content(db);
249-
re?;
250-
let src = re.unwrap();
251-
log::trace!("src {:#?} id {:?}", src.text(db), src.path(db));
252-
let parse_result = parse(db, src);
253-
if let Err(e) = parse_result {
254-
log::error!("source code parse failed {}", e);
255-
return None;
256-
}
257-
let node = parse_result.unwrap();
258-
let program = Program::new(
259-
db,
260-
node,
261-
docs.get_emit_params(db),
262-
docs.docs(db),
263-
docs.config(db),
264-
docs.opt(db),
251+
252+
let entry_file_content = parser_entry.get_file_content(db).unwrap();
253+
log::trace!(
254+
"src {:#?} id {:?}",
255+
entry_file_content.text(db),
256+
entry_file_content.path(db)
265257
);
266-
log::trace!("entering emit");
267-
Some(program.emit(db))
258+
259+
let parsing_result = parse(db, entry_file_content);
260+
match parsing_result {
261+
Err(e) => {
262+
log::error!("source code parse failed {}", e);
263+
None
264+
}
265+
Ok(root_node) => {
266+
let program = Program::new(
267+
db,
268+
root_node,
269+
parser_entry.get_emit_params(db),
270+
parser_entry.docs(db),
271+
parser_entry.config(db),
272+
parser_entry.opt(db),
273+
);
274+
log::trace!("entering emit");
275+
Some(program.emit(db))
276+
}
277+
}
268278
}
269279

270280
#[cfg(feature = "llvm")]

src/ast/node/program.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl Program {
257257
let p = self.config(db).project;
258258
log::trace!("load dep {:?} for {:?} (project {:?})", path, pkgname, p);
259259
let ff = path.to_str().unwrap().to_string();
260-
let mut f = self.docs(db).get_file_params(db, ff.clone(), false);
260+
let mut f = self.docs(db).finalize_parser_input(db, ff.clone(), false);
261261
let mut symbol_opt = None;
262262
#[cfg(target_arch = "wasm32")]
263263
if ff.starts_with("core") || ff.starts_with("std") {
@@ -270,7 +270,7 @@ impl Program {
270270
if let Some(p) = path.parent() {
271271
mod_id = Some(p.file_name().unwrap().to_str().unwrap().to_string());
272272
let file = p.with_extension("pi").to_str().unwrap().to_string();
273-
f = self.docs(db).get_file_params(db, file, false);
273+
f = self.docs(db).finalize_parser_input(db, file, false);
274274
symbol_opt = Some(
275275
path.with_extension("")
276276
.file_name()

src/jar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct Jar(
99
crate::lsp::mem_docs::MemDocsInput,
1010
crate::lsp::mem_docs::MemDocsInput_get_current_file_content,
1111
crate::lsp::mem_docs::MemDocsInput_get_file_content,
12-
crate::lsp::mem_docs::MemDocsInput_get_file_params,
12+
crate::lsp::mem_docs::MemDocsInput_finalize_parser_input,
1313
crate::lsp::mem_docs::EmitParams,
1414
crate::lsp::mem_docs::FileCompileInput,
1515
crate::lsp::mem_docs::FileCompileInput_get_file_content,
@@ -37,7 +37,7 @@ pub struct Jar(
3737
crate::ast::node::program::emit_file,
3838
crate::ast::node::program::LspParams,
3939
crate::ast::node::program::Program_is_active_file,
40-
crate::utils::read_config::get_config,
40+
crate::utils::read_config::prepare_build_envs,
4141
crate::utils::read_config::ConfigWrapper,
4242
crate::utils::read_config::ConfigWrapper_resolve_dep_path,
4343
);

src/lsp/mem_docs.rs

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
range::Pos,
1515
},
1616
nomparser::SourceProgram,
17-
utils::read_config::{get_config, search_config_file, Config},
17+
utils::read_config::{prepare_build_envs, search_config_file, Config},
1818
Db,
1919
};
2020

@@ -39,9 +39,13 @@ pub struct EmitParams {
3939
#[salsa::input]
4040
pub struct MemDocsInput {
4141
pub docs: Arc<Mutex<RefCell<MemDocs>>>,
42+
/// file is the absolute path of the input file to be build,
43+
/// currently we will build the whole project instead of this file only.
4244
#[return_ref]
4345
pub file: String,
4446
pub op: Options,
47+
48+
/// the following fields are used for lsp server to hold additional information
4549
pub action: ActionType,
4650
pub params: Option<(Pos, Option<String>)>,
4751
pub edit_pos: Option<Pos>,
@@ -50,6 +54,7 @@ pub struct MemDocsInput {
5054
/// 必须有#[id],否则会导致lru cache失效
5155
#[salsa::tracked]
5256
pub struct FileCompileInput {
57+
/// file represents the entry file path to parse
5358
#[return_ref]
5459
pub file: String,
5560
#[return_ref]
@@ -61,6 +66,7 @@ pub struct FileCompileInput {
6166
#[salsa::tracked]
6267
impl FileCompileInput {
6368
#[salsa::tracked]
69+
// get_file_content gets the file content from cache or reads from file with the self.file
6470
pub fn get_file_content(self, db: &dyn Db) -> Option<SourceProgram> {
6571
// let f = self.file(db);
6672
// eprintln!("get_file_content {}", f);
@@ -71,11 +77,14 @@ impl FileCompileInput {
7177
.unwrap()
7278
.borrow_mut()
7379
.get_file_content(db, self.file(db));
74-
if re.is_none() {
75-
let f = self.file(db);
76-
log::error!("lsp error: get_file_content failed {}", f);
80+
match re {
81+
None => {
82+
let f = self.file(db);
83+
log::error!("lsp error: get_file_content failed {}", f);
84+
None
85+
}
86+
_ => re,
7787
}
78-
re
7988
}
8089
#[salsa::tracked]
8190
pub fn get_emit_params(self, db: &dyn Db) -> EmitParams {
@@ -138,47 +147,69 @@ impl MemDocsInput {
138147
.get_file_content(db, &f);
139148
re
140149
}
150+
151+
/// finalize_parser_input prepares the building environments according to the kagari.toml,
152+
/// and generates the parser entry file.
153+
/// it applies the entry_file as the parser entry if override_with_kagari_entry is false,
154+
/// otherwise the entry field in kagari.toml will be applied.
141155
#[salsa::tracked]
142-
pub fn get_file_params(self, db: &dyn Db, f: String, entry: bool) -> Option<FileCompileInput> {
143-
let f = crate::utils::canonicalize(f);
144-
if f.is_err() {
145-
log::debug!("lsp error: {}", f.err().unwrap());
146-
return None;
156+
pub fn finalize_parser_input(
157+
self,
158+
db: &dyn Db,
159+
entry_file: String,
160+
override_with_kagari_entry: bool,
161+
) -> Option<FileCompileInput> {
162+
let mut final_entry_file: String;
163+
let re_entry_file = crate::utils::canonicalize(entry_file);
164+
match re_entry_file {
165+
Err(e) => {
166+
log::debug!("lsp error: {}", e);
167+
return None;
168+
}
169+
Ok(f) => {
170+
final_entry_file = f.to_string_lossy().to_string();
171+
}
147172
}
148-
let mut file = f.unwrap().to_string_lossy().to_string();
149-
let path = search_config_file(file.clone());
150-
if path.is_err() {
151-
log::debug!("lsp error: {}", path.err().unwrap());
173+
174+
let re_kagari_path = search_config_file(final_entry_file.clone());
175+
if re_kagari_path.is_err() {
176+
log::debug!("lsp error: {}", re_kagari_path.err().unwrap());
152177
return None;
153178
}
154-
let path = path.unwrap();
155-
let buf = crate::utils::canonicalize(PathBuf::from(path.clone())).unwrap();
156-
let parant = buf.parent().unwrap();
157-
let re = get_config(
179+
180+
let kagari_path = re_kagari_path.unwrap();
181+
let re_config = prepare_build_envs(
158182
db,
159183
self.docs(db)
160184
.lock()
161185
.unwrap()
162186
.borrow_mut()
163-
.get_file_content(db, &path)
187+
.get_file_content(db, &kagari_path)
164188
.unwrap(),
165189
);
166-
if re.is_err() {
167-
log::debug!("lsp error: {}", re.err().unwrap());
168-
return None;
169-
}
170-
let config = re.unwrap();
171-
if entry {
172-
file = config.entry.clone();
190+
191+
match re_config {
192+
Err(info) => {
193+
log::debug!("lsp error: {}", info);
194+
None
195+
}
196+
Ok(config) => {
197+
// use entry path inside kagari.toml instead of the input file
198+
if override_with_kagari_entry {
199+
final_entry_file = config.entry.clone();
200+
}
201+
let buf = crate::utils::canonicalize(PathBuf::from(kagari_path.clone())).unwrap();
202+
let parant = buf.parent().unwrap();
203+
Some(FileCompileInput::new(
204+
db,
205+
final_entry_file,
206+
parant.to_str().unwrap().to_string(),
207+
self,
208+
config,
209+
self.op(db).optimization,
210+
))
211+
}
173212
}
174-
Some(FileCompileInput::new(
175-
db,
176-
file,
177-
parant.to_str().unwrap().to_string(),
178-
self,
179-
config,
180-
self.op(db).optimization,
181-
))
182213
}
183214
}
184215

src/nomparser/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub enum ComplexOp {
8181
Field(Option<Box<VarNode>>),
8282
}
8383

84+
/// SourceProgram represents a single file with its contents and the absulate path
8485
#[salsa::input]
8586
pub struct SourceProgram {
8687
#[return_ref]
@@ -94,12 +95,12 @@ pub struct SourceProgram {
9495
pub fn parse(db: &dyn Db, source: SourceProgram) -> Result<ProgramNodeWrapper, String> {
9596
let text = source.text(db);
9697
let re = program(Span::new_extra(text, false));
97-
if let Err(e) = re {
98-
return Err(format!("{:?}", e));
98+
match re {
99+
Err(e) => Err(format!("{:?}", e)),
100+
Ok((_, node)) => {
101+
log::info!("parse {:?}", source.path(db));
102+
Ok(ProgramNodeWrapper::new(db, node))
103+
}
99104
}
100-
let (_, node) = re.unwrap();
101-
log::info!("parse {:?}", source.path(db));
102-
103-
Ok(ProgramNodeWrapper::new(db, node))
104105
}
105106
// ANCHOR_END: parse

0 commit comments

Comments
 (0)