Skip to content

Commit 197cf57

Browse files
committed
fix #99: implement commented string_quote_removal
1 parent 3f6b76c commit 197cf57

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

bashlex/heredoc.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from bashlex import ast, errors
2+
import re
23

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

15+
16+
def string_quote_removal(word):
17+
"""remove single quotes for heredoc
18+
>>> string_quote_removal("'EOF'")
19+
'EOF'
20+
>>> string_quote_removal("EOF")
21+
'EOF'
22+
"""
23+
if m := re.search("^'(.*)'$", word):
24+
word = m.group(1)
25+
return word
26+
1427
def makeheredoc(tokenizer, redirnode, lineno, killleading):
15-
# redirword = string_quote_removal(redirectnode.word)
16-
redirword = redirnode.output.word
28+
redirword = string_quote_removal(redirnode.output.word)
1729
document = []
1830

1931
startpos = tokenizer._shell_input_line_index

tests/test_parser.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,23 @@ def test_heredoc_with_actual_doc(self):
779779
"delimited by end-of-file \\(wanted 'EOF'",
780780
parse, s)
781781

782+
def test_heredoc_singlequotes(self):
783+
doc = 'foo\nbar\nEOF'
784+
s = '''a <<'EOF'
785+
%s''' % doc
786+
787+
self.assertASTEquals(s,
788+
commandnode("a <<'EOF'",
789+
wordnode('a'),
790+
redirectnode("<<'EOF'\n%s" % doc, None, '<<', wordnode("'EOF'"),
791+
heredocnode(doc))
792+
))
793+
794+
s = "a <<'EOF'\nb"
795+
self.assertRaisesRegex(errors.ParsingError,
796+
"delimited by end-of-file \\(wanted 'EOF'",
797+
parse, s)
798+
782799
def test_herestring(self):
783800
s = 'a <<<"b\nc"'
784801
self.assertASTEquals(s,

0 commit comments

Comments
 (0)