Skip to content

Commit 5ea244f

Browse files
committed
Merge remote-tracking branch 'origin/oscar/formatter' into phated/formatter-record-pattern
2 parents 7431b85 + 3811390 commit 5ea244f

30 files changed

+1009
-614
lines changed

compiler/src/formatting/fmt.re

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ let needs_grouping = (~parent, ~side: infix_side, expr) => {
253253
} else {
254254
FormatterGrouping;
255255
}
256-
| (PExpConstant(PConstNumber(PConstNumberRational(_, _))), _)
256+
| (PExpConstant(PConstNumber(PConstNumberRational(_))), _)
257257
when op_precedence('/') <= precedence(parent) =>
258258
ParenGrouping
259259
| _ => FormatterGrouping
@@ -270,7 +270,7 @@ let has_disable_formatting_comment = (~comment_tree, loc: Location.t) => {
270270
type formatter = {
271271
print_original_code: (formatter, Location.t) => Doc.t,
272272
print_infix_prefix_op: (formatter, expression) => Doc.t,
273-
print_constant: (formatter, ~loc: Location.t, constant) => Doc.t,
273+
print_constant: (formatter, constant) => Doc.t,
274274
print_punnable_pattern: (formatter, (loc(Identifier.t), pattern)) => Doc.t,
275275
print_lambda_argument: (formatter, lambda_argument) => Doc.t,
276276
print_pattern: (formatter, pattern) => Doc.t,
@@ -367,8 +367,48 @@ let print_infix_prefix_op = (fmt, expr) => {
367367
};
368368
};
369369

370-
let print_constant = (fmt, ~loc, constant) => {
371-
fmt.print_original_code(fmt, loc);
370+
let print_constant = (fmt, constant) => {
371+
switch (constant) {
372+
| PConstNumber(PConstNumberInt({txt: value})) => string(value)
373+
| PConstNumber(PConstNumberFloat({txt: value})) => string(value)
374+
| PConstNumber(PConstNumberRational({numerator, slash, denominator})) =>
375+
string(numerator.txt)
376+
++ fmt.print_comment_range(
377+
~lead=space,
378+
~trail=space,
379+
numerator.loc,
380+
slash,
381+
)
382+
++ string("/")
383+
++ fmt.print_comment_range(
384+
~lead=space,
385+
~trail=space,
386+
slash,
387+
denominator.loc,
388+
)
389+
++ string(denominator.txt)
390+
| PConstInt8({txt: value})
391+
| PConstUint8({txt: value})
392+
| PConstInt16({txt: value})
393+
| PConstUint16({txt: value})
394+
| PConstInt32({txt: value})
395+
| PConstUint32({txt: value})
396+
| PConstInt64({txt: value})
397+
| PConstUint64({txt: value})
398+
| PConstFloat32({txt: value})
399+
| PConstFloat64({txt: value})
400+
| PConstWasmI32({txt: value})
401+
| PConstWasmI64({txt: value})
402+
| PConstWasmF32({txt: value})
403+
| PConstWasmF64({txt: value})
404+
| PConstBigInt({txt: value})
405+
| PConstRational({txt: value})
406+
| PConstBytes({txt: value})
407+
| PConstString({txt: value})
408+
| PConstChar({txt: value}) => string(value)
409+
| PConstBool(value) => string(value ? "true" : "false")
410+
| PConstVoid => string("void")
411+
};
372412
};
373413

374414
let print_punnable_pattern =
@@ -565,8 +605,7 @@ let print_pattern = (fmt, {ppat_desc, ppat_loc}) => {
565605
typ.ptyp_loc,
566606
)
567607
++ fmt.print_type(fmt, typ)
568-
| PPatConstant(constant) =>
569-
fmt.print_constant(fmt, ~loc=ppat_loc, constant)
608+
| PPatConstant(constant) => fmt.print_constant(fmt, constant)
570609
| PPatRecord(pats, closed_flag) =>
571610
braces(
572611
indent(
@@ -1300,8 +1339,7 @@ let print_expression =
13001339
++ (
13011340
switch (expr.pexp_desc) {
13021341
| PExpId({txt: ident}) => fmt.print_identifier(fmt, ident)
1303-
| PExpConstant(constant) =>
1304-
fmt.print_constant(fmt, ~loc=expr.pexp_loc, constant)
1342+
| PExpConstant(constant) => fmt.print_constant(fmt, constant)
13051343
| PExpConstruct({txt: ident, loc: ident_loc}, cstr_expr) =>
13061344
fmt.print_identifier(fmt, ident)
13071345
++ (

compiler/src/parsing/ast_helper.re

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ let record_pattern_info = record_pats =>
4141
([], Asttypes.Closed),
4242
);
4343

44+
// This normalizes STRING items in the parsetree that aren't constants so we don't need
45+
// to scatter them throughout the rest of the compiler.
46+
let normalize_string = (~loc, item) => {
47+
switch (Grain_utils.Literals.conv_string(item.txt)) {
48+
| Ok(i) => {loc: item.loc, txt: i}
49+
| Error(msg) => raise(SyntaxError(loc, msg))
50+
};
51+
};
52+
53+
module Number = {
54+
let rational = (numerator, slash, denominator) => {
55+
PConstNumberRational({numerator, slash, denominator});
56+
};
57+
};
58+
4459
module Constant = {
4560
let bytes = b => PConstBytes(b);
4661
let string = s => PConstString(s);
@@ -50,25 +65,18 @@ module Constant = {
5065
let int16 = i => PConstInt16(i);
5166
let int32 = i => PConstInt32(i);
5267
let int64 = i => PConstInt64(i);
53-
let uint8 = (is_neg, i) => PConstUint8(is_neg, i);
54-
let uint16 = (is_neg, i) => PConstUint16(is_neg, i);
55-
let uint32 = (is_neg, i) => PConstUint32(is_neg, i);
56-
let uint64 = (is_neg, i) => PConstUint64(is_neg, i);
68+
let uint8 = i => PConstUint8(i);
69+
let uint16 = i => PConstUint16(i);
70+
let uint32 = i => PConstUint32(i);
71+
let uint64 = i => PConstUint64(i);
5772
let float32 = f => PConstFloat32(f);
5873
let float64 = f => PConstFloat64(f);
5974
let wasmi32 = i => PConstWasmI32(i);
6075
let wasmi64 = i => PConstWasmI64(i);
6176
let wasmf32 = f => PConstWasmF32(f);
6277
let wasmf64 = f => PConstWasmF64(f);
6378
let bigint = i => PConstBigInt(i);
64-
let rational = r => {
65-
let (n, d) =
66-
switch (String.split_on_char('/', r)) {
67-
| [n, d] => (n, d)
68-
| _ => failwith("Impossible: rational literal without forward slash")
69-
};
70-
PConstRational(n, d);
71-
};
79+
let rational = r => PConstRational(r);
7280
let bool = b => PConstBool(b);
7381
let void = PConstVoid;
7482
};
@@ -349,15 +357,20 @@ module Expression = {
349357
);
350358
switch (f, a, b) {
351359
| (
352-
{pexp_desc: PExpId({txt: IdentName({txt: "/"})})},
353-
{pexp_desc: PExpConstant(PConstNumber(PConstNumberInt(x)))},
354-
{pexp_desc: PExpConstant(PConstNumber(PConstNumberInt(y)))},
360+
{pexp_desc: PExpId({txt: IdentName({txt: "/", loc: slash_loc})})},
361+
{
362+
pexp_desc: PExpConstant(PConstNumber(PConstNumberInt(numerator))),
363+
},
364+
{
365+
pexp_desc:
366+
PExpConstant(PConstNumber(PConstNumberInt(denominator))),
367+
},
355368
) =>
356369
constant(
357370
~loc,
358371
~core_loc,
359372
~attributes?,
360-
PConstNumber(PConstNumberRational(x, y)),
373+
Constant.number(Number.rational(numerator, slash_loc, denominator)),
361374
)
362375
| _ =>
363376
mk(
@@ -426,14 +439,18 @@ module Toplevel = {
426439

427440
module PrimitiveDescription = {
428441
let mk = (~loc, ~ident, ~name, ()) => {
429-
{pprim_ident: ident, pprim_name: name, pprim_loc: loc};
442+
{
443+
pprim_ident: ident,
444+
pprim_name: normalize_string(~loc, name),
445+
pprim_loc: loc,
446+
};
430447
};
431448
};
432449

433450
module ValueDescription = {
434451
let mk = (~loc, ~mod_, ~name, ~alias, ~typ, ()) => {
435452
{
436-
pval_mod: mod_,
453+
pval_mod: normalize_string(~loc, mod_),
437454
pval_name: name,
438455
pval_name_alias: alias,
439456
pval_type: typ,
@@ -456,7 +473,11 @@ module MatchBranch = {
456473

457474
module IncludeDeclaration = {
458475
let mk = (~loc, path, alias) => {
459-
{pinc_alias: alias, pinc_path: path, pinc_loc: loc};
476+
{
477+
pinc_alias: alias,
478+
pinc_path: normalize_string(~loc, path),
479+
pinc_loc: loc,
480+
};
460481
};
461482
};
462483

@@ -504,3 +525,13 @@ module ModuleDeclaration = {
504525
{pmod_name: name, pmod_stmts: stmts, pmod_loc: loc};
505526
};
506527
};
528+
529+
module Attribute = {
530+
let mk = (~loc, attr_name, attr_args) => {
531+
{
532+
Asttypes.attr_name,
533+
attr_args: List.map(normalize_string(~loc), attr_args),
534+
attr_loc: loc,
535+
};
536+
};
537+
};

compiler/src/parsing/ast_helper.rei

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,31 @@ type id = loc(Identifier.t);
2626
type str = loc(string);
2727
type loc = Location.t;
2828

29+
module Number: {
30+
let rational: (str, Location.t, str) => number_type;
31+
};
32+
2933
module Constant: {
30-
let bytes: string => constant;
31-
let string: string => constant;
32-
let char: string => constant;
34+
let bytes: str => constant;
35+
let string: str => constant;
36+
let char: str => constant;
3337
let number: number_type => constant;
34-
let int8: string => constant;
35-
let int16: string => constant;
36-
let int32: string => constant;
37-
let int64: string => constant;
38-
let uint8: (bool, string) => constant;
39-
let uint16: (bool, string) => constant;
40-
let uint32: (bool, string) => constant;
41-
let uint64: (bool, string) => constant;
42-
let float32: string => constant;
43-
let float64: string => constant;
44-
let wasmi32: string => constant;
45-
let wasmi64: string => constant;
46-
let wasmf32: string => constant;
47-
let wasmf64: string => constant;
48-
let bigint: string => constant;
49-
let rational: string => constant;
38+
let int8: str => constant;
39+
let int16: str => constant;
40+
let int32: str => constant;
41+
let int64: str => constant;
42+
let uint8: str => constant;
43+
let uint16: str => constant;
44+
let uint32: str => constant;
45+
let uint64: str => constant;
46+
let float32: str => constant;
47+
let float64: str => constant;
48+
let wasmi32: str => constant;
49+
let wasmi64: str => constant;
50+
let wasmf32: str => constant;
51+
let wasmf64: str => constant;
52+
let bigint: str => constant;
53+
let rational: str => constant;
5054
let bool: bool => constant;
5155
let void: constant;
5256
};
@@ -551,3 +555,7 @@ module LambdaArgument: {
551555
module ModuleDeclaration: {
552556
let mk: (~loc: loc, str, list(toplevel_stmt)) => module_declaration;
553557
};
558+
559+
module Attribute: {
560+
let mk: (~loc: loc, str, list(str)) => attribute;
561+
};

0 commit comments

Comments
 (0)