aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/error.h29
-rw-r--r--module-test/lexer_test.cc70
-rw-r--r--src/lexer/ast.cc13
-rw-r--r--src/lexer/token.cc1
4 files changed, 76 insertions, 37 deletions
diff --git a/include/error.h b/include/error.h
new file mode 100644
index 0000000..d8be4e5
--- /dev/null
+++ b/include/error.h
@@ -0,0 +1,29 @@
+#pragma once
+
+#include <stdexcept>
+
+namespace ov4
+{
+
+enum class error_code
+{
+
+ // tokenizer
+ TOKEN_UNMATCHED_BRACKET,
+
+ // AST
+ AST_MISS_LEFT_CHILD,
+ AST_MISS_RIGHT_CHILD,
+ AST_EXIST_LEFT_CHILLD,
+ AST_EXIST_RIGHT_CHILD,
+
+};
+
+class T_error : public std::runtime_error {
+public:
+ error_code code;
+ T_error(error_code k, std::string msg)
+ : std::runtime_error(std::move(msg)), code(k) {}
+};
+
+}
diff --git a/module-test/lexer_test.cc b/module-test/lexer_test.cc
index d3f5a14..c94a4ef 100644
--- a/module-test/lexer_test.cc
+++ b/module-test/lexer_test.cc
@@ -16,6 +16,7 @@ g++ \
#include <cstring>
#include <vector>
#include <print>
+#include <stdexcept>
#include "magic_enum/magic_enum.hpp"
@@ -23,6 +24,7 @@ g++ \
#include "lexer/enum_type.h"
#include "lexer/token.h"
#include "global.h"
+#include "error.h"
using namespace std;
using namespace ov4;
@@ -106,40 +108,50 @@ int main()
return 0;
}
-
- T_lexer lexer_instance;
-
- lexer_instance.tokenizer(s);
- cout << "---------- tokenizer ----------" << endl;
- for (auto c : lexer_instance.token)
+ try
{
- if (c.token_type == LOGIC_AND)
- println("{} LOGIC_AND", c.bracket_depth);
- else if (c.token_type == LOGIC_OR)
- println("{} LOGIC_OR", c.bracket_depth);
- else if (c.token_type == ASYNC)
- println("{} ASYNC", c.bracket_depth);
- else if (c.token_type == PIPE)
- println("{} PIPE", c.bracket_depth);
- else if (c.token_type == LEFT_BRACKET)
- println("{} LEFT_BRACKET", c.bracket_depth);
- else if (c.token_type == RIGHT_BRACKET)
- println("{} RIGHT_BRACKET", c.bracket_depth);
- else if (c.token_type == TEXT)
- println("{} TEXT .{}.", c.bracket_depth, c.text);
- }
+ T_lexer lexer_instance;
+ lexer_instance.tokenizer(s);
+ cout << "---------- tokenizer ----------" << endl;
+ for (auto c : lexer_instance.token)
+ {
+ if (c.token_type == LOGIC_AND)
+ println("{} LOGIC_AND", c.bracket_depth);
+ else if (c.token_type == LOGIC_OR)
+ println("{} LOGIC_OR", c.bracket_depth);
+ else if (c.token_type == ASYNC)
+ println("{} ASYNC", c.bracket_depth);
+ else if (c.token_type == PIPE)
+ println("{} PIPE", c.bracket_depth);
+ else if (c.token_type == LEFT_BRACKET)
+ println("{} LEFT_BRACKET", c.bracket_depth);
+ else if (c.token_type == RIGHT_BRACKET)
+ println("{} RIGHT_BRACKET", c.bracket_depth);
+ else if (c.token_type == TEXT)
+ println("{} TEXT .{}.", c.bracket_depth, c.text);
+ }
- cout << "---------- parse ----------" << endl;
+ cout << "---------- parse ----------" << endl;
- lexer_instance.parse(0, lexer_instance.token.size(), lexer_instance.alloc_ast());
+ lexer_instance.parse(0, lexer_instance.token.size(), lexer_instance.alloc_ast());
- cout << "---------- AST ----------" << endl;
- for (size_t i = 0; i < lexer_instance.ast.size(); i++)
+ cout << "---------- AST ----------" << endl;
+ for (size_t i = 0; i < lexer_instance.ast.size(); i++)
+ {
+ print("{}: type: {} left: {} right: {}", i, magic_enum::enum_name(lexer_instance.ast[i].token_type), lexer_instance.ast[i].left, lexer_instance.ast[i].right);
+ if (lexer_instance.ast[i].token_type == TEXT)
+ print(" command: {}", lexer_instance.ast[i].command_text);
+ println();
+ }
+ }
+ catch(const T_error &e)
{
- print("{}: type: {} left: {} right: {}", i, magic_enum::enum_name(lexer_instance.ast[i].token_type), lexer_instance.ast[i].left, lexer_instance.ast[i].right);
- if (lexer_instance.ast[i].token_type == TEXT)
- print(" command: {}", lexer_instance.ast[i].command_text);
- println();
+ std::cerr << e.what() << '\n';
}
+
+
+
+
+
return 0;
}
diff --git a/src/lexer/ast.cc b/src/lexer/ast.cc
index 755e909..4fbb62a 100644
--- a/src/lexer/ast.cc
+++ b/src/lexer/ast.cc
@@ -9,6 +9,7 @@
#include "lexer/token.h"
#include "lexer/symbol_property.h"
#include "util/io.h"
+#include "error.h"
using namespace std;
@@ -112,29 +113,25 @@ void T_lexer::parse(size_t cmd_begin, size_t cmd_end, size_t ast_vec)
// miss left
if (symbol_property[token_str].left == EXIST && cmd_begin == found_pos)
{
- loggerln("parse: missing left child");
- exit(-1); // TODO
+ 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)
{
- loggerln("parse: missing right child");
- exit(-1); // TODO
+ 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)
{
- loggerln("parse: left child should NOT exist");
- exit(-1); // TODO
+ 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)
{
- loggerln("parse: right child should NOT exist");
- exit(-1); // TODO
+ throw T_error{error_code::AST_EXIST_LEFT_CHILLD, "parse: right child should NOT exist"};
}
size_t l = alloc_ast();
diff --git a/src/lexer/token.cc b/src/lexer/token.cc
index 071d00c..091a7ab 100644
--- a/src/lexer/token.cc
+++ b/src/lexer/token.cc
@@ -8,6 +8,7 @@
#include "lexer/enum_type.h"
#include "lexer/symbol_property.h"
#include "util/io.h"
+#include "error.h"
using namespace std; // remove this