diff options
| author | _ov4 <ov4@ov4.org> | 2026-09-09 12:39:15 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-09-09 12:39:15 +0000 |
| commit | da12cca68e56a8e75103a7a12b6cdb4299517b4e (patch) | |
| tree | a0ab23d46ab88a05a7dec425b78bab9cdb62c8be | |
| parent | ffcf768d07c82a2213674fbec623c684e8feb9ee (diff) | |
fix enum leaking and ast children conditions
| -rw-r--r-- | include/lexer/enum_type.h | 2 | ||||
| -rw-r--r-- | include/lexer/symbol_property.h | 12 | ||||
| -rw-r--r-- | module-test/lexer_test.cc | 6 | ||||
| -rw-r--r-- | src/lexer/ast.cc | 45 |
4 files changed, 32 insertions, 33 deletions
diff --git a/include/lexer/enum_type.h b/include/lexer/enum_type.h index e1c5b6f..bfc7cdc 100644 --- a/include/lexer/enum_type.h +++ b/include/lexer/enum_type.h @@ -25,7 +25,7 @@ enum guard_type GUARD_STRONG, }; -enum child_existance_type +enum class child_existence_type { NOT_EXIST, OPTIONAL, diff --git a/include/lexer/symbol_property.h b/include/lexer/symbol_property.h index 53120e0..a69d665 100644 --- a/include/lexer/symbol_property.h +++ b/include/lexer/symbol_property.h @@ -19,8 +19,8 @@ public: // require: MUST // accept: optional, but if it doesn't accept, but there is indeed a child, then error - child_existance_type left; - child_existance_type right; + child_existence_type left; + child_existence_type right; enum_token_type enum_type = TEXT; }; @@ -35,10 +35,10 @@ inline unordered_map<string, T_property> symbol_property = { {")", {.continuable = false, .enum_type = RIGHT_BRACKET}}, // operator - {"&", {.operand_number = 1, .continuable = true, .pivot_priority = 2000, .left = EXIST, .right = NOT_EXIST, .enum_type = ASYNC}}, - {"|", {.operand_number = 1, .continuable = true, .pivot_priority = 100, .left = EXIST, .right = EXIST, .enum_type = PIPE}}, - {"&&", {.operand_number = 1, .continuable = true, .pivot_priority = 1000, .left = EXIST, .right = EXIST, .enum_type = LOGIC_AND}}, - {"||", {.operand_number = 1, .continuable = true, .pivot_priority = 1000, .left = EXIST, .right = EXIST, .enum_type = LOGIC_OR}}, + {"&", {.operand_number = 1, .continuable = true, .pivot_priority = 2000, .left = child_existence_type::EXIST, .right = child_existence_type::NOT_EXIST, .enum_type = ASYNC}}, + {"|", {.operand_number = 1, .continuable = true, .pivot_priority = 100, .left = child_existence_type::EXIST, .right = child_existence_type::EXIST, .enum_type = PIPE}}, + {"&&", {.operand_number = 1, .continuable = true, .pivot_priority = 1000, .left = child_existence_type::EXIST, .right = child_existence_type::EXIST, .enum_type = LOGIC_AND}}, + {"||", {.operand_number = 1, .continuable = true, .pivot_priority = 1000, .left = child_existence_type::EXIST, .right = child_existence_type::EXIST, .enum_type = LOGIC_OR}}, }; diff --git a/module-test/lexer_test.cc b/module-test/lexer_test.cc index c94a4ef..623b9ec 100644 --- a/module-test/lexer_test.cc +++ b/module-test/lexer_test.cc @@ -34,7 +34,7 @@ constexpr int MAXLINE = 1024; int main() { - verbose = false; + verbose = true; int test_case = 13; string s; @@ -143,10 +143,12 @@ int main() print(" command: {}", lexer_instance.ast[i].command_text); println(); } + } catch(const T_error &e) { - std::cerr << e.what() << '\n'; + cerr << "Error: " << magic_enum::enum_name(e.code) << endl; + cerr << "what: " << e.what() << endl; } diff --git a/src/lexer/ast.cc b/src/lexer/ast.cc index 4fbb62a..72ad016 100644 --- a/src/lexer/ast.cc +++ b/src/lexer/ast.cc @@ -106,44 +106,41 @@ void T_lexer::parse(size_t cmd_begin, size_t cmd_end, size_t ast_vec) return; } - // left child - string token_str = symbol_enum2string(token[found_pos].token_type); // miss left - if (symbol_property[token_str].left == EXIST && cmd_begin == found_pos) - { + if (symbol_property[token_str].left == child_existence_type::EXIST && cmd_begin == found_pos) throw T_error{error_code::AST_MISS_LEFT_CHILD, "parse: missing left child"}; - } - // miss right - if (symbol_property[token_str].right == EXIST && cmd_end-1 == found_pos) - { + if (symbol_property[token_str].right == child_existence_type::EXIST && cmd_end-1 == found_pos) throw T_error{error_code::AST_MISS_RIGHT_CHILD, "parse: missing right child"}; - } - // should not exist left - if (symbol_property[token_str].left == NOT_EXIST && cmd_begin != found_pos) - { + if (symbol_property[token_str].left == child_existence_type::NOT_EXIST && cmd_begin != found_pos) throw T_error{error_code::AST_EXIST_LEFT_CHILLD, "parse: left child should NOT exist"}; - } - // should not exist right - if (symbol_property[token_str].right == NOT_EXIST && cmd_end-1 != found_pos) - { + if (symbol_property[token_str].right == child_existence_type::NOT_EXIST && cmd_end-1 != found_pos) throw T_error{error_code::AST_EXIST_LEFT_CHILLD, "parse: right child should NOT exist"}; - } - size_t l = alloc_ast(); - size_t r = alloc_ast(); - ast[ast_vec].left = l; - ast[ast_vec].right = r; - ast[ast_vec].token_type = token[found_pos].token_type; + ast[ast_vec].token_type = token[found_pos].token_type; loggerln("Parse sub: {}, {}, {}", cmd_begin, found_pos, cmd_end); - parse(cmd_begin, found_pos, l); - parse(found_pos+1, cmd_end, r); + if (symbol_property[token_str].left == child_existence_type::EXIST + || symbol_property[token_str].left == child_existence_type::OPTIONAL) + { + size_t l = alloc_ast(); + ast[ast_vec].left = l; + parse(cmd_begin, found_pos, l); + } + + if (symbol_property[token_str].left == child_existence_type::EXIST + || symbol_property[token_str].left == child_existence_type::OPTIONAL) + { + size_t r = alloc_ast(); + ast[ast_vec].right = r; + parse(found_pos+1, cmd_end, r); + } + } } |
