Skip to content

Commit 9a6ec78

Browse files
committed
awk: improve compiler errors
1 parent 13c7463 commit 9a6ec78

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

awk/src/compiler.rs

+25-22
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,12 @@ impl Compiler {
16561656
})
16571657
}
16581658

1659-
fn declare_program_functions(&mut self, errors: &mut Vec<PestError>, program: Pairs<Rule>) {
1659+
fn declare_program_functions(
1660+
&mut self,
1661+
program: Pairs<Rule>,
1662+
filename: &str,
1663+
errors: &mut Vec<PestError>,
1664+
) {
16601665
for item in program {
16611666
if item.as_rule() == Rule::function_definition {
16621667
let mut inner = item.into_inner();
@@ -1676,10 +1681,14 @@ impl Compiler {
16761681
},
16771682
);
16781683
if previous_value.is_some() {
1679-
errors.push(pest_error_from_span(
1680-
name.as_span(),
1681-
format!("function '{}' is defined multiple times", name),
1682-
));
1684+
let error = improve_error(
1685+
pest_error_from_span(
1686+
name.as_span(),
1687+
format!("function '{}' is defined multiple times", name),
1688+
),
1689+
filename,
1690+
);
1691+
errors.push(error);
16831692
}
16841693
}
16851694
}
@@ -1740,12 +1749,6 @@ fn gather_errors(first_error: PestError, source: &str, errors: &mut Vec<PestErro
17401749
}
17411750
}
17421751

1743-
fn append_if_err(errors: &mut Vec<PestError>, result: Result<(), PestError>) {
1744-
if let Err(err) = result {
1745-
errors.push(err);
1746-
}
1747-
}
1748-
17491752
pub struct SourceFile {
17501753
pub filename: String,
17511754
pub contents: String,
@@ -1777,8 +1780,8 @@ pub fn compile_program(sources: &[SourceFile]) -> Result<Program, CompilerErrors
17771780
}
17781781

17791782
let mut compiler = Compiler::default();
1780-
for (_, program_iter) in &parsed_sources {
1781-
compiler.declare_program_functions(&mut errors, program_iter.clone());
1783+
for (filename, program_iter) in &parsed_sources {
1784+
compiler.declare_program_functions(program_iter.clone(), filename, &mut errors);
17821785
}
17831786

17841787
let mut begin_actions = Vec::new();
@@ -1791,14 +1794,14 @@ pub fn compile_program(sources: &[SourceFile]) -> Result<Program, CompilerErrors
17911794
Rule::begin_action | Rule::end_action => {
17921795
let is_begin_action = item.as_rule() == Rule::begin_action;
17931796
let mut instructions = Instructions::default();
1794-
append_if_err(
1795-
&mut errors,
1796-
compiler.compile_action(
1797-
first_child(item),
1798-
&mut instructions,
1799-
&HashMap::new(),
1800-
),
1797+
let result = compiler.compile_action(
1798+
first_child(item),
1799+
&mut instructions,
1800+
&HashMap::new(),
18011801
);
1802+
if let Err(err) = result {
1803+
errors.push(improve_error(err, &filename));
1804+
}
18021805
if is_begin_action {
18031806
begin_actions.push(instructions.into_action(filename.clone()));
18041807
} else {
@@ -1807,12 +1810,12 @@ pub fn compile_program(sources: &[SourceFile]) -> Result<Program, CompilerErrors
18071810
}
18081811
Rule::rule => match compiler.compile_rule(item, filename.clone()) {
18091812
Ok(rule) => rules.push(rule),
1810-
Err(err) => errors.push(err),
1813+
Err(err) => errors.push(improve_error(err, &filename)),
18111814
},
18121815
Rule::function_definition => {
18131816
match compiler.compile_function_definition(item, filename.clone()) {
18141817
Ok(function) => functions.push(function),
1815-
Err(err) => errors.push(err),
1818+
Err(err) => errors.push(improve_error(err, &filename)),
18161819
}
18171820
}
18181821
Rule::EOI => {}

0 commit comments

Comments
 (0)