Skip to content

Commit 234ffba

Browse files
committed
More
1 parent 45c8c49 commit 234ffba

File tree

8 files changed

+416
-365
lines changed

8 files changed

+416
-365
lines changed

imlformat/.prettierrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"printWidth": 80,
3+
"tabWidth": 2,
4+
"tabs": false,
5+
"semi": false,
6+
"singleQuote": false
7+
}

imlformat/iml-prettier.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ function ifnonempty(x, d: Doc): Doc {
135135
return [x, d];
136136
}
137137

138+
function trim_parentheses(s: string): string {
139+
while (s[0] == "(" && s[s.length - 1] == ")")
140+
s = s.slice(1, s.length - 1);
141+
return s;
142+
}
143+
138144
function print_longident(node: AST, options: Options): Doc {
139145
const constructor = node[0];
140146
const args = node.slice(1);
@@ -189,8 +195,7 @@ function print_constant_desc(node: AST, options: Options): Doc {
189195
// Suffixes [g-z][G-Z] are accepted by the parser.
190196
// Suffixes are rejected by the typechecker.
191197
// *)
192-
niy();
193-
198+
return args[1] ? args[0].concat(args[1]) : args[0];
194199
default:
195200
throw new Error(`Unexpected node type: ${constructor}`);
196201
}
@@ -556,8 +561,11 @@ function print_pattern_desc(node: AST, options: Options): Doc {
556561
let cargs = join([";", line], args[1][0].map(sl => print_string_loc(sl, options)));
557562
if (cargs.length > 0)
558563
cargs = ["(type", line, cargs, softline, ")"];
559-
return f([print_longident_loc(args[0], options), line,
560-
print_pattern(args[1][1], options), line, cargs]);
564+
let r = [print_longident_loc(args[0], options), line,
565+
print_pattern(args[1][1], options)];
566+
if (cargs && cargs.length > 0)
567+
r = r.concat([line, cargs]);
568+
return f(r);
561569
}
562570
else
563571
return print_longident_loc(args[0], options);
@@ -637,7 +645,9 @@ function print_pattern(node: AST, options: Options): Doc {
637645
// ppat_loc_stack: location_stack;
638646
// ppat_attributes: attributes; (** [... [\@id1] [\@id2]] *)
639647
// }
640-
return f([print_pattern_desc(node.ppat_desc, options), ifnonempty(line, print_attributes(node.ppat_attributes, 1, options))]);
648+
let r: Doc[] = [print_pattern_desc(node.ppat_desc, options)];
649+
r = r.concat(ifnonempty(line, print_attributes(node.ppat_attributes, 1, options)));
650+
return f(r);
641651
}
642652

643653
function print_value_binding(node: AST, options: Options): Doc {
@@ -875,12 +885,14 @@ function print_case(node: AST, options: Options): Doc {
875885
// pc_guard: expression option;
876886
// pc_rhs: expression;
877887
// }
878-
return f([
879-
print_pattern(node.pc_lhs, options), line,
880-
node.pc_guard ? ["when", line, print_pattern(node.pc_guard, options)] : [],
888+
let r = [print_pattern(node.pc_lhs, options)];
889+
if (node.pc_guard)
890+
r = r.concat(["when", line, print_pattern(node.pc_guard, options)]);
891+
r = r.concat([
881892
line, "->", line,
882-
print_expression(node.pc_rhs, options, false), line,
893+
print_expression(node.pc_rhs, options, false)
883894
]);
895+
return f(r);
884896
}
885897

886898
function print_binding_op(node: AST, options: Options): Doc {
@@ -1035,7 +1047,7 @@ function print_expression_desc(node: AST, options: Options, need_pars: boolean):
10351047
else if (is_zconst(obj, children) || is_qconst(obj, children)) {
10361048
// Keep the source formatting so as not to break integer/real constants.
10371049
const cloc = children[0][1].pexp_loc;
1038-
return get_source(cloc, cloc, options);
1050+
return trim_parentheses(get_source(cloc, cloc, options));
10391051
}
10401052
else
10411053
return f([need_pars ? "(" : "",
@@ -1235,7 +1247,7 @@ function print_expression_desc(node: AST, options: Options, need_pars: boolean):
12351247
return print_letop(args[0], options);
12361248
case "Pexp_extension":
12371249
// | Pexp_extension of extension (** [[%id]] *)
1238-
return f(["%", print_extension(args[0], options)]);
1250+
return f(["[%", print_extension(args[0], options), "]"]);
12391251
case "Pexp_unreachable":
12401252
// | Pexp_unreachable (** [.] *)
12411253
return ".";
@@ -1313,11 +1325,10 @@ function print_type_kind(node: AST, options: Options): Doc {
13131325
case "Ptype_record":
13141326
// | Ptype_record of label_declaration list (** Invariant: non-empty list *)
13151327
return f(["{",
1316-
indent([line, join([";", line], args[0].map(x => {
1317-
return f([x.pld_name.txt, line, ":", line, print_label_declaration(x, options)]);
1318-
})),
1319-
";", line,
1320-
"}"])]);
1328+
indent([line, join([";", line], args[0].map(x =>
1329+
f([x.pld_name.txt, line, ":", line, print_label_declaration(x, options)])
1330+
)),
1331+
";", line, "}"])]);
13211332
case "Ptype_open":
13221333
// | Ptype_open
13231334
niy();

imlformat/iml2json.bc.js

Lines changed: 335 additions & 335 deletions
Large diffs are not rendered by default.

imlformat/imlformat.format.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ import { Buffer } from 'node:buffer';
44
import * as prettier from 'prettier';
55
import * as iml_prettier from './iml-prettier';
66

7-
export async function format(text : string): Promise<string> {
7+
export async function format(text: string): Promise<string> {
88
try {
9-
let formatted = await prettier.format(text, {
10-
semi: false,
11-
parser: "iml-parse",
12-
plugins: [iml_prettier],
13-
});
14-
return formatted;
9+
const config_file = await prettier.resolveConfigFile();
10+
let options: prettier.Options | null = null;
11+
if (config_file)
12+
options = await prettier.resolveConfig(config_file);
13+
else {
14+
options = {
15+
semi: false
16+
}
17+
}
18+
options.parser = "iml-parse";
19+
options.plugins = [iml_prettier];
20+
return await prettier.format(text, options);
1521
}
1622
catch (e: any) {
1723
console.log("Prettier error: " + e.toString() + "\n" + e.stack.toString());

imlformat/imlformat.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
import * as fs from 'node:fs';
2-
import { Buffer } from 'node:buffer';
3-
4-
import * as prettier from 'prettier';
5-
import * as iml_prettier from './iml-prettier';
61
import * as fmt from './imlformat.format';
72

83
// Run me like so:

imlformat/test/comments.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ let f = 1
2020
expect(x).toEqual(`\
2121
let f = 1
2222
(** This is a docstring *)`))
23-
})
23+
})

imlformat/test/demo.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let
66
let
77
g y =
88
(y *.
9-
1.23)
9+
1.23e-4g +. 3.14)
1010

1111
theorem
1212
f_gt x

imlformat/test/theorems.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { expect, test } from '@jest/globals';
2+
3+
import { format } from "../imlformat.format";
4+
5+
test("theorem 1", () => {
6+
format(`
7+
let f x = (x) + (1)
8+
9+
theorem
10+
thm1 x y z
11+
= ((f x ) >
12+
x)
13+
&& ((f y ) > y)
14+
&& ((f z ) > z)
15+
[@@timeout 3600]
16+
[@@disable f] [@@by [%expand "f"] @> auto]
17+
[@@by
18+
some
19+
other
20+
tactic]
21+
22+
`).then(x =>
23+
expect(x).toEqual(`\
24+
let f x = x + 1
25+
26+
theorem thm1 x y z =
27+
((f x) > x) && (((f y) > y) && ((f z) > z))
28+
[@@timeout 3600]
29+
[@@disable f]
30+
[@@by [%expand "f"] @> auto]
31+
[@@by some other tactic]`))
32+
})

0 commit comments

Comments
 (0)