@@ -3,11 +3,16 @@ pub enum JSError {
33 #[ error( "Tokenization failed" ) ]
44 TokenizationError ,
55
6- #[ error( "Parsing failed" ) ]
7- ParseError ,
6+ #[ error( "Parsing failed at {method} {file}:{line} " ) ]
7+ ParseError { file : String , line : usize , method : String } ,
88
9- #[ error( "Evaluation failed: {message}" ) ]
10- EvaluationError { message : String } ,
9+ #[ error( "Evaluation failed at {method} {file}:{line}: {message}" ) ]
10+ EvaluationError {
11+ message : String ,
12+ file : String ,
13+ line : usize ,
14+ method : String ,
15+ } ,
1116
1217 #[ error( "Infinite loop detected (executed {iterations} iterations)" ) ]
1318 InfiniteLoopError { iterations : usize } ,
@@ -39,3 +44,46 @@ impl From<JSError> for std::io::Error {
3944 }
4045 }
4146}
47+
48+ // Macro that constructs a ParseError using the compile-time caller
49+ // location. Using a macro (rather than a function) ensures `file!()` and
50+ // `line!()` expand to the site where the macro is invoked.
51+ #[ macro_export]
52+ macro_rules! parse_error_here {
53+ ( ) => {
54+ $crate:: JSError :: ParseError {
55+ file: file!( ) . to_string( ) ,
56+ line: line!( ) as usize ,
57+ method: $crate:: function_name!( ) . to_string( ) ,
58+ }
59+ } ;
60+ }
61+
62+ // Macro that constructs an EvaluationError using the compile-time caller
63+ // location and the provided message. Using a macro (rather than a
64+ // function) ensures `file!()` and `line!()` expand to the site where the
65+ // macro is invoked.
66+ #[ macro_export]
67+ macro_rules! eval_error_here {
68+ ( $msg: expr) => {
69+ $crate:: JSError :: EvaluationError {
70+ message: $msg. to_string( ) ,
71+ file: file!( ) . to_string( ) ,
72+ line: line!( ) as usize ,
73+ method: $crate:: function_name!( ) . to_string( ) ,
74+ }
75+ } ;
76+ }
77+
78+ #[ macro_export]
79+ macro_rules! function_name {
80+ ( ) => { {
81+ fn f( ) { }
82+ fn type_name_of<T >( _: T ) -> & ' static str {
83+ std:: any:: type_name:: <T >( )
84+ }
85+ let name = type_name_of( f) ;
86+ // remove the trailing "::f"
87+ & name[ ..name. len( ) - 3 ]
88+ } } ;
89+ }
0 commit comments