@@ -1160,13 +1160,40 @@ let concat_segment =
11601160let (@ ) = (seg1: Segment . t , seg2: Segment . t ): Segment . t =>
11611161 concat_segment(~secondary= AutoFormat , seg1, seg2);
11621162
1163+ /* Projector-construction hooks, injected by ProjectorInit at module
1164+ * initialization. ExpToSegment needs to construct projectors (folds,
1165+ * tables) while printing, but ProjectorInit needs ExpToSegment to print
1166+ * term-level init overrides, so ExpToSegment cannot depend on
1167+ * ProjectorInit directly. If the hooks are unregistered, projector
1168+ * construction degrades to a no-op (the plain syntax is used). */
1169+ module ProjectorHooks = {
1170+ type t = {
1171+ init_or_noop:
1172+ (ProjectorCore . Kind . t , Base . segment , Any . t ) => Base . segment ,
1173+ init_or_noop_from_str:
1174+ (ProjectorCore . Kind . t , Base . segment , Any . t , string ) => Base . segment ,
1175+ };
1176+ let registered : ref (option (t )) = ref (None );
1177+ let register = (hooks: t ): unit => registered := Some (hooks);
1178+ let init_or_noop = (kind, seg, any) =>
1179+ switch (registered^ ) {
1180+ | Some (hooks ) => hooks. init_or_noop(kind, seg, any)
1181+ | None => seg
1182+ };
1183+ let init_or_noop_from_str = (kind, seg, any, str) =>
1184+ switch (registered^ ) {
1185+ | Some (hooks ) => hooks. init_or_noop_from_str(kind, seg, any, str)
1186+ | None => seg
1187+ };
1188+ };
1189+
11631190let fold_if = (condition, pieces) =>
11641191 if (condition) {
11651192 let wrapped =
11661193 mk_form(~secondary= AutoFormat , ParensExp , Id . mk() , [ pieces] );
11671194 switch (MakeTerm . for_projection([ wrapped] )) {
11681195 | None => failwith ("ExpToSegment.fold_if" )
1169- | Some (any ) => ProjectorInit . init_or_noop(Fold , pieces, any)
1196+ | Some (any ) => ProjectorHooks . init_or_noop(Fold , pieces, any)
11701197 };
11711198 } else {
11721199 pieces;
@@ -1182,7 +1209,7 @@ let fold_fun_if = (condition, f_name: string, pieces, exp) =>
11821209 always_render: true ,
11831210 })
11841211 |> Sexplib . Sexp . to_string;
1185- ProjectorInit . init_or_noop_from_str(Fold , pieces, Exp (exp), str);
1212+ ProjectorHooks . init_or_noop_from_str(Fold , pieces, Exp (exp), str);
11861213 | ` Text =>
11871214 let name =
11881215 if (String . length(f_name) >= 2 ) {
@@ -1205,7 +1232,7 @@ let project_table_if = (should_project, pieces) =>
12051232 if (should_project) {
12061233 switch (MakeTerm . for_projection([ pieces] )) {
12071234 | None => [ pieces]
1208- | Some (any ) => ProjectorInit . init_or_noop(Table , [ pieces] , any)
1235+ | Some (any ) => ProjectorHooks . init_or_noop(Table , [ pieces] , any)
12091236 };
12101237 } else {
12111238 [ pieces] ;
0 commit comments