Browse Source

maybe like this?

Simon Krajewski 5 months ago
parent
commit
271248ffa4
2 changed files with 21 additions and 16 deletions
  1. 9 7
      src/syntax/lexer.ml
  2. 12 9
      src/syntax/parserEntry.ml

+ 9 - 7
src/syntax/lexer.ml

@@ -99,13 +99,15 @@ let newline ctx lexbuf =
 	cur.lline <- cur.lline + 1;
 	cur.llines <- (lexeme_end lexbuf,cur.lline) :: cur.llines
 
-let copy_file source dest =
-	dest.lline <- source.lline;
-	dest.lmaxline <- source.lmaxline;
-	dest.llines <- source.llines;
-	dest.lalines <- source.lalines;
-	dest.llast <- source.llast;
-	dest.llastindex <- source.llastindex
+let copy_file source = {
+	lfile = source.lfile;
+	lline = source.lline;
+	lmaxline = source.lmaxline;
+	llines = source.llines;
+	lalines = source.lalines;
+	llast = source.llast;
+	llastindex = source.llastindex;
+}
 
 let print_file file =
 	let sllines = String.concat ";" (List.map (fun (i1,i2) -> Printf.sprintf "(%i,%i)" i1 i2) file.llines) in

+ 12 - 9
src/syntax/parserEntry.ml

@@ -393,19 +393,22 @@ let parse_string entry defines s p error inlined =
 		end;
 		syntax_errors := old_syntax_errors;
 	in
-	let lctx = if inlined then begin
-		begin try
+	let lctx = if inlined then
+		(* In inline mode, we always work with a temp context. *)
+		Lexer.create_temp_ctx p.pfile
+	else begin
+		display_position#reset;
+		in_display_file := false;
+		try
 			let old_file = ThreadSafeHashtbl.find Lexer.all_files p.pfile in
-			let new_file = Lexer.make_file p.pfile in
-			Lexer.copy_file old_file new_file;
+			(* If the file exists we work with a copy of it. This prevents any mutations from messing
+				up the orignal state. Note that we still don't use a real file context here, so the
+				data is not added to all_files. *)
+			let new_file = Lexer.copy_file old_file in
 			Lexer.create_context new_file
 		with Not_found ->
+			(* Otherwise we have to work with a temp file. *)
 			Lexer.create_temp_ctx p.pfile
-		end
-	end else begin
-		display_position#reset;
-		in_display_file := false;
-		Lexer.create_temp_ctx p.pfile
 	end in
 	let result = try
 		parse entry lctx defines (Sedlexing.Utf8.from_string s) p.pfile