Skip to content

Commit

Permalink
Refactor to converge APIs with LR-based parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Evans committed Aug 16, 2024
1 parent f5c7ff3 commit d98b92c
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 176 deletions.
63 changes: 36 additions & 27 deletions bootstrap/bin/hocc/code.ml
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,9 @@ let expand_hmi_template template_indentation template Spec.{symbols; _} formatte
] in
formatter |> expand ~template_indentation template expanders

let generate_hmi conf Parse.(Hmhi {prelude; hocc; postlude; eoi=Eoi {eoi}}) io spec =
let generate_hmi conf Parse.(Hmhi {prelude; hocc_; postlude; eoi=Eoi {eoi}}) io spec =
assert (Spec.conflicts spec = 0L);
let indentation = indentation_of_hocc hocc in
let indentation = indentation_of_hocc hocc_ in
let module_name = module_name conf in
let hmhi_name = module_name ^ ".hmhi" in
let hmi_name = module_name ^ ".hmi" in
Expand All @@ -478,12 +478,12 @@ let generate_hmi conf Parse.(Hmhi {prelude; hocc; postlude; eoi=Eoi {eoi}}) io s
|> Fmt.fmt "[:" |> Fmt.fmt directive_pathstr |> Fmt.fmt ":1]"
|> (fun formatter ->
match prelude with
| Parse.Matter {token; _} -> begin
let base = match token with
| Parse.Matter {token_; _} -> begin
let base = match token_ with
| HmcToken {source; _} -> Hmc.Source.Slice.base source
| HoccToken _ -> not_reached ()
in
let past = match hocc with
let past = match hocc_ with
| HmcToken _ -> not_reached ()
| HoccToken {source; _} -> Hmc.Source.Slice.base source
in
Expand All @@ -497,7 +497,7 @@ let generate_hmi conf Parse.(Hmhi {prelude; hocc; postlude; eoi=Eoi {eoi}}) io s
|> (fun formatter ->
match postlude with
| Parse.Matter _ -> begin
let base = match hocc with
let base = match hocc_ with
| HmcToken _ -> not_reached ()
| HoccToken {source; _} -> Hmc.Source.Slice.past source
in
Expand Down Expand Up @@ -1240,7 +1240,7 @@ let expand_hm_precs precs ~indentation formatter =
| _ -> begin
formatter
|> Fmt.fmt "of_list Uns "
|> (Ordset.to_list doms |> List.fmt ~alt:true ~width:indentation Prec.Index.pp)
|> (Ordset.to_list doms |> List.pp Prec.Index.pp)
end
)
|> Fmt.fmt ")"
Expand Down Expand Up @@ -1750,8 +1750,13 @@ let expand_hm_callbacks hocc_block symbols callbacks ~indentation formatter =
|> indent
|> String.fmt ~pad:overline ~just:Fmt.Left ~width:(100L - indentation) " # "
|> Fmt.fmt "\n"
|> indent |> Fmt.fmt " )), tl\n"
|> indent |> Fmt.fmt " | _ -> not_reached ()"
|> indent |> Fmt.fmt " )), tl"
|> (fun formatter ->
match Callback.is_epsilon callback with
| false ->
formatter |> Fmt.fmt "\n" |> indent |> Fmt.fmt " | _ -> not_reached ()"
| true -> formatter
)
end
| true -> formatter |> Fmt.fmt "fn _stack -> not_reached ()"
)
Expand Down Expand Up @@ -1835,9 +1840,9 @@ let expand_hm_template template_indentation template hocc_block
formatter |> expand ~template_indentation template expanders

let generate_hm conf
Parse.(Hmh {prelude; hocc=(Hocc {hocc; _} as hocc_block); postlude; eoi=Eoi {eoi}}) io spec =
Parse.(Hmh {prelude; hocc_=(Hocc {hocc_; _} as hocc_block); postlude; eoi=Eoi {eoi}}) io spec =
assert (Spec.conflicts spec = 0L);
let indentation = indentation_of_hocc hocc in
let indentation = indentation_of_hocc hocc_ in
let module_name = module_name conf in
let hmh_name = module_name ^ ".hmh" in
let hm_name = module_name ^ ".hm" in
Expand All @@ -1853,12 +1858,12 @@ let generate_hm conf
|> Fmt.fmt "[:" |> Fmt.fmt directive_pathstr |> Fmt.fmt ":1]"
|> (fun formatter ->
match prelude with
| Parse.Matter {token; _} -> begin
let base = match token with
| Parse.Matter {token_; _} -> begin
let base = match token_ with
| HmcToken {source; _} -> Hmc.Source.Slice.base source
| HoccToken _ -> not_reached ()
in
let past = match hocc with
let past = match hocc_ with
| HmcToken _ -> not_reached ()
| HoccToken {source; _} -> Hmc.Source.Slice.base source
in
Expand Down Expand Up @@ -2287,9 +2292,9 @@ let expand_mli_template template_indentation template Spec.{symbols; _} formatte
] in
formatter |> expand ~template_indentation template expanders

let generate_mli conf Parse.(Hmhi {prelude; hocc; postlude; eoi=Eoi {eoi}}) io spec =
let generate_mli conf Parse.(Hmhi {prelude; hocc_; postlude; eoi=Eoi {eoi}}) io spec =
assert (Spec.conflicts spec = 0L);
let indentation = indentation_of_hocc hocc in
let indentation = indentation_of_hocc hocc_ in
let module_name = module_name conf in
let hmhi_name = module_name ^ ".hmhi" in
let mli_name = module_name ^ ".mli" in
Expand All @@ -2302,12 +2307,12 @@ let generate_mli conf Parse.(Hmhi {prelude; hocc; postlude; eoi=Eoi {eoi}}) io s
|> Fmt.fmt "*)\n"
|> (fun formatter ->
match prelude with
| Parse.Matter {token; _} -> begin
let base = match token with
| Parse.Matter {token_; _} -> begin
let base = match token_ with
| HmcToken {source; _} -> Hmc.Source.Slice.base source
| HoccToken _ -> not_reached ()
in
let past = match hocc with
let past = match hocc_ with
| HmcToken _ -> not_reached ()
| HoccToken {source; _} -> Hmc.Source.Slice.base source
in
Expand All @@ -2320,7 +2325,7 @@ let generate_mli conf Parse.(Hmhi {prelude; hocc; postlude; eoi=Eoi {eoi}}) io s
|> (fun formatter ->
match postlude with
| Parse.Matter _ -> begin
let base = match hocc with
let base = match hocc_ with
| HmcToken _ -> not_reached ()
| HoccToken {source; _} -> Hmc.Source.Slice.past source
in
Expand Down Expand Up @@ -3090,7 +3095,7 @@ let expand_ml_precs precs ~indentation formatter =
| _ -> begin
formatter
|> Fmt.fmt "of_list (module Uns) "
|> (Ordset.to_list doms |> List.fmt ~alt:true ~width:indentation ml_uns_pp)
|> (Ordset.to_list doms |> List.pp ml_uns_pp)
end
)
|> Fmt.fmt ")"
Expand Down Expand Up @@ -3613,7 +3618,11 @@ let expand_ml_callbacks hocc_block symbols callbacks ~indentation formatter =
|> String.fmt ~pad:overline ~just:Fmt.Left ~width:(98L - indentation) " (*"
|> Fmt.fmt "*)\n"
|> indent |> Fmt.fmt " )), tl\n"
|> indent |> Fmt.fmt " | _ -> not_reached ()\n"
|> (fun formatter ->
match Callback.is_epsilon callback with
| false -> formatter |> indent |> Fmt.fmt " | _ -> not_reached ()\n"
| true -> formatter
)
|> indent |> Fmt.fmt ")"
end
| true -> formatter |> Fmt.fmt "(fun _stack -> not_reached ())"
Expand Down Expand Up @@ -3698,9 +3707,9 @@ let expand_ml_template template_indentation template hocc_block
formatter |> expand ~template_indentation template expanders

let generate_ml conf
Parse.(Hmh {prelude; hocc=(Hocc {hocc; _} as hocc_block); postlude; eoi=Eoi {eoi}}) io spec =
Parse.(Hmh {prelude; hocc_=(Hocc {hocc_; _} as hocc_block); postlude; eoi=Eoi {eoi}}) io spec =
assert (Spec.conflicts spec = 0L);
let indentation = indentation_of_hocc hocc in
let indentation = indentation_of_hocc hocc_ in
let module_name = module_name conf in
let hmh_name = module_name ^ ".hmh" in
let ml_name = module_name ^ ".ml" in
Expand All @@ -3714,12 +3723,12 @@ let generate_ml conf

|> (fun formatter ->
match prelude with
| Parse.Matter {token; _} -> begin
let base = match token with
| Parse.Matter {token_; _} -> begin
let base = match token_ with
| HmcToken {source; _} -> Hmc.Source.Slice.base source
| HoccToken _ -> not_reached ()
in
let past = match hocc with
let past = match hocc_ with
| HmcToken _ -> not_reached ()
| HoccToken {source; _} -> Hmc.Source.Slice.base source
in
Expand Down
Loading

0 comments on commit d98b92c

Please sign in to comment.