1
1
use std:: fmt;
2
2
3
- use rant:: { compiler:: CompilerErrorKind , runtime:: RuntimeError , Rant , RantOptions , RantValue } ;
3
+ use rant:: {
4
+ compiler:: { CompilerErrorKind , CompilerMessage } ,
5
+ runtime:: RuntimeError ,
6
+ Rant , RantOptions , RantValue ,
7
+ } ;
4
8
use wasm_bindgen:: prelude:: * ;
5
9
6
10
#[ wasm_bindgen]
@@ -18,25 +22,51 @@ fn _rant(input: &str, seed: u32) -> Result<RantValue, RantError> {
18
22
..Default :: default ( )
19
23
} ;
20
24
let mut rant = Rant :: with_options ( options) ;
21
- let program = rant. compile_quiet ( input) . map_err ( RantError :: Compiler ) ?;
22
- rant. run ( & program) . map_err ( RantError :: Runtime )
25
+ let mut msgs: Vec < CompilerMessage > = vec ! [ ] ;
26
+ let program = rant. compile ( input, & mut msgs) ;
27
+ match program {
28
+ Ok ( p) => rant. run ( & p) . map_err ( RantError :: Runtime ) ,
29
+ Err ( err) => Err ( RantError :: Compiler ( CompilerErrorWithMsgs { err, msgs } ) ) ,
30
+ }
23
31
}
24
32
25
33
#[ derive( Debug ) ]
26
34
enum RantError {
27
- Compiler ( CompilerErrorKind ) ,
35
+ Compiler ( CompilerErrorWithMsgs ) ,
28
36
Runtime ( RuntimeError ) ,
29
37
}
30
38
31
39
impl fmt:: Display for RantError {
32
40
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
33
41
match * self {
34
- RantError :: Compiler ( ref err) => write ! ( f, "Rant Compile error: {}" , err) ,
42
+ RantError :: Compiler ( ref err) => write ! ( f, "{}" , err) ,
35
43
RantError :: Runtime ( ref err) => write ! ( f, "Rant Runtime error: {}" , err) ,
36
44
}
37
45
}
38
46
}
39
47
48
+ #[ derive( Debug ) ]
49
+ struct CompilerErrorWithMsgs {
50
+ err : CompilerErrorKind ,
51
+ msgs : Vec < CompilerMessage > ,
52
+ }
53
+
54
+ impl fmt:: Display for CompilerErrorWithMsgs {
55
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
56
+ let msgs = self
57
+ . msgs
58
+ . iter ( )
59
+ . map ( |msg| format ! ( "{:#?}" , msg) )
60
+ . collect :: < Vec < _ > > ( )
61
+ . join ( "\n " ) ;
62
+ write ! (
63
+ f,
64
+ "Rant Compiler error: {}\n Compiler messages:\n {}" ,
65
+ self . err, msgs
66
+ )
67
+ }
68
+ }
69
+
40
70
#[ cfg( test) ]
41
71
pub mod tests {
42
72
use super :: _rant;
0 commit comments