Skip to content

Commit b3c4184

Browse files
cknittclaude
andauthored
Represent %raw, %ffi and %%raw with parsetree nodes (#8879)
* Represent %raw, %ffi and %%raw with parsetree nodes `Pexp_raw` (with a `Raw_expression` / `Raw_function` kind) and `Pstr_raw` hold the JavaScript source as written, so `Pconst_raw_source` is gone. PPXs still see the extensions, and v0 templates now keep their decoding. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NP47qMfjMbjUTKZDsLthDo Signed-off-by: Christoph Knittel <ck@cca.io> * Decode braced %raw strings as before A braced string payload is an ordinary string, with or without a PPX. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NP47qMfjMbjUTKZDsLthDo Signed-off-by: Christoph Knittel <ck@cca.io> * Escape raw sources printed as templates A source that needs a template now escapes backquotes, ${ and backslashes, and attributed templates stay templates through the v0 bridge. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NP47qMfjMbjUTKZDsLthDo Signed-off-by: Christoph Knittel <ck@cca.io> --------- Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4e11b25 commit b3c4184

40 files changed

Lines changed: 701 additions & 188 deletions

‎analysis/reanalyze/src/annotation.ml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ let rec get_attribute_payload check_text (attributes : Typedtree.attributes) =
6161
match payload with
6262
| PStr [] -> Some UnrecognizedPayload
6363
| PStr ({pstr_desc = Pstr_eval (expr, _)} :: _) -> expr |> from_expr
64-
| PStr ({pstr_desc = Pstr_extension _} :: _) -> Some UnrecognizedPayload
64+
| PStr ({pstr_desc = Pstr_extension _ | Pstr_raw _} :: _) ->
65+
Some UnrecognizedPayload
6566
| PStr ({pstr_desc = Pstr_value _} :: _) -> Some UnrecognizedPayload
6667
| PStr ({pstr_desc = Pstr_primitive _} :: _) -> Some UnrecognizedPayload
6768
| PStr ({pstr_desc = Pstr_type _} :: _) -> Some UnrecognizedPayload

‎analysis/src/completion_front_end.ml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ let rec expr_to_context_path_inner ~(in_jsx_context : bool)
210210
(e : Parsetree.expression) =
211211
match e.pexp_desc with
212212
| Pexp_braces {expr} -> expr_to_context_path_inner ~in_jsx_context expr
213-
| Pexp_constant (Pconst_string _ | Pconst_raw_source _) ->
214-
Some Completable.CPString
213+
| Pexp_constant (Pconst_string _) -> Some Completable.CPString
215214
| Pexp_template _ -> Some Completable.CPString
216215
| Pexp_tagged_template {tag} -> (
217216
match expr_to_context_path ~in_jsx_context tag with
@@ -1289,6 +1288,8 @@ let completion_with_parser1 ~debug ~offset ~pos_cursor ~kind_file
12891288
| Pexp_object_literal fields ->
12901289
List.iter (fun (_, e) -> iterator.expr iterator e) fields
12911290
| Pexp_extension ({txt}, _) -> set_result (CextensionNode txt)
1291+
| Pexp_raw {kind} ->
1292+
set_result (CextensionNode (Ast_raw.extension_name kind))
12921293
| Pexp_constant _ -> set_result Cnone
12931294
| Pexp_ident lid ->
12941295
let lid_path = flatten_lid_check_dot lid in

‎analysis/src/document_symbol.ml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ let get_symbols ~source ~kind_file =
2020
| Pexp_fun _ -> Lsp.Types.SymbolKind.Function
2121
| Pexp_braces {expr} -> expr_kind expr
2222
| Pexp_constraint (e, _) -> expr_kind e
23-
| Pexp_constant (Pconst_string _ | Pconst_raw_source _) ->
24-
Lsp.Types.SymbolKind.String
23+
| Pexp_constant (Pconst_string _) -> Lsp.Types.SymbolKind.String
2524
| Pexp_template _ -> Lsp.Types.SymbolKind.String
2625
| Pexp_constant (Pconst_float _ | Pconst_integer _) ->
2726
Lsp.Types.SymbolKind.Number

‎analysis/src/dump_ast.ml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ let print_constant const =
5151
^ ", semantic="
5252
^ String_literal.string_semantic payload
5353
^ ")"
54-
| Pconst_raw_source source -> "Pconst_raw_source(" ^ source ^ ")"
5554
| Pconst_float (s, _) -> "Pconst_float(" ^ s ^ ")"
5655

5756
let print_core_type typ ~pos =

‎analysis/src/utils.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ let identify_pexp pexp =
117117
| Pexp_assert _ -> "Pexp_assert"
118118
| Pexp_pack _ -> "Pexp_pack"
119119
| Pexp_extension _ -> "Pexp_extension"
120+
| Pexp_raw _ -> "Pexp_raw"
120121
| Pexp_open _ -> "Pexp_open"
121122
| Pexp_await _ -> "Pexp_await"
122123
| Pexp_jsx_element _ -> "Pexp_jsx_element"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Represent `%raw`, `%ffi` and `%%raw` with dedicated parsetree nodes. https://github.com/rescript-lang/rescript/pull/8879

‎changes/raw-template-ppx.fix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix template and braced payloads of `%raw` and `%ffi` compiling differently when a PPX is used. https://github.com/rescript-lang/rescript/pull/8879

‎compiler/core/js_implementation.ml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ let all_module_alias (ast : Parsetree.structure) =
8787
| Pstr_attribute _ -> true
8888
| Pstr_eval _ | Pstr_value _ | Pstr_primitive _ | Pstr_type _
8989
| Pstr_typext _ | Pstr_exception _ | Pstr_module _ | Pstr_recmodule _
90-
| Pstr_modtype _ | Pstr_open _ | Pstr_include _ | Pstr_extension _ ->
90+
| Pstr_modtype _ | Pstr_open _ | Pstr_include _ | Pstr_extension _
91+
| Pstr_raw _ ->
9192
false)
9293

9394
let no_export (rest : Parsetree.structure) : Parsetree.structure =

‎compiler/ext/config.ml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
let cmi_magic_number = "Caml1999I037"
1+
let cmi_magic_number = "Caml1999I038"
22

33
(* Magic numbers for marshaled values of the *current* parsetree, whose layout
44
changes across compiler versions. *)
5-
and ast_impl_magic_number = "ResImpl01316"
5+
and ast_impl_magic_number = "ResImpl01317"
66

7-
and ast_intf_magic_number = "ResIntf01316"
7+
and ast_intf_magic_number = "ResIntf01317"
88

99
(* Magic numbers of the frozen Parsetree0 (OCaml 4.06) layout used on the
1010
external-PPX wire. They must never be written in front of a
@@ -13,7 +13,7 @@ and ast0_impl_magic_number = "Caml1999M022"
1313

1414
and ast0_intf_magic_number = "Caml1999N022"
1515

16-
and cmt_magic_number = "Caml1999T044"
16+
and cmt_magic_number = "Caml1999T045"
1717

1818
let get_load_path () = (Compiler_request_state.current ()).load_path
1919
let set_load_path path = (Compiler_request_state.current ()).load_path <- path

‎compiler/frontend/ast_exp_handle_external.ml‎

Lines changed: 79 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -34,100 +34,101 @@ let handle_debugger loc (payload : Ast_payload.t) =
3434
| _ ->
3535
Location.raise_errorf ~loc "%%debugger extension doesn't accept arguments"
3636

37+
let raw_type () =
38+
Ast_helper.Typ.arrow
39+
[Parg_type {attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}]
40+
(Ast_helper.Typ.any ())
41+
42+
(* [exp] is the string constant holding the JavaScript source. *)
43+
let raw_expression loc ~is_function (exp : Parsetree.expression) =
44+
{
45+
exp with
46+
pexp_desc =
47+
Ast_external_mk.local_external_apply loc
48+
~pval_prim:(Prim_name "#raw_expr") ~pval_type:(raw_type ()) [exp];
49+
pexp_attributes =
50+
(match !is_function with
51+
| None -> exp.pexp_attributes
52+
| Some _ -> Ast_attributes.internal_expansive :: exp.pexp_attributes);
53+
}
54+
55+
let ffi_error loc =
56+
Location.raise_errorf ~loc
57+
"%%ffi extension can only be applied to a string containing a JavaScript \
58+
function such as \"(x) => ...\""
59+
60+
let ffi_expression loc ~is_function (exp : Parsetree.expression) =
61+
(* Wrap a type constraint based on arity.
62+
E.g. for arity 2 constrain to type (_, _) => _ *)
63+
let wrap_type_constraint (e : Parsetree.expression) =
64+
let loc = e.pexp_loc in
65+
let any = Ast_helper.Typ.any ~loc:e.pexp_loc () in
66+
let arrow ~arity =
67+
let effective_arity = if arity = 0 then 1 else arity in
68+
let args =
69+
Ext_list.init effective_arity (fun _ ->
70+
Parsetree.Parg_type {attrs = []; lbl = Nolabel; typ = any})
71+
in
72+
Ast_helper.Typ.arrow ~loc args any
73+
in
74+
match !is_function with
75+
| Some arity -> Ast_helper.Exp.constraint_ ~loc e (arrow ~arity)
76+
| _ -> ffi_error loc
77+
in
78+
match !is_function with
79+
| None -> ffi_error loc
80+
| Some _ -> wrap_type_constraint (raw_expression loc ~is_function exp)
81+
82+
let raw_structure loc (exp : Parsetree.expression) =
83+
Ast_helper.Str.eval
84+
{
85+
exp with
86+
pexp_desc =
87+
Ast_external_mk.local_external_apply loc
88+
~pval_prim:(Prim_name "#raw_stmt") ~pval_type:(raw_type ()) [exp];
89+
}
90+
3791
let handle_raw loc payload =
3892
let is_function = ref None in
3993
match
4094
Ast_payload.raw_as_string_exp_exn ~kind:Raw_exp ~is_function payload
4195
with
4296
| None ->
4397
Location.raise_errorf ~loc "%%raw extension can only be applied to a string"
44-
| Some exp ->
45-
{
46-
exp with
47-
pexp_desc =
48-
Ast_external_mk.local_external_apply loc
49-
~pval_prim:(Prim_name "#raw_expr")
50-
~pval_type:
51-
(Ast_helper.Typ.arrow
52-
[
53-
Parg_type
54-
{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()};
55-
]
56-
(Ast_helper.Typ.any ()))
57-
[exp];
58-
pexp_attributes =
59-
(match !is_function with
60-
| None -> exp.pexp_attributes
61-
| Some _ -> Ast_attributes.internal_expansive :: exp.pexp_attributes);
62-
}
98+
| Some exp -> raw_expression loc ~is_function exp
6399

64100
let handle_ffi ~loc ~payload =
65101
let is_function = ref None in
66-
let err () =
67-
Location.raise_errorf ~loc
68-
"%%ffi extension can only be applied to a string containing a JavaScript \
69-
function such as \"(x) => ...\""
70-
in
71102
match
72103
Ast_payload.raw_as_string_exp_exn ~kind:Raw_exp ~is_function payload
73104
with
74-
| None -> err ()
75-
| Some exp ->
76-
(* Wrap a type constraint based on arity.
77-
E.g. for arity 2 constrain to type (_, _) => _ *)
78-
let wrap_type_constraint (e : Parsetree.expression) =
79-
let loc = e.pexp_loc in
80-
let any = Ast_helper.Typ.any ~loc:e.pexp_loc () in
81-
let arrow ~arity =
82-
let effective_arity = if arity = 0 then 1 else arity in
83-
let args =
84-
Ext_list.init effective_arity (fun _ ->
85-
Parsetree.Parg_type {attrs = []; lbl = Nolabel; typ = any})
86-
in
87-
Ast_helper.Typ.arrow ~loc args any
88-
in
89-
match !is_function with
90-
| Some arity -> Ast_helper.Exp.constraint_ ~loc e (arrow ~arity)
91-
| _ -> err ()
92-
in
93-
wrap_type_constraint
94-
{
95-
exp with
96-
pexp_desc =
97-
Ast_external_mk.local_external_apply loc
98-
~pval_prim:(Prim_name "#raw_expr")
99-
~pval_type:
100-
(Ast_helper.Typ.arrow
101-
[
102-
Parg_type
103-
{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()};
104-
]
105-
(Ast_helper.Typ.any ()))
106-
[exp];
107-
pexp_attributes =
108-
(match !is_function with
109-
| None -> exp.pexp_attributes
110-
| Some _ -> Ast_attributes.internal_expansive :: exp.pexp_attributes);
111-
}
105+
| None -> ffi_error loc
106+
| Some exp -> ffi_expression loc ~is_function exp
112107

113108
let handle_raw_structure loc payload =
114109
match Ast_payload.raw_as_string_exp_exn ~kind:Raw_program payload with
115-
| Some exp ->
116-
Ast_helper.Str.eval
117-
{
118-
exp with
119-
pexp_desc =
120-
Ast_external_mk.local_external_apply loc
121-
~pval_prim:(Prim_name "#raw_stmt")
122-
~pval_type:
123-
(Ast_helper.Typ.arrow
124-
[
125-
Parg_type
126-
{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()};
127-
]
128-
(Ast_helper.Typ.any ()))
129-
[exp];
130-
}
110+
| Some exp -> raw_structure loc exp
131111
| None ->
132112
Location.raise_errorf ~loc
133113
"%%%%raw extension can only be applied to a string"
114+
115+
(* The string constant holding the JavaScript source of a raw node, validated
116+
like a raw payload. *)
117+
let raw_source_expression ~(kind : Js_raw_info.raw_kind) ?is_function
118+
(source : Parsetree.raw_source) =
119+
let str = Ast_raw.js_source source in
120+
let loc = Ast_raw.payload_loc source in
121+
Ast_payload.validate_raw_source ~kind ?is_function ~loc
122+
~offset:(Bs_flow_ast_utils.flow_deli_offset (Some "js"))
123+
str;
124+
Ast_helper.Exp.constant ~loc (Ast_helper.Const.string str)
125+
126+
let handle_raw_node ~loc (kind : Parsetree.raw_kind) source =
127+
let is_function = ref None in
128+
let exp = raw_source_expression ~kind:Raw_exp ~is_function source in
129+
match kind with
130+
| Raw_expression -> raw_expression loc ~is_function exp
131+
| Raw_function -> ffi_expression loc ~is_function exp
132+
133+
let handle_raw_structure_node ~loc source =
134+
raw_structure loc (raw_source_expression ~kind:Raw_program source)

0 commit comments

Comments
 (0)