Skip to content

Commit 759dd5b

Browse files
committed
feat: add err & warn num output
1 parent 3419a23 commit 759dd5b

File tree

6 files changed

+63
-98
lines changed

6 files changed

+63
-98
lines changed

src/ast/compiler.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ pub use jit::*;
5353
#[cfg(feature = "llvm")]
5454
#[salsa::tracked]
5555
pub fn compile(db: &dyn Db, docs: MemDocsInput, out: String, op: Options) {
56+
use std::process::exit;
57+
5658
IS_JIT.store(op.jit, Ordering::Relaxed);
5759

5860
#[cfg(feature = "jit")]
@@ -68,7 +70,10 @@ pub fn compile(db: &dyn Db, docs: MemDocsInput, out: String, op: Options) {
6870
op,
6971
format!("{}[{:2}/{:2}]", LOOKING_GLASS, 1, total_steps),
7072
);
71-
compile_dry(db, docs).unwrap();
73+
let _ = compile_dry(db, docs).map_err(|e| {
74+
pb.abandon_with_message(format!("{}", e.red()));
75+
exit(1);
76+
});
7277

7378
pb.finish_with_message("中间代码分析完成");
7479
let is_present_only = op.printast || op.flow;
@@ -86,11 +91,11 @@ pub fn compile(db: &dyn Db, docs: MemDocsInput, out: String, op: Options) {
8691
/// compile_dry compiles the source code of pivot-lang into the pivot-lang AST with a wrapper.
8792
/// the `dry` refers the function ends up parsing at LLVM IR or LSP analysis.
8893
#[salsa::tracked]
89-
pub fn compile_dry(db: &dyn Db, docs: MemDocsInput) -> Option<ModWrapper> {
94+
pub fn compile_dry(db: &dyn Db, docs: MemDocsInput) -> Result<ModWrapper, String> {
9095
let path = search_config_file(docs.file(db).to_string());
9196
if path.is_err() {
92-
log::error!("lsp error: {}", path.err().unwrap());
93-
return None;
97+
log::warn!("lsp error: {}", path.err().unwrap());
98+
return Err("project config file not found".to_string());
9499
}
95100

96101
let parser_entry = docs
@@ -102,7 +107,7 @@ pub fn compile_dry(db: &dyn Db, docs: MemDocsInput) -> Option<ModWrapper> {
102107

103108
// calculate find references results for lsp
104109
if docs.action(db) != ActionType::FindReferences {
105-
return re;
110+
return re.ok_or("compile failed".to_string());
106111
}
107112

108113
if let Some(res) = db.get_ref_str() {
@@ -113,7 +118,7 @@ pub fn compile_dry(db: &dyn Db, docs: MemDocsInput) -> Option<ModWrapper> {
113118
}
114119
db.set_ref_str(None);
115120
}
116-
re
121+
re.ok_or("compile failed".to_string())
117122
}
118123

119124
/// compile_dry_file parses the file inside parser_entry into AST,

src/ast/compiler/progress.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ lazy_static! {
1111
pub static ref CHECK_PROGRESS: ProgressBar = ProgressBar::hidden();
1212
}
1313

14+
pub(crate) static CHECK: Emoji<'_, '_> = Emoji("🧐 ", ":-)");
15+
1416
pub(crate) static LOOKING_GLASS: Emoji<'_, '_> = Emoji("🔍 ", ":-)");
1517

1618
pub(crate) static TRUCK: Emoji<'_, '_> = Emoji("🚚 ", ":-)");

src/ast/diag.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ impl PLDiag {
323323
DiagCode::Warn(_) => false,
324324
}
325325
}
326+
pub fn is_warn(&self) -> bool {
327+
match self.raw.code {
328+
DiagCode::Err(_) => false,
329+
DiagCode::Warn(_) => true,
330+
}
331+
}
326332
pub fn get_msg(&self) -> String {
327333
match self.raw.code {
328334
DiagCode::Err(code) => ERROR_MSG[&code].to_string(),
@@ -486,12 +492,11 @@ impl<'a> Cache<&str> for PLFileCache<'a> {
486492
}
487493
}
488494

489-
mod dot;
490-
491495
/// ensure_no_error validates the results after parsing from db and docs to ensure there is no error.
492496
/// it panic directly if there is an error.
493497
pub(crate) fn ensure_no_error(db: &dyn Db, docs: MemDocsInput) {
494498
let mut errs_num = 0;
499+
let mut warn_num = 0;
495500
let errs = compile_dry::accumulated::<Diagnostics>(db, docs);
496501
if !errs.is_empty() {
497502
for e in errs.iter() {
@@ -509,24 +514,23 @@ pub(crate) fn ensure_no_error(db: &dyn Db, docs: MemDocsInput) {
509514
);
510515
if e.is_err() {
511516
errs_num += 1
517+
} else if e.is_warn() {
518+
warn_num += 1
512519
}
513520
}
514521
}
515522
if errs_num > 0 {
516-
if errs_num == 1 {
517-
log::error!(
518-
"{}",
519-
format!("check failed: there is {} error", errs_num).bright_red()
520-
);
521-
println!("{}", dot::ERROR);
522-
exit(1);
523-
}
524-
log::error!(
525-
"{}",
526-
format!("check failed: there are {} errors", errs_num).bright_red()
523+
eprintln!(
524+
"check failed: {} error(s), {} warning(s)",
525+
errs_num.to_string().bright_red(),
526+
warn_num.to_string().yellow()
527527
);
528-
println!("{}", dot::TOOMANYERROR);
529528
exit(1);
530529
}
530+
eprintln!(
531+
" {} error(s), {} warning(s)",
532+
errs_num.to_string().bright_red(),
533+
warn_num.to_string().yellow()
534+
);
531535
}
532536
}

src/ast/diag/dot.rs

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

0 commit comments

Comments
 (0)