diff options
| author | _ov4 <ov4@ov4.org> | 2026-09-09 12:45:08 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-09-09 12:45:08 +0000 |
| commit | 66fd6696283507643c4b08d3bd91480ae2971e73 (patch) | |
| tree | 01fbfaa4c6621b3616888a6ca146e1c283ff7231 | |
| parent | da12cca68e56a8e75103a7a12b6cdb4299517b4e (diff) | |
make symbol_property const, and fix typo
| -rw-r--r-- | include/lexer/symbol_property.h | 14 | ||||
| -rw-r--r-- | src/lexer/ast.cc | 32 | ||||
| -rw-r--r-- | src/lexer/token.cc | 4 |
3 files changed, 23 insertions, 27 deletions
diff --git a/include/lexer/symbol_property.h b/include/lexer/symbol_property.h index a69d665..8b05c14 100644 --- a/include/lexer/symbol_property.h +++ b/include/lexer/symbol_property.h @@ -13,8 +13,10 @@ namespace ov4 class T_property { public: - int operand_number = 0; bool continuable = true; + + // -1 means it's NOT operator + // any positive values mean it's operator int pivot_priority = -1; // require: MUST @@ -25,7 +27,7 @@ public: enum_token_type enum_type = TEXT; }; -inline unordered_map<string, T_property> symbol_property = { +inline const unordered_map<string, T_property> symbol_property = { // non-operator {"\'", {.enum_type = SINGLE_QUOTATION}}, {"\"", {.enum_type = SINGLE_QUOTATION}}, @@ -35,10 +37,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 = 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}}, + {"&", {.continuable = true, .pivot_priority = 2000, .left = child_existence_type::EXIST, .right = child_existence_type::NOT_EXIST, .enum_type = ASYNC}}, + {"|", {.continuable = true, .pivot_priority = 100, .left = child_existence_type::EXIST, .right = child_existence_type::EXIST, .enum_type = PIPE}}, + {"&&", {.continuable = true, .pivot_priority = 1000, .left = child_existence_type::EXIST, .right = child_existence_type::EXIST, .enum_type = LOGIC_AND}}, + {"||", {.continuable = true, .pivot_priority = 1000, .left = child_existence_type::EXIST, .right = child_existence_type::EXIST, .enum_type = LOGIC_OR}}, }; diff --git a/src/lexer/ast.cc b/src/lexer/ast.cc index 72ad016..55b4c42 100644 --- a/src/lexer/ast.cc +++ b/src/lexer/ast.cc @@ -40,7 +40,7 @@ int T_lexer::get_pivot_order(enum_token_type x) string s = symbol_enum2string(x); if (s != "") { - return symbol_property[s].pivot_priority; + return symbol_property.at(s).pivot_priority; } // not found return -1; @@ -48,16 +48,10 @@ int T_lexer::get_pivot_order(enum_token_type x) bool T_lexer::is_operator(enum_token_type x) { - switch(x) - { - case LOGIC_AND: - case LOGIC_OR: - case PIPE: - case ASYNC: - return true; - default: - return false; - } + string s = symbol_enum2string(x); + if (s != "") + return (symbol_property.at(s).pivot_priority >= 0) ? true : false; + return false; } // region [begin, end) @@ -109,32 +103,32 @@ void T_lexer::parse(size_t cmd_begin, size_t cmd_end, size_t ast_vec) string token_str = symbol_enum2string(token[found_pos].token_type); // miss left - if (symbol_property[token_str].left == child_existence_type::EXIST && cmd_begin == found_pos) + if (symbol_property.at(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 == child_existence_type::EXIST && cmd_end-1 == found_pos) + if (symbol_property.at(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 == child_existence_type::NOT_EXIST && cmd_begin != found_pos) + if (symbol_property.at(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 == child_existence_type::NOT_EXIST && cmd_end-1 != found_pos) + if (symbol_property.at(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"}; ast[ast_vec].token_type = token[found_pos].token_type; loggerln("Parse sub: {}, {}, {}", cmd_begin, found_pos, cmd_end); - if (symbol_property[token_str].left == child_existence_type::EXIST - || symbol_property[token_str].left == child_existence_type::OPTIONAL) + if (symbol_property.at(token_str).left == child_existence_type::EXIST + || symbol_property.at(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) + if (symbol_property.at(token_str).right == child_existence_type::EXIST + || symbol_property.at(token_str).right == child_existence_type::OPTIONAL) { size_t r = alloc_ast(); ast[ast_vec].right = r; diff --git a/src/lexer/token.cc b/src/lexer/token.cc index 091a7ab..ca0545d 100644 --- a/src/lexer/token.cc +++ b/src/lexer/token.cc @@ -77,7 +77,7 @@ bool T_lexer::token_continue(const string &s, char next) T_property tmp; if (symbol_property.contains(s)) { - tmp = symbol_property[s]; + tmp = symbol_property.at(s); if (tmp.continuable) { string prev_and_next = s + next; @@ -107,7 +107,7 @@ void T_lexer::token_push(const string &push_s, int bracket_depth, bool force_tex if (symbol_property.contains(trimmed) && !force_text) // symbol { loggerln("token_push: adding symbol"); - tmp.token_type = symbol_property[trimmed].enum_type; + tmp.token_type = symbol_property.at(trimmed).enum_type; } else { |
