Skip to content

Commit

Permalink
Merge branch 'master' of github.com:realworldocaml/mdx into jupyter
Browse files Browse the repository at this point in the history
  • Loading branch information
avsm committed Sep 13, 2019
2 parents 3155e5c + ddc6c1e commit 6e2b449
Show file tree
Hide file tree
Showing 25 changed files with 424 additions and 460 deletions.
22 changes: 12 additions & 10 deletions bin/pp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ let run () file section =
| Mdx.Section _ | Text _ -> ()
| Block b ->
let b = Mdx.Block.eval b in
Log.debug (fun l -> l "pp: %a" Mdx.Block.dump b);
let pp_lines = Fmt.(list ~sep:(unit "\n") string) in
let contents = Mdx.Block.executable_contents b in
match b.value with
| Toplevel _ -> Fmt.pr "%a\n" pp_lines contents
| OCaml ->
Fmt.pr "%a\n%a\n"
Mdx.Block.pp_line_directive (file, b.line)
pp_lines contents
| _ -> ()
if not (Mdx.Block.skip b) then (
Log.debug (fun l -> l "pp: %a" Mdx.Block.dump b);
let pp_lines = Fmt.(list ~sep:(unit "\n") string) in
let contents = Mdx.Block.executable_contents b in
match b.value with
| Toplevel _ -> Fmt.pr "%a\n" pp_lines contents
| OCaml ->
Fmt.pr "%a\n%a\n"
Mdx.Block.pp_line_directive (file, b.line)
pp_lines contents
| _ -> ()
)
) t;
0

Expand Down
47 changes: 25 additions & 22 deletions bin/rule.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let prepend_root r b = match r with
| None -> b
| Some r -> Filename.concat r b

let print_rule ~nd ~prelude ~md_file ~ml_files ~dirs ~root options =
let print_rule ~nd ~prelude ~md_file ~ml_files ~dirs ~root ~requires options =
let ml_files = String.Set.elements ml_files in
let ml_files = List.map (prepend_root root) ml_files in
let dirs = match root with
Expand All @@ -44,6 +44,10 @@ let print_rule ~nd ~prelude ~md_file ~ml_files ~dirs ~root options =
let f (cpt, acc) _ = cpt + 1, ("y" ^ string_of_int cpt) :: acc in
List.fold_left f (0, []) ml_files |> snd
in
let requires = String.Set.elements requires in
let pp_package_deps fmt name =
Fmt.pf fmt "\ (package %s)" name
in
let pp_ml_deps fmt (var_name, ml_file) =
Fmt.pf fmt "\ (:%s %s)" var_name ml_file
in
Expand All @@ -57,30 +61,24 @@ let print_rule ~nd ~prelude ~md_file ~ml_files ~dirs ~root options =
let files = String.Set.of_list (List.map prelude_file prelude) in
String.Set.elements files
|> List.map (fun f -> Fmt.strf " %s" f)
|> String.concat ~sep:"\n"
in
let root = match root with None -> "" | Some r -> Fmt.strf "--root=%s " r in
let deps =
let sep1 = if var_names <> [] then "\n" else "" in
let sep2 = if dirs <> [] && prelude <> "" then "\n" else "" in
let x = Fmt.strf "%a%s%a%s%s"
Fmt.(list ~sep:(unit "\n") pp_ml_deps) (List.combine var_names ml_files)
sep1
Fmt.(list ~sep:(unit "\n") pp_dir_deps) dirs
sep2
prelude
in
match x with
| "" -> ""
| s -> "\n" ^ s
match
List.map (Fmt.to_to_string pp_package_deps) requires @
List.map (Fmt.to_to_string pp_ml_deps) (List.combine var_names ml_files) @
List.map (Fmt.to_to_string pp_dir_deps) dirs @
prelude
with
| [] -> ""
| s -> String.concat ~sep:"\n" ("" :: s)
in
let pp name arg =
Fmt.pr
"\
(alias\n\
\ (name %s)\n\
\ (deps (:x %s)\n\
\ (package mdx)%s)\n\
\ (deps (:x %s)%s)\n\
\ (action (progn\n\
\ (run ocaml-mdx test %a %s%s%%{x})\n%a\n\
\ (diff? %%{x} %%{x}.corrected))))\n"
Expand All @@ -100,7 +98,7 @@ let pp_direction fmt = function
| `To_ml -> Fmt.pf fmt "--direction=to-ml"

let pp_prelude fmt s = Fmt.pf fmt "--prelude=%s" s
let pp_prelude_str fmt s = Fmt.pf fmt "--prelude-str=%S" s
let pp_prelude_str fmt s = Fmt.pf fmt "--prelude-str %S" s

let add_opt e s = match e with None -> s | Some e -> String.Set.add e s

Expand All @@ -117,7 +115,11 @@ let run () md_file section direction prelude prelude_str root =
let on_item acc = function
| Mdx.Section _ | Text _ -> acc
| Block b when active b ->
let files, dirs, nd = acc in
let files, dirs, nd, requires = acc in
let requires =
Mdx.Block.required_packages b
|> List.fold_left (fun s e -> String.Set.add e s) requires
in
let nd = nd || match Mdx.Block.mode b with
| `Non_det _ -> true
| _ -> false
Expand All @@ -129,19 +131,20 @@ let run () md_file section direction prelude prelude_str root =
|> String.Set.union source_trees
in
let files = add_opt (Mdx.Block.file b) files in
files, dirs, nd
files, dirs, nd, requires
| Block _ -> acc
in
let on_file file_contents items =
let ml_files, dirs, nd =
List.fold_left on_item (String.Set.empty, String.Set.empty, false) items
let ml_files, dirs, nd, requires =
let empty = String.Set.empty in
List.fold_left on_item (empty, empty, false, empty) items
in
let options =
List.map (Fmt.to_to_string pp_prelude) prelude @
List.map (Fmt.to_to_string pp_prelude_str) prelude_str @
[Fmt.to_to_string pp_direction direction]
in
print_rule ~md_file ~prelude ~nd ~ml_files ~dirs ~root options;
print_rule ~md_file ~prelude ~nd ~ml_files ~dirs ~root ~requires options;
file_contents
in
Mdx.run md_file ~f:on_file;
Expand Down
11 changes: 1 addition & 10 deletions bin/test/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,6 @@ let run_toplevel_tests ?root c ppf tests t =
) tests;
Block.pp_footer ppf ()

let trim l =
let rec aux = function
| [] -> []
| h::t -> if String.trim h = "" then aux t else String.trim h :: t
in
let no_head = aux l in
let no_tail = List.rev (aux (List.rev no_head)) in
no_tail

type file = { first: Mdx_top.Part.file; current: Mdx_top.Part.file }

let files: (string, file) Hashtbl.t = Hashtbl.create 8
Expand Down Expand Up @@ -228,8 +219,8 @@ let update_block_with_file ppf t file part =
(match part with None -> "" | Some p -> p)
file
| Some lines ->
let lines = trim lines in
let contents = String.concat "\n" lines in
let contents = String.trim contents in
Output.pp ppf (`Output contents);
Block.pp_footer ppf ()

Expand Down
10 changes: 10 additions & 0 deletions lib/block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ let labels = [
`Label "skip" , [`None];
`Label "non-deterministic", [`None; `Some "command"; `Some "output"];
`Label "version" , [`Any];
`Label "require-package" , [`Any];
`Prefix "set-" , [`Any];
`Prefix "unset-" , [`None];
]
Expand Down Expand Up @@ -290,6 +291,15 @@ let unset_variables t =
in
List.map f (get_prefixed_labels t "unset-")

let required_packages t =
let f = function
| `Eq, "" ->
Fmt.failwith "invalid `require-package` label value: requires a value"
| `Eq, pkg -> pkg
| _ -> Fmt.failwith "invalid `require-package` label value"
in
List.map f (get_labels t "require-package")

let value t = t.value
let section t = t.section
let header t = t.header
Expand Down
3 changes: 3 additions & 0 deletions lib/block.mli
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ val set_variables: t -> (string * string) list
val unset_variables: t -> string list
(** [unset_variable t] is the list of environment variable to unset *)

val required_packages: t -> string list
(** [required_packages t] is the names of the required packages *)

val skip: t -> bool
(** [skip t] is true iff [skip] is in the labels of [t]. *)

Expand Down
1 change: 1 addition & 0 deletions lib/top/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(library
(name mdx_top)
(public_name mdx.top)
(modes byte)
(preprocess (action (run %{bin:cppo} -V OCAML:%{ocaml_version} %{input-file})))
(libraries unix mdx compiler-libs compiler-libs.toplevel threads findlib.top))
Loading

0 comments on commit 6e2b449

Please sign in to comment.