Skip to content

Commit

Permalink
rest
Browse files Browse the repository at this point in the history
  • Loading branch information
rvantonder committed Mar 18, 2021
1 parent 63d8089 commit feffc11
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 56 deletions.
8 changes: 7 additions & 1 deletion lib/comby.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ end
type rule = Rule.t

module Replacement = Replacement
module Rewriter = Rewriter
type replacement = Replacement.result

module Rewrite = struct
include Rewriter.Rewrite
include Rewriter.Rewrite_template
end

module Pipeline = struct
module Specification = Configuration.Specification
type specification = Specification.t
include Configuration.Command_input
include Pipeline
end
94 changes: 47 additions & 47 deletions lib/comby.mli
Original file line number Diff line number Diff line change
Expand Up @@ -483,69 +483,66 @@ end

type rule = Rule.t

(** {2 Replacement}
Defines the result of a rewrite operation. *)
module Replacement : sig
(** A replacement consists of the replaced range, the replacement content, and
the environment associated with the replacement content. *)
type t =
{ range : Match.range
; replacement_content : string
; environment : Match.environment
}

(** A replacement result is the rewritten source, and the replacements of
fragments. *)
type result =
{ rewritten_source : string
; in_place_substitutions : t list
}

val to_yojson : t -> Yojson.Safe.json
val of_yojson : Yojson.Safe.json -> (t, string) Result.t

val to_json
: ?path:string
-> ?replacements:t list
-> ?rewritten_source:string
-> diff:string
-> unit
-> Yojson.Safe.json
end

module Rewriter : sig
module Rewrite : sig
(** [all] rewrites a list of matches to an output result. Each match is
substituted in the [rewrite_template] to create a rewrite result. If
[source] is specified, each rewrite result is substituted in-place in the
source. If [source] is not specified, rewritten matches are
newline-separated.
If the rewrite template contains the syntax :[id()], then it is
substituted with fresh values. [sequential] determines whether fresh
values are monitonically increasing or a random hash. See [substitute]
for more. *)

val all
: ?source:string
-> ?sequential:bool
-> rewrite_template:string
-> match' list
-> Replacement.result option
end
type replacement = Replacement.result

module Rewrite_template : sig
(** [substitute] takes a template and match environment and substitutes
variables in the template for values.
module Rewrite : sig
(** [all source sequential rewrite_template matches] substitutes
[rewrite_template] with each match in [matches] to create a rewrite result.
If [source] is specified, each rewrite result is substituted in-place in
the source. If [source] is not specified, rewritten matches are
newline-separated.
The syntax :[id()] is substituted with fresh values. If [sequential] is
true, it substitutes :[id()] starting with 1, and subsequent :[id()]
values increment the ID. Otherwise if [sequential] is false, it
substitutes the pattern :[id()] with a fresh hex string based on the last
48-bit part of a UUID v3 identifier. *)
val substitute : ?sequential:bool -> string -> Match.environment -> (string * string list)
end
If the rewrite template contains the syntax :[id()], then it is
substituted with fresh values. [sequential] determines whether fresh values
are monitonically increasing or a random hash. See [substitute] for more. *)
val all
: ?source:string
-> ?sequential:bool
-> rewrite_template:string
-> match' list
-> replacement option

(** [substitute sequential template environment] substitutes [template] with
the variable and value pairs in the [environment]. It returns the result
after substitution, and the list of variables in [environment] that were
substituted for.
The syntax :[id()] is substituted with fresh values. If [sequential] is
true, it substitutes :[id()] starting with 1, and subsequent :[id()] values
increment the ID. Otherwise if [sequential] is false, it substitutes the
pattern :[id()] with a fresh hex string based on the last 48-bit part of a
UUID v3 identifier. *)
val substitute : ?sequential:bool -> string -> Match.environment -> (string * string list)
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. *)
to generate matches, and then process matches with Rewrite functions. *)
module Pipeline : sig
(** Source inputs for a pipeline is either a string, or a file path. *)
type single_source =
Expand All @@ -572,22 +569,25 @@ module Pipeline : sig
-> t
end

type specification = Specification.t

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

val process_single_source
(** [execute matcher subst timeout config source spec] runs a [matcher] on
[source] for [spec] parameterized by [config]. [substitute_in_place] sets
whether rewrite output should substitute rewritten values in place.
[timeout] specifies a timeout in seconds (default 3).
*)
val execute
: (module Matchers.Matcher.S)
-> ?sequential:bool
-> ?omega:bool
-> ?fast_offset_conversion:bool
-> ?substitute_in_place:bool
-> ?verbose:bool
-> ?timeout:int
-> Matchers.Configuration.t
-> Matchers.configuration
-> single_source
-> Specification.t
-> specification
-> processed_source_result
end
23 changes: 21 additions & 2 deletions lib/pipeline/pipeline.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ let process_single_source
?(fast_offset_conversion = false)
?(substitute_in_place = false)
?(verbose = false)
?(timeout = 3000)
?(timeout = 3)
configuration
source
(Specification.{ rewrite_template; _ } as specification)
Expand Down Expand Up @@ -334,4 +334,23 @@ let run
compute_mode
interactive_review
in
if dump_statistics then write_statistics count sources start_time;
if dump_statistics then write_statistics count sources start_time

let execute
matcher
?substitute_in_place
?timeout
configuration
source
specification =
process_single_source
matcher
~sequential:true
~omega:false
~fast_offset_conversion:false
?substitute_in_place
~verbose:false
?timeout
configuration
source
specification
9 changes: 9 additions & 0 deletions lib/pipeline/pipeline.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ val process_single_source
-> Specification.t
-> processed_source_result

val execute
: (module Matchers.Matcher.S)
-> ?substitute_in_place:bool
-> ?timeout:int
-> Matchers.Configuration.t
-> single_source
-> Specification.t
-> processed_source_result

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

val run : Command_configuration.t -> unit
9 changes: 3 additions & 6 deletions src/server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ open Opium

open Comby
open Matchers
open Rewriter
open Server_types

let (>>|) = Lwt.Infix.(>|=)
Expand Down Expand Up @@ -52,9 +51,8 @@ let perform_match request =
let configuration = Matchers.Configuration.create ~match_kind:Fuzzy () in
let specification = Pipeline.Specification.create ~match_template ?rule () in
let matches =
Pipeline.process_single_source
Pipeline.execute
matcher
~sequential:true
configuration
(String source)
specification
Expand Down Expand Up @@ -105,10 +103,9 @@ let perform_rewrite request =
let configuration = Configuration.create ~match_kind:Fuzzy () in
let specification = Pipeline.Specification.create ~match_template ?rule () in
let matches =
Pipeline.process_single_source
Pipeline.execute
matcher
~substitute_in_place
~sequential:true
configuration
(String source)
specification
Expand Down Expand Up @@ -146,7 +143,7 @@ let perform_environment_substitution request =
let code, result =
200,
Out.Substitution.to_string
{ result = fst @@ Rewrite_template.substitute rewrite_template environment
{ result = fst @@ Rewrite.substitute rewrite_template environment
; id
}
in
Expand Down

0 comments on commit feffc11

Please sign in to comment.