Skip to content

Commit

Permalink
fix #99: implement commented string_quote_removal
Browse files Browse the repository at this point in the history
  • Loading branch information
WillForan committed Dec 30, 2024
1 parent 3f6b76c commit c8058c1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
18 changes: 16 additions & 2 deletions bashlex/heredoc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from bashlex import ast, errors
import re

def gatherheredocuments(tokenizer):
# if we're at the end of the input and we're not strict, allow skipping
Expand All @@ -11,9 +12,22 @@ def gatherheredocuments(tokenizer):
redirnode, killleading = tokenizer.redirstack.pop(0)
makeheredoc(tokenizer, redirnode, 0, killleading)


def string_quote_removal(word):
"""remove single quotes for heredoc
>>> string_quote_removal("'EOF'")
'EOF'
>>> string_quote_removal("EOF")
'EOF'
"""
quote_match = re.search("^'(.*)'$", word)
if quote_match:
word = quote_match.group(1)
return word


def makeheredoc(tokenizer, redirnode, lineno, killleading):
# redirword = string_quote_removal(redirectnode.word)
redirword = redirnode.output.word
redirword = string_quote_removal(redirnode.output.word)
document = []

startpos = tokenizer._shell_input_line_index
Expand Down
17 changes: 17 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,23 @@ def test_heredoc_with_actual_doc(self):
"delimited by end-of-file \\(wanted 'EOF'",
parse, s)

def test_heredoc_singlequotes(self):
doc = 'foo\nbar\nEOF'
s = '''a <<'EOF'
%s''' % doc

self.assertASTEquals(s,
commandnode("a <<'EOF'",
wordnode('a'),
redirectnode("<<'EOF'\n%s" % doc, None, '<<', wordnode("'EOF'"),
heredocnode(doc))
))

s = "a <<'EOF'\nb"
self.assertRaisesRegex(errors.ParsingError,
"delimited by end-of-file \\(wanted 'EOF'",
parse, s)

def test_herestring(self):
s = 'a <<<"b\nc"'
self.assertASTEquals(s,
Expand Down

0 comments on commit c8058c1

Please sign in to comment.