Browse Source

[parser] refactor parsing result handling

Simon Krajewski 5 years ago
parent
commit
0c875f7547

+ 4 - 4
src/compiler/server.ml

@@ -142,13 +142,13 @@ let parse_file cs com file p =
 			try
 				let cfile = cc#find_file ffile in
 				if cfile.c_time <> ftime then raise Not_found;
-				Parser.ParseSuccess(cfile.c_package,cfile.c_decls)
+				Parser.ParseSuccess((cfile.c_package,cfile.c_decls),false,cfile.c_pdi)
 			with Not_found ->
 				let parse_result = TypeloadParse.parse_file com file p in
 				let info,is_unusual = match parse_result with
 					| ParseError(_,_,_) -> "not cached, has parse error",true
-					| ParseDisplayFile _ -> "not cached, is display file",true
-					| ParseSuccess data ->
+					| ParseSuccess(_,true,_) -> "not cached, is display file",true
+					| ParseSuccess(data,_,pdi) ->
 						begin try
 							(* We assume that when not in display mode it's okay to cache stuff that has #if display
 							checks. The reasoning is that non-display mode has more information than display mode. *)
@@ -156,7 +156,7 @@ let parse_file cs com file p =
 							let ident = Hashtbl.find Parser.special_identifier_files ffile in
 							Printf.sprintf "not cached, using \"%s\" define" ident,true
 						with Not_found ->
-							cc#cache_file ffile ftime data;
+							cc#cache_file ffile ftime data pdi;
 							"cached",false
 						end
 				in

+ 3 - 2
src/context/compilationServer.ml

@@ -9,6 +9,7 @@ type cached_file = {
 	c_package : string list;
 	c_decls : type_decl list;
 	mutable c_module_name : string option;
+	mutable c_pdi : Parser.parser_display_information;
 }
 
 type cached_directory = {
@@ -33,8 +34,8 @@ class context_cache (index : int) = object(self)
 	method find_file key =
 		Hashtbl.find files key
 
-	method cache_file key time data =
-		Hashtbl.replace files key { c_time = time; c_package = fst data; c_decls = snd data; c_module_name = None }
+	method cache_file key time data pdi =
+		Hashtbl.replace files key { c_time = time; c_package = fst data; c_decls = snd data; c_module_name = None; c_pdi = pdi }
 
 	method remove_file key =
 		if Hashtbl.mem files key then begin

+ 1 - 1
src/macro/eval/evalDebugMisc.ml

@@ -81,7 +81,7 @@ exception Parse_expr_error of string
 let parse_expr ctx s p =
 	let error s = raise (Parse_expr_error s) in
 	match ParserEntry.parse_expr_string (ctx.curapi.get_com()).Common.defines s p error true with
-	| ParseSuccess data | ParseDisplayFile(data,_) -> data
+	| ParseSuccess(data,_,_) -> data
 	| ParseError(_,(msg,_),_) -> error (Parser.error_msg msg)
 
 (* Vars *)

+ 1 - 3
src/syntax/parser.ml

@@ -77,10 +77,8 @@ type parser_display_information = {
 }
 
 type 'a parse_result =
-	(* Parsed display file. There can be errors. *)
-	| ParseDisplayFile of 'a * parser_display_information
 	(* Parsed non-display-file without errors. *)
-	| ParseSuccess of 'a
+	| ParseSuccess of 'a * bool * parser_display_information
 	(* Parsed non-display file with errors *)
 	| ParseError of 'a * parse_error * parse_error list
 

+ 4 - 4
src/syntax/parserEntry.ml

@@ -385,10 +385,11 @@ let parse ctx code file =
 		let was_display_file = !in_display_file in
 		restore();
 		Lexer.restore old;
+		let pdi = {pd_errors = List.rev !syntax_errors;pd_dead_blocks = dbc#get_dead_blocks;pd_conditions = conds#get_conditions} in
 		if was_display_file then
-			ParseDisplayFile(l,{pd_errors = List.rev !syntax_errors;pd_dead_blocks = dbc#get_dead_blocks;pd_conditions = conds#get_conditions})
+			ParseSuccess(l,true,pdi)
 		else begin match List.rev !syntax_errors with
-			| [] -> ParseSuccess l
+			| [] -> ParseSuccess(l,false,pdi)
 			| error :: errors -> ParseError(l,error,errors)
 		end
 	with
@@ -447,6 +448,5 @@ let parse_expr_string com s p error inl =
 		| _ -> raise Exit
 	in
 	match parse_string com (head ^ s ^ ";}") p error inl with
-	| ParseSuccess data -> ParseSuccess(extract_expr data)
+	| ParseSuccess(data,is_display_file,pdi) -> ParseSuccess(extract_expr data,is_display_file,pdi)
 	| ParseError(data,error,errors) -> ParseError(extract_expr data,error,errors)
-	| ParseDisplayFile(data,pdi) -> ParseDisplayFile(extract_expr data,pdi)

+ 5 - 9
src/typing/macroContext.ml

@@ -128,9 +128,8 @@ let make_macro_api ctx p =
 		typing_timer ctx false (fun() ->
 			try
 				begin match ParserEntry.parse_expr_string ctx.com.defines s p error inl with
-					| ParseSuccess data -> data
-					| ParseDisplayFile(data,_) when inl -> data (* ignore errors when inline-parsing in display file *)
-					| ParseDisplayFile _ -> assert false (* cannot happen because ParserEntry.parse_string sets `display_position := null_pos;` *)
+					| ParseSuccess(data,true,_) when inl -> data (* ignore errors when inline-parsing in display file *)
+					| ParseSuccess(data,false,_) -> data
 					| ParseError _ -> raise MacroApi.Invalid_expr
 				end
 			with Exit ->
@@ -139,8 +138,7 @@ let make_macro_api ctx p =
 	let parse_metadata s p =
 		try
 			match ParserEntry.parse_string ctx.com.defines (s ^ " typedef T = T") null_pos error false with
-			| ParseSuccess(_,[ETypedef t,_]) -> t.d_meta
-			| ParseDisplayFile _ -> assert false (* cannot happen because null_pos is used *)
+			| ParseSuccess((_,[ETypedef t,_]),_,_) -> t.d_meta
 			| ParseError(_,_,_) -> error "Malformed metadata string" p
 			| _ -> assert false
 		with _ ->
@@ -234,8 +232,7 @@ let make_macro_api ctx p =
 			typing_timer ctx false (fun() ->
 				let v = (match v with None -> None | Some s ->
 					match ParserEntry.parse_string ctx.com.defines ("typedef T = " ^ s) null_pos error false with
-					| ParseSuccess(_,[ETypedef { d_data = ct },_]) -> Some ct
-					| ParseDisplayFile _ -> assert false (* cannot happen because null_pos is used *)
+					| ParseSuccess((_,[ETypedef { d_data = ct },_]),_,_) -> Some ct
 					| ParseError(_,(msg,p),_) -> Parser.error msg p (* p is null_pos, but we don't have anything else here... *)
 					| _ -> assert false
 				) in
@@ -742,9 +739,8 @@ let call_init_macro ctx e =
 	let e = try
 		if String.get e (String.length e - 1) = ';' then error "Unexpected ;" p;
 		begin match ParserEntry.parse_expr_string ctx.com.defines e p error false with
-		| ParseSuccess data -> data
+		| ParseSuccess(data,_,_) -> data
 		| ParseError(_,(msg,p),_) -> (Parser.error msg p)
-		| ParseDisplayFile _ -> assert false (* cannot happen *)
 		end
 	with err ->
 		display_error ctx ("Could not parse `" ^ e ^ "`") p;

+ 1 - 2
src/typing/typeloadModule.ml

@@ -915,8 +915,7 @@ let handle_import_hx ctx m decls p =
 		with Not_found ->
 			if Sys.file_exists path then begin
 				let _,r = match !TypeloadParse.parse_hook ctx.com path p with
-					| ParseSuccess data -> data
-					| ParseDisplayFile(data,_) -> data
+					| ParseSuccess(data,_,_) -> data
 					| ParseError(_,(msg,p),_) -> Parser.error msg p
 				in
 				List.iter (fun (d,p) -> match d with EImport _ | EUsing _ -> () | _ -> error "Only import and using is allowed in import.hx files" p) r;

+ 9 - 8
src/typing/typeloadParse.ml

@@ -47,7 +47,7 @@ let parse_file_from_lexbuf com file p lexbuf =
 	in
 	begin match !Parser.display_mode,parse_result with
 		| DMModuleSymbols (Some ""),_ -> ()
-		| DMModuleSymbols filter,(ParseSuccess data | ParseDisplayFile(data,_)) when filter = None && DisplayPosition.display_position#is_in_file file ->
+		| DMModuleSymbols filter,(ParseSuccess(data,_,_)) when filter = None && DisplayPosition.display_position#is_in_file file ->
 			let ds = DocumentSymbols.collect_module_symbols (filter = None) data in
 			DisplayException.raise_module_symbols (DocumentSymbols.Printer.print_module_symbols com [file,ds] filter);
 		| _ ->
@@ -126,7 +126,7 @@ let resolve_module_file com m remap p =
 			| [] -> []
 		in
 		let meta =  match parse_result with
-			| ParseSuccess(_,decls) | ParseDisplayFile((_,decls),_) -> loop decls
+			| ParseSuccess((_,decls),_,_) -> loop decls
 			| ParseError _ -> []
 		in
 		if not (Meta.has Meta.NoPackageRestrict meta) then begin
@@ -284,13 +284,14 @@ let handle_parser_result com file p result =
 			| EPCollect -> add_diagnostics_message com msg p DKParserError Error
 	in
 	match result with
-		| ParseSuccess data -> data
-		| ParseDisplayFile(data,pdi) ->
-			begin match pdi.pd_errors with
-			| (msg,p) :: _ -> handle_parser_error msg p
-			| [] -> ()
+		| ParseSuccess(data,is_display_file,pdi) ->
+			if is_display_file then begin
+				begin match pdi.pd_errors with
+				| (msg,p) :: _ -> handle_parser_error msg p
+				| [] -> ()
+				end;
+				PdiHandler.handle_pdi com file pdi;
 			end;
-			PdiHandler.handle_pdi com file pdi;
 			data
 		| ParseError(data,(msg,p),_) ->
 			handle_parser_error msg p;

+ 1 - 1
src/typing/typer.ml

@@ -1631,7 +1631,7 @@ and format_string ctx s p =
 				let ep = { p with pmin = !pmin + pos + 2; pmax = !pmin + send + 1 } in
 				try
 					begin match ParserEntry.parse_expr_string ctx.com.defines scode ep error true with
-						| ParseSuccess data | ParseDisplayFile(data,_) -> data
+						| ParseSuccess(data,_,_) -> data
 						| ParseError(_,(msg,p),_) -> error (Parser.error_msg msg) p
 					end
 				with Exit ->