Skip to content

Commit

Permalink
pipeline intf
Browse files Browse the repository at this point in the history
  • Loading branch information
rvantonder committed Mar 18, 2021
1 parent d1bbd30 commit 63d8089
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 86 deletions.
11 changes: 6 additions & 5 deletions lib/comby.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ module Matchers = struct
module Languages = Matchers.Languages
end

module Specification = Configuration.Specification

module Configuration = Configuration.Command_input

module Rule = struct
open Language
type t = Rule.t
Expand All @@ -36,6 +32,11 @@ module Rule = struct
end
type rule = Rule.t

module Pipeline = Pipeline
module Replacement = Replacement
module Rewriter = Rewriter

module Pipeline = struct
module Specification = Configuration.Specification
include Configuration.Command_input
include Pipeline
end
92 changes: 51 additions & 41 deletions lib/comby.mli
Original file line number Diff line number Diff line change
Expand Up @@ -483,46 +483,6 @@ end

type rule = Rule.t

(** {2 Specification}
Defines an internal type that represents an atomic operation for matching,
rule application and rewriting *)
module Specification : sig
type t

(** [create rewrite_template rule match_template] creates a new specification.
If [rule] is supplied, it will be applied to matches of [match_template].
If [rewrite_template] is supplied, running a specification will return
replacements rather than just matches (see [process_single_source] below). *)
val create
: ?rewrite_template:string
-> ?rule:rule
-> match_template:string
-> unit
-> t
end

module Configuration : sig
type single_source =
| Path of string
| String of string
end

module Pipeline : sig
val with_timeout : int -> Configuration.single_source -> f:(unit -> 'a list) -> 'a list

val timed_run
: (module Matchers.Matcher.S)
-> ?fast_offset_conversion:bool
-> ?omega:bool
-> ?substitute_in_place:bool
-> configuration:Matchers.Configuration.t
-> source:string
-> specification:Specification.t
-> unit
-> match' list
end

module Replacement : sig
type t =
{ range : Match.range
Expand All @@ -547,7 +507,6 @@ module Replacement : sig
-> Yojson.Safe.json
end


module Rewriter : sig
module Rewrite : sig
(** [all] rewrites a list of matches to an output result. Each match is
Expand Down Expand Up @@ -581,3 +540,54 @@ module Rewriter : sig
val substitute : ?sequential:bool -> string -> Match.environment -> (string * string list)
end
end

(** {2 Pipeline}
Exposes top level functions for running matchers and generating
replacements. For finer control over matching and rewriting, use Matcher.all
generate output with a Rewriter. *)
module Pipeline : sig
(** Source inputs for a pipeline is either a string, or a file path. *)
type single_source =
| Path of string
| String of string

(** {2 Specification}
Defines an internal type that represents an atomic operation for matching,
rule application and rewriting *)
module Specification : sig
type t

(** [create rewrite_template rule match_template] creates a new specification.
If [rule] is supplied, it will be applied to matches of [match_template].
If [rewrite_template] is supplied, running a specification will return
replacements rather than just matches (see [process_single_source] below).
*)
val create
: ?rewrite_template:string
-> ?rule:rule
-> match_template:string
-> unit
-> t
end

(** The output of running a specification *)
type processed_source_result =
| Matches of (Match.t list * int)
| Replacement of (Replacement.t list * string * int)
| Nothing

val process_single_source
: (module Matchers.Matcher.S)
-> ?sequential:bool
-> ?omega:bool
-> ?fast_offset_conversion:bool
-> ?substitute_in_place:bool
-> ?verbose:bool
-> ?timeout:int
-> Matchers.Configuration.t
-> single_source
-> Specification.t
-> processed_source_result
end
12 changes: 6 additions & 6 deletions lib/pipeline/pipeline.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ let log_to_file path =

let process_single_source
matcher
~sequential
~omega
~fast_offset_conversion
~substitute_in_place
~verbose
~timeout
?(sequential = false)
?(omega = false)
?(fast_offset_conversion = false)
?(substitute_in_place = false)
?(verbose = false)
?(timeout = 3000)
configuration
source
(Specification.{ rewrite_template; _ } as specification)
Expand Down
22 changes: 6 additions & 16 deletions lib/pipeline/pipeline.mli
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,17 @@ type processed_source_result =

val process_single_source
: (module Matchers.Matcher.S)
-> sequential:bool
-> omega:bool
-> fast_offset_conversion:bool
-> substitute_in_place:bool
-> verbose:bool
-> timeout:int
-> ?sequential:bool
-> ?omega:bool
-> ?fast_offset_conversion:bool
-> ?substitute_in_place:bool
-> ?verbose:bool
-> ?timeout:int
-> Matchers.Configuration.t
-> single_source
-> Specification.t
-> processed_source_result

val timed_run
: (module Matchers.Matcher.S)
-> ?fast_offset_conversion:bool
-> ?omega:bool
-> ?substitute_in_place:bool
-> configuration:Matchers.Configuration.t
-> source:string
-> specification:Specification.t -> unit
-> Match.t list

val with_timeout : int -> Command_input.single_source -> f:(unit -> 'a list) -> 'a list

val run : Command_configuration.t -> unit
35 changes: 22 additions & 13 deletions src/server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ let perform_match request =
in
let run ?rule () =
let configuration = Matchers.Configuration.create ~match_kind:Fuzzy () in
let matches : Match.t list =
Pipeline.with_timeout timeout (String "") ~f:(fun () ->
let specification = Specification.create ~match_template ?rule () in
Pipeline.timed_run matcher ~configuration ~specification ~source ())
let specification = Pipeline.Specification.create ~match_template ?rule () in
let matches =
Pipeline.process_single_source
matcher
~sequential:true
configuration
(String source)
specification
|> function
| Matches (m, _) -> m
| _ -> []
in
Out.Matches.to_string { matches; source; id }
in
Expand Down Expand Up @@ -96,16 +103,18 @@ let perform_rewrite request =
in
let run ?rule () =
let configuration = Configuration.create ~match_kind:Fuzzy () in
let specification = Pipeline.Specification.create ~match_template ?rule () in
let matches =
Pipeline.with_timeout timeout (String "") ~f:(fun () ->
let specification = Specification.create ~match_template ?rule () in
Pipeline.timed_run
matcher
~substitute_in_place
~configuration
~source
~specification
())
Pipeline.process_single_source
matcher
~substitute_in_place
~sequential:true
configuration
(String source)
specification
|> function
| Matches (m, _) -> m
| _ -> []
in
Rewrite.all matches ?source:source_substitution ~rewrite_template
|> Option.value_map ~default ~f:(fun Replacement.{ rewritten_source; in_place_substitutions } ->
Expand Down
12 changes: 7 additions & 5 deletions test/common/test_offset_conversion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ let run source match_template format : (string * string) =
let specification = Configuration.Specification.create ~match_template () in
let run ~fast =
let result =
Pipeline.timed_run
Pipeline.process_single_source
(module Omega.C)
~fast_offset_conversion:fast
~configuration
~specification
~source
()
configuration
(String source)
specification
|> function
| Matches (m, _) -> m
| _ -> []
in
to_string format result
in
Expand Down

0 comments on commit 63d8089

Please sign in to comment.