Skip to content

Commit

Permalink
Merge fb28037 into 53f9188
Browse files Browse the repository at this point in the history
  • Loading branch information
rvantonder committed May 29, 2019
2 parents 53f9188 + fb28037 commit 8511144
Show file tree
Hide file tree
Showing 25 changed files with 1,037 additions and 391 deletions.
1 change: 1 addition & 0 deletions lib/comby.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Language = Language
module Matchers = Matchers
module Match = Match
module Replacement = Replacement
module Rewriter = Rewriter
module Statistics = Statistics
1 change: 1 addition & 0 deletions lib/comby.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Language = Language
module Matchers = Matchers
module Match = Match
module Replacement = Replacement
module Rewriter = Rewriter
module Statistics = Statistics
1 change: 1 addition & 0 deletions lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
mparser
mparser.pcre
comby.matchers
comby.replacement
comby.rewriter
comby.match
comby.language
Expand Down
6 changes: 6 additions & 0 deletions lib/match/match.mli
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ type t =
[@@deriving yojson]

val create : unit -> t

val pp_json_pretty : Format.formatter -> string option * t list -> unit

val pp_json_lines : Format.formatter -> string option * t list -> unit

val pp_match_result : Format.formatter -> string option * t list -> unit
32 changes: 32 additions & 0 deletions lib/match/match_context.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
open Core

type t =
{ range : Range.t
; environment : Environment.t
Expand All @@ -10,3 +12,33 @@ let create () =
; environment = Environment.create ()
; matched = ""
}

let to_json source_path matches =
let json_matches matches = `List (List.map ~f:(fun x -> to_yojson x) matches) in
match source_path with
| None -> `Assoc [("uri", `Null); ("matches", json_matches matches)]
| Some path -> `Assoc [("uri", `String path); ("matches", json_matches matches)]

let pp_json_pretty ppf (source_path, matches) =
let f = Yojson.Safe.pretty_to_string in
let json_string = f @@ to_json source_path matches in
Format.fprintf ppf "%s" json_string

let pp_json_lines ppf (source_path, matches) =
let f = Yojson.Safe.to_string in
let json_string = f @@ to_json source_path matches in
Format.fprintf ppf "%s" json_string

let pp_match_result ppf (source_path, matches) =
let pp_source_path ppf source_path =
match source_path with
| Some path -> Format.fprintf ppf " in %s " path
| None -> Format.fprintf ppf "%s" " "
in
(* FIXME *)
let spec_number = 0 in
Format.fprintf ppf
"%d matches%afor spec %d (use -json-pretty for json format)\n"
(List.length matches)
pp_source_path source_path
(spec_number + 1)
27 changes: 27 additions & 0 deletions lib/matchers/languages.ml
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,30 @@ module C_nested_comments = struct

include Matcher.Make(Syntax)
end

let select_with_extension extension : (module Types.Matcher.S) =
match extension with
| ".c" | ".h" | ".cc" | ".cpp" | ".hpp" -> (module C)
| ".clj" -> (module Clojure)
| ".css" -> (module CSS)
| ".dart" -> (module Dart)
| ".elm" -> (module Elm)
| ".erl" -> (module Erlang)
| ".ex" -> (module Elixir)
| ".html" | ".xml" -> (module Html)
| ".hs" -> (module Haskell)
| ".go" -> (module Go)
| ".java" -> (module Java)
| ".js" | ".ts" -> (module Javascript)
| ".ml" | ".mli" -> (module OCaml)
| ".php" -> (module Php)
| ".py" -> (module Python)
| ".rb" -> (module Ruby)
| ".rs" -> (module Rust)
| ".s" | ".asm" -> (module Assembly)
| ".scala" -> (module Scala)
| ".sql" -> (module SQL)
| ".sh" -> (module Bash)
| ".swift" -> (module Swift)
| ".tex" | ".bib" -> (module Latex)
| _ -> (module Generic)
5 changes: 2 additions & 3 deletions lib/matchers/matchers.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
include Languages

module Configuration = Configuration

module type Matcher = Types.Matcher.S

include Languages
5 changes: 2 additions & 3 deletions lib/matchers/matchers.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
include module type of Languages

module Configuration = Configuration

module type Matcher = Types.Matcher.S

include module type of Languages
5 changes: 5 additions & 0 deletions lib/replacement/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(name replacement)
(public_name comby.replacement)
(preprocess (pps ppx_deriving.show ppx_deriving.eq ppx_sexp_conv ppx_sexp_message ppx_deriving_yojson bisect_ppx -conditional -no-comment-parsing))
(libraries comby.parsers comby.match ppxlib core mparser mparser.pcre yojson ppx_deriving_yojson ppx_deriving_yojson.runtime))
20 changes: 20 additions & 0 deletions lib/replacement/replacement.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
open Match

type t =
{ range : range
; replacement_content : string
; environment : environment
}
[@@deriving yojson]

type result =
{ rewritten_source : string
; in_place_substitutions : t list
}
[@@deriving yojson]

let empty_result =
{ rewritten_source = ""
; in_place_substitutions = []
}
[@@deriving yojson]
16 changes: 16 additions & 0 deletions lib/replacement/replacement.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
open Match

type t =
{ range : range
; replacement_content : string
; environment : environment
}
[@@deriving yojson]

type result =
{ rewritten_source : string
; in_place_substitutions : t list
}
[@@deriving yojson]

val empty_result : result
2 changes: 1 addition & 1 deletion lib/rewriter/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
(name rewriter)
(public_name comby.rewriter)
(preprocess (pps ppx_deriving.show ppx_sexp_conv ppx_deriving_yojson bisect_ppx -conditional -no-comment-parsing))
(libraries comby.matchers ppxlib core core.uuid))
(libraries comby.matchers comby.replacement ppxlib core core.uuid))
20 changes: 1 addition & 19 deletions lib/rewriter/rewrite.ml
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
open Core

open Match

type match_context_replacement =
{ range : range
; replacement_content : string
; environment : environment
}
[@@deriving yojson]

type result =
{ rewritten_source : string
; in_place_substitutions : match_context_replacement list
}
[@@deriving yojson]

let empty_result =
{ rewritten_source = ""
; in_place_substitutions = []
}
[@@deriving yojson]
open Replacement

let substitute_match_contexts (matches: Match.t list) source replacements =
let rewrite_template, environment =
Expand Down
17 changes: 1 addition & 16 deletions lib/rewriter/rewrite.mli
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
open Match

type match_context_replacement =
{ range : range
; replacement_content : string
; environment : environment
}
[@@deriving yojson]

type result =
{ rewritten_source : string
; in_place_substitutions : match_context_replacement list
}
[@@deriving yojson]

(** if [source] is given, substitute in-place. If not,
emit result separated by newlines *)
val all
: ?source:string
-> rewrite_template:string
-> Match.t list
-> result option
-> Replacement.result option
2 changes: 2 additions & 0 deletions lib/rewriter/rewriter.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Rewrite = Rewrite
module Rewrite_template = Rewrite_template
2 changes: 2 additions & 0 deletions lib/rewriter/rewriter.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Rewrite = Rewrite
module Rewrite_template = Rewrite_template
Loading

0 comments on commit 8511144

Please sign in to comment.