From 95ea04978d8ecc70029722a27ba429b63611ccb6 Mon Sep 17 00:00:00 2001 From: Lukas Kaufmann Date: Sun, 17 Jan 2021 21:13:28 +0100 Subject: [PATCH 1/2] ruby multiline support --- comment_parser/parsers/ruby_parser.py | 11 +++++++++-- comment_parser/parsers/tests/ruby_parser_test.py | 11 ++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/comment_parser/parsers/ruby_parser.py b/comment_parser/parsers/ruby_parser.py index 5e042e9..a97b6e2 100644 --- a/comment_parser/parsers/ruby_parser.py +++ b/comment_parser/parsers/ruby_parser.py @@ -21,7 +21,9 @@ def extract_comments(code): """ pattern = r""" (?P ([\"'])((?:\\\2|(?:(?!\2)).)*)(\2)) | - (?P \#(?P.*?)$) + (?P \#(?P.*?)$) | + (?P ^=begin$(?P(.|\n)*?)?^=end$) | + (?P ^=begin$\*(.*)?) """ compiled = re.compile(pattern, re.VERBOSE | re.MULTILINE) @@ -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 diff --git a/comment_parser/parsers/tests/ruby_parser_test.py b/comment_parser/parsers/tests/ruby_parser_test.py index 44babf2..6496dd1 100644 --- a/comment_parser/parsers/tests/ruby_parser_test.py +++ b/comment_parser/parsers/tests/ruby_parser_test.py @@ -6,7 +6,7 @@ from comment_parser.parsers import ruby_parser -class ShellParserTest(unittest.TestCase): +class RubyParserTest(unittest.TestCase): def testComment(self): code = '# comment' @@ -63,3 +63,12 @@ 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) From 4ae2750f82c12f73f41bd935f86bcf068b2e72cd Mon Sep 17 00:00:00 2001 From: Lukas Kaufmann Date: Sun, 17 Jan 2021 23:56:47 +0100 Subject: [PATCH 2/2] Removed preceeding and trailing \n from Comment --- comment_parser/parsers/ruby_parser.py | 2 +- comment_parser/parsers/tests/ruby_parser_test.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/comment_parser/parsers/ruby_parser.py b/comment_parser/parsers/ruby_parser.py index a97b6e2..3f25075 100644 --- a/comment_parser/parsers/ruby_parser.py +++ b/comment_parser/parsers/ruby_parser.py @@ -22,7 +22,7 @@ def extract_comments(code): pattern = r""" (?P ([\"'])((?:\\\2|(?:(?!\2)).)*)(\2)) | (?P \#(?P.*?)$) | - (?P ^=begin$(?P(.|\n)*?)?^=end$) | + (?P ^=begin\n(?P(.|\n)*?)?\n=end$) | (?P ^=begin$\*(.*)?) """ compiled = re.compile(pattern, re.VERBOSE | re.MULTILINE) diff --git a/comment_parser/parsers/tests/ruby_parser_test.py b/comment_parser/parsers/tests/ruby_parser_test.py index 6496dd1..c9e0eb3 100644 --- a/comment_parser/parsers/tests/ruby_parser_test.py +++ b/comment_parser/parsers/tests/ruby_parser_test.py @@ -69,6 +69,9 @@ def testMultilineComment(self): '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) + common.Comment( + "This is a multiline comment.It is not terminated by =end ", + 1, + multiline=True) ] self.assertEqual(comments, expected)