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

fix #99: implement commented string_quote_removal #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading