aboutsummaryrefslogtreecommitdiff
path: root/include/lexer
diff options
context:
space:
mode:
Diffstat (limited to 'include/lexer')
-rw-r--r--include/lexer/ast.h16
-rw-r--r--include/lexer/lexer_class.h41
-rw-r--r--include/lexer/shared.h8
-rw-r--r--include/lexer/token.h16
4 files changed, 56 insertions, 25 deletions
diff --git a/include/lexer/ast.h b/include/lexer/ast.h
index 640547f..b823c09 100644
--- a/include/lexer/ast.h
+++ b/include/lexer/ast.h
@@ -2,18 +2,16 @@
#include <string>
+#include "lexer/lexer_class.h"
#include "lexer/enum_type.h"
#include "lexer/token.h"
-#include "lexer/shared.h"
using namespace std; // remove this
namespace ov4
{
-class T_token;
-
-class T_ast
+class T_lexer::T_ast
{
public:
T_ast() = default;
@@ -24,7 +22,7 @@ public:
size_t right = string::npos;
};
-string final_trim(size_t _begin, size_t _end)
+string T_lexer::final_trim(size_t _begin, size_t _end)
{
string s;
for (size_t i = _begin; i < _end; i++)
@@ -37,13 +35,13 @@ string final_trim(size_t _begin, size_t _end)
return s;
}
-size_t alloc_ast()
+size_t T_lexer::alloc_ast()
{
ast.emplace_back();
return ast_index++;
}
-int get_pivot_order(enum_token_type x)
+int T_lexer::get_pivot_order(enum_token_type x)
{
string s = symbol_enum2string(x);
if (s != "")
@@ -54,7 +52,7 @@ int get_pivot_order(enum_token_type x)
return -1;
}
-bool is_operator(enum_token_type x)
+bool T_lexer::is_operator(enum_token_type x)
{
switch(x)
{
@@ -69,7 +67,7 @@ bool is_operator(enum_token_type x)
}
// region [begin, end)
-void parse(size_t cmd_begin, size_t cmd_end, size_t ast_vec)
+void T_lexer::parse(size_t cmd_begin, size_t cmd_end, size_t ast_vec)
{
int current_depth = 0;
int current_order = -1;
diff --git a/include/lexer/lexer_class.h b/include/lexer/lexer_class.h
new file mode 100644
index 0000000..881ef47
--- /dev/null
+++ b/include/lexer/lexer_class.h
@@ -0,0 +1,41 @@
+#pragma once
+
+#include <string>
+#include <unordered_map>
+
+#include "lexer/enum_type.h"
+
+using namespace std;
+
+namespace ov4
+{
+
+class T_lexer
+{
+public:
+
+ // token.h
+ class T_token;
+ string trim(const string &s, const string &trim_target = " \t");
+ bool is_single_symbol(char c);
+ void bracket_depth_changer(char c, int &bracket_depth);
+ bool token_continue(const string &s, char next);
+ void token_push(const string &push_s, int bracket_depth, bool force_text = false);
+ int tokenizer(const string &s);
+
+ // ast.h
+ class T_ast;
+ string final_trim(size_t _begin, size_t _end);
+ size_t alloc_ast();
+ int get_pivot_order(enum_token_type x);
+ bool is_operator(enum_token_type x);
+ void parse(size_t cmd_begin, size_t cmd_end, size_t ast_vec);
+
+ // shared
+ std::vector<T_ast> ast;
+ std::vector<T_token> token;
+ size_t ast_index = 0;
+
+};
+
+}
diff --git a/include/lexer/shared.h b/include/lexer/shared.h
index eadef14..077b7c3 100644
--- a/include/lexer/shared.h
+++ b/include/lexer/shared.h
@@ -8,12 +8,4 @@
namespace ov4
{
-class T_ast;
-class T_token;
-
-inline size_t ast_index = 0;
-
-inline std::vector<T_ast> ast;
-inline std::vector<T_token> token;
-
}
diff --git a/include/lexer/token.h b/include/lexer/token.h
index a79cd2a..f2de46b 100644
--- a/include/lexer/token.h
+++ b/include/lexer/token.h
@@ -2,8 +2,8 @@
#include <string>
+#include "lexer/lexer_class.h"
#include "lexer/enum_type.h"
-#include "lexer/shared.h"
#include "lexer/symbol_property.h"
using namespace std; // remove this
@@ -11,7 +11,7 @@ using namespace std; // remove this
namespace ov4
{
-class T_token
+class T_lexer::T_token
{
public:
T_token() = default;
@@ -22,7 +22,7 @@ public:
int bracket_depth = 0;
};
-string trim(const string &s, const string &trim_target = " \t")
+string T_lexer::trim(const string &s, const string &trim_target) // trim_target = " \t"
{
size_t b = s.find_first_not_of(trim_target);
if (b == string::npos)
@@ -32,7 +32,7 @@ string trim(const string &s, const string &trim_target = " \t")
}
-bool is_single_symbol(char c)
+bool T_lexer::is_single_symbol(char c)
{
string s = " ";
s[0] = c;
@@ -41,7 +41,7 @@ bool is_single_symbol(char c)
return false;
}
-void bracket_depth_changer(char c, int &bracket_depth)
+void T_lexer::bracket_depth_changer(char c, int &bracket_depth)
{
if (c == '(')
{
@@ -63,7 +63,7 @@ void bracket_depth_changer(char c, int &bracket_depth)
}
-bool token_continue(const string &s, char next)
+bool T_lexer::token_continue(const string &s, char next)
{
println("token_continue: receiving {} {}", s, next);
// s is empty, so next can be part of the token
@@ -102,7 +102,7 @@ bool token_continue(const string &s, char next)
}
// handle [prev, current)
-void token_push(const string &push_s, int bracket_depth, bool force_text = false)
+void T_lexer::token_push(const string &push_s, int bracket_depth, bool force_text) // force_text = false
{
T_token tmp;
tmp.bracket_depth = bracket_depth;
@@ -127,7 +127,7 @@ void token_push(const string &push_s, int bracket_depth, bool force_text = false
token.push_back(tmp);
}
-int tokenizer(const string &s)
+int T_lexer::tokenizer(const string &s)
{
if (s.size() == 0) return -1;