@@ -61,7 +61,7 @@ let test parse_fun pprint print map filename ~extra_checks =
61
61
| ast ->
62
62
let str = to_string pprint ast in
63
63
begin
64
- match extra_checks str with
64
+ match extra_checks (to_string print ast) str with
65
65
| Ok () -> ()
66
66
| Error reason ->
67
67
Printf. printf " %s: FAIL, %s\n " filename reason;
@@ -112,7 +112,7 @@ let rec process path ~extra_checks =
112
112
path
113
113
~extra_checks
114
114
115
- let process ?(extra_checks = fun _ -> Ok () ) text = process text ~extra_checks
115
+ let process ?(extra_checks = fun _ _ -> Ok () ) text = process text ~extra_checks
116
116
117
117
(* Produce an error if any attribute/extension node does not start with the
118
118
text prefix.
@@ -128,7 +128,7 @@ let process ?(extra_checks = fun _ -> Ok ()) text = process text ~extra_checks
128
128
We've chosen to keep those constructs out of the test file in preference
129
129
to updating this logic to properly handle them (which is hard).
130
130
*)
131
- let check_all_attributes_and_extensions_start_with text ~prefix =
131
+ let check_all_printed_attributes_and_extensions_start_with text ~prefix =
132
132
let check introduction_string =
133
133
String. split_on_char '[' text
134
134
|> List. for_all (fun s ->
@@ -146,14 +146,46 @@ let check_all_attributes_and_extensions_start_with text ~prefix =
146
146
prefix)
147
147
;;
148
148
149
+ let check_all_ast_attributes_and_extensions_start_with raw_parsetree_str ~prefixes =
150
+ (* Sadly can't use Ast_mapper here because it decodes Jane Syntax by default and
151
+ we will need quite a bit of code duplication for it to work for this use case. *)
152
+ let check introduction_string =
153
+ Misc.Stdlib.String. split_on_string ~split_on: (introduction_string ^ " \" " )
154
+ raw_parsetree_str
155
+ |> List. tl
156
+ |> List. for_all (fun s ->
157
+ List. exists
158
+ (fun prefix -> String. starts_with s ~prefix )
159
+ prefixes)
160
+ in
161
+ if check " extension" && check " attribute"
162
+ then Ok ()
163
+ else
164
+ Error
165
+ (Printf. sprintf
166
+ " Printast produced an extension node or attribute that doesn't \
167
+ begin with one of [%s]"
168
+ (String. concat " , " prefixes))
169
+ ;;
170
+
149
171
let () =
150
172
process " source.ml" ;
151
173
Language_extension. enable_maximal () ;
152
- process " source_jane_street.ml" ~extra_checks: (fun text ->
153
- (* Check that printing Jane Street language extensions produces no more
174
+ process " source_jane_street.ml" ~extra_checks: (fun raw_parsetree_str text ->
175
+ (* Additionally check that:
176
+
177
+ 1. Jane Street language extensions only use "extension." and "jane." prefixed
178
+ attributes and exntensions for its parsetree encoding. This is important for
179
+ ppx support.
180
+
181
+ 2. Printing Jane Street language extensions produces no more
154
182
attributes or extension nodes than the input program, all of whose
155
183
attributes begin with "test". This ensures that Jane Syntax attributes
156
184
aren't printed.
157
185
*)
158
- check_all_attributes_and_extensions_start_with text ~prefix: " test" );
186
+ Result. bind
187
+ (check_all_ast_attributes_and_extensions_start_with raw_parsetree_str
188
+ ~prefixes: [" extension." ; " jane." ; " test." ])
189
+ (fun () -> check_all_printed_attributes_and_extensions_start_with text
190
+ ~prefix: " test" ));
159
191
;;
0 commit comments