Browse Source

[macro] add Context.reportError

closes #10096
Simon Krajewski 4 years ago
parent
commit
cb258e22a7
3 changed files with 21 additions and 5 deletions
  1. 7 0
      src/macro/macroApi.ml
  2. 1 0
      src/typing/macroContext.ml
  3. 13 5
      std/haxe/macro/Context.hx

+ 7 - 0
src/macro/macroApi.ml

@@ -50,6 +50,7 @@ type 'value compiler_api = {
 	encode_ctype : Ast.type_hint -> 'value;
 	encode_ctype : Ast.type_hint -> 'value;
 	decode_type : 'value -> t;
 	decode_type : 'value -> t;
 	flush_context : (unit -> t) -> t;
 	flush_context : (unit -> t) -> t;
+	display_error : (string -> pos -> unit);
 }
 }
 
 
 
 
@@ -1572,6 +1573,12 @@ let macro_api ccom get_api =
 			let p = decode_pos p in
 			let p = decode_pos p in
 			raise (Error.Fatal_error (msg,p))
 			raise (Error.Fatal_error (msg,p))
 		);
 		);
+		"report_error", vfun2 (fun msg p ->
+			let msg = decode_string msg in
+			let p = decode_pos p in
+			(get_api()).display_error msg p;
+			vnull
+		);
 		"warning", vfun2 (fun msg p ->
 		"warning", vfun2 (fun msg p ->
 			let msg = decode_string msg in
 			let msg = decode_string msg in
 			let p = decode_pos p in
 			let p = decode_pos p in

+ 1 - 0
src/typing/macroContext.ml

@@ -394,6 +394,7 @@ let make_macro_api ctx p =
 		MacroApi.encode_expr = Interp.encode_expr;
 		MacroApi.encode_expr = Interp.encode_expr;
 		MacroApi.encode_ctype = Interp.encode_ctype;
 		MacroApi.encode_ctype = Interp.encode_ctype;
 		MacroApi.decode_type = Interp.decode_type;
 		MacroApi.decode_type = Interp.decode_type;
+		MacroApi.display_error = Typecore.display_error ctx;
 	}
 	}
 
 
 let rec init_macro_interp ctx mctx mint =
 let rec init_macro_interp ctx mctx mint =

+ 13 - 5
std/haxe/macro/Context.hx

@@ -59,6 +59,14 @@ class Context {
 		return load("fatal_error", 2)(msg, pos);
 		return load("fatal_error", 2)(msg, pos);
 	}
 	}
 
 
+	/**
+		Displays a compilation error `msg` at the given `Position` `pos`
+		without aborting the current macro call.
+	**/
+	public static function reportError(msg:String, pos:Position):Void {
+		load("report_error", 2)(msg, pos);
+	}
+
 	/**
 	/**
 		Displays a compilation warning `msg` at the given `Position` `pos`.
 		Displays a compilation warning `msg` at the given `Position` `pos`.
 	**/
 	**/
@@ -76,16 +84,16 @@ class Context {
 	/**
 	/**
 		Gets a list of all current compilation info/warning messages.
 		Gets a list of all current compilation info/warning messages.
 	**/
 	**/
-	public static function getMessages() : Array<Message> {
-		return load("get_messages",0)();
+	public static function getMessages():Array<Message> {
+		return load("get_messages", 0)();
 	}
 	}
 
 
 	/**
 	/**
 		Filters all current info/warning messages. Filtered out messages will
 		Filters all current info/warning messages. Filtered out messages will
 		not be displayed by the compiler.
 		not be displayed by the compiler.
 	**/
 	**/
-	public static function filterMessages( predicate : Message -> Bool ) {
-		load("filter_messages",1)(predicate);
+	public static function filterMessages(predicate:Message->Bool) {
+		load("filter_messages", 1)(predicate);
 	}
 	}
 
 
 	/**
 	/**
@@ -606,7 +614,7 @@ class Context {
 		stopTimer();
 		stopTimer();
 		```
 		```
 	**/
 	**/
-	public static function timer(id:String):()->Void {
+	public static function timer(id:String):() -> Void {
 		return load("timer", 1)(id);
 		return load("timer", 1)(id);
 	}
 	}