Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplicate implementation of ;; detection #386

Merged
merged 2 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ let ends_by_semi_semi c =
match List.rev c with
| h :: _ ->
let len = String.length h in
len > 2 && h.[len - 1] = ';' && h.[len - 2] = ';'
len > 2 && Astring.String.is_suffix ~affix:";;" h
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why len > 2 rather than len >= 2 ? (the latter is implicitly checked by is_suffix)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed that it also checks that there is something in there besides just the ;; but this might have been a typo in the original code. Maybe it really was just a guard for the string subindexing. Removing the check altogether makes the tests all pass so I've amended the code to remove the check.

| _ -> false

let pp_line_directive ppf (file, line) = Fmt.pf ppf "#%d %S" line file
Expand Down
4 changes: 4 additions & 0 deletions lib/block.mli
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,7 @@ val executable_contents : syntax:Syntax.t -> t -> string list
(e.g. the phrase result is discarded). *)

val is_active : ?section:string -> t -> bool

(** {2 Helpers} *)

val ends_by_semi_semi : string list -> bool
17 changes: 4 additions & 13 deletions lib/top/mdx_top.ml
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,10 @@ module Phrase = struct
let endpos = lexbuf.Lexing.lex_curr_p in
{ doc = { lexbuf; contents }; startpos; endpos; parsed }

let ends_by_semi_semi c =
match List.rev c with
| h :: _ ->
let len = String.length h in
len > 2 && h.[len - 1] = ';' && h.[len - 2] = ';'
| _ -> false

let parse lines =
let lines = if ends_by_semi_semi lines then lines else lines @ [ ";;" ] in
let lines =
if Mdx.Block.ends_by_semi_semi lines then lines else lines @ [ ";;" ]
in
match parse lines with exception End_of_file -> None | t -> Some t

(** Returns the name of the toplevel directive or [None] if the given phrase
Expand Down Expand Up @@ -394,13 +389,9 @@ let rtrim l = List.rev (ltrim (List.rev l))
let trim l = ltrim (rtrim (List.map trim_line l))

let cut_into_sentences l =
let ends_by_semi_semi h =
let len = String.length h in
len > 2 && h.[len - 1] = ';' && h.[len - 2] = ';'
in
let rec aux acc sentence = function
| [] -> List.rev (List.rev sentence :: acc)
| h :: t when ends_by_semi_semi h ->
| h :: t when Mdx.Block.ends_by_semi_semi [ h ] ->
aux (List.rev (h :: sentence) :: acc) [] t
| h :: t -> aux acc (h :: sentence) t
in
Expand Down