Skip to content

Commit

Permalink
Merge 577b3af into 0687a5a
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuschen authored Dec 12, 2023
2 parents 0687a5a + 577b3af commit 2afafcf
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 91 deletions.
67 changes: 39 additions & 28 deletions src/ast/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ pub fn pl_link(llvmmod: Module, oxbjs: Vec<PathBuf>, out: String, op: Options) {
}
}

/// 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 = get_config_path(docs.file(db).to_string());
Expand All @@ -216,15 +218,18 @@ pub fn compile_dry(db: &dyn Db, docs: MemDocsInput) -> Option<ModWrapper> {
return None;
}

let input = docs.get_file_params(db, docs.file(db).clone(), true);
input?;
let input = input.unwrap();
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, input);
// calculate find references results
let re = compile_dry_file(db, parser_entry);

// calculate find references results for lsp
if docs.action(db) != ActionType::FindReferences {
return re;
}

if let Some(res) = db.get_ref_str() {
if let Some(plmod) = re {
plmod
Expand All @@ -237,33 +242,39 @@ pub fn compile_dry(db: &dyn Db, docs: MemDocsInput) -> Option<ModWrapper> {
}

#[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)
);
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);
return 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");
return 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 @@ use crate::{
range::Pos,
},
nomparser::SourceProgram,
utils::read_config::{get_config, get_config_path, Config},
utils::read_config::{get_config_path, prepare_build_envs, Config},
Db,
};

Expand All @@ -39,9 +39,13 @@ pub struct EmitParams {
#[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 @@ pub struct MemDocsInput {
/// 必须有#[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 @@ pub struct FileCompileInput {
#[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 @@ impl FileCompileInput {
.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);
None
}
_ => re,
}
re
}
#[salsa::tracked]
pub fn get_emit_params(self, db: &dyn Db) -> EmitParams {
Expand Down Expand Up @@ -138,47 +147,69 @@ impl MemDocsInput {
.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 = get_config_path(file.clone());
if path.is_err() {
log::debug!("lsp error: {}", path.err().unwrap());

let re_kagari_path = get_config_path(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);
return 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
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ fn main() {
// You can check the value provided by positional arguments, or option arguments
if let Some(name) = cli.name.as_deref() {
logger.timestamp(stderrlog::Timestamp::Off).init().unwrap();
let db = Database::default();
let filepath = Path::new(name);
let abs = crate::utils::canonicalize(filepath).unwrap();

let op = compiler::Options {
genir: cli.genir,
printast: cli.printast,
Expand All @@ -166,6 +164,10 @@ fn main() {
} else {
ActionType::Compile
};

let db = Database::default();
let filepath = Path::new(name);
let abs = crate::utils::canonicalize(filepath).unwrap();
let mem = MemDocsInput::new(
&db,
Arc::new(Mutex::new(RefCell::new(mem_docs::MemDocs::default()))),
Expand Down
15 changes: 9 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,14 @@ 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) => {
return Err(format!("{:?}", e));
}
Ok((_, node)) => {
log::info!("parse {:?}", source.path(db));
return Ok(ProgramNodeWrapper::new(db, node));
}
}
let (_, node) = re.unwrap();
log::info!("parse {:?}", source.path(db));

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

0 comments on commit 2afafcf

Please sign in to comment.