Skip to content

Commit b669302

Browse files
committed
string_quote_removal: rm escaped and double quotes (#99)
1 parent c8058c1 commit b669302

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

bashlex/heredoc.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,32 @@ def gatherheredocuments(tokenizer):
1414

1515

1616
def string_quote_removal(word):
17-
"""remove single quotes for heredoc
17+
"""
18+
Remove surrounding quotes for heredoc token.
19+
This function should also remove escaped quotes (\\", \\')
20+
See definition in bash's source
21+
https://github.com/bminor/bash/blob/master/subst.c#L11892
22+
23+
>>> string_quote_removal("EOF")
24+
'EOF'
1825
>>> string_quote_removal("'EOF'")
1926
'EOF'
20-
>>> string_quote_removal("EOF")
27+
>>> string_quote_removal('"EOF"')
28+
'EOF'
29+
>>> string_quote_removal('\\"EOF\\"')
2130
'EOF'
31+
32+
# TODO: confirm only first level of surrounding quotes should be removed
33+
>>> string_quote_removal('""EOF""')
34+
'"EOF"'
35+
>>> string_quote_removal('"\\"EOF"\\"')
36+
'\"EOF\"'
2237
"""
23-
quote_match = re.search("^'(.*)'$", word)
38+
# match input starting with a single, double, or escaped quote (' " \" \')
39+
# and ending with the same (optionally escaped) quote
40+
quote_match = re.search("^(\\\\*[\"'])(.*)\\1$", word)
2441
if quote_match:
25-
word = quote_match.group(1)
42+
word = quote_match.group(2)
2643
return word
2744

2845

0 commit comments

Comments
 (0)