Skip to content

Commit

Permalink
[macro] Expose Context.resolveFilePositions() instead
Browse files Browse the repository at this point in the history
  • Loading branch information
kLabz committed Jan 10, 2025
1 parent 09f3d1a commit 4e48bc2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/macro/macroApi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type 'value compiler_api = {
after_generate : (unit -> unit) -> unit;
on_type_not_found : (string -> 'value) -> unit;
parse_string : string -> Globals.pos -> bool -> Ast.expr;
parse_file_positions : string -> unit;
parse : 'a . ((Ast.token * Globals.pos) Stream.t -> 'a) -> string -> 'a;
type_expr : Ast.expr -> Type.texpr;
resolve_type : Ast.complex_type -> Globals.pos -> t;
Expand Down Expand Up @@ -1908,6 +1909,11 @@ let macro_api ccom get_api =
if s = "" then (get_api()).exc_string "Invalid expression";
encode_expr ((get_api()).parse_string s (decode_pos p) (decode_bool b))
);
"parse_file_positions", vfun1 (fun f ->
let f = decode_string f in
(get_api()).parse_file_positions f;
vnull
);
"make_expr", vfun2 (fun v p ->
encode_expr (value_to_expr v (decode_pos p))
);
Expand Down
4 changes: 1 addition & 3 deletions src/syntax/parserEntry.ml
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,7 @@ let parse_string entry com s p error inlined =
let old_syntax_errors = !syntax_errors in
syntax_errors := [];
let restore() =
(match old_file with
| None -> Hashtbl.remove Lexer.all_files p.pfile
| Some f -> Hashtbl.replace Lexer.all_files p.pfile f);
Option.may (Hashtbl.replace Lexer.all_files p.pfile) old_file;
if not inlined then begin
display_position#set old_display;
in_display_file := old_in_display_file;
Expand Down
4 changes: 4 additions & 0 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ let make_macro_com_api com mcom p =
| ParseSuccess(r,_,_) -> r
| ParseError(_,(msg,p),_) -> Parser.error msg p
);
parse_file_positions = (fun file ->
let f = Lexer.resolve_pos file in
Hashtbl.add Lexer.all_files file f;
);
type_expr = (fun e ->
Interp.exc_string "unsupported"
);
Expand Down
8 changes: 8 additions & 0 deletions std/haxe/macro/Context.hx
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,14 @@ class Context {
return load("do_parse", 3)(expr, pos, true);
}

/**
Parse a file for newlines, allowing positions to be resolved properly
inside that file later on (using `Context.parseInlineString` for example).
**/
public static function resolveFilePositions(file:String):Void {
load("parse_file_positions", 1)(file);
}

/**
Builds an expression from `v`.
Expand Down

0 comments on commit 4e48bc2

Please sign in to comment.