Skip to content

Commit

Permalink
Polishing reviewer
Browse files Browse the repository at this point in the history
Signed-off-by: Kakadu <Kakadu@pm.me>
  • Loading branch information
Kakadu committed Sep 30, 2023
1 parent f8e7ce5 commit 1e79aaf
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 126 deletions.
71 changes: 13 additions & 58 deletions review/diff_parser.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
(** Copyright 2021-2023, Kakadu. *)

(** SPDX-License-Identifier: LGPL-3.0-or-later *)

(* https://git-scm.com/docs/diff-format *)

open Angstrom
Expand Down Expand Up @@ -106,10 +110,6 @@ let is_correct_chunk info lines =
true
;;

let pp_lines_db xs =
Format.pp_print_list ~pp_sep:Format.pp_print_space [%show: int * (int * int)] xs
;;

let recover_lines input =
let extend acc ~line start ~fin =
if fin >= start then (line, (start + 1, fin)) :: acc else acc
Expand All @@ -124,57 +124,12 @@ let recover_lines input =
loop [] 1 0
;;

let make_lines_index input =
let db = recover_lines input in
(* Format.printf "db : %a\n%!" pp_lines_db db; *)
fun pos ->
List.find_map
(fun (line, (start, fin)) -> if start <= pos && pos <= fin then Some line else None)
db
;;

let%test "file head 1 " =
let input =
{|
diff -N -u old/changed.txt new/changed.txt
--- old/changed.txt 2022-09-18 16:48:36.487062439 +0300
+++ new/changed.txt 2022-09-18 16:48:36.487062439 +0300
@@ -1,3 +1,4 @@
|}
in
Angstrom.parse_string ~consume:Consume.Prefix file_head input
= Result.ok ("old/changed.txt", "new/changed.txt")
;;

let%test "chunk item 1" =
let input = "+ helper b (a+b) (n-1)" in
match Angstrom.parse_string ~consume:Consume.All Line_parser.(run chunk_item) input with
| Result.Error _ -> false
| Ok (Add, str) when str = " helper b (a+b) (n-1)" -> true
| Ok (Add, _) -> false
| _ -> false
;;

let%expect_test _ =
let lst = recover_lines "aaaa\nbbbbb\ncccc\naaaaaaaaaaaaaa" in
Format.printf "[%a]\n%!" pp_lines_db lst;
[%expect {|
[(1, (1, 4)) (2, (6, 10)) (3, (12, 15))
(4, (17, 29))] |}]
;;

let%expect_test _ =
let lst = recover_lines "a\nb\nc\nd" in
Format.printf "[%a]\n%!" pp_lines_db lst;
[%expect {|
[(1, (1, 1)) (2, (3, 3)) (3, (5, 5))
(4, (7, 6))] |}]
;;

let%expect_test _ =
let lst = recover_lines "\n\n\n" in
Format.printf "[%a]\n%!" pp_lines_db lst;
[%expect {|
[(1, (1, 0)) (2, (2, 1))
(3, (3, 2))] |}]
;;
(* let make_lines_index input =
let db = recover_lines input in
(* Format.printf "db : %a\n%!" pp_lines_db db; *)
fun pos ->
List.find_map
(fun (line, (start, fin)) -> if start <= pos && pos <= fin then Some line else None)
db
;;
*)
31 changes: 31 additions & 0 deletions review/diff_parser.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(** Copyright 2021-2023, Kakadu. *)

(** SPDX-License-Identifier: LGPL-3.0-or-later *)

(** Parser of the diff file. The grammar is similar too
diff ::= ( head chunk* )*
See also [file_head] and [a_chunk]. *)
val parse_whole_file : Types.file_info list Angstrom.t

(** Main entry point. Uses [parse_whole_file] under the hood *)
val parse_string : string -> (Types.file_info list, string) result

(** Parse header of the diff file *)
val file_head : (string * string) Angstrom.t

(** Parses hunk for the file *)
val a_chunk : Types.chunk Angstrom.t

val recover_lines : string -> (int * (int * int)) list

(** The call [lookup db ~file ~line] searches in the list of file differences
by a file name [file] and a file line [line]
the corresponding line of in diff file (counting from the beginning of the information about [file]).
Github API requires this information to submit a review *)
val lookup : Types.file_info list -> file:string -> line:int -> int option

(** Enable or disable trace logging *)
val set_logging : bool -> unit
19 changes: 13 additions & 6 deletions review/dune
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,33 @@
(name main)
(public_name reviewer)
(modules main)
(libraries diff_parser curly)
(libraries yojson diff_parser curly)
(instrumentation
(backend bisect_ppx)))

(library
(name diff_parser)
(public_name zanuda.diff_parser)
(synopsis "Parser for diff file format")
(modules types line_parser diff_parser)
(wrapped false)
(libraries angstrom yojson ppx_show.runtime)
(inline_tests)
(preprocess
(pps ppx_inline_test ppx_show ppx_expect))
(libraries angstrom)
(instrumentation
(backend bisect_ppx)))

(executable
(name parser)
(modules parser)
(libraries diff_parser)
(instrumentation
(backend bisect_ppx)))

(library
(name diff_parser_tests)
(modules tests)
(libraries diff_parser)
(inline_tests)
(preprocess
(pps ppx_show))
(pps ppx_inline_test ppx_expect))
(instrumentation
(backend bisect_ppx)))
66 changes: 4 additions & 62 deletions review/line_parser.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
(** Copyright 2021-2023, Kakadu. *)

(** SPDX-License-Identifier: LGPL-3.0-or-later *)

open Angstrom
open Types

Expand Down Expand Up @@ -100,65 +104,3 @@ let run : ?info:string -> _ parser -> _ parser =
;;

let parse parser str = parse_string ~consume:Consume.All parser str

let%test "diff cmd" =
let input = "diff -N -u old/added.txt new/added.txt" in
parse diff_cmd input = Result.Ok ()
;;

let%test "file mode new" =
let input = "new file mode 100644" in
parse file_mode input = Result.Ok ()
;;

let%test "file mode deleted" =
let input = "deleted file mode 100644" in
parse file_mode input = Result.Ok ()
;;

let%test "index" =
let input = "index 000000000..a71382926" in
parse index input = Result.Ok ()
;;

let%test "remove file" =
let input = "--- a/main.ml" in
parse remove_file input = Result.Ok "main.ml"
;;

let%test "add file" =
let input = "+++ b/main.ml" in
parse add_file input = Result.Ok "main.ml"
;;

let%test "pos num" =
let input = "123" in
parse pos_num input = Result.Ok 123
;;

let%test "chunk head 1" =
let input = "@@ -1,3 +1,4 @@" in
parse chunk_head input
= Result.Ok { old = 1; old_range = 3; fresh = 1; fresh_range = 4 }
;;

let%test "chunk head 2" =
let input = "@@ -1 +1 @@" in
parse chunk_head input
= Result.Ok { old = 1; old_range = 1; fresh = 1; fresh_range = 1 }
;;

let%test "chunk item add" =
let input = "+input" in
parse chunk_item input = Result.Ok (Add, "input")
;;

let%test "chunk item del" =
let input = "-input" in
parse chunk_item input = Result.Ok (Del, "input")
;;

let%test "chunk item leave" =
let input = " input" in
parse chunk_item input = Result.Ok (Leave, "input")
;;
19 changes: 19 additions & 0 deletions review/line_parser.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(** Copyright 2021-2023, Kakadu. *)

(** SPDX-License-Identifier: LGPL-3.0-or-later *)

(** [run ?info p] trims line break in the input string and runs parser [p]. *)
val run : ?info:string -> 'a Angstrom.t -> 'a Angstrom.t

val diff_cmd : unit Angstrom.t
val file_mode : unit Angstrom.t
val similarity : unit Angstrom.t
val rename : unit Angstrom.t
val index : unit Angstrom.t
val remove_file : string Angstrom.t
val add_file : string Angstrom.t
val pos_num : int Angstrom.t
val chunk_head : Types.chunk_info Angstrom.t
val chunk_item : (Types.kind * string) Angstrom.t
val no_new_line_eof : unit Angstrom.t
val set_logging : bool -> unit
4 changes: 4 additions & 0 deletions review/main.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
(** Copyright 2021-2023, Kakadu. *)

(** SPDX-License-Identifier: LGPL-3.0-or-later *)

(* https://docs.github.com/en/rest/pulls/reviews#create-a-review-for-a-pull-request *)

let _example () =
Expand Down
4 changes: 4 additions & 0 deletions review/parser.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
(** Copyright 2021-2023, Kakadu. *)

(** SPDX-License-Identifier: LGPL-3.0-or-later *)

(* let lookup line_of_pos parsed ~file ~line =
(* Format.printf "Parsed stuff:@[%a@]\n%!" [%show: Types.file_info list] parsed; *)
let open Types in
Expand Down
Loading

0 comments on commit 1e79aaf

Please sign in to comment.