Skip to content

Commit

Permalink
Move merge_leading_literals to CSTHelpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Niols committed Sep 17, 2023
1 parent acdb35c commit e2b0d81
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/CSTHelpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,17 @@ let io_redirect_list_of_simple_command = function
io_redirect_list_of_cmd_suffix cmd_suffix'.value
| SimpleCommand_CmdName _ ->
[]

let merge_leading_literals =
let buf = Buffer.create 80 in
let rec extract_leading_literals = function
| WordLiteral lit :: rest ->
Buffer.add_string buf lit;
extract_leading_literals rest
| rest -> rest
in
fun word ->
let rest = extract_leading_literals word in
let lit = Buffer.contents buf in
Buffer.reset buf;
if lit = "" then word else WordLiteral lit :: rest
3 changes: 3 additions & 0 deletions src/CSTHelpers.mli
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ val io_redirect_list_of_cmd_prefix : cmd_prefix -> io_redirect' list
val io_redirect_list_of_cmd_suffix : cmd_suffix -> io_redirect' list
val io_redirect_list_of_simple_command : simple_command -> io_redirect' list
val io_redirect_list_of_redirect_list : redirect_list -> io_redirect' list

(** Merges several leading [WordLiteral] into one. *)
val merge_leading_literals : word_cst -> word_cst
17 changes: 1 addition & 16 deletions src/tildePrefix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,10 @@ let extract_tilde_prefix_from_literal (literal : string) : word_cst =
let (first, rest) = ExtPervasives.string_split i literal in
[WordTildePrefix (strip_tilde first); WordLiteral rest]

(** Merges several leading [WordLiteral] into one. *)
let merge_leading_literals : word_cst -> word_cst =
let buf = Buffer.create 80 in
let rec extract_leading_literals = function
| WordLiteral lit :: rest ->
Buffer.add_string buf lit;
extract_leading_literals rest
| rest -> rest
in
fun word ->
let rest = extract_leading_literals word in
let lit = Buffer.contents buf in
Buffer.reset buf;
if lit = "" then word else WordLiteral lit :: rest

(** Extracts the tilde-prefix at the beginning of the given word CST if there is
one. Otherwise, returns the word as-is. *)
let extract_tilde_prefix_from_word_if_present (word : word_cst) : word_cst =
match merge_leading_literals word with
match CSTHelpers.merge_leading_literals word with
| WordLiteral literal :: word when starts_with_tilde literal ->
extract_tilde_prefix_from_literal literal @ word
| word -> word
Expand Down

0 comments on commit e2b0d81

Please sign in to comment.