Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

annotation filters with cli arguments #418

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions atd/src/annot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,16 @@ let validate schema root =
List.iter (fun sec ->
validate_section sec root
) schema

let filter_by_tags ~tags full_module =
let aux (annot : t) =
annot |> List.filter (fun (section, _) -> (
match fields annot ~section ~field:"tag" with
| [] -> true
| fields ->
fields
|> List.filter_map snd
|> List.exists (fun value -> List.mem value tags)
))
in
Ast.map_all_annot aux full_module
7 changes: 7 additions & 0 deletions atd/src/annot.mli
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,10 @@ type schema = schema_section list
(** Check that annotations of interest are not misplaced.
Raises an exception with an error message when the check fails. *)
val validate : schema -> Ast.any -> unit

(**
Remove annotations which do not have at least one of the provided [tags] found in their tag field.
*)
val filter_by_tags :
tags : string list ->
Ast.full_module -> Ast.full_module
6 changes: 6 additions & 0 deletions atd/src/json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ let annot_schema_json : Annot.schema = [
Variant, "name";
Field, "name";
Field, "tag_field";
Module_head, "tag";
Type_def, "tag";
Type_expr, "tag";
Variant, "tag";
Cell, "tag";
Field, "tag";
]
};
{
Expand Down
17 changes: 11 additions & 6 deletions atd/src/util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let read_lexbuf
?(inherit_variants = false)
?(pos_fname = "")
?(pos_lnum = 1)
~tags
lexbuf =

Lexer.init_fname lexbuf pos_fname pos_lnum;
Expand All @@ -30,12 +31,16 @@ let read_lexbuf
| Some schema ->
Annot.validate schema (Ast.Full_module full_module)
);
let full_module = match tags with
| [] -> full_module
| tags -> Annot.filter_by_tags ~tags full_module
in
(full_module, original_types)

let read_channel
?annot_schema ?expand ?keep_builtins ?keep_poly ?xdebug
?inherit_fields ?inherit_variants
?pos_fname ?pos_lnum
?pos_fname ?pos_lnum ~tags
ic =
let lexbuf = Lexing.from_channel ic in
let pos_fname =
Expand All @@ -45,12 +50,12 @@ let read_channel
pos_fname
in
read_lexbuf ?annot_schema ?expand ?keep_builtins ?keep_poly ?xdebug
?inherit_fields ?inherit_variants ?pos_fname ?pos_lnum lexbuf
?inherit_fields ?inherit_variants ?pos_fname ?pos_lnum ~tags lexbuf

let load_file
?annot_schema ?expand ?keep_builtins ?keep_poly ?xdebug
?inherit_fields ?inherit_variants
?pos_fname ?pos_lnum
?pos_fname ?pos_lnum ~tags
file =
let ic = open_in file in
let finally () = close_in_noerr ic in
Expand All @@ -63,7 +68,7 @@ let load_file
let ast =
read_channel
?annot_schema ?expand ?keep_builtins ?keep_poly ?xdebug
?inherit_fields ?inherit_variants ?pos_fname ?pos_lnum ic
?inherit_fields ?inherit_variants ?pos_fname ?pos_lnum ~tags ic
in
finally ();
ast
Expand All @@ -74,11 +79,11 @@ let load_file
let load_string
?annot_schema ?expand ?keep_builtins ?keep_poly ?xdebug
?inherit_fields ?inherit_variants
?pos_fname ?pos_lnum
?pos_fname ?pos_lnum ~tags
s =
let lexbuf = Lexing.from_string s in
read_lexbuf ?annot_schema ?expand ?keep_builtins ?keep_poly ?xdebug
?inherit_fields ?inherit_variants ?pos_fname ?pos_lnum lexbuf
?inherit_fields ?inherit_variants ?pos_fname ?pos_lnum ~tags lexbuf

module Tsort = Sort.Make (
struct
Expand Down
4 changes: 4 additions & 0 deletions atd/src/util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ val read_lexbuf :
?inherit_variants:bool ->
?pos_fname:string ->
?pos_lnum:int ->
tags:string list ->
Lexing.lexbuf -> Ast.full_module * Expand.original_types
(** Read an ATD file from a lexbuf. See also [read_channel], [load_file]
and [load_string].
Expand Down Expand Up @@ -66,6 +67,7 @@ val read_channel :
?inherit_variants:bool ->
?pos_fname:string ->
?pos_lnum:int ->
tags:string list ->
in_channel -> Ast.full_module * Expand.original_types
(** Read an ATD file from an [in_channel]. Options: see [read_lexbuf].
The default [pos_fname] is set to ["<stdin>"] when appropriate. *)
Expand All @@ -80,6 +82,7 @@ val load_file :
?inherit_variants:bool ->
?pos_fname:string ->
?pos_lnum:int ->
tags:string list ->
string -> Ast.full_module * Expand.original_types
(** Read an ATD file. Options: see [read_lexbuf].
The default [pos_fname] is the given input file name. *)
Expand All @@ -94,6 +97,7 @@ val load_string :
?inherit_variants:bool ->
?pos_fname:string ->
?pos_lnum:int ->
tags:string list ->
string -> Ast.full_module * Expand.original_types
(** Read ATD data from a string. Options: see [read_lexbuf]. *)

Expand Down
4 changes: 2 additions & 2 deletions atd/test/annot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ let schema : Atd.Annot.schema = [
]

let test_valid_input atd_input =
let root, _orig_types = Atd.Util.load_string atd_input in
let root, _orig_types = Atd.Util.load_string ~tags:[] atd_input in
Atd.Annot.validate schema (Atd.Ast.Full_module root)

let test_invalid_input atd_input =
let root, _orig_types = Atd.Util.load_string atd_input in
let root, _orig_types = Atd.Util.load_string ~tags:[] atd_input in
try
Atd.Annot.validate schema (Atd.Ast.Full_module root);
assert false
Expand Down
2 changes: 1 addition & 1 deletion atdcat/src/atdcat.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let parse
fun file ->
let m, _orig_defs =
Atd.Util.load_file ~annot_schema ~expand ~keep_poly ~xdebug
~inherit_fields ~inherit_variants file
~inherit_fields ~inherit_variants ~tags:[] file
in
if remove_wraps then
Atd.Ast.remove_wrap_constructs m
Expand Down
14 changes: 12 additions & 2 deletions atdcpp/src/bin/Atdcpp_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ open Cmdliner

type conf = {
input_files: string list;
tags: string list;
version: bool;
}

Expand All @@ -18,7 +19,7 @@ let run conf =
else
conf.input_files
|> List.iter (fun atd_file ->
Atdcpp.Codegen.run_file atd_file
Atdcpp.Codegen.run_file ~tags:conf.tags atd_file
)

(***************************************************************************)
Expand All @@ -38,6 +39,13 @@ let input_files_term =
let default = [] in
Arg.value (Arg.pos_all Arg.file default info)

let tags_term =
let info =
Arg.info ["tag"]
~doc:"Only evaluate annotations which have either the provided TAG as a field value <... tag=TAG>, or have no tags specified.\nOption can be used multiple times to specify several tags"
in
Arg.value (Arg.opt_all Arg.string [] info)

let version_term =
let info =
Arg.info ["version"]
Expand Down Expand Up @@ -94,14 +102,16 @@ type bar = [
]

let cmdline_term run =
let combine input_files version =
let combine input_files tags version =
run {
input_files;
version;
tags;
}
in
Term.(const combine
$ input_files_term
$ tags_term
$ version_term
)

Expand Down
9 changes: 8 additions & 1 deletion atdcpp/src/lib/Codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ let annot_schema_cpp : Atd.Annot.schema_section =
Field, "default";
Module_head, "include";
Module_head, "namespace";
Module_head, "tag";
Type_def, "tag";
Type_expr, "tag";
Variant, "tag";
Cell, "tag";
Field, "tag";
]
}

Expand Down Expand Up @@ -1576,7 +1582,7 @@ let to_cpp_file ~atd_filename (items : A.module_body) dst_path namespace_name =
|> double_spaced
|> Indent.to_file ~indent:4 dst_path

let run_file src_path =
let run_file ~tags src_path =
let src_name = Filename.basename src_path in
let dst_name =
(if Filename.check_suffix src_name ".atd" then
Expand All @@ -1594,6 +1600,7 @@ let run_file src_path =
~keep_builtins:true
~inherit_fields:true
~inherit_variants:true
~tags
src_path
in
let full_module = Atd.Ast.use_only_specific_variants full_module in
Expand Down
2 changes: 1 addition & 1 deletion atdcpp/src/lib/Codegen.mli
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

(** Take ATD type definitions and translate them to C++, writing
them out to a file which should have the '.d' extension. *)
val run_file : string -> unit
val run_file : tags:string list -> string -> unit
14 changes: 12 additions & 2 deletions atdd/src/bin/Atdd_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ open Cmdliner

type conf = {
input_files: string list;
tags: string list;
version: bool;
}

Expand All @@ -18,7 +19,7 @@ let run conf =
else
conf.input_files
|> List.iter (fun atd_file ->
Atdd.Codegen.run_file atd_file
Atdd.Codegen.run_file ~tags:conf.tags atd_file
)

(***************************************************************************)
Expand All @@ -38,6 +39,13 @@ let input_files_term =
let default = [] in
Arg.value (Arg.pos_all Arg.file default info)

let tags_term =
let info =
Arg.info ["tag"]
~doc:"Only evaluate annotations which have either the provided TAG as a field value <... tag=TAG>, or have no tags specified.\nOption can be used multiple times to specify several tags"
in
Arg.value (Arg.opt_all Arg.string [] info)

let version_term =
let info =
Arg.info ["version"]
Expand Down Expand Up @@ -94,14 +102,16 @@ type bar = [
]

let cmdline_term run =
let combine input_files version =
let combine input_files tags version =
run {
input_files;
version;
tags;
}
in
Term.(const combine
$ input_files_term
$ tags_term
$ version_term
)

Expand Down
9 changes: 8 additions & 1 deletion atdd/src/lib/Codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ let annot_schema_dlang : Atd.Annot.schema_section =
Type_expr, "wrap";
Field, "default";
Module_head, "import";
Module_head, "tag";
Type_def, "tag";
Type_expr, "tag";
Variant, "tag";
Cell, "tag";
Field, "tag";
]
}

Expand Down Expand Up @@ -1126,7 +1132,7 @@ let to_file ~atd_filename ~head (items : A.module_body) dst_path =
|> double_spaced
|> Indent.to_file ~indent:4 dst_path

let run_file src_path =
let run_file ~tags src_path =
let src_name = Filename.basename src_path in
let dst_name =
(if Filename.check_suffix src_name ".atd" then
Expand All @@ -1143,6 +1149,7 @@ let run_file src_path =
~keep_builtins:true
~inherit_fields:true
~inherit_variants:true
~tags
src_path
in
let full_module = Atd.Ast.use_only_specific_variants full_module in
Expand Down
2 changes: 1 addition & 1 deletion atdd/src/lib/Codegen.mli
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

(** Take ATD type definitions and translate them to Dlang, writing
them out to a file which should have the '.d' extension. *)
val run_file : string -> unit
val run_file : tags:string list -> string -> unit
1 change: 1 addition & 0 deletions atddiff/src/lib/Atddiff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ let compare_files
|> Atd.Util.load_file
~inherit_fields:true (* simplifies comparison *)
~inherit_variants:true (* simplifies comparison *)
~tags:[]
|> fst
|> Atd.Ast.remove_wrap_constructs
in
Expand Down
13 changes: 13 additions & 0 deletions atdgen/bin/ag_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ let main () =
let l = Re.Str.split (Re.Str.regexp " *, *\\| +") s in
opens := List.rev_append l !opens
in
let tags = ref [] in
let add_tags t =
tags := List.rev_append t !tags
in
let pp_convs : Ocaml.pp_convs ref = ref (Ocaml.Ppx_deriving []) in
let set_pp_convs arg =
match !pp_convs with
Expand Down Expand Up @@ -104,6 +108,14 @@ let main () =
Insert '[@@ATTR]' after OCaml type definitions.
Option can be used multiple times to specify several attributes
";
"-tag", Arg.String (fun s -> add_tags [ s ]),
"
TAG
Only evaluate annotations which have either
- the provided TAG as a field value <... tag=TAG>,
- or have no tags specified.
Option can be used multiple times to specify several tags
";
"-t", Arg.Unit (fun () ->
set_once "output type" mode T;
set_once "no function definitions" with_fundefs false),
Expand Down Expand Up @@ -422,6 +434,7 @@ Recommended usage: %s (-t|-b|-j|-v|-dep|-list|-mel) example.atd" Sys.argv.(0) in
~type_aliases
~force_defaults
~ocaml_version
~tags:!tags
atd_file ocaml_prefix

let () =
Expand Down
12 changes: 12 additions & 0 deletions atdgen/src/biniou.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ let annot_schema_biniou : Atd.Annot.schema = [
section = "biniou";
fields = [
Type_expr, "repr";
Module_head, "tag";
Type_def, "tag";
Type_expr, "tag";
Variant, "tag";
Cell, "tag";
Field, "tag";
]
};
{
Expand All @@ -50,6 +56,12 @@ let annot_schema_biniou : Atd.Annot.schema = [
Type_def, "module";
Type_def, "predef";
Type_def, "t";
Module_head, "tag";
Type_def, "tag";
Type_expr, "tag";
Variant, "tag";
Cell, "tag";
Field, "tag";
]
};
]
Expand Down
Loading