Created
July 17, 2016 17:40
-
-
Save charlesetc/42fdd7295bb364ae3ecd77ecc8a9da01 to your computer and use it in GitHub Desktop.
an edited version of simple_example.ml which shows the formatter break at width 68
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A fairly complete demonstration of the features provided | |
by Easy-format. | |
*/ | |
open Easy_format; | |
let list = { | |
...list, | |
list_style: Some "list", | |
opening_style: Some "op", | |
body_style: Some "body", | |
separator_style: Some "sep", | |
closing_style: Some "cl" | |
}; | |
let atom = {atom_style: Some "atom"}; | |
let label = {...label, label_style: Some "label"}; | |
let tuple_param = { | |
...list, | |
space_after_opening: false, | |
space_before_closing: false, | |
align_closing: false | |
}; | |
let operator_param = { | |
...list, | |
space_after_opening: false, | |
space_before_closing: false, | |
separators_stick_left: false, | |
space_before_separator: true, | |
space_after_separator: true, | |
align_closing: true | |
}; | |
let html_escape_string s => { | |
let buf = Buffer.create (2 * String.length s); | |
for i in 0 to (String.length s - 1) { | |
switch s.[i] { | |
| '&' => Buffer.add_string buf "&" | |
| '<' => Buffer.add_string buf "<" | |
| '>' => Buffer.add_string buf ">" | |
| c => Buffer.add_char buf c | |
} | |
}; | |
Buffer.contents buf | |
}; | |
let html_escape = `Escape_string html_escape_string; | |
let html_style = [ | |
("atom", {tag_open: "<a>", tag_close: "</a>"}), | |
("body", {tag_open: "<lb>", tag_close: "</lb>"}), | |
("list", {tag_open: "<l>", tag_close: "</l>"}), | |
("op", {tag_open: "<op>", tag_close: "</op>"}), | |
("cl", {tag_open: "<cl>", tag_close: "</cl>"}), | |
("sep", {tag_open: "<sep>", tag_close: "</sep>"}), | |
("label", {tag_open: "<la>", tag_close: "</la>"}) | |
]; | |
let format_tuple f l => List ("(", ",", ")", tuple_param) (List.map f l) [@implicit_arity]; | |
let format_int x => Atom (string_of_int x) atom [@implicit_arity]; | |
let format_float x => Atom (Printf.sprintf "%.5f" x) atom [@implicit_arity]; | |
let format_sum wrap::wrap=`Wrap_atoms l => | |
List ("(", "+", ")", {...operator_param, wrap_body: wrap}) (List.map format_int l) | |
[@implicit_arity]; | |
let format_array align_closing::align_closing wrap::wrap f a => { | |
let l = Array.to_list (Array.map f a); | |
List ("[|", ";", "|]", {...list, align_closing, wrap_body: wrap}) l [@implicit_arity] | |
}; | |
let format_matrix | |
align_closing1::align_closing1=true | |
align_closing2::align_closing2=true | |
wrap1::wrap1=`Wrap_atoms | |
wrap2::wrap2=`Wrap_atoms | |
m => | |
format_array | |
align_closing::align_closing1 | |
wrap::wrap1 | |
(format_array align_closing::align_closing2 wrap::wrap2 format_float) | |
m; | |
let format_record f l0 => { | |
let l = | |
List.map | |
(fun (s, x) => Label (Atom (s ^ ":") atom [@implicit_arity], label) (f x) [@implicit_arity]) | |
l0; | |
List ("{", ";", "}", list) l [@implicit_arity] | |
}; | |
let begin_style = ( | |
{...label, indent_after_label: 0}, | |
("begin", ";", "end", {...list, stick_to_label: false}) | |
); | |
let curly_style = (label, ("{", ";", "}", list)); | |
let format_function_definition (body_label, body_param) name param body => | |
Label | |
( | |
Label | |
(Atom ("function " ^ name) atom [@implicit_arity], label) | |
( | |
List | |
("(", ",", ")", tuple_param) (List.map (fun s => Atom s atom [@implicit_arity]) param) | |
[@implicit_arity] | |
) | |
[@implicit_arity], | |
body_label | |
) | |
(List body_param (List.map (fun s => Atom s atom [@implicit_arity]) body) [@implicit_arity]) | |
[@implicit_arity]; | |
/* | |
Illustrate the difference between `Force_break and `Force_breaks_rec on | |
labels. | |
*/ | |
let label_one_atom = Atom "reallyLongLabelOne" atom [@implicit_arity]; | |
let label_two_atom = Atom "reallyLongLabelTwo" atom [@implicit_arity]; | |
let label_three_atom = Atom "reallyLongLabelABC" atom [@implicit_arity]; | |
let make_list_in_labels wrap => | |
Label | |
(label_one_atom, label) | |
( | |
Label | |
(label_two_atom, label) | |
( | |
Label | |
(label_three_atom, label) | |
( | |
List | |
("[", ",", "]", {...list, wrap_body: wrap}) | |
[ | |
Atom "1.23456" atom [@implicit_arity], | |
Atom "9.87654" atom [@implicit_arity], | |
Atom "9.87654" atom [@implicit_arity], | |
Atom "9.87654" atom [@implicit_arity] | |
] | |
[@implicit_arity] | |
) | |
[@implicit_arity] | |
) | |
[@implicit_arity] | |
) | |
[@implicit_arity]; | |
/* | |
Illustrate the difference between `Force_break and `Force_breaks_rec | |
*/ | |
let make_heterogenous_list (container_wrap, wrap) => | |
List | |
("[", ",", "]", {...list, wrap_body: container_wrap}) | |
[ | |
Atom "0" atom [@implicit_arity], | |
List | |
("[", ",", "]", {...list, wrap_body: wrap}) | |
[Atom "1.23456" atom [@implicit_arity], Atom "9.87654" atom [@implicit_arity]] | |
[@implicit_arity], | |
Atom "1" atom [@implicit_arity], | |
Atom "2" atom [@implicit_arity], | |
Atom "3" atom [@implicit_arity] | |
] | |
[@implicit_arity]; | |
let print_margin fmt () => { | |
let margin = Format.pp_get_margin fmt (); | |
for i in 1 to margin { | |
print_char '+' | |
}; | |
print_newline () | |
}; | |
let with_margin html::html=false margin f x => { | |
let fmt = Format.formatter_of_out_channel stdout; | |
Format.pp_set_margin fmt margin; | |
if html { | |
Pretty.define_styles fmt html_escape html_style | |
}; | |
print_margin fmt (); | |
f fmt x; | |
Format.pp_print_flush fmt (); | |
print_newline () | |
}; | |
let print s => Printf.printf "\n*** %s ***\n%!" s; | |
let print_tuple fmt l => Pretty.to_formatter fmt (format_tuple format_int l); | |
let print_heterogenous_list fmt wrap => Pretty.to_formatter fmt (make_heterogenous_list wrap); | |
let print_list_in_labels fmt wrap => Pretty.to_formatter fmt (make_list_in_labels wrap); | |
let print_sum wrap::wrap=? fmt l => Pretty.to_formatter fmt (format_sum wrap::?wrap l); | |
let print_matrix | |
align_closing1::align_closing1=? | |
align_closing2::align_closing2=? | |
wrap1::wrap1=? | |
wrap2::wrap2=? | |
m | |
fmt | |
() => | |
Pretty.to_formatter | |
fmt | |
( | |
format_matrix | |
align_closing1::?align_closing1 | |
align_closing2::?align_closing2 | |
wrap1::?wrap1 | |
wrap2::?wrap2 | |
m | |
); | |
let print_function_definition style name param fmt body => | |
Pretty.to_formatter fmt (format_function_definition style name param body); | |
let () = { | |
let ints = Array.to_list (Array.init 10 (fun i => i)); | |
let program html margin style => | |
for i in 67 to 69 { | |
print_int i; | |
print_string "\n"; | |
with_margin | |
html::html | |
i | |
(print_function_definition begin_style "hello" ["arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg1", "arg2", "arg3"]) | |
["print \"hello\"", "return (1 < 2)"]}; | |
program false 599 begin_style; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment