فهرست منبع

Update macro API to handle sub errors

Rudy Ges 2 ماه پیش
والد
کامیت
cf0904cf94
4فایلهای تغییر یافته به همراه46 افزوده شده و 17 حذف شده
  1. 3 2
      src/context/common.ml
  2. 10 0
      src/core/error.ml
  3. 21 9
      src/macro/macroApi.ml
  4. 12 6
      std/haxe/macro/Context.hx

+ 3 - 2
src/context/common.ml

@@ -18,6 +18,7 @@
  *)
 open Ast
 open Type
+open Error
 open Globals
 open Lookup
 open Define
@@ -1092,8 +1093,8 @@ let display_error_ext com err =
 	end else
 		com.error_ext err
 
-let display_error com msg p =
-	display_error_ext com (Error.make_error (Custom msg) p)
+let display_error com ?(sub:macro_error list = []) msg pos =
+	display_error_ext com (convert_error {msg; pos; sub})
 
 let adapt_defines_to_macro_context defines =
 	let to_remove = "java" :: List.map Globals.platform_name Globals.platforms in

+ 10 - 0
src/core/error.ml

@@ -33,6 +33,12 @@ type error = {
 	err_from_macro : bool;
 }
 
+type macro_error = {
+	msg : string;
+	pos : pos;
+	sub : macro_error list;
+}
+
 let make_error ?(from_macro = false) ?(sub = []) msg p = {
 	err_message = msg;
 	err_pos = p;
@@ -40,6 +46,10 @@ let make_error ?(from_macro = false) ?(sub = []) msg p = {
 	err_sub = sub;
 }
 
+let rec convert_error (err:macro_error) =
+	let sub = List.map convert_error err.sub in
+	make_error ~sub (Custom err.msg) err.pos
+
 let recurse_error cb err =
 	let rec loop depth err =
 		cb depth err;

+ 21 - 9
src/macro/macroApi.ml

@@ -2,6 +2,7 @@ open Ast
 open DisplayTypes.DisplayMode
 open Type
 open Common
+open Error
 open PlatformConfig
 open DefineList
 open MetaList
@@ -65,7 +66,7 @@ type 'value compiler_api = {
 	decode_type : 'value -> t;
 	info : ?depth:int -> string -> pos -> unit;
 	warning : ?depth:int -> Warning.warning -> string -> pos -> unit;
-	display_error : string -> pos -> unit;
+	display_error : ?sub:macro_error list -> string -> pos -> unit;
 	with_imports : 'a . import list -> placed_name list list -> (unit -> 'a) -> 'a;
 	with_options : 'a . compiler_options -> (unit -> 'a) -> 'a;
 	exc_string : 'a . string -> 'a;
@@ -73,7 +74,6 @@ type 'value compiler_api = {
 	set_hxb_writer_config : 'value -> unit;
 }
 
-
 type enum_type =
 	| IExpr
 	| IEFieldKind
@@ -747,6 +747,15 @@ let decode_placed_name vp v =
 let decode_opt_array f v =
 	if v = vnull then [] else List.map f (decode_array v)
 
+let decode_sub_errors sub =
+	let rec decode_sub o =
+		let msg = decode_string (field o "msg") in
+		let pos = decode_pos (field o "pos") in
+		let sub = decode_opt_array decode_sub (field o "sub") in
+		{msg; pos; sub}
+	in
+	decode_opt_array decode_sub sub
+
 (* Ast.placed_type_path *)
 let rec decode_ast_path t =
 	let pack = List.map decode_string (decode_array (field t "pack"))
@@ -1800,21 +1809,24 @@ let macro_api ccom get_api =
 		"init_macros_done", vfun0 (fun () ->
 			vbool ((get_api()).init_macros_done ())
 		);
-		"error", vfun3 (fun msg p depth ->
+		"error", vfun3 (fun msg p sub ->
 			let msg = decode_string msg in
 			let p = decode_pos p in
-			(get_api()).display_error msg p;
+			let sub = decode_sub_errors sub in
+			(get_api()).display_error ~sub msg p;
 			raise Abort
 		);
-		"fatal_error", vfun3 (fun msg p depth ->
+		"fatal_error", vfun3 (fun msg p sub ->
 			let msg = decode_string msg in
-			let p = decode_pos p in
-			raise (Error.Fatal_error (Error.make_error (Custom msg) p))
+			let pos = decode_pos p in
+			let sub = decode_sub_errors sub in
+			raise (Error.Fatal_error (Error.convert_error {msg; pos; sub}))
 		);
-		"report_error", vfun3 (fun msg p depth ->
+		"report_error", vfun3 (fun msg p sub ->
 			let msg = decode_string msg in
 			let p = decode_pos p in
-			(get_api()).display_error msg p;
+			let sub = decode_sub_errors sub in
+			(get_api()).display_error ~sub msg p;
 			vnull
 		);
 		"warning", vfun3 (fun msg p depth ->

+ 12 - 6
std/haxe/macro/Context.hx

@@ -30,6 +30,12 @@ enum Message {
 	Warning(msg:String, pos:Position);
 }
 
+typedef MacroError = {
+	msg:String,
+	pos:Position,
+	?sub:Array<MacroError>
+}
+
 /**
 	Context provides an API for macro programming.
 
@@ -47,24 +53,24 @@ class Context {
 		Displays a compilation error `msg` at the given `Position` `pos`
 		and aborts the current macro call.
 	**/
-	public static function error(msg:String, pos:Position, ?depth:Int = 0):Dynamic {
-		return load("error", 2)(msg, pos, depth);
+	public static function error(msg:String, pos:Position, ?sub:Array<MacroError>):Dynamic {
+		return load("error", 2)(msg, pos, sub);
 	}
 
 	/**
 		Displays a compilation error `msg` at the given `Position` `pos`
 		and aborts the compilation.
 	**/
-	public static function fatalError(msg:String, pos:Position, ?depth:Int = 0):Dynamic {
-		return load("fatal_error", 2)(msg, pos, depth);
+	public static function fatalError(msg:String, pos:Position, ?sub:Array<MacroError>):Dynamic {
+		return load("fatal_error", 2)(msg, pos, sub);
 	}
 
 	/**
 		Displays a compilation error `msg` at the given `Position` `pos`
 		without aborting the current macro call.
 	**/
-	public static function reportError(msg:String, pos:Position, ?depth:Int = 0):Void {
-		load("report_error", 2)(msg, pos, depth);
+	public static function reportError(msg:String, pos:Position, ?sub:Array<MacroError>):Void {
+		load("report_error", 2)(msg, pos, sub);
 	}
 
 	/**