From 67a111cd5bf6a30fdb45f5142ae10cc11b8c9128 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 26 Jun 2026 07:45:47 +0300 Subject: [PATCH] feat: unclosed expression test added and fulfilled --- test/test_parse_unclosed_expression_2.wbt | 6 ++++++ test_parse.cpp | 18 +++++++++++++----- wbt/wbt.cpp | 18 +++++++++++++++--- 3 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 test/test_parse_unclosed_expression_2.wbt diff --git a/test/test_parse_unclosed_expression_2.wbt b/test/test_parse_unclosed_expression_2.wbt new file mode 100644 index 0000000..ea66218 --- /dev/null +++ b/test/test_parse_unclosed_expression_2.wbt @@ -0,0 +1,6 @@ + + +

Hello World

+

{{ INVALID EXPRESSION

+ + \ No newline at end of file diff --git a/test_parse.cpp b/test_parse.cpp index 69ab16f..a3f1e0b 100644 --- a/test_parse.cpp +++ b/test_parse.cpp @@ -31,15 +31,23 @@ void test_parse_unclosed_expression() { p.parse(); } +void test_parse_unclosed_expression_2() { + std::ifstream i("test/test_parse_unclosed_expression_2.wbt", std::ios_base::in); + std::ostream o(std::cout.rdbuf()); + const WBT_Parser p(i, o); + p.parse(); +} int main() { - std::cout << std::endl << "-----" << std::endl; + std::cout << std::endl << "1 -----" << std::endl; test_parse_no_expressions(); - std::cout << std::endl << "-----" << std::endl; + std::cout << std::endl << "2 -----" << std::endl; test_parse_single_expression(); - std::cout << std::endl << "-----" << std::endl; + std::cout << std::endl << "3 -----" << std::endl; test_parse_invalid_character_expression(); - std::cout << std::endl << "-----" << std::endl; + std::cout << std::endl << "4 -----" << std::endl; test_parse_unclosed_expression(); - std::cout << std::endl << "-----" << std::endl; + std::cout << std::endl << "5 -----" << std::endl; + test_parse_unclosed_expression_2(); + std::cout << std::endl << "END -----" << std::endl; } diff --git a/wbt/wbt.cpp b/wbt/wbt.cpp index cfa52bb..bde3848 100644 --- a/wbt/wbt.cpp +++ b/wbt/wbt.cpp @@ -5,13 +5,20 @@ module; // This module is the transpiler from WBT to HTML for site build export module wbt; -export enum WBT_States { +enum WBT_States { WBT_STATE_INIT, WBT_STATE_OPEN, WBT_STATE_EXPRESSION, WBT_STATE_CLOSE, }; +struct WBT_Parser_Internal_Data { + int32_t line = 1; + int32_t column = 1; + int32_t expr_start_line = 1; + int32_t expr_start_column = 1; +}; + export class WBT_Parser { std::istream& input; std::ostream& output; @@ -38,8 +45,7 @@ public: void parse() const { auto state = WBT_STATE_INIT; std::string expr; - auto line = 1; - auto column = 1; + auto [line, column, expr_start_line, expr_start_column] = WBT_Parser_Internal_Data(); while (input.peek() != std::char_traits::eof()) { char c; input.get(c); @@ -47,6 +53,8 @@ public: switch (state) { case WBT_STATE_INIT: if (c == '{') { + expr_start_line = line; + expr_start_column = column; state = WBT_STATE_OPEN; } else { output_char(c); @@ -89,5 +97,9 @@ public: column++; } } + // Final state cannot be WBT_STATE_EXPRESSION + if (state == WBT_STATE_EXPRESSION) { + error("Unclosed expression", expr_start_line, expr_start_column); + } } };