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

Strip tilde prefixes from WordTildePrefix #164

Merged
merged 23 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e5f4702
Expect no tilde in `WordTildePrefix`
Niols May 8, 2023
88d6a2c
Add two test cases for tilde prefixes
Niols May 8, 2023
cda079a
Add two more cases for non-assignment words
Niols May 9, 2023
79b685e
Strip tilde prefix in `WordTildePrefix`
Niols May 8, 2023
36832ce
Document the `TildePrefix` module
Niols May 8, 2023
d61a2a9
Rewrite the `TildePrefix` entirely
Niols May 9, 2023
5ca22ee
Also generate CST printers
Niols May 9, 2023
368c561
More tests and update expected file
Niols May 9, 2023
7ce0eac
Merge remote-tracking branch 'origin/main' into tilde-prefixes
Niols May 10, 2023
01bbdce
Merge branch 'main' into tilde-prefixes
Niols May 12, 2023
f32bb2c
Merge branch 'main' into tilde-prefixes
Niols May 12, 2023
1676c76
Merge branch 'main' into tilde-prefixes
Niols May 24, 2023
e2410a1
Add an `ExtList` module with `ft`, `bd` and `cr`
Niols May 24, 2023
759df5b
Add tests for `ft`, `bd` and `cr`
Niols May 24, 2023
4f88046
Replace `extract_list_head_and_foot` by `cr`, etc.
Niols May 24, 2023
cbeceda
Document `string_split`
Niols May 24, 2023
c5bdfbe
Add test cases for `string_split`
Niols May 24, 2023
e928623
More efficient `extract_tilde_prefix_from_literal`
Niols May 24, 2023
02af6a6
More efficient `merge_leading_literals`
Niols May 24, 2023
4b7dbdf
Bump minimal `QCheck` dependency to 0.18
Niols May 24, 2023
71de5ab
Fix documentation of `recognize`
Niols May 24, 2023
50749e9
Clearer `merge_leading_literals`
Niols May 24, 2023
736705d
Reset the buffer as soon as possible
Niols May 24, 2023
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
25 changes: 23 additions & 2 deletions src/tildePrefix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,30 @@
*)
open CST

(** Remove the ['~'] character at the beginning of the given string or fail with
[Invalid_arg] if the string is empty or does not start with a tilde. *)
let strip_tilde s =
if s = "" || s.[0] != '~' then
invalid_arg "strip_tilde";
String.(sub s 1 (length s - 1))

(** Takes a string assumed to come from a [WordLiteral] and to start with ['~']
and splits it between a [WordTildePrefix] containing everything between
['~'] and the first ['/'] and a [WordLiteral] containing everything else. *)
let find_login s =
match String.split_on_char '/' s with
| login :: rem -> [WordTildePrefix login; WordLiteral (String.concat "/" rem)]
| _ -> assert false (* Because there is slash, or not. *)
| login :: rem ->
[
WordTildePrefix (strip_tilde login);
WordLiteral (String.concat "/" rem)
]
| _ ->
(* [String.split_on_char] yields a list of at least one element. *)
assert false

(** Analyse a [word_component] and lifts the [WordLiteral] that start with a
['~'] in the right places to a [WordTildePrefix] followed by a
[WordLiteral]. *)
let rec make_tilde_prefix_explicit rhs_assignment = function
| (WordLiteral s) as cst when s <> "" ->
if s.[0] = '~' then (
Expand All @@ -57,5 +76,7 @@ let rec make_tilde_prefix_explicit rhs_assignment = function
| cst ->
[cst]

(** Crawls through the [word_component]s of a [word] and process them. See
Niols marked this conversation as resolved.
Show resolved Hide resolved
{!make_tilde_prefix_explicit}. *)
and recognize ?(rhs_assignment=false) csts =
List.(flatten (map (make_tilde_prefix_explicit rhs_assignment) csts))
4 changes: 4 additions & 0 deletions tests/good/2.6-word-expansions/2.6.1-tilde-prefix/login.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
echo ~knuth/foo/bar
echo ~kn:/bonjour:~/bonsoir:and/this/one
echo /bonjour:~kn:~/bonsoir:and/this/one
X=~kn:~/bin:~darkvador/secret
Y=~kn:/bonjour:~/bonsoir:and/this/one
Z=/bonjour:~kn:~/bonsoir:and/this/one
238 changes: 179 additions & 59 deletions tests/good/2.6-word-expansions/2.6.1-tilde-prefix/login.sh.expected
Original file line number Diff line number Diff line change
@@ -1,52 +1,198 @@
[
"Program_LineBreak_CompleteCommands_LineBreak",
[
"LineBreak_Empty"
],
[ "LineBreak_Empty" ],
[
"CompleteCommands_CompleteCommands_NewlineList_CompleteCommand",
[
"CompleteCommands_CompleteCommand",
"CompleteCommands_CompleteCommands_NewlineList_CompleteCommand",
[
"CompleteCommand_CList",
"CompleteCommands_CompleteCommands_NewlineList_CompleteCommand",
[
"CList_AndOr",
"CompleteCommands_CompleteCommands_NewlineList_CompleteCommand",
[
"AndOr_Pipeline",
"CompleteCommands_CompleteCommands_NewlineList_CompleteCommand",
[
"Pipeline_PipeSequence",
"CompleteCommands_CompleteCommand",
[
"PipeSequence_Command",
"CompleteCommand_CList",
[
"Command_SimpleCommand",
"CList_AndOr",
[
"SimpleCommand_CmdName_CmdSuffix",
"AndOr_Pipeline",
[
"CmdName_Word",
"Pipeline_PipeSequence",
[
"Word",
"echo",
"PipeSequence_Command",
[
"Command_SimpleCommand",
[
"WordName",
"echo"
"SimpleCommand_CmdName_CmdSuffix",
[
"CmdName_Word",
[ "Word", "echo", [ [ "WordName", "echo" ] ] ]
],
[
"CmdSuffix_Word",
[
"Word",
"~knuth/foo/bar",
[
[ "WordTildePrefix", "knuth" ],
[ "WordLiteral", "/foo/bar" ]
]
]
]
]
]
]
],
]
]
]
]
],
[ "NewLineList_NewLine" ],
[
"CompleteCommand_CList",
[
"CList_AndOr",
[
"AndOr_Pipeline",
[
"Pipeline_PipeSequence",
[
"CmdSuffix_Word",
"PipeSequence_Command",
[
"Word",
"~knuth/foo/bar",
"Command_SimpleCommand",
[
"SimpleCommand_CmdName_CmdSuffix",
[
"WordTildePrefix",
"~knuth"
"CmdName_Word",
[ "Word", "echo", [ [ "WordName", "echo" ] ] ]
],
[
"WordLiteral",
"foo/bar"
"CmdSuffix_Word",
[
"Word",
"~kn:/bonjour:~/bonsoir:and/this/one",
[
[ "WordTildePrefix", "kn:" ],
[ "WordLiteral", "/bonjour:~/bonsoir:and/this/one" ]
]
]
]
]
]
]
]
]
]
]
],
[ "NewLineList_NewLine" ],
[
"CompleteCommand_CList",
[
"CList_AndOr",
[
"AndOr_Pipeline",
[
"Pipeline_PipeSequence",
[
"PipeSequence_Command",
[
"Command_SimpleCommand",
[
"SimpleCommand_CmdName_CmdSuffix",
[
"CmdName_Word",
[ "Word", "echo", [ [ "WordName", "echo" ] ] ]
],
[
"CmdSuffix_Word",
[
"Word",
"/bonjour:~kn:~/bonsoir:and/this/one",
[
[ "WordLiteral", "/bonjour:~kn:~/bonsoir:and/this/one" ]
]
]
]
]
]
]
]
]
]
]
],
[ "NewLineList_NewLine" ],
[
"CompleteCommand_CList",
[
"CList_AndOr",
[
"AndOr_Pipeline",
[
"Pipeline_PipeSequence",
[
"PipeSequence_Command",
[
"Command_SimpleCommand",
[
"SimpleCommand_CmdPrefix",
[
"CmdPrefix_AssignmentWord",
[
[ "Name", "X" ],
[
"Word",
"~kn:~/bin:~darkvador/secret",
[
[ "WordTildePrefix", "kn" ],
[ "WordLiteral", ":" ],
[ "WordTildePrefix", "" ],
[ "WordLiteral", "/bin:" ],
[ "WordTildePrefix", "darkvador" ],
[ "WordLiteral", "/secret" ]
]
]
]
]
]
]
]
]
]
]
]
],
[ "NewLineList_NewLine" ],
[
"CompleteCommand_CList",
[
"CList_AndOr",
[
"AndOr_Pipeline",
[
"Pipeline_PipeSequence",
[
"PipeSequence_Command",
[
"Command_SimpleCommand",
[
"SimpleCommand_CmdPrefix",
[
"CmdPrefix_AssignmentWord",
[
[ "Name", "Y" ],
[
"Word",
"~kn:/bonjour:~/bonsoir:and/this/one",
[
[ "WordTildePrefix", "kn" ],
[ "WordLiteral", ":/bonjour:" ],
[ "WordTildePrefix", "" ],
[ "WordLiteral", "/bonsoir:and/this/one" ]
]
]
]
Expand All @@ -59,9 +205,7 @@
]
]
],
[
"NewLineList_NewLine"
],
[ "NewLineList_NewLine" ],
[
"CompleteCommand_CList",
[
Expand All @@ -79,38 +223,16 @@
[
"CmdPrefix_AssignmentWord",
[
[
"Name",
"X"
],
[ "Name", "Z" ],
[
"Word",
"~kn:~/bin:~darkvador/secret",
"/bonjour:~kn:~/bonsoir:and/this/one",
[
[
"WordTildePrefix",
"~kn"
],
[
"WordLiteral",
""
],
[
"WordTildePrefix",
"~"
],
[
"WordLiteral",
"bin"
],
[
"WordTildePrefix",
"~darkvador"
],
[
"WordLiteral",
"secret"
]
[ "WordLiteral", "/bonjour:" ],
[ "WordTildePrefix", "kn" ],
[ "WordLiteral", ":" ],
[ "WordTildePrefix", "" ],
[ "WordLiteral", "/bonsoir:and/this/one" ]
]
]
]
Expand All @@ -123,7 +245,5 @@
]
]
],
[
"LineBreak_Empty"
]
[ "LineBreak_Empty" ]
]