Skip to content

Commit

Permalink
Implement record pattern for formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Feb 19, 2024
1 parent 5fb6f39 commit 7431b85
Show file tree
Hide file tree
Showing 16 changed files with 2,477 additions and 2,136 deletions.
15 changes: 5 additions & 10 deletions compiler/grainformat/grainformat.re
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ let format_code =
(
~eol,
~output=?,
~original_source: array(string),
~source: array(string),
program: Parsetree.parsed_program,
) => {
switch (output) {
Expand All @@ -69,19 +69,14 @@ let format_code =
set_binary_mode_out(oc, true);
Grain_formatting.Fmt.format(
~write=output_string(oc),
~original_source,
~source,
~eol,
program,
);
close_out(oc);
| None =>
set_binary_mode_out(stdout, true);
Grain_formatting.Fmt.format(
~write=print_string,
~original_source,
~eol,
program,
);
Grain_formatting.Fmt.format(~write=print_string, ~source, ~eol, program);
flush(stdout);
};
};
Expand Down Expand Up @@ -150,8 +145,8 @@ let enumerate_runs = opts =>
let grainformat = runs => {
List.iter(
({input_path, output_path}) => {
let (program, original_source, eol) = compile_parsed(input_path);
try(format_code(~eol, ~output=?output_path, ~original_source, program)) {
let (program, source, eol) = compile_parsed(input_path);
try(format_code(~eol, ~output=?output_path, ~source, program)) {
| exn =>
Stdlib.Format.eprintf("@[%s@]@.", Printexc.to_string(exn));
exit(2);
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/diagnostics/commenttree.re
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type t = {
line_map: Hashtbl.t(int, Parsetree.comment),
};

let empty: t = {comments: [||], line_map: Hashtbl.create(0)};

let loc = cmt => {
switch (cmt) {
| Parsetree.Doc({cmt_loc})
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/diagnostics/commenttree.rei
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ open Grain_parsing;

type t;

let empty: t;

let from_comments: list(Parsetree.comment) => t;

let query: (t, Location.t) => list(Parsetree.comment);
Expand Down
45 changes: 13 additions & 32 deletions compiler/src/formatting/doc.re
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ let if_broken = (breaking, flat) =>
flat_width: flat_width(flat),
breaking_width: breaking_width(breaking),
});
let indent = (c, doc) =>
let indent = (~count=2, doc) =>
Indent({
count: c,
count,
doc,
has_group_breaker: has_group_breaker(doc),
flat_width: flat_width(doc),
Expand Down Expand Up @@ -273,35 +273,16 @@ let concat_map = (~sep, ~lead, ~trail, ~f: (~final: bool, 'a) => t, l) => {
};
};

let parens = (~lead=?, ~trail=?, doc) =>
group(
Option.fold(~none=string("("), ~some=lead => lead ++ string("("), lead)
++ indent(2, break ++ doc)
++ break
++ Option.fold(
~none=string(")"),
~some=trail => string(")") ++ trail,
trail,
),
);
let braces = doc =>
group(
string("{")
++ indent(2, breakable_space ++ doc)
++ breakable_space
++ string("}"),
);
let block_braces = (~lead, ~trail, doc) =>
group(
~print_width=2,
string("{") ++ indent(2, lead ++ doc) ++ trail ++ string("}"),
);
let array_brackets = doc =>
group(string("[>") ++ indent(2, break ++ doc) ++ break ++ string("]"));
let list_brackets = doc =>
group(string("[") ++ indent(2, break ++ doc) ++ break ++ string("]"));
let angle_brackets = doc =>
group(string("<") ++ indent(2, break ++ doc) ++ break ++ string(">"));
let parens = (~wrap=doc => group(doc), doc) =>
wrap(string("(") ++ doc ++ string(")"));
let braces = (~wrap=doc => group(doc), doc) =>
wrap(string("{") ++ doc ++ string("}"));
let array_brackets = (~wrap=doc => group(doc), doc) =>
wrap(string("[>") ++ doc ++ string("]"));
let list_brackets = (~wrap=doc => group(doc), doc) =>
wrap(string("[") ++ doc ++ string("]"));
let angle_brackets = (~wrap=doc => group(doc), doc) =>
wrap(string("<") ++ doc ++ string(">"));

let double_quotes = doc => string("\"") ++ doc ++ string("\"");

Expand Down Expand Up @@ -437,7 +418,7 @@ module Engine = {
mode,
global_indent: group.global_indent,
local_indent: 0,
broken: false,
broken: has_group_breaker(doc),
};
print(~group, doc);
| Concat({left, right}) =>
Expand Down
13 changes: 6 additions & 7 deletions compiler/src/formatting/doc.rei
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let break: t;
let hardline: t;
let phantom_hardline: t;
let if_broken: (t, t) => t;
let indent: (int, t) => t;
let indent: (~count: int=?, t) => t;
let group: (~print_width: int=?, ~kind: group_type=?, t) => t;
let concat: (t, t) => t;
let (++): (t, t) => t;
Expand All @@ -32,12 +32,11 @@ let concat_map:
let comma: t;
let comma_breakable_space: t;
let trailing_comma: t;
let parens: (~lead: t=?, ~trail: t=?, t) => t;
let braces: t => t;
let block_braces: (~lead: t, ~trail: t, t) => t;
let array_brackets: t => t;
let list_brackets: t => t;
let angle_brackets: t => t;
let parens: (~wrap: t => t=?, t) => t;
let braces: (~wrap: t => t=?, t) => t;
let array_brackets: (~wrap: t => t=?, t) => t;
let list_brackets: (~wrap: t => t=?, t) => t;
let angle_brackets: (~wrap: t => t=?, t) => t;
let double_quotes: t => t;

module Engine: {
Expand Down
Loading

0 comments on commit 7431b85

Please sign in to comment.