From 118837f6d8461b1fafddcef271a205ae01202528 Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Mon, 6 Jun 2022 03:08:34 +0200 Subject: [PATCH] Fixed multiline comment bug We forgot to skip '-' when parsing multiline comment begining --- src/errors.cc | 6 +++--- src/lexer.cc | 46 +++++++++++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/errors.cc b/src/errors.cc index fb4c624..b11b834 100644 --- a/src/errors.cc +++ b/src/errors.cc @@ -86,7 +86,7 @@ void assert(bool condition, std::string message, Location loc) encourage_contact(std::cerr); - std::exit(1); + std::exit(42); } [[noreturn]] void unimplemented(std::string_view message, Location loc) @@ -99,14 +99,14 @@ void assert(bool condition, std::string message, Location loc) encourage_contact(std::cerr); - std::exit(1); + std::exit(42); } [[noreturn]] void unreachable(Location loc) { error_heading(std::cerr, loc, Error_Level::Bug, "Reached unreachable state"); encourage_contact(std::cerr); - std::exit(1); + std::exit(42); } std::ostream& operator<<(std::ostream& os, Error const& err) diff --git a/src/lexer.cc b/src/lexer.cc index 2c80972..cd4d3e5 100644 --- a/src/lexer.cc +++ b/src/lexer.cc @@ -24,25 +24,35 @@ static_assert(Keywords.size() == Keywords_Count, "Table above should contain all void Lexer::skip_whitespace_and_comments() { for (;;) { - bool done_something = false; - - while (consume_if(unicode::is_space)) { - done_something = true; + { // Skip whitespace + bool done_something = false; + while (consume_if(unicode::is_space)) done_something = true; + if (done_something) continue; } - // #! line comments - if (consume_if('#', '!')) { - done_something = true; - while (peek() && peek() != '\n') { - consume(); + { // Skip #! comments + if (consume_if('#', '!')) { + while (peek() && peek() != '\n') consume(); + continue; } } // -- line and multiline coments if (consume_if('-', '-')) { - done_something = true; - if (consume_if('-')) { - // multiline + if (!consume_if('-')) { + // skiping a single line comment requires advancing until newline + while (peek() && peek() != '\n') consume(); + continue; + } + + // multiline comments begins with at least 3 '-' and ends with at least 3 '-' + // so when we encounter 3 '-', we must explicitly skip remaining '-', so they + // wil not end multiline comment + + // skip remaining from multiline comment begining + while (consume_if('-')) {} + + { // parse multiline comment until '---' marking end unsigned count = 0; while (count < 3) if (consume_if('-')) { ++count; @@ -50,17 +60,15 @@ void Lexer::skip_whitespace_and_comments() consume(); count = 0; } + // skip remaining from multiline comment ending while (consume_if('-')) {} - } else { - // single line - while (peek() && peek() != '\n') { - consume(); - } } + continue; } - if (not done_something) - break; + // When we reached this place, we didn't skip any whitespace or comment + // so our work is done + break; } }