@@ -135,6 +135,12 @@ function ifnonempty(x, d: Doc): Doc {
135
135
return [ x , d ] ;
136
136
}
137
137
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
+
138
144
function print_longident ( node : AST , options : Options ) : Doc {
139
145
const constructor = node [ 0 ] ;
140
146
const args = node . slice ( 1 ) ;
@@ -189,8 +195,7 @@ function print_constant_desc(node: AST, options: Options): Doc {
189
195
// Suffixes [g-z][G-Z] are accepted by the parser.
190
196
// Suffixes are rejected by the typechecker.
191
197
// *)
192
- niy ( ) ;
193
-
198
+ return args [ 1 ] ? args [ 0 ] . concat ( args [ 1 ] ) : args [ 0 ] ;
194
199
default :
195
200
throw new Error ( `Unexpected node type: ${ constructor } ` ) ;
196
201
}
@@ -556,8 +561,11 @@ function print_pattern_desc(node: AST, options: Options): Doc {
556
561
let cargs = join ( [ ";" , line ] , args [ 1 ] [ 0 ] . map ( sl => print_string_loc ( sl , options ) ) ) ;
557
562
if ( cargs . length > 0 )
558
563
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 ) ;
561
569
}
562
570
else
563
571
return print_longident_loc ( args [ 0 ] , options ) ;
@@ -637,7 +645,9 @@ function print_pattern(node: AST, options: Options): Doc {
637
645
// ppat_loc_stack: location_stack;
638
646
// ppat_attributes: attributes; (** [... [\@id1] [\@id2]] *)
639
647
// }
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 ) ;
641
651
}
642
652
643
653
function print_value_binding ( node : AST , options : Options ) : Doc {
@@ -875,12 +885,14 @@ function print_case(node: AST, options: Options): Doc {
875
885
// pc_guard: expression option;
876
886
// pc_rhs: expression;
877
887
// }
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 ( [
881
892
line , "->" , line ,
882
- print_expression ( node . pc_rhs , options , false ) , line ,
893
+ print_expression ( node . pc_rhs , options , false )
883
894
] ) ;
895
+ return f ( r ) ;
884
896
}
885
897
886
898
function print_binding_op ( node : AST , options : Options ) : Doc {
@@ -1035,7 +1047,7 @@ function print_expression_desc(node: AST, options: Options, need_pars: boolean):
1035
1047
else if ( is_zconst ( obj , children ) || is_qconst ( obj , children ) ) {
1036
1048
// Keep the source formatting so as not to break integer/real constants.
1037
1049
const cloc = children [ 0 ] [ 1 ] . pexp_loc ;
1038
- return get_source ( cloc , cloc , options ) ;
1050
+ return trim_parentheses ( get_source ( cloc , cloc , options ) ) ;
1039
1051
}
1040
1052
else
1041
1053
return f ( [ need_pars ? "(" : "" ,
@@ -1235,7 +1247,7 @@ function print_expression_desc(node: AST, options: Options, need_pars: boolean):
1235
1247
return print_letop ( args [ 0 ] , options ) ;
1236
1248
case "Pexp_extension" :
1237
1249
// | Pexp_extension of extension (** [[%id]] *)
1238
- return f ( [ "%" , print_extension ( args [ 0 ] , options ) ] ) ;
1250
+ return f ( [ "[ %" , print_extension ( args [ 0 ] , options ) , "]" ] ) ;
1239
1251
case "Pexp_unreachable" :
1240
1252
// | Pexp_unreachable (** [.] *)
1241
1253
return "." ;
@@ -1313,11 +1325,10 @@ function print_type_kind(node: AST, options: Options): Doc {
1313
1325
case "Ptype_record" :
1314
1326
// | Ptype_record of label_declaration list (** Invariant: non-empty list *)
1315
1327
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 , "}" ] ) ] ) ;
1321
1332
case "Ptype_open" :
1322
1333
// | Ptype_open
1323
1334
niy ( ) ;
0 commit comments