@@ -6,7 +6,6 @@ const { group, indent, indentIfBreak, dedent, join, ifBreak, breakParent, line,
6
6
7
7
import { iml2json } from './iml2json.bc' ;
8
8
import { assert } from 'node:console' ;
9
- import { DEFAULT_ENCODING } from 'node:crypto' ;
10
9
11
10
export const languages = [
12
11
{
@@ -330,27 +329,31 @@ function print_string_loc(node: AST, options: Options): Doc {
330
329
return node . txt ;
331
330
}
332
331
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
+
333
342
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 ) ) ) ;
345
345
}
346
346
347
+ function has_attribute ( attrs , x ) : boolean {
348
+ return attrs . find ( a => a . attr_name . txt == x ) ;
349
+ }
347
350
348
351
function print_comment ( node : AST , options : Options ) {
349
352
return f ( [ "(*" , indent ( [ get_attr_payload_string ( node ) , "*)" ] ) , hardline ] ) ;
350
353
}
351
354
352
355
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 " ) ;
354
357
return join ( line , filtered . map ( x => print_comment ( x , options ) ) ) ;
355
358
}
356
359
@@ -1376,6 +1379,7 @@ function print_attribute(node: AST, level: number, options: Options): Doc {
1376
1379
case 3 : {
1377
1380
switch ( node . attr_name . txt ) {
1378
1381
case "ocaml.text" : {
1382
+ // Comments are filtered out, so this branch is dead code.
1379
1383
if ( node . attr_payload [ 0 ] = "Pstr" ) {
1380
1384
const str = get_attr_payload_string ( node ) ;
1381
1385
return f ( [ "(**" , indent ( str ) , "*)" ] ) ;
@@ -1404,14 +1408,6 @@ function print_attribute(node: AST, level: number, options: Options): Doc {
1404
1408
return f ( [ "[" , "@" . repeat ( level ) , node . attr_name . txt , ifnonempty ( line , payload ) , "]" ] ) ;
1405
1409
}
1406
1410
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
-
1415
1411
function print_value_description ( node : AST , options : Options ) : Doc {
1416
1412
// {
1417
1413
// pval_name: string loc;
@@ -1463,7 +1459,7 @@ function print_structure_item_desc(node: AST, options: Options): Doc {
1463
1459
let r : Doc [ ] = [ ] ;
1464
1460
const comments = ifnonempty ( line , print_comments ( args [ 1 ] , options ) ) ;
1465
1461
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 ) , ")" ] ;
1467
1463
else
1468
1464
r = [ comments , print_expression ( args [ 0 ] , options , false ) ] ;
1469
1465
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 {
1514
1510
r . push ( "rec" ) ;
1515
1511
}
1516
1512
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 ) ] ) ;
1518
1514
if ( is_instance ) {
1519
1515
// Do this for all lambdas?
1520
- return g ( [
1521
- comments ,
1516
+ return [ comments , g ( [
1522
1517
r , indent ( [ line , "(" , softline , "fun" , line ,
1523
1518
join ( line , pvb . pvb_expr . pexp_desc [ 1 ] . map ( x => print_function_param ( x , options ) ) ) , line ,
1524
1519
"->" , line ,
1525
1520
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 ) ) ] ) ] ;
1527
1522
}
1528
1523
else if ( args [ 1 ] . length > 0 ) {
1529
1524
if ( pvb . pvb_expr . pexp_desc [ 0 ] == "Pexp_function" ) {
1530
1525
// For function definitions we want to hoist the arguments
1531
- return g ( [ comments , r ,
1526
+ return [ comments , g ( [ r ,
1532
1527
f ( [ indent ( [ line , print_pattern ( pvb . pvb_pat , options ) , line ,
1533
1528
join ( line , pvb . pvb_expr . pexp_desc [ 1 ] . map ( x => print_function_param ( x , options ) ) ) ,
1534
1529
] ) ,
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 ) ) , ] ) ] ) ] ) ] ;
1538
1533
}
1539
1534
}
1540
1535
// 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 ) ) ] ) ] ;
1543
1538
case "Pstr_primitive" :
1544
1539
// | Pstr_primitive of value_description
1545
1540
// (** - [val x: T]
@@ -1691,12 +1686,18 @@ function print(path: AstPath<Tree>, options: Options, print
1691
1686
let q = print_toplevel_phrase ( n , options ) ;
1692
1687
if ( false ) {
1693
1688
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
+ }
1696
1694
}
1697
1695
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
+ }
1700
1701
}
1701
1702
console . log ( n ) ;
1702
1703
console . log ( q ) ;
0 commit comments