Skip to content

Commit 45c8c49

Browse files
committed
More, including jests
1 parent 7fe6677 commit 45c8c49

File tree

13 files changed

+57804
-53851
lines changed

13 files changed

+57804
-53851
lines changed

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,21 @@
4646
"${workspaceFolder}/node_modules/out/**"
4747
],
4848
},
49+
{
50+
"name": "Debug imlformat Jests",
51+
"type": "node",
52+
"request": "launch",
53+
"cwd": "${workspaceFolder}/imlformat",
54+
"env": {
55+
"NODE_OPTIONS": "--experimental-vm-modules"
56+
},
57+
"runtimeArgs": [
58+
"--inspect-brk",
59+
"${workspaceRoot}/imlformat/node_modules/.bin/jest",
60+
"--runInBand"
61+
],
62+
"console": "integratedTerminal",
63+
"internalConsoleOptions": "neverOpen"
64+
}
4965
]
5066
}

imlformat/iml-prettier.ts

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const { group, indent, indentIfBreak, dedent, join, ifBreak, breakParent, line,
66

77
import { iml2json } from './iml2json.bc';
88
import { assert } from 'node:console';
9-
import { DEFAULT_ENCODING } from 'node:crypto';
109

1110
export const languages = [
1211
{
@@ -330,27 +329,31 @@ function print_string_loc(node: AST, options: Options): Doc {
330329
return node.txt;
331330
}
332331

332+
let attribute_filter = [
333+
"ocaml.text",
334+
"imandra_verify", "imandra_instance", "imandra_theorem",
335+
"imandra_eval", "imandra_axiom", "imandra_rule_spec"
336+
];
337+
338+
function filter_attributes(attrs) {
339+
return attrs.filter(x => !attribute_filter.find(y => y == x.attr_name.txt));
340+
}
341+
333342
function print_attributes(node: AST, level: number, options: Options): Doc {
334-
let filter = [
335-
"ocaml.comment",
336-
"imandra_verify", "imandra_instance", "imandra_theorem",
337-
"imandra_eval", "imandra_axiom", "imandra_rule_spec"
338-
];
339-
return join(line, node.map(x => {
340-
if (filter.find(y => y == x.attr_name.txt))
341-
return [];
342-
else
343-
return print_attribute(x, level, options);
344-
}));
343+
let filtered = filter_attributes(node);
344+
return join(line, filtered.map(x => print_attribute(x, level, options)));
345345
}
346346

347+
function has_attribute(attrs, x): boolean {
348+
return attrs.find(a => a.attr_name.txt == x);
349+
}
347350

348351
function print_comment(node: AST, options: Options) {
349352
return f(["(*", indent([get_attr_payload_string(node), "*)"]), hardline]);
350353
}
351354

352355
function print_comments(node: AST, options: Options): Doc {
353-
let filtered = node.filter(x => x.attr_name.txt == "ocaml.comment");
356+
let filtered = node.filter(x => x.attr_name.txt == "ocaml.text");
354357
return join(line, filtered.map(x => print_comment(x, options)));
355358
}
356359

@@ -1376,6 +1379,7 @@ function print_attribute(node: AST, level: number, options: Options): Doc {
13761379
case 3: {
13771380
switch (node.attr_name.txt) {
13781381
case "ocaml.text": {
1382+
// Comments are filtered out, so this branch is dead code.
13791383
if (node.attr_payload[0] = "Pstr") {
13801384
const str = get_attr_payload_string(node);
13811385
return f(["(**", indent(str), "*)"]);
@@ -1404,14 +1408,6 @@ function print_attribute(node: AST, level: number, options: Options): Doc {
14041408
return f(["[", "@".repeat(level), node.attr_name.txt, ifnonempty(line, payload), "]"]);
14051409
}
14061410

1407-
function has_attribute(attrs, x): boolean {
1408-
return attrs.find(a => a.attr_name.txt == x);
1409-
}
1410-
1411-
function filter_attributes(attrs) {
1412-
return attrs.filter(a => a.attr_name.txt != "imandra_theorem" && a.attr_name.txt != "imandra_instance");
1413-
}
1414-
14151411
function print_value_description(node: AST, options: Options): Doc {
14161412
// {
14171413
// pval_name: string loc;
@@ -1463,7 +1459,7 @@ function print_structure_item_desc(node: AST, options: Options): Doc {
14631459
let r: Doc[] = [];
14641460
const comments = ifnonempty(line, print_comments(args[1], options));
14651461
if (args.length > 1 && has_attribute(args[1], "imandra_eval"))
1466-
r = [comments, "eval", line, "(", softline, print_expression(args[0], options, false), softline, ")"];
1462+
r = [comments, "eval", line, "(", softline, print_expression(args[0], options, false), ")"];
14671463
else
14681464
r = [comments, print_expression(args[0], options, false)];
14691465
return f([r, ifnonempty(line, print_attributes(args[1], 3, options))]);
@@ -1514,32 +1510,31 @@ function print_structure_item_desc(node: AST, options: Options): Doc {
15141510
r.push("rec");
15151511
}
15161512
attrs = filter_attributes(attrs);
1517-
const comments = ifnonempty(line, print_comments(pvb.pvb_attributes, options));
1513+
const comments = g([print_comments(pvb.pvb_attributes, options)]);
15181514
if (is_instance) {
15191515
// Do this for all lambdas?
1520-
return g([
1521-
comments,
1516+
return [comments, g([
15221517
r, indent([line, "(", softline, "fun", line,
15231518
join(line, pvb.pvb_expr.pexp_desc[1].map(x => print_function_param(x, options))), line,
15241519
"->", line,
15251520
print_function_body(pvb.pvb_expr.pexp_desc[3], options), softline, ")"]),
1526-
ifnonempty(hardline, print_attributes(attrs, 2, options))]);
1521+
ifnonempty(hardline, print_attributes(attrs, 2, options))])];
15271522
}
15281523
else if (args[1].length > 0) {
15291524
if (pvb.pvb_expr.pexp_desc[0] == "Pexp_function") {
15301525
// For function definitions we want to hoist the arguments
1531-
return g([comments, r,
1526+
return [comments, g([r,
15321527
f([indent([line, print_pattern(pvb.pvb_pat, options), line,
15331528
join(line, pvb.pvb_expr.pexp_desc[1].map(x => print_function_param(x, options))),
15341529
]),
1535-
line, "=", line,
1536-
g([indent([print_function_body(pvb.pvb_expr.pexp_desc[3], options)]),
1537-
ifnonempty(hardline, print_attributes(attrs, 2, options)),])])]);
1530+
line, "=",
1531+
g([indent([line, print_function_body(pvb.pvb_expr.pexp_desc[3], options)]),
1532+
ifnonempty(hardline, print_attributes(attrs, 2, options)),])])])];
15381533
}
15391534
}
15401535
// Generic version
1541-
return g([comments, r, indent([line, join([line, "and", line], args[1].map(x => print_value_binding(x, options)))]),
1542-
ifnonempty(hardline, print_attributes(attrs, 2, options))]);
1536+
return [comments, f([r, indent([line, join([line, "and", line], args[1].map(x => print_value_binding(x, options)))]),
1537+
ifnonempty(hardline, print_attributes(attrs, 2, options))])];
15431538
case "Pstr_primitive":
15441539
// | Pstr_primitive of value_description
15451540
// (** - [val x: T]
@@ -1691,12 +1686,18 @@ function print(path: AstPath<Tree>, options: Options, print
16911686
let q = print_toplevel_phrase(n, options);
16921687
if (false) {
16931688
if (n[0] == "Ptop_def") {
1694-
let src = get_source(n[1][0].pstr_loc, n[1][0].pstr_loc, options);
1695-
console.log(src);
1689+
let obj: any = n[1][0];
1690+
if (obj.hasOwnProperty("pstr_loc")) {
1691+
let src = get_source(obj.pstr_loc, obj.pstr_loc, options);
1692+
console.log(src);
1693+
}
16961694
}
16971695
else {
1698-
let src = get_source(n[1].pdir_loc, n[1].pdir_loc, options);
1699-
console.log(src);
1696+
let obj: any = n[1];
1697+
if (obj.hasOwnProperty("pdir_loc")) {
1698+
let src = get_source(obj.pdir_loc, obj.pdir_loc, options);
1699+
console.log(src);
1700+
}
17001701
}
17011702
console.log(n);
17021703
console.log(q);

imlformat/iml2json.bc.js

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

imlformat/imlformat.format.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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';
6+
7+
export async function format(text : string): Promise<string> {
8+
try {
9+
let formatted = await prettier.format(text, {
10+
semi: false,
11+
parser: "iml-parse",
12+
plugins: [iml_prettier],
13+
});
14+
return formatted;
15+
}
16+
catch (e: any) {
17+
console.log("Prettier error: " + e.toString() + "\n" + e.stack.toString());
18+
}
19+
}
20+
21+
export async function format_file(filename: string): Promise<string> {
22+
const stats = fs.statSync(filename);
23+
const buffer = Buffer.allocUnsafe(stats.size);
24+
const fd = fs.openSync(filename, "r");
25+
fs.readSync(fd, buffer, 0, buffer.length, 0);
26+
return await format(buffer.toString());
27+
}

imlformat/imlformat.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Buffer } from 'node:buffer';
33

44
import * as prettier from 'prettier';
55
import * as iml_prettier from './iml-prettier';
6+
import * as fmt from './imlformat.format';
67

78
// Run me like so:
89
// npm install
@@ -12,28 +13,10 @@ import * as iml_prettier from './iml-prettier';
1213
// deno compile --unstable-sloppy-imports --no-check -A -o runme imlformat.ts
1314

1415
(async function main() {
15-
if (process.argv.length <= 2) {
16-
console.log("Usage: npx ts-node imlformat.ts <filename>");
17-
}
18-
else {
19-
const filename = process.argv[2];
20-
const stats = fs.statSync(filename);
21-
const buffer = Buffer.allocUnsafe(stats.size);
22-
const fd = fs.openSync(filename, "r");
23-
fs.readSync(fd, buffer, 0, buffer.length, 0);
24-
25-
let formatted = "";
26-
27-
try {
28-
formatted = await prettier.format(buffer.toString(), {
29-
semi: false,
30-
parser: "iml-parse",
31-
plugins: [iml_prettier],
32-
});
33-
console.log(formatted);
34-
}
35-
catch (e: any) {
36-
console.log("Prettier error: " + e.toString() + "\n" + e.stack.toString());
37-
}
38-
}
16+
if (process.argv.length <= 2)
17+
console.log("Usage: npx ts-node imlformat.ts <filename>");
18+
else {
19+
let r = await fmt.format_file(process.argv[2]);
20+
console.log(r);
21+
}
3922
})();

imlformat/jest.config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type {Config} from '@jest/types';
2+
3+
// Sync object
4+
5+
const config: Config.InitialOptions = {
6+
verbose: true,
7+
transform: {
8+
'^.+\\.tsx?$': 'ts-jest',
9+
},
10+
};
11+
12+
export default config;

0 commit comments

Comments
 (0)