Skip to content

Commit

Permalink
fix(grainfmt): Resugar match lists properly
Browse files Browse the repository at this point in the history
fix(grainfmt): Handle recursive enums

Co-authored-by: Marcus Roberts <marcus@marcusr.com>
  • Loading branch information
marcusroberts and Marcus Roberts authored Nov 16, 2021
1 parent 477662b commit 7dc77cd
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
44 changes: 35 additions & 9 deletions compiler/grainformat/reformat.re
Original file line number Diff line number Diff line change
Expand Up @@ -473,24 +473,45 @@ let rec resugar_list_patterns =
~original_source: array(string),
) => {
let processed_list = resugar_pattern_list_inner(patterns, parent_loc);
let last_item_was_spread = ref(false);

let items =
List.map(
i =>
switch (i) {
| RegularPattern(e) =>
print_pattern(~pat=e, ~parent_loc, ~original_source)
last_item_was_spread := false;

Doc.group(print_pattern(~pat=e, ~parent_loc, ~original_source));
| SpreadPattern(e) =>
Doc.concat([
Doc.text("..."),
print_pattern(~pat=e, ~parent_loc, ~original_source),
])
last_item_was_spread := true;
Doc.group(
Doc.concat([
Doc.text("..."),
print_pattern(~pat=e, ~parent_loc, ~original_source),
]),
);
},
processed_list,
);

Doc.group(
Doc.concat([
Doc.lbracket,
Doc.join(Doc.concat([Doc.comma, Doc.line]), items),
Doc.indent(
Doc.concat([
Doc.lbracket,
Doc.concat([
Doc.softLine,
Doc.join(Doc.concat([Doc.comma, Doc.line]), items),
]),
if (last_item_was_spread^) {
Doc.nil;
} else {
Doc.ifBreaks(Doc.comma, Doc.nil);
},
]),
),
Doc.softLine,
Doc.rbracket,
]),
);
Expand All @@ -511,9 +532,13 @@ and resugar_pattern_list_inner =

if (func == "[]") {
[RegularPattern(arg1)];
} else if (func == list_cons) {
let inner = resugar_pattern_list_inner(innerpatterns, parent_loc);
List.append([RegularPattern(arg1)], inner);
} else {
[RegularPattern(arg1), SpreadPattern(arg2)];
};

| _ => [RegularPattern(arg1), SpreadPattern(arg2)]
};
}
Expand Down Expand Up @@ -2962,7 +2987,7 @@ let data_print =
original_source: array(string),
) => {
Doc.join(
Doc.comma,
Doc.concat([Doc.comma, Doc.hardLine]),
List.map(
data => {
let (expt, decl) = data;
Expand Down Expand Up @@ -3505,7 +3530,6 @@ let reformat_ast =
//Doc.debug(final_doc);
//

Doc.toString(~width=80, final_doc);
//use this to see the AST in JSON
// print_endline(
// Yojson.Basic.pretty_to_string(
Expand All @@ -3514,4 +3538,6 @@ let reformat_ast =
// ),
// ),
// );

Doc.toString(~width=80, final_doc);
};
5 changes: 5 additions & 0 deletions compiler/test/formatter_inputs/enums.gr
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@ enum ParsedRegularExpression {
RERange(List<(Number, Number)>),
REUnicodeCategories(List<UnicodeCategory>, Bool) // symlist, true=match/false=does-not-match
}


record Node { node: AstValue },

enum AstValue { NodeValue(Node), StringValue(String) }
9 changes: 8 additions & 1 deletion compiler/test/formatter_inputs/matches.gr
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ match (A(1, 2)) {
b,
c
) => false
}
}

let testFormatting = charlist => {
match (charlist) {
['f', 'o', 'r', 'm', 'a', 't', ...rest] => true,
_ => false
}
}
1 change: 1 addition & 0 deletions compiler/test/formatter_inputs/nested_matches.gr
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ let join = (separator: String, items: List<String>) => {
}
}
}

3 changes: 3 additions & 0 deletions compiler/test/formatter_outputs/enums.gr
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,6 @@ enum ParsedRegularExpression {
Bool
), // symlist, true=match/false=does-not-match
}

record Node { node: AstValue },
enum AstValue { NodeValue(Node), StringValue(String) }
7 changes: 7 additions & 0 deletions compiler/test/formatter_outputs/matches.gr
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ match (A(1, 2)) {
A(a, b) => true,
B(b, c) => false,
}

let testFormatting = charlist => {
match (charlist) {
['f', 'o', 'r', 'm', 'a', 't', ...rest] => true,
_ => false,
}
}

0 comments on commit 7dc77cd

Please sign in to comment.