Skip to content
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
11 changes: 9 additions & 2 deletions comment_parser/parsers/ruby_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def extract_comments(code):
"""
pattern = r"""
(?P<literal> ([\"'])((?:\\\2|(?:(?!\2)).)*)(\2)) |
(?P<single> \#(?P<single_content>.*?)$)
(?P<single> \#(?P<single_content>.*?)$) |
(?P<multi> ^=begin\n(?P<multi_content>(.|\n)*?)?\n=end$) |
(?P<error> ^=begin$\*(.*)?)
"""
compiled = re.compile(pattern, re.VERBOSE | re.MULTILINE)

Expand All @@ -40,5 +42,10 @@ def extract_comments(code):
comment_content = match.group("single_content")
comment = common.Comment(comment_content, line_no + 1)
comments.append(comment)

elif kind == "multi":
comment_content = match.group("multi_content")
comment = common.Comment(comment_content, line_no + 1, multiline=True)
comments.append(comment)
elif kind == "error":
raise common.UnterminatedCommentError()
return comments
14 changes: 13 additions & 1 deletion comment_parser/parsers/tests/ruby_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from comment_parser.parsers import ruby_parser


class ShellParserTest(unittest.TestCase):
class RubyParserTest(unittest.TestCase):

def testComment(self):
code = '# comment'
Expand Down Expand Up @@ -63,3 +63,15 @@ def testDifferentLiteralsSeparatedByComment(self):
comments = ruby_parser.extract_comments(code)
expected = [common.Comment(code[11:], 1, multiline=False)]
self.assertEqual(comments, expected)

def testMultilineComment(self):
code = '=begin\nThis is a multiline comment.' \
'It is not terminated by =end \n=end'
comments = ruby_parser.extract_comments(code)
expected = [
common.Comment(
"This is a multiline comment.It is not terminated by =end ",
1,
multiline=True)
]
self.assertEqual(comments, expected)