Skip to content

Commit 07bfcd3

Browse files
fix(grainfmt): Handle chained value bindings properly (#1467)
1 parent 0212247 commit 07bfcd3

File tree

11 files changed

+134
-36
lines changed

11 files changed

+134
-36
lines changed

compiler/src/formatting/format.re

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3358,9 +3358,9 @@ and print_value_bind =
33583358
| Mutable => Doc.text("mut ")
33593359
};
33603360

3361-
let value_bindings =
3361+
let formatted_items =
33623362
switch (vbs) {
3363-
| [] => Doc.nil
3363+
| [] => []
33643364
| [first, ...rem] =>
33653365
let get_loc = (vb: Parsetree.value_binding) => vb.pvb_loc;
33663366
let print_item = (~comments, vb: Parsetree.value_binding) => {
@@ -3443,25 +3443,46 @@ and print_value_bind =
34433443
]);
34443444
};
34453445

3446-
let items =
3447-
item_iterator(
3448-
~get_loc,
3449-
~print_item,
3450-
~comments,
3451-
~separator=Doc.comma,
3452-
vbs,
3453-
);
3454-
Doc.join(~sep=Doc.space, items);
3446+
item_iterator(
3447+
~get_loc,
3448+
~print_item,
3449+
~comments,
3450+
~separator=Doc.comma,
3451+
vbs,
3452+
);
34553453
};
34563454

3455+
let value_bindings =
3456+
List.mapi(
3457+
(index, doc) => {
3458+
let item =
3459+
if (index > 0) {
3460+
Doc.concat([Doc.line, doc]);
3461+
} else {
3462+
doc;
3463+
};
3464+
let vb = List.nth(vbs, index);
3465+
switch (vb.pvb_expr.pexp_desc) {
3466+
| PExpLambda(fn, _) => item
3467+
| _ =>
3468+
if (index > 0) {
3469+
Doc.indent(item);
3470+
} else {
3471+
item;
3472+
}
3473+
};
3474+
},
3475+
formatted_items,
3476+
);
3477+
34573478
Doc.group(
34583479
Doc.concat([
34593480
exported,
34603481
Doc.text("let"),
34613482
Doc.space,
34623483
recursive,
34633484
mutble,
3464-
value_bindings,
3485+
Doc.concat(value_bindings),
34653486
]),
34663487
);
34673488
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
3+
let g = 7, // g represents the precision desired, p is the values of p[i] to plug into Lanczos' formula
4+
p = 8, // another comment
5+
q = 32
6+
7+
8+
let myFunction2 = x => {
9+
let inter = x + 1
10+
"some string"
11+
}, myFunction3 = y => {
12+
let myVal = 5
13+
"some string"
14+
}
15+
16+
let g = 7,
17+
p = 8,
18+
q = 32
19+
20+
let myFunction2 = x => {
21+
let inter = x + 1
22+
"some string"
23+
}, // a comment
24+
myFunction3 = y => {
25+
let myVal = 5
26+
"some string"
27+
}

compiler/test/formatter_inputs/lets.gr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ let rotate = (count, list) => {
3131
else part(length(list) + count, list)
3232
append(end, beginning)
3333
}
34+
35+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
let g = 7, // g represents the precision desired, p is the values of p[i] to plug into Lanczos' formula
2+
p = 8, // another comment
3+
q = 32
4+
5+
let myFunction2 = x => {
6+
let inter = x + 1
7+
"some string"
8+
},
9+
myFunction3 = y => {
10+
let myVal = 5
11+
"some string"
12+
}
13+
14+
let g = 7, p = 8, q = 32
15+
16+
let myFunction2 = x => {
17+
let inter = x + 1
18+
"some string"
19+
}, // a comment
20+
myFunction3 = y => {
21+
let myVal = 5
22+
"some string"
23+
}

compiler/test/formatter_outputs/lets.gr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ let myBlock = {
77
let myFunction2 = x => {
88
let inter = x + 1
99
"some string"
10-
}, myFunction3 = y => {
10+
},
11+
myFunction3 = y => {
1112
let myVal = 5
1213
"some string"
1314
}

compiler/test/suites/formatter.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ describe("formatter", ({test, testSkip}) => {
5050
assertFormatOutput("only_comments", "only_comments");
5151
assertFormatOutput("data_docs", "data_docs");
5252
assertFormatOutput("custom_operators", "custom_operators");
53+
assertFormatOutput("chained", "chained");
5354
});

stdlib/regex.gr

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ let rec rangeAdd = (rng: CharRange, v: CharRangeElt) => {
241241
_ when rangeContains(rng, v) => rng,
242242
_ => rangeUnion(rng, [(v, v)]),
243243
}
244-
}, rangeUnion = (rng1, rng2) => {
244+
},
245+
rangeUnion = (rng1, rng2) => {
245246
match ((rng1, rng2)) {
246247
([], _) => rng2,
247248
(_, []) => rng1,
@@ -636,7 +637,8 @@ let rec parseRangeNot = (buf: RegExBuf) => {
636637
Ok(_) => parseRange(buf),
637638
}
638639
}
639-
}, parseRange = (buf: RegExBuf) => {
640+
},
641+
parseRange = (buf: RegExBuf) => {
640642
if (!more(buf)) {
641643
Err(parseErr(buf, "Missing closing `]`", 0))
642644
} else {
@@ -659,7 +661,8 @@ let rec parseRangeNot = (buf: RegExBuf) => {
659661
Ok(_) => parseRangeRest(buf, [], None, None),
660662
}
661663
}
662-
}, parseClass = (buf: RegExBuf) => {
664+
},
665+
parseClass = (buf: RegExBuf) => {
663666
if (!more(buf)) {
664667
Err(
665668
"no chars"
@@ -694,7 +697,8 @@ let rec parseRangeNot = (buf: RegExBuf) => {
694697
Ok(c) => Err("unknown class: " ++ toString(c)),
695698
}
696699
}
697-
}, parsePosixCharClass = (buf: RegExBuf) => {
700+
},
701+
parsePosixCharClass = (buf: RegExBuf) => {
698702
if (!more(buf)) {
699703
Err(parseErr(buf, "Missing POSIX character class after `[`", 0))
700704
} else {
@@ -810,7 +814,8 @@ let rec parseRangeNot = (buf: RegExBuf) => {
810814
),
811815
}
812816
}
813-
}, parseRangeRest =
817+
},
818+
parseRangeRest =
814819
(
815820
buf: RegExBuf,
816821
rng: CharRange,
@@ -962,7 +967,8 @@ let rec parseRangeNot = (buf: RegExBuf) => {
962967
},
963968
}
964969
}
965-
}, parseRangeRestSpan =
970+
},
971+
parseRangeRestSpan =
966972
(
967973
buf: RegExBuf,
968974
c,
@@ -1213,7 +1219,8 @@ let rec parseAtom = (buf: RegExBuf) => {
12131219
_ => parseLiteral(buf),
12141220
},
12151221
}
1216-
}, parseLook = (buf: RegExBuf) => {
1222+
},
1223+
parseLook = (buf: RegExBuf) => {
12171224
let preNumGroups = unbox(buf.config.groupNumber)
12181225
let spanNumGroups = () => unbox(buf.config.groupNumber) - preNumGroups
12191226
// (isMatch, isAhead)
@@ -1279,7 +1286,8 @@ let rec parseAtom = (buf: RegExBuf) => {
12791286
}
12801287
},
12811288
}
1282-
}, parseTest = (buf: RegExBuf) => {
1289+
},
1290+
parseTest = (buf: RegExBuf) => {
12831291
if (!more(buf)) {
12841292
Err(parseErr(buf, "Expected test", 0))
12851293
} else {
@@ -1316,7 +1324,8 @@ let rec parseAtom = (buf: RegExBuf) => {
13161324
),
13171325
}
13181326
}
1319-
}, parseInteger = (buf: RegExBuf, n) => {
1327+
},
1328+
parseInteger = (buf: RegExBuf, n) => {
13201329
if (!more(buf)) {
13211330
Ok(n)
13221331
} else {
@@ -1331,7 +1340,8 @@ let rec parseAtom = (buf: RegExBuf) => {
13311340
Ok(_) => Ok(n),
13321341
}
13331342
}
1334-
}, parseMode = (buf: RegExBuf) => {
1343+
},
1344+
parseMode = (buf: RegExBuf) => {
13351345
let processState = ((cs, ml)) => {
13361346
let withCs = match (cs) {
13371347
None => buf.config,
@@ -1390,7 +1400,8 @@ let rec parseAtom = (buf: RegExBuf) => {
13901400
}
13911401
}
13921402
help((None, None))
1393-
}, parseUnicodeCategories = (buf: RegExBuf, pC: String) => {
1403+
},
1404+
parseUnicodeCategories = (buf: RegExBuf, pC: String) => {
13941405
if (!more(buf)) {
13951406
Err(parseErr(buf, "Expected unicode category", 0))
13961407
} else {
@@ -1558,7 +1569,8 @@ let rec parseAtom = (buf: RegExBuf) => {
15581569
Ok(_) => Err(parseErr(buf, "Expected `{` after `\\" ++ pC ++ "`", 0)),
15591570
}
15601571
}
1561-
}, parseLiteral = (buf: RegExBuf) => {
1572+
},
1573+
parseLiteral = (buf: RegExBuf) => {
15621574
if (!more(buf)) {
15631575
Err(parseErr(buf, "Expected literal", 0))
15641576
} else {
@@ -1592,7 +1604,8 @@ let rec parseAtom = (buf: RegExBuf) => {
15921604
},
15931605
}
15941606
}
1595-
}, parseBackslashLiteral = (buf: RegExBuf) => {
1607+
},
1608+
parseBackslashLiteral = (buf: RegExBuf) => {
15961609
if (!more(buf)) {
15971610
// Special case: EOS after backslash matches null
15981611
Err(parseErr(buf, "Expected to find escaped value after backslash", 0))
@@ -1655,7 +1668,8 @@ let rec parseAtom = (buf: RegExBuf) => {
16551668
},
16561669
}
16571670
}
1658-
}, parseNonGreedy = (buf: RegExBuf) => {
1671+
},
1672+
parseNonGreedy = (buf: RegExBuf) => {
16591673
let checkNotNested = res => {
16601674
if (!more(buf)) {
16611675
res
@@ -1681,7 +1695,8 @@ let rec parseAtom = (buf: RegExBuf) => {
16811695
Ok(_) => checkNotNested(Ok(false)),
16821696
}
16831697
}
1684-
}, parsePCE = (buf: RegExBuf) => {
1698+
},
1699+
parsePCE = (buf: RegExBuf) => {
16851700
match (parseAtom(buf)) {
16861701
Err(e) => Err(e),
16871702
Ok(atom) => {
@@ -1775,7 +1790,8 @@ let rec parseAtom = (buf: RegExBuf) => {
17751790
}
17761791
},
17771792
}
1778-
}, parsePCEs = (buf: RegExBuf, toplevel: Bool) => {
1793+
},
1794+
parsePCEs = (buf: RegExBuf, toplevel: Bool) => {
17791795
if (!more(buf)) {
17801796
Ok([])
17811797
} else {
@@ -1801,7 +1817,8 @@ let rec parseAtom = (buf: RegExBuf) => {
18011817
},
18021818
}
18031819
}
1804-
}, parseRegex = (buf: RegExBuf) => {
1820+
},
1821+
parseRegex = (buf: RegExBuf) => {
18051822
if (!more(buf)) {
18061823
Ok(REEmpty)
18071824
} else {
@@ -1836,7 +1853,8 @@ let rec parseAtom = (buf: RegExBuf) => {
18361853
},
18371854
}
18381855
}
1839-
}, parseRegexNonEmpty = (buf: RegExBuf) => {
1856+
},
1857+
parseRegexNonEmpty = (buf: RegExBuf) => {
18401858
match (parsePCEs(buf, false)) {
18411859
Err(e) => Err(e),
18421860
Ok(pces) => {

stdlib/runtime/compare.gr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ let rec heapCompareHelp = (heapTag, xptr, yptr) => {
146146
tagSimpleNumber(xptr - yptr)
147147
},
148148
}
149-
}, compareHelp = (x, y) => {
149+
},
150+
compareHelp = (x, y) => {
150151
let xtag = x & Tags._GRAIN_GENERIC_TAG_MASK
151152
let ytag = y & Tags._GRAIN_GENERIC_TAG_MASK
152153
if ((xtag & ytag) != Tags._GRAIN_GENERIC_HEAP_TAG_TYPE) {

stdlib/runtime/equal.gr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ let rec heapEqualHelp = (heapTag, xptr, yptr) => {
189189
xptr == yptr
190190
},
191191
}
192-
}, equalHelp = (x, y) => {
192+
},
193+
equalHelp = (x, y) => {
193194
if (
194195
(x & Tags._GRAIN_GENERIC_TAG_MASK) != 0n &&
195196
(y & Tags._GRAIN_GENERIC_TAG_MASK) != 0n

stdlib/runtime/gc.gr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ let rec decRef = (userPtr: WasmI32, ignoreZeros: Bool) => {
213213
} else {
214214
userPtr
215215
}
216-
}, decRefChildren = (userPtr: WasmI32) => {
216+
},
217+
decRefChildren = (userPtr: WasmI32) => {
217218
match (WasmI32.load(userPtr, 0n)) {
218219
t when t == Tags._GRAIN_BOXED_NUM_HEAP_TAG => {
219220
let tag = WasmI32.load(userPtr, 4n)

0 commit comments

Comments
 (0)