Skip to content

Commit 8318653

Browse files
committed
Merged master with bug fix for #474
2 parents e87c675 + 9df22b9 commit 8318653

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

cmd2/parsing.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,25 +152,23 @@ def __init__(
152152
# double or single quotes.
153153
#
154154
# this big regular expression can be broken down into 3 regular
155-
# expressions that are OR'ed together.
155+
# expressions that are OR'ed together with a pipe character
156156
#
157-
# /\*.*?(\*/|$) matches C-style comments, with an optional
158-
# closing '*/'. The optional closing '*/' is
159-
# there to retain backward compatibility with
160-
# the pyparsing implementation of cmd2 < 0.9.0
161-
# \'(?:\\.|[^\\\'])*\' matches a single quoted string, allowing
157+
# /\*.*\*/ Matches C-style comments (i.e. /* comment */)
158+
# does not match unclosed comments.
159+
# \'(?:\\.|[^\\\'])*\' Matches a single quoted string, allowing
162160
# for embedded backslash escaped single quote
163-
# marks
164-
# "(?:\\.|[^\\"])*" matches a double quoted string, allowing
161+
# marks.
162+
# "(?:\\.|[^\\"])*" Matches a double quoted string, allowing
165163
# for embedded backslash escaped double quote
166-
# marks
164+
# marks.
167165
#
168166
# by way of reminder the (?:...) regular expression syntax is just
169167
# a non-capturing version of regular parenthesis. We need the non-
170168
# capturing syntax because _comment_replacer() looks at match
171169
# groups
172170
self.comment_pattern = re.compile(
173-
r'/\*.*?(\*/|$)|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
171+
r'/\*.*\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
174172
re.DOTALL | re.MULTILINE
175173
)
176174

tests/test_parsing.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,20 @@ def test_parse_c_comment_empty(parser):
180180
assert not statement.argv
181181
assert statement == ''
182182

183+
def test_parse_c_comment_no_closing(parser):
184+
statement = parser.parse('cat /tmp/*.txt')
185+
assert statement.command == 'cat'
186+
assert statement.args == '/tmp/*.txt'
187+
assert not statement.pipe_to
188+
assert statement.argv == ['cat', '/tmp/*.txt']
189+
190+
def test_parse_c_comment_multiple_opening(parser):
191+
statement = parser.parse('cat /tmp/*.txt /tmp/*.cfg')
192+
assert statement.command == 'cat'
193+
assert statement.args == '/tmp/*.txt /tmp/*.cfg'
194+
assert not statement.pipe_to
195+
assert statement.argv == ['cat', '/tmp/*.txt', '/tmp/*.cfg']
196+
183197
def test_parse_what_if_quoted_strings_seem_to_start_comments(parser):
184198
statement = parser.parse('what if "quoted strings /* seem to " start comments?')
185199
assert statement.command == 'what'
@@ -344,14 +358,13 @@ def test_parse_multiline_command_ignores_redirectors_within_it(parser, line, ter
344358
def test_parse_multiline_with_incomplete_comment(parser):
345359
"""A terminator within a comment will be ignored and won't terminate a multiline command.
346360
Un-closed comments effectively comment out everything after the start."""
347-
line = 'multiline command /* with comment in progress;'
361+
line = 'multiline command /* with unclosed comment;'
348362
statement = parser.parse(line)
349363
assert statement.multiline_command == 'multiline'
350364
assert statement.command == 'multiline'
351-
assert statement.args == 'command'
352-
assert statement == statement.args
353-
assert statement.argv == ['multiline', 'command']
354-
assert not statement.terminator
365+
assert statement.args == 'command /* with unclosed comment'
366+
assert statement.argv == ['multiline', 'command', '/*', 'with', 'unclosed', 'comment']
367+
assert statement.terminator == ';'
355368

356369
def test_parse_multiline_with_complete_comment(parser):
357370
line = 'multiline command /* with comment complete */ is done;'

0 commit comments

Comments
 (0)