diff --git a/cpp/grammar_parser.cc b/cpp/grammar_parser.cc index 5caf8094..c5139e1f 100644 --- a/cpp/grammar_parser.cc +++ b/cpp/grammar_parser.cc @@ -92,10 +92,11 @@ void EBNFParser::ConsumeSpace(bool allow_newline) { (allow_newline && (Peek() == '\n' || Peek() == '\r')))) { Consume(); if (Peek(-1) == '#') { + auto start = cur_column_ - 1; while (Peek() && Peek() != '\n' && Peek() != '\r') { Consume(); } - if (!Peek()) { + if (!Peek() || start != 1 /* Reserve \n for inline comment */) { return; } Consume(); diff --git a/tests/python/test_grammar_parser.py b/tests/python/test_grammar_parser.py index fe594e04..d86c1789 100644 --- a/tests/python/test_grammar_parser.py +++ b/tests/python/test_grammar_parser.py @@ -18,6 +18,20 @@ def test_bnf_simple(): after = str(grammar) assert after == expected +def test_bnf_comment(): + before = """# top comment +root ::= a b # inline comment +a ::= "a" +b ::= "b" +# bottom comment +""" + expected = """root ::= ((a b)) +a ::= (("a")) +b ::= (("b")) +""" + grammar = xgr.Grammar.from_ebnf(before) + after = str(grammar) + assert after == expected def test_ebnf(): before = """root ::= b c | b root