Skip to content

Commit b5b577d

Browse files
authored
Merge 6515356 into 0687a5a
2 parents 0687a5a + 6515356 commit b5b577d

File tree

7 files changed

+136
-91
lines changed

7 files changed

+136
-91
lines changed

src/ast/compiler.rs

Lines changed: 33 additions & 28 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 = get_config_path(docs.file(db).to_string());
@@ -216,15 +218,16 @@ pub fn compile_dry(db: &dyn Db, docs: MemDocsInput) -> Option<ModWrapper> {
216218
return None;
217219
}
218220

219-
let input = docs.get_file_params(db, docs.file(db).clone(), true);
220-
input?;
221-
let input = input.unwrap();
221+
let parser_entry = docs.finalize_parser_input(db, docs.file(db).clone(), true).unwrap();
222+
222223
log::trace!("entering compile_dry_file");
223-
let re = compile_dry_file(db, input);
224-
// calculate find references results
224+
let re = compile_dry_file(db, parser_entry);
225+
226+
// calculate find references results for lsp
225227
if docs.action(db) != ActionType::FindReferences {
226228
return re;
227229
}
230+
228231
if let Some(res) = db.get_ref_str() {
229232
if let Some(plmod) = re {
230233
plmod
@@ -237,33 +240,35 @@ pub fn compile_dry(db: &dyn Db, docs: MemDocsInput) -> Option<ModWrapper> {
237240
}
238241

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

269274
#[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: 60 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, get_config_path, Config},
17+
utils::read_config::{ get_config_path, prepare_build_envs, 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,64 @@ 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(self, db: &dyn Db, entry_file: String, override_with_kagari_entry: bool) -> Option<FileCompileInput> {
157+
let mut final_entry_file: String;
158+
let re_entry_file = crate::utils::canonicalize(entry_file);
159+
match re_entry_file{
160+
Err(e) =>{
161+
log::debug!("lsp error: {}", e );
162+
return None;
163+
},
164+
Ok(f) =>{
165+
final_entry_file = f.to_string_lossy().to_string();
166+
}
147167
}
148-
let mut file = f.unwrap().to_string_lossy().to_string();
149-
let path = get_config_path(file.clone());
150-
if path.is_err() {
151-
log::debug!("lsp error: {}", path.err().unwrap());
168+
169+
let re_kagari_path = get_config_path(final_entry_file.clone());
170+
if re_kagari_path.is_err() {
171+
log::debug!("lsp error: {}", re_kagari_path.err().unwrap());
152172
return None;
153173
}
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(
174+
175+
let kagari_path = re_kagari_path.unwrap();
176+
let re_config = prepare_build_envs(
158177
db,
159178
self.docs(db)
160179
.lock()
161180
.unwrap()
162181
.borrow_mut()
163-
.get_file_content(db, &path)
182+
.get_file_content(db, &kagari_path)
164183
.unwrap(),
165184
);
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();
185+
186+
match re_config{
187+
Err(info) => {
188+
log::debug!("lsp error: {}", info);
189+
return None;
190+
},
191+
Ok(config ) => {
192+
// use entry path inside kagari.toml instead of the input file
193+
if override_with_kagari_entry {
194+
final_entry_file = config.entry.clone();
195+
}
196+
let buf = crate::utils::canonicalize(PathBuf::from(kagari_path.clone())).unwrap();
197+
let parant = buf.parent().unwrap();
198+
Some(FileCompileInput::new(
199+
db,
200+
final_entry_file,
201+
parant.to_str().unwrap().to_string(),
202+
self,
203+
config,
204+
self.op(db).optimization,
205+
))
206+
},
173207
}
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-
))
182208
}
183209
}
184210

src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,7 @@ fn main() {
146146
// You can check the value provided by positional arguments, or option arguments
147147
if let Some(name) = cli.name.as_deref() {
148148
logger.timestamp(stderrlog::Timestamp::Off).init().unwrap();
149-
let db = Database::default();
150-
let filepath = Path::new(name);
151-
let abs = crate::utils::canonicalize(filepath).unwrap();
149+
152150
let op = compiler::Options {
153151
genir: cli.genir,
154152
printast: cli.printast,
@@ -166,6 +164,10 @@ fn main() {
166164
} else {
167165
ActionType::Compile
168166
};
167+
168+
let db = Database::default();
169+
let filepath = Path::new(name);
170+
let abs = crate::utils::canonicalize(filepath).unwrap();
169171
let mem = MemDocsInput::new(
170172
&db,
171173
Arc::new(Mutex::new(RefCell::new(mem_docs::MemDocs::default()))),

src/nomparser/mod.rs

Lines changed: 9 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,14 @@ 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)=>{
100+
return Err(format!("{:?}", e));
101+
},
102+
Ok((_,node)) => {
103+
log::info!("parse {:?}", source.path(db));
104+
return Ok(ProgramNodeWrapper::new(db, node))
105+
}
99106
}
100-
let (_, node) = re.unwrap();
101-
log::info!("parse {:?}", source.path(db));
102-
103-
Ok(ProgramNodeWrapper::new(db, node))
104107
}
105108
// ANCHOR_END: parse

0 commit comments

Comments
 (0)