@@ -1656,7 +1656,12 @@ impl Compiler {
1656
1656
} )
1657
1657
}
1658
1658
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
+ ) {
1660
1665
for item in program {
1661
1666
if item. as_rule ( ) == Rule :: function_definition {
1662
1667
let mut inner = item. into_inner ( ) ;
@@ -1676,10 +1681,14 @@ impl Compiler {
1676
1681
} ,
1677
1682
) ;
1678
1683
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) ;
1683
1692
}
1684
1693
}
1685
1694
}
@@ -1740,12 +1749,6 @@ fn gather_errors(first_error: PestError, source: &str, errors: &mut Vec<PestErro
1740
1749
}
1741
1750
}
1742
1751
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
-
1749
1752
pub struct SourceFile {
1750
1753
pub filename : String ,
1751
1754
pub contents : String ,
@@ -1777,8 +1780,8 @@ pub fn compile_program(sources: &[SourceFile]) -> Result<Program, CompilerErrors
1777
1780
}
1778
1781
1779
1782
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 ) ;
1782
1785
}
1783
1786
1784
1787
let mut begin_actions = Vec :: new ( ) ;
@@ -1791,14 +1794,14 @@ pub fn compile_program(sources: &[SourceFile]) -> Result<Program, CompilerErrors
1791
1794
Rule :: begin_action | Rule :: end_action => {
1792
1795
let is_begin_action = item. as_rule ( ) == Rule :: begin_action;
1793
1796
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 ( ) ,
1801
1801
) ;
1802
+ if let Err ( err) = result {
1803
+ errors. push ( improve_error ( err, & filename) ) ;
1804
+ }
1802
1805
if is_begin_action {
1803
1806
begin_actions. push ( instructions. into_action ( filename. clone ( ) ) ) ;
1804
1807
} else {
@@ -1807,12 +1810,12 @@ pub fn compile_program(sources: &[SourceFile]) -> Result<Program, CompilerErrors
1807
1810
}
1808
1811
Rule :: rule => match compiler. compile_rule ( item, filename. clone ( ) ) {
1809
1812
Ok ( rule) => rules. push ( rule) ,
1810
- Err ( err) => errors. push ( err) ,
1813
+ Err ( err) => errors. push ( improve_error ( err, & filename ) ) ,
1811
1814
} ,
1812
1815
Rule :: function_definition => {
1813
1816
match compiler. compile_function_definition ( item, filename. clone ( ) ) {
1814
1817
Ok ( function) => functions. push ( function) ,
1815
- Err ( err) => errors. push ( err) ,
1818
+ Err ( err) => errors. push ( improve_error ( err, & filename ) ) ,
1816
1819
}
1817
1820
}
1818
1821
Rule :: EOI => { }
0 commit comments