@@ -89,22 +89,33 @@ impl TemplateProgram {
8989 source : CanonSourceFile ,
9090 dependency_map : & DependencyMap ,
9191 jet_hinter : Box < dyn ast:: JetHinter > ,
92- ) -> Result < Self , String > {
92+ ) -> Result < Self , ErrorCollector > {
9393 let mut error_handler = ErrorCollector :: new ( ) ;
9494
95- let ( resolved_program, source_map) =
96- Self :: dependency_helper ( source. clone ( ) , dependency_map, & mut error_handler) ?;
97- let resolved_program = resolved_program. ok_or_else ( || error_handler. to_string ( ) ) ?;
98-
99- let ast_program = ast:: Program :: analyze ( & resolved_program, jet_hinter. clone_box ( ) )
100- . with_source ( source. clone ( ) ) ?;
101- Ok ( Self {
102- simfony : ast_program,
103- file : source. content ( ) ,
104- jet_hinter,
105- source_map,
106- resolved_program,
107- } )
95+ let build_program = || -> Result < Self , ( ) > {
96+ let ( resolved_program, source_map) =
97+ Self :: dependency_helper ( source. clone ( ) , dependency_map, & mut error_handler)
98+ . map_err ( |_| ( ) ) ?;
99+
100+ let resolved_program = resolved_program. ok_or ( ( ) ) ?;
101+
102+ let ast_program = ast:: Program :: analyze ( & resolved_program, jet_hinter. clone_box ( ) )
103+ . with_source ( source. clone ( ) )
104+ . map_err ( |e| error_handler. push ( e) ) ?;
105+
106+ Ok ( Self {
107+ simfony : ast_program,
108+ file : source. content ( ) ,
109+ jet_hinter,
110+ source_map,
111+ resolved_program,
112+ } )
113+ } ;
114+
115+ match build_program ( ) {
116+ Ok ( instance) => Ok ( instance) ,
117+ Err ( ( ) ) => Err ( error_handler) ,
118+ }
108119 }
109120
110121 /// Parse the template of a SimplicityHL program.
@@ -115,15 +126,21 @@ impl TemplateProgram {
115126 pub fn new < Str : Into < Arc < str > > > (
116127 s : Str ,
117128 jet_hinter : Box < dyn ast:: JetHinter > ,
118- ) -> Result < Self , String > {
129+ ) -> Result < Self , ErrorCollector > {
119130 let file = s. into ( ) ;
120131 let source = SourceFile :: anonymous ( file. clone ( ) ) ;
121132 let mut error_handler = ErrorCollector :: new ( ) ;
133+
122134 let parse_program = parse:: Program :: parse_from_str_with_errors ( source, & mut error_handler) ;
123135
124136 if let Some ( program) = parse_program {
125- let ast_program = ast:: Program :: analyze ( & program, jet_hinter. clone_box ( ) )
126- . with_content ( Arc :: clone ( & file) ) ?;
137+ let Ok ( ast_program) = ast:: Program :: analyze ( & program, jet_hinter. clone_box ( ) )
138+ . with_content ( Arc :: clone ( & file) )
139+ . map_err ( |e| error_handler. push ( e) )
140+ else {
141+ Err ( error_handler) ?
142+ } ;
143+
127144 Ok ( Self {
128145 simfony : ast_program,
129146 file,
@@ -132,7 +149,7 @@ impl TemplateProgram {
132149 resolved_program : program,
133150 } )
134151 } else {
135- Err ( error_handler. to_string ( ) ) ?
152+ Err ( error_handler) ?
136153 }
137154 }
138155
@@ -143,13 +160,20 @@ impl TemplateProgram {
143160 ) -> Result < ( Option < parse:: Program > , SourceMap ) , String > {
144161 let program = parse:: Program :: parse_from_str_with_errors ( source. clone ( ) , handler)
145162 . ok_or_else ( || handler. to_string ( ) ) ?;
163+ // TODO: we should remove this errors push after refactoring errors
146164 let graph =
147- DependencyGraph :: new ( source, Arc :: from ( dependency_map. clone ( ) ) , & program, handler) ?
165+ DependencyGraph :: new ( source, Arc :: from ( dependency_map. clone ( ) ) , & program, handler)
166+ . map_err ( |e| {
167+ handler. push ( error:: RichError :: parsing_error ( & e) ) ;
168+ handler. to_string ( )
169+ } ) ?
148170 . ok_or_else ( || handler. to_string ( ) ) ?;
149171
150172 let program = graph. linearize_and_build ( handler) ;
151173
152- program. map ( |opt| ( opt, graph. source_map ( ) . clone ( ) ) )
174+ program
175+ . map ( |opt| ( opt, graph. source_map ( ) . clone ( ) ) )
176+ . inspect_err ( |e| handler. push ( error:: RichError :: parsing_error ( e) ) )
153177 }
154178
155179 /// Access the parameters of the program.
@@ -234,6 +258,7 @@ impl CompiledProgram {
234258 jet_hinter : Box < dyn ast:: JetHinter > ,
235259 ) -> Result < Self , String > {
236260 TemplateProgram :: new_with_dep ( source, dependency_map, jet_hinter. clone_box ( ) )
261+ . map_err ( |e| e. to_string ( ) )
237262 . and_then ( |template| template. instantiate ( arguments, include_debug_symbols) )
238263 }
239264
@@ -250,6 +275,7 @@ impl CompiledProgram {
250275 jet_hinter : Box < dyn ast:: JetHinter > ,
251276 ) -> Result < Self , String > {
252277 TemplateProgram :: new ( s, jet_hinter. clone_box ( ) )
278+ . map_err ( |e| e. to_string ( ) )
253279 . and_then ( |template| template. instantiate ( arguments, include_debug_symbols) )
254280 }
255281
@@ -1414,7 +1440,7 @@ mod error_tests {
14141440 let dependency_source = canon ( & bad_path) . as_path ( ) . display ( ) . to_string ( ) ;
14151441
14161442 assert ! (
1417- err. contains( & dependency_source) ,
1443+ err. to_string ( ) . contains( & dependency_source) ,
14181444 "expected diagnostic to point at dependency source {dependency_source}, got:\n {err}"
14191445 ) ;
14201446 }
@@ -1462,7 +1488,7 @@ mod error_tests {
14621488 . expect_err ( "missing imported module should fail" ) ;
14631489
14641490 assert ! (
1465- err. contains( "missing.simf" ) ,
1491+ err. to_string ( ) . contains( "missing.simf" ) ,
14661492 "diagnostic should mention the missing module path, got:\n {err}"
14671493 ) ;
14681494 }
0 commit comments