Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add documents and rename some functions #360

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 37 additions & 27 deletions src/ast/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@
}
}

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

let input = docs
.get_file_params(db, docs.file(db).clone(), true)
let parser_entry = docs
.finalize_parser_input(db, docs.file(db).clone(), true)
.unwrap();

log::trace!("entering compile_dry_file");
let re = compile_dry_file(db, parser_entry);

let re = compile_dry_file(db, input);
// calculate find references results
// calculate find references results for lsp
xieyuschen marked this conversation as resolved.
Show resolved Hide resolved
if docs.action(db) != ActionType::FindReferences {
return re;
}

if let Some(res) = db.get_ref_str() {
if let Some(plmod) = re {
plmod
Expand All @@ -238,33 +242,39 @@
}

#[salsa::tracked(recovery_fn=cycle_deps_recover)]
pub fn compile_dry_file(db: &dyn Db, docs: FileCompileInput) -> Option<ModWrapper> {
if docs.file(db).ends_with(".toml") {
log::error!("lsp error: toml file {}", docs.file(db));
pub fn compile_dry_file(db: &dyn Db, parser_entry: FileCompileInput) -> Option<ModWrapper> {
if parser_entry.file(db).ends_with(".toml") {
log::error!("lsp error: toml file {}", parser_entry.file(db));
// skip toml
return None;
}
// eprintln!("compile_dry_file: {:#?}", docs.debug_all(db));
let re = docs.get_file_content(db);
re?;
let src = re.unwrap();
log::trace!("src {:#?} id {:?}", src.text(db), src.path(db));
let parse_result = parse(db, src);
if let Err(e) = parse_result {
log::error!("source code parse failed {}", e);
return None;
}
let node = parse_result.unwrap();
let program = Program::new(
db,
node,
docs.get_emit_params(db),
docs.docs(db),
docs.config(db),
docs.opt(db),

let entry_file_content = parser_entry.get_file_content(db).unwrap();
log::trace!(
"src {:#?} id {:?}",
entry_file_content.text(db),
entry_file_content.path(db)

Check warning on line 256 in src/ast/compiler.rs

View check run for this annotation

Codecov / codecov/patch

src/ast/compiler.rs#L255-L256

Added lines #L255 - L256 were not covered by tests
);
log::trace!("entering emit");
Some(program.emit(db))

let parsing_result = parse(db, entry_file_content);
match parsing_result {
Err(e) => {
log::error!("source code parse failed {}", e);

Check warning on line 262 in src/ast/compiler.rs

View check run for this annotation

Codecov / codecov/patch

src/ast/compiler.rs#L262

Added line #L262 was not covered by tests
None
}
Ok(root_node) => {
let program = Program::new(
db,
root_node,
parser_entry.get_emit_params(db),
parser_entry.docs(db),
parser_entry.config(db),
parser_entry.opt(db),
);
log::trace!("entering emit");
Some(program.emit(db))
}
}
}

#[cfg(feature = "llvm")]
Expand Down
4 changes: 2 additions & 2 deletions src/ast/node/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl Program {
let p = self.config(db).project;
log::trace!("load dep {:?} for {:?} (project {:?})", path, pkgname, p);
let ff = path.to_str().unwrap().to_string();
let mut f = self.docs(db).get_file_params(db, ff.clone(), false);
let mut f = self.docs(db).finalize_parser_input(db, ff.clone(), false);
let mut symbol_opt = None;
#[cfg(target_arch = "wasm32")]
if ff.starts_with("core") || ff.starts_with("std") {
Expand All @@ -270,7 +270,7 @@ impl Program {
if let Some(p) = path.parent() {
mod_id = Some(p.file_name().unwrap().to_str().unwrap().to_string());
let file = p.with_extension("pi").to_str().unwrap().to_string();
f = self.docs(db).get_file_params(db, file, false);
f = self.docs(db).finalize_parser_input(db, file, false);
symbol_opt = Some(
path.with_extension("")
.file_name()
Expand Down
4 changes: 2 additions & 2 deletions src/jar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Jar(
crate::lsp::mem_docs::MemDocsInput,
crate::lsp::mem_docs::MemDocsInput_get_current_file_content,
crate::lsp::mem_docs::MemDocsInput_get_file_content,
crate::lsp::mem_docs::MemDocsInput_get_file_params,
crate::lsp::mem_docs::MemDocsInput_finalize_parser_input,
crate::lsp::mem_docs::EmitParams,
crate::lsp::mem_docs::FileCompileInput,
crate::lsp::mem_docs::FileCompileInput_get_file_content,
Expand Down Expand Up @@ -37,7 +37,7 @@ pub struct Jar(
crate::ast::node::program::emit_file,
crate::ast::node::program::LspParams,
crate::ast::node::program::Program_is_active_file,
crate::utils::read_config::get_config,
crate::utils::read_config::prepare_build_envs,
crate::utils::read_config::ConfigWrapper,
crate::utils::read_config::ConfigWrapper_resolve_dep_path,
);
Expand Down
99 changes: 65 additions & 34 deletions src/lsp/mem_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
range::Pos,
},
nomparser::SourceProgram,
utils::read_config::{get_config, search_config_file, Config},
utils::read_config::{prepare_build_envs, search_config_file, Config},
Db,
};

Expand All @@ -39,9 +39,13 @@
#[salsa::input]
pub struct MemDocsInput {
pub docs: Arc<Mutex<RefCell<MemDocs>>>,
/// file is the absolute path of the input file to be build,
/// currently we will build the whole project instead of this file only.
#[return_ref]
pub file: String,
pub op: Options,

/// the following fields are used for lsp server to hold additional information
pub action: ActionType,
pub params: Option<(Pos, Option<String>)>,
pub edit_pos: Option<Pos>,
Expand All @@ -50,6 +54,7 @@
/// 必须有#[id],否则会导致lru cache失效
#[salsa::tracked]
pub struct FileCompileInput {
/// file represents the entry file path to parse
#[return_ref]
pub file: String,
#[return_ref]
Expand All @@ -61,6 +66,7 @@
#[salsa::tracked]
impl FileCompileInput {
#[salsa::tracked]
// get_file_content gets the file content from cache or reads from file with the self.file
pub fn get_file_content(self, db: &dyn Db) -> Option<SourceProgram> {
// let f = self.file(db);
// eprintln!("get_file_content {}", f);
Expand All @@ -71,11 +77,14 @@
.unwrap()
.borrow_mut()
.get_file_content(db, self.file(db));
if re.is_none() {
let f = self.file(db);
log::error!("lsp error: get_file_content failed {}", f);
match re {
None => {
let f = self.file(db);
log::error!("lsp error: get_file_content failed {}", f);

Check warning on line 83 in src/lsp/mem_docs.rs

View check run for this annotation

Codecov / codecov/patch

src/lsp/mem_docs.rs#L83

Added line #L83 was not covered by tests
None
}
_ => re,
}
re
}
#[salsa::tracked]
pub fn get_emit_params(self, db: &dyn Db) -> EmitParams {
Expand Down Expand Up @@ -138,47 +147,69 @@
.get_file_content(db, &f);
re
}

/// finalize_parser_input prepares the building environments according to the kagari.toml,
/// and generates the parser entry file.
/// it applies the entry_file as the parser entry if override_with_kagari_entry is false,
/// otherwise the entry field in kagari.toml will be applied.
#[salsa::tracked]
pub fn get_file_params(self, db: &dyn Db, f: String, entry: bool) -> Option<FileCompileInput> {
let f = crate::utils::canonicalize(f);
if f.is_err() {
log::debug!("lsp error: {}", f.err().unwrap());
return None;
pub fn finalize_parser_input(
self,
db: &dyn Db,
entry_file: String,
override_with_kagari_entry: bool,
) -> Option<FileCompileInput> {
let mut final_entry_file: String;
let re_entry_file = crate::utils::canonicalize(entry_file);
match re_entry_file {
Err(e) => {
log::debug!("lsp error: {}", e);
return None;
}
Ok(f) => {
final_entry_file = f.to_string_lossy().to_string();
}
}
let mut file = f.unwrap().to_string_lossy().to_string();
let path = search_config_file(file.clone());
if path.is_err() {
log::debug!("lsp error: {}", path.err().unwrap());

let re_kagari_path = search_config_file(final_entry_file.clone());
if re_kagari_path.is_err() {
log::debug!("lsp error: {}", re_kagari_path.err().unwrap());
return None;
}
let path = path.unwrap();
let buf = crate::utils::canonicalize(PathBuf::from(path.clone())).unwrap();
let parant = buf.parent().unwrap();
let re = get_config(

let kagari_path = re_kagari_path.unwrap();
let re_config = prepare_build_envs(
db,
self.docs(db)
.lock()
.unwrap()
.borrow_mut()
.get_file_content(db, &path)
.get_file_content(db, &kagari_path)
.unwrap(),
);
if re.is_err() {
log::debug!("lsp error: {}", re.err().unwrap());
return None;
}
let config = re.unwrap();
if entry {
file = config.entry.clone();

match re_config {
Err(info) => {
log::debug!("lsp error: {}", info);

Check warning on line 193 in src/lsp/mem_docs.rs

View check run for this annotation

Codecov / codecov/patch

src/lsp/mem_docs.rs#L193

Added line #L193 was not covered by tests
None
}
Ok(config) => {
// use entry path inside kagari.toml instead of the input file
if override_with_kagari_entry {
final_entry_file = config.entry.clone();
}
let buf = crate::utils::canonicalize(PathBuf::from(kagari_path.clone())).unwrap();
let parant = buf.parent().unwrap();
Some(FileCompileInput::new(
db,
final_entry_file,
parant.to_str().unwrap().to_string(),
self,
config,
self.op(db).optimization,
))
}
}
Some(FileCompileInput::new(
db,
file,
parant.to_str().unwrap().to_string(),
self,
config,
self.op(db).optimization,
))
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/nomparser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub enum ComplexOp {
Field(Option<Box<VarNode>>),
}

/// SourceProgram represents a single file with its contents and the absulate path
#[salsa::input]
pub struct SourceProgram {
#[return_ref]
Expand All @@ -94,12 +95,12 @@ pub struct SourceProgram {
pub fn parse(db: &dyn Db, source: SourceProgram) -> Result<ProgramNodeWrapper, String> {
let text = source.text(db);
let re = program(Span::new_extra(text, false));
if let Err(e) = re {
return Err(format!("{:?}", e));
match re {
Err(e) => Err(format!("{:?}", e)),
Ok((_, node)) => {
log::info!("parse {:?}", source.path(db));
Ok(ProgramNodeWrapper::new(db, node))
}
}
let (_, node) = re.unwrap();
log::info!("parse {:?}", source.path(db));

Ok(ProgramNodeWrapper::new(db, node))
}
// ANCHOR_END: parse
Loading
Loading