From 022dcfed314b7794139a0041d2eae01318d1b88e Mon Sep 17 00:00:00 2001 From: Matthias Urlichs Date: Thu, 4 Jan 2024 18:47:41 +0100 Subject: [PATCH] Fix PEG parsers' "comment" rule The problem with the previous version was that if there is no text after the '//', regular whitespace processing kicked in and the regexp was applied to the next non-empty line. The effect of this was that the next line gets commented off unconditionally, which is kindof not what you want. --- CHANGELOG.md | 2 ++ arpeggio/cleanpeg.py | 2 +- arpeggio/peg.py | 4 ++-- arpeggio/tests/test_peg_parser.py | 1 + 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b91cb3..6e5f4a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,8 @@ please take a look at related PRs and issues and see if the change affects you. - fix: #98 suppressed match in zero-or-more [#98]. Thanks @vpavlu for reporting the issue. +- fix: empty comments in .peg files hid the next line, commented or not. + [#101]: https://github.com/textX/Arpeggio/issues/101 [#98]: https://github.com/textX/Arpeggio/issues/98 [#96]: https://github.com/textX/Arpeggio/issues/96 diff --git a/arpeggio/cleanpeg.py b/arpeggio/cleanpeg.py index b503428..af5c602 100644 --- a/arpeggio/cleanpeg.py +++ b/arpeggio/cleanpeg.py @@ -58,7 +58,7 @@ def rule_name(): return _(r"[a-zA-Z_]([a-zA-Z_]|[0-9])*") def rule_crossref(): return rule_name def str_match(): return _(r'''(?s)('[^'\\]*(?:\\.[^'\\]*)*')|''' r'''("[^"\\]*(?:\\.[^"\\]*)*")''') -def comment(): return "//", _(".*\n") +def comment(): return _("//.*\n", multiline=False) class ParserPEG(ParserPEGOrig): diff --git a/arpeggio/peg.py b/arpeggio/peg.py index bbc8717..e6f6ec9 100644 --- a/arpeggio/peg.py +++ b/arpeggio/peg.py @@ -67,8 +67,8 @@ def regex(): return [("r'", _(r'''[^'\\]*(?:\\.[^'\\]*)*'''), "'"), def rule_name(): return _(r"[a-zA-Z_]([a-zA-Z_]|[0-9])*") def rule_crossref(): return rule_name def str_match(): return _(r'''(?s)('[^'\\]*(?:\\.[^'\\]*)*')|''' - r'''("[^"\\]*(?:\\.[^"\\]*)*")''') -def comment(): return "//", _(".*\n") + r'''("[^"\\]*(?:\\.[^"\\]*)*")''') +def comment(): return _("//.*\n", multiline=False) # Escape sequences supported in PEG literal string matches diff --git a/arpeggio/tests/test_peg_parser.py b/arpeggio/tests/test_peg_parser.py index 22e45a5..c796898 100644 --- a/arpeggio/tests/test_peg_parser.py +++ b/arpeggio/tests/test_peg_parser.py @@ -16,6 +16,7 @@ factor <- ("+" / "-")? (number / "(" expression ")"); // This is another comment + // term <- factor (( "*" / "/") factor)*; expression <- term (("+" / "-") term)*; calc <- expression+ EOF;