wip explain matcher

This commit is contained in:
Rijnard van Tonder 2019-08-29 01:56:58 -04:00
parent c5a86ab5df
commit d2da2fff47
3 changed files with 78 additions and 76 deletions

View File

@ -1,4 +1,4 @@
let enable_alphanum_delimiters = true
open Syntax_config
module Text = struct
module Syntax = struct
@ -156,21 +156,19 @@ module Ruby = struct
let user_defined_delimiters =
Generic.Syntax.user_defined_delimiters
@ if enable_alphanum_delimiters then
[ "class", "end"
; "def", "end"
; "do", "end"
; "if", "end"
; "case", "end"
; "unless", "end"
; "while", "end"
; "until", "end"
; "for", "end"
; "begin", "end"
; "module", "end"
]
else
[]
@
[ "class", "end"
; "def", "end"
; "do", "end"
; "if", "end"
; "case", "end"
; "unless", "end"
; "while", "end"
; "until", "end"
; "for", "end"
; "begin", "end"
; "module", "end"
]
let raw_string_literals =
[ ({|"|}, {|"|})
@ -192,16 +190,14 @@ module Elixir = struct
let user_defined_delimiters =
Generic.Syntax.user_defined_delimiters
@ if enable_alphanum_delimiters then
[ "fn", "end"
; "do", "end"
; "case", "end"
; "cond", "end"
; "if", "end"
; "<", ">"
]
else
[]
@
[ "fn", "end"
; "do", "end"
; "case", "end"
; "cond", "end"
; "if", "end"
; "<", ">"
]
let raw_string_literals =
[ ({|"""|}, {|"""|})
@ -268,13 +264,11 @@ module Erlang = struct
let user_defined_delimiters =
Generic.Syntax.user_defined_delimiters
@ if enable_alphanum_delimiters then
[ "fun", "end"
; "case", "end"
; "if", "end"
]
else
[]
@
[ "fun", "end"
; "case", "end"
; "if", "end"
]
let comment_parser =
[ Until_newline "%"
@ -388,13 +382,11 @@ module OCaml = struct
let user_defined_delimiters =
Generic.Syntax.user_defined_delimiters
@ if enable_alphanum_delimiters then
[ "begin", "end"
; "struct", "end"
; "sig", "end"
]
else
[]
@
[ "begin", "end"
; "struct", "end"
; "sig", "end"
]
(* Override ' as escapable string literal, since
@ -531,16 +523,19 @@ let select_with_extension extension : (module Types.Matcher.S) =
| ".txt" -> (module Text)
| _ -> (module Generic)
let create (t : Syntax_config.t) =
let create
{ user_defined_delimiters
; escapable_string_literals
; escape_char
; raw_string_literals
; comment_parser
} =
let module User_language = struct
let user_defined_delimiters = t.user_defined_delimiters
let escapable_string_literals = t.escapable_string_literals
let escape_char = t.escape_char
let raw_string_literals = t.raw_string_literals
let comment_parser = t.comment_parser
end in
let user_defined_delimiters = user_defined_delimiters
let escapable_string_literals = escapable_string_literals
let escape_char = escape_char
let raw_string_literals = raw_string_literals
let comment_parser = comment_parser
end
in
(module Matcher.Make (User_language) : Types.Matcher.S)

View File

@ -138,23 +138,22 @@ let select_matcher custom_matcher override_matcher configuration =
Yojson.Safe.from_file matcher_path
|> Matchers.Syntax_config.of_yojson
|> function
| Ok c -> Matchers.create c
| Ok c -> Matchers.create c, None
| Error error ->
Format.eprintf "%s@." error;
exit 1
else if Option.is_some override_matcher then
Matchers.select_with_extension (Option.value_exn override_matcher), None
else
if Option.is_some override_matcher then
Matchers.select_with_extension (Option.value_exn override_matcher)
else
match configuration.file_filters with
| None | Some [] ->
Matchers.select_with_extension ".generic"
| Some (filter::_) ->
match Filename.split_extension filter with
| _, Some extension ->
Matchers.select_with_extension ("." ^ extension)
| _ ->
Matchers.select_with_extension ".generic"
let extension =
match configuration.file_filters with
| None | Some [] -> ".generic"
| Some (filter::_) ->
match Filename.split_extension filter with
| _, Some extension -> "." ^ extension
| extension, None -> "." ^ extension
in
Matchers.select_with_extension extension, Some extension
let write_statistics number_of_matches paths total_time dump_statistics =
if dump_statistics then
@ -414,10 +413,14 @@ let base_command_parameters : (unit -> 'result) Command.Param.t =
Format.eprintf "%s@." @@ Error.to_string_hum error;
exit 1
in
let matcher = select_matcher custom_matcher override_matcher configuration
in
let matcher, extension = select_matcher custom_matcher override_matcher configuration in
fun () ->
run matcher configuration
run matcher configuration;
match extension with
| Some ".generic" ->
Format.eprintf "WARNING: the GENERIC matcher was used, because a language could not be inferred from the file extension. Use something like '-matcher .go' (including the dot, '.') to force using the matcher for Go, and similarly '-matcher .java' for Java, and so on for other languages.@."
| Some _
| None -> ()
]
let default_command =

View File

@ -210,9 +210,10 @@ let%expect_test "with_rewrite_rule_stdin_default_no_extension" =
Format.sprintf "-sequential '%s' '%s' -rule '%s' -stdin" match_template rewrite_template rule
in
let command = Format.sprintf "%s %s" binary_path command_args in
let result = read_source_from_stdin command source in
let result = read_expect_stdin_and_stdout command source in
print_string result;
[%expect_exact {|------ /dev/null
[%expect_exact {|WARNING: the GENERIC matcher was used, because a language could not be inferred from the file extension. Use something like '-matcher .go' (including the dot, '.') to force using the matcher for Go, and similarly '-matcher .java' for Java, and so on for other languages.
------ /dev/null
++++++ /dev/null
@|-1,1 +1,1 ============================================================
!|hello world
@ -226,9 +227,10 @@ let%expect_test "generic_matcher" =
Format.sprintf "-stdin -sequential '%s' '%s' -f .generic" match_template rewrite_template
in
let command = Format.sprintf "%s %s" binary_path command_args in
let result = read_source_from_stdin command source in
let result = read_expect_stdin_and_stdout command source in
print_string result;
[%expect_exact {|------ /dev/null
[%expect_exact {|WARNING: the GENERIC matcher was used, because a language could not be inferred from the file extension. Use something like '-matcher .go' (including the dot, '.') to force using the matcher for Go, and similarly '-matcher .java' for Java, and so on for other languages.
------ /dev/null
++++++ /dev/null
@|-1,1 +1,1 ============================================================
-|\footnote{\small \url{https://github.com}}
@ -422,9 +424,7 @@ let%expect_test "nested_templates" =
let template_dir = "example" ^/ "multiple-nested-templates" in
let command_args = Format.sprintf "-stdin -sequential -f .c -templates %s -stdout" template_dir in
let command = Format.sprintf "%s %s" binary_path command_args in
let result =
read_expect_stdin_and_stdout command source
in
let result = read_expect_stdin_and_stdout command source in
print_string result;
[%expect{|
Warning: Could not read required match file in example/multiple-nested-templates/invalid-subdir
@ -720,9 +720,10 @@ let%expect_test "infer_and_honor_extensions" =
let src_dir = "example" ^/ "src" ^/ "honor-file-extensions" in
let command_args = Format.sprintf "'foo()' 'bar()' .generic -sequential -d %s -diff" src_dir in
let command = Format.sprintf "%s %s" binary_path command_args in
let result = read_source_from_stdin command source in
let result = read_expect_stdin_and_stdout command source in
print_string result;
[%expect{|
WARNING: the GENERIC matcher was used, because a language could not be inferred from the file extension. Use something like '-matcher .go' (including the dot, '.') to force using the matcher for Go, and similarly '-matcher .java' for Java, and so on for other languages.
--- example/src/honor-file-extensions/honor.pb.generic
+++ example/src/honor-file-extensions/honor.pb.generic
@@ -1,3 +1,3 @@
@ -738,6 +739,8 @@ let%expect_test "diff_only" =
let result = read_source_from_stdin command source in
print_string result;
[%expect{|
WARNING: the GENERIC matcher was used, because a language could not be inferred from the file extension. Use something like '-matcher .go' (including the dot, '.') to force using the matcher for Go, and similarly '-matcher .java' for Java, and so on for other languages.
{"uri":null,"diff":"--- /dev/null\n+++ /dev/null\n@@ -1,1 +1,1 @@\n-hello world\n+world world"} |}];
let source = "hello world" in
@ -770,9 +773,10 @@ let%expect_test "zip_exclude_dir_no_extension" =
let exclude_dir = "sample-repo/vendor" in
let command_args = Format.sprintf "'main' 'pain' -zip %s -sequential -diff -exclude-dir %s" zip exclude_dir in
let command = Format.sprintf "%s %s" binary_path command_args in
let result = read_source_from_stdin command source in
let result = read_expect_stdin_and_stdout command source in
print_string result;
[%expect{|
WARNING: the GENERIC matcher was used, because a language could not be inferred from the file extension. Use something like '-matcher .go' (including the dot, '.') to force using the matcher for Go, and similarly '-matcher .java' for Java, and so on for other languages.
--- sample-repo/src/main.go
+++ sample-repo/src/main.go
@@ -1,2 +1,2 @@