Skip to content

Commit 1892b89

Browse files
Adds multiple single line delimiters to assembly file + updates file types
-Adds multiple delimiters for single line comments for assembly files - Adds a more assembly file types to Lang selector dict - Adds testing to assert behaviour change is correct Signed-off-by: McGuire, Kevin <[email protected]>
1 parent 19e097e commit 1892b89

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

comment_filter/language.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
class Lang:
55
def __init__(self, line_comment, comment_bookends, nested_comments):
6-
self.line_comment = line_comment
6+
# Support both single string and list of strings for line_comment
7+
if isinstance(line_comment, str):
8+
self.line_comment = [line_comment]
9+
else:
10+
self.line_comment = line_comment
711
self.comment_bookends = comment_bookends
812
self.nested_comments = nested_comments
913
self.string_literal_start = '"'
@@ -15,7 +19,7 @@ def __init__(self, line_comment, comment_bookends, nested_comments):
1519
nested_comments=False)
1620

1721
assembly = Lang(
18-
line_comment=';',
22+
line_comment=[';', '#'],
1923
comment_bookends=[('/*', '*/')],
2024
nested_comments=False)
2125

@@ -62,7 +66,9 @@ def __init__(self, line_comment, comment_bookends, nested_comments):
6266
'.cxx': c,
6367
'.cpp': c,
6468
'.h': c,
65-
'.S': c,
69+
'.S': assembly,
70+
'.asm': assembly,
71+
'.nasm': assembly,
6672
'.java': java,
6773
'.go': go,
6874
'.hs': haskell,

comment_filter/rfc.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,16 @@ def parse_code(lang, state):
160160
while True:
161161
line = state.line
162162
multi_start_tokens = [start for start, end in lang.comment_bookends]
163-
tokens = multi_start_tokens + [
164-
lang.line_comment,
163+
tokens = multi_start_tokens + lang.line_comment + [
165164
lang.string_literal_start,
166165
lang.string_literal2_start]
167166
i = index_of_first_found(line, tokens)
168167
if i != -1:
169168
state.line = line[i:]
170169
code += line[:i]
171-
if line.startswith(lang.line_comment, i) or \
170+
# Check if any line comment delimiter starts at position i
171+
line_comment_found = line.startswith(tuple(lang.line_comment), i)
172+
if line_comment_found or \
172173
index_of_first_found(line, multi_start_tokens) == i:
173174
return code, state
174175
elif line.startswith(lang.string_literal_start, i):
@@ -274,15 +275,15 @@ def parse_line_comment(lang, state, keep_tokens=True):
274275
(string, State)
275276
"""
276277
line = state.line
277-
line_comment = lang.line_comment
278-
if line.startswith(line_comment):
279-
state.line = ''
280-
i = len(line_comment)
281-
if not keep_tokens:
282-
line_comment = ' ' * i
283-
return line_comment + line[i:], state
284-
else:
285-
return '', state
278+
# Check each possible line comment delimiter
279+
for line_comment in lang.line_comment:
280+
if line.startswith(line_comment):
281+
state.line = ''
282+
i = len(line_comment)
283+
if not keep_tokens:
284+
line_comment = ' ' * i
285+
return line_comment + line[i:], state
286+
return '', state
286287

287288

288289
def parse_multiline_comment(lang, state, keep_tokens=True):

comment_filter/rfc_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,18 @@ def test_parse_file():
263263
assert assembly_comments('/* line1\nline2\n*/line3\n') == ['/* line1\n', 'line2\n', '*/ \n']
264264

265265

266+
def test_assembly_hash_comments():
267+
"""Test that # works as line comment delimiter for assembly"""
268+
assert assembly_comments('# comment\ncode\n') == ['# comment\n', ' \n']
269+
assert assembly_comments('# hash comment\n') == ['# hash comment\n']
270+
271+
272+
def test_assembly_mixed_comments():
273+
"""Test that both ; and # work together in assembly"""
274+
assert assembly_comments('; semicolon comment\n# hash comment\ncode\n') == ['; semicolon comment\n', '# hash comment\n', ' \n']
275+
assert assembly_comments('code ; inline semicolon\ncode # inline hash\n') == [' ; inline semicolon\n', ' # inline hash\n']
276+
277+
266278
def test_parse_comments_via_reduce():
267279
def f(st, x):
268280
st.line = x

0 commit comments

Comments
 (0)