Commits

Paweł Wieczorek committed 3c868eb

minor improvements

  • Participants
  • Parent commits fbbb333

Comments (0)

Files changed (4)

 author          = "Pawel Wieczorek"
 name            = "stringpainter"
-version         = "0.1"
+version         = "0.2"
 description     = "Pretty printing combinators, designed for printing ASTs."
 
 archive(native) = "StringPainter.cmxa"
 install: lib
 	${OCAMLFIND} install stringpainter META ${LIB_FILES}
 
-uninstall:
+remove deinstall uninstall:
 	${OCAMLFIND} remove stringpainter
 
 doc:

File src/Prioritized.ml

 
 let psp_nested prio (xs : painter list) : painter = (prio, Basic.sp_nested (List.map get_painter xs))
 
+let psp_group_with_priority prio (xs : painter list) : painter = (prio, Basic.sp_nested (List.map get_painter xs))
+
+let psp_group = psp_group_with_priority 0
+
+let psp_indent_group_with_priority prio xs = (prio, Basic.sp_indent (List.map get_painter xs))
+
+let psp_indent_group = psp_indent_group_with_priority 0
+
 let psp_indent prio xs = (prio, Basic.sp_indent (List.map get_painter xs))
 
+let psp_indent_with_priority' prio x = psp_indent_group_with_priority prio [x]
+
+let psp_indent' = psp_indent_with_priority' 0
+
+
 let psp_reprioritize n (_, sp) = (n, sp)
 
 let psp_max_priority = pred max_int / 2
 
+let psp_min_priority = pred min_int / 2
+
 let is_multiline    = unhask_sp0 Basic.is_multiline
 
 let any_multiline   = List.exists is_multiline
  * 
  *)
 
+let psp_indent_when_multiline prio psp =
+    if any_multiline psp
+    then psp_indent prio psp
+    else psp_nested prio psp
+
+(*--------------------------------------------------------------------------------------------------------------------
+ * correct_priority
+ *)
+
+let psp_correct_priority_after f_prio f x = 
+    psp_reprioritize (f_prio x) (f x)
+
+
+(*--------------------------------------------------------------------------------------------------------------------
+ * 
+ *)
+
 let psp_associative_infix f_left f_right prio oper left right =
     let prio_left  = get_priority left in
     let prio_right = get_priority right in 
     | RightAssociative -> psp_right_associative_infix 
     | NoAssociative    -> psp_no_associative_infix 
 
+let psp_gen_infix f_assoc f_prio f_paint oper =
+    psp_infix (f_assoc oper) (f_prio oper) (f_paint oper)
+
 (*--------------------------------------------------------------------------------------------------------------------
  * Output
  *)

File src/Prioritized.mli

 (** Is multiline? *)
 val is_multiline : painter -> bool
 
+(** Check if any of painter is multiline. *)
 val any_multiline : painter list -> bool
 
 (** Returns priority of given painter. *)
 (** Returns basic painter from prioritized one. *)
 val get_painter :  painter -> Basic.painter
 
-(** Combine list of painters priorityo one painter with given priority. *)
+(** Group list of painters as one painter with given priority. (deprecated) *)
 val psp_nested : priority -> painter list -> painter
 
+(** Group list of painters as one painter with given priority. *)
+val psp_group_with_priority : priority -> painter list -> painter
+
+(** Group list of painters as one painter with the [psp_min_priority] priority. *)
+val psp_group: painter list -> painter
+
+(** deprecated *)
 val psp_indent : priority->  painter list -> painter
 
+val psp_indent_with_priority' : priority->  painter -> painter
+
+val psp_indent' : painter -> painter
+
+(** Group and indent list of painters as one painter with given priority. *)
+val psp_indent_group_with_priority : priority->  painter list -> painter
+
+(** Group and indent list of painters as one painter with the [psp_min_priority] priority. *)
+val psp_indent_group : painter list -> painter
+
 (** Return painter with different priority. *)
 val psp_reprioritize : priority -> painter -> painter
 
+(**
+    Align priority of obtained painter. 
+
+    {[ psp_correct_priority_after f_prio f t = psp_reprioritize (f_prio t) (f t) ]}
+
+    With this helper a function in similar shape to defined below
+
+    {[
+    let rec psp_expr t = match t with
+        | SomeNode(...) -> psp_group_with_priority (expr_priority t)
+            [ ...
+            ]
+
+    ]}
+
+    can be rewritten to form where priority of constructed painter is set in one place:
+
+    {[
+
+    let rec _psp_expr = function
+        | SomeNode(...) -> psp_group
+            [ ...
+            ]
+
+    and psp_expr = psp_correct_priority_after expr_priority _psp_expr
+
+    ]}
+
+
+*)
+val psp_correct_priority_after : ('a -> priority) -> ('a -> painter) -> 'a -> painter
+
+
 (** Maximal priority. *)
 val psp_max_priority : priority
 
+(** Minimal priority. *)
+val psp_min_priority : priority
+
 (** Breaks not empty line. *)
 val psp_break : painter
 
 (** Paints a word. *)
 val psp_word : string -> painter
 
+(** Paints a label. *)
 val psp_label : string -> painter
 
+(** Paints a special. *)
 val psp_special : string -> painter
 
 (** Paints a keyword. *)
  *)
 val psp_infix : associativity -> priority -> painter -> painter -> painter -> painter
 
+
+(**
+   Practical helper.
+
+   {[
+    let psp_gen_infix f_assoc f_prio f_oper f_paint oper = psp_infix (f_assoc oper) (f_prio oper) (f_paint oper)
+   ]}
+
+ *)
+val psp_gen_infix : ('a -> associativity) -> ('a -> priority) -> ('a -> painter)
+                 -> 'a -> painter -> painter -> painter
+
 (**
    Prints on standard output with newline.
  *)