aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2020-02-17 17:56:05 +0800
committerLi Jin <dragon-fly@qq.com>2020-02-17 17:56:05 +0800
commit27e0f69843f412f25703c2c9165dbc1a0c6d6218 (patch)
treec97b87c3d0c64a818c352823bd3a181a1f0eca2c
parent71d9ad9506524fcd3e124c4b4a460afa8fbf35eb (diff)
downloadyuescript-27e0f69843f412f25703c2c9165dbc1a0c6d6218.tar.gz
yuescript-27e0f69843f412f25703c2c9165dbc1a0c6d6218.tar.bz2
yuescript-27e0f69843f412f25703c2c9165dbc1a0c6d6218.zip
change method to generate unique type id.
-rw-r--r--src/MoonP/moon_ast.h17
-rw-r--r--src/MoonP/parser.cpp24
2 files changed, 18 insertions, 23 deletions
diff --git a/src/MoonP/moon_ast.h b/src/MoonP/moon_ast.h
index 4525df1..0b572b5 100644
--- a/src/MoonP/moon_ast.h
+++ b/src/MoonP/moon_ast.h
@@ -13,28 +13,31 @@ using namespace parserlib;
13 13
14namespace MoonP { 14namespace MoonP {
15 15
16// str hash helper functions (djb2) 16template<size_t NUM> struct Counter { enum { value = Counter<NUM-1>::value }; };
17inline constexpr size_t hash(char const* input) { 17template<> struct Counter<0> { enum { value = 0 }; };
18 return *input ? *input + 33ull * hash(input + 1) : 5381; 18
19} 19#define COUNTER_READ Counter<__LINE__>::value
20#define COUNTER_INC template<> struct Counter<__LINE__> { enum { value = Counter<__LINE__-1>::value + 1}; }
20 21
21template<class T> 22template<class T>
22constexpr typename std::enable_if<std::is_base_of<ast_node,T>::value,size_t>::type 23constexpr typename std::enable_if<std::is_base_of<ast_node,T>::value,size_t>::type
23id() { return 0; } 24id() { return 0; }
24 25
25#define AST_LEAF(type) \ 26#define AST_LEAF(type) \
27COUNTER_INC;\
26class type##_t : public ast_node \ 28class type##_t : public ast_node \
27{ \ 29{ \
28public: \ 30public: \
29 virtual int get_type() override { return ast_type<type##_t>(); } \ 31 virtual int get_type() override { return ast_type<type##_t>(); } \
30 virtual size_t getId() const override { return hash(#type); } 32 virtual size_t getId() const override { return COUNTER_READ; }
31 33
32#define AST_NODE(type) \ 34#define AST_NODE(type) \
35COUNTER_INC;\
33class type##_t : public ast_container \ 36class type##_t : public ast_container \
34{ \ 37{ \
35public: \ 38public: \
36 virtual int get_type() override { return ast_type<type##_t>(); } \ 39 virtual int get_type() override { return ast_type<type##_t>(); } \
37 virtual size_t getId() const override { return hash(#type); } \ 40 virtual size_t getId() const override { return COUNTER_READ; } \
38 41
39#define AST_MEMBER(type, ...) \ 42#define AST_MEMBER(type, ...) \
40 type##_t() { \ 43 type##_t() { \
@@ -43,7 +46,7 @@ public: \
43 46
44#define AST_END(type) \ 47#define AST_END(type) \
45}; \ 48}; \
46template<> constexpr size_t id<type##_t>() { return hash(#type); } 49template<> constexpr size_t id<type##_t>() { return COUNTER_READ; }
47 50
48AST_LEAF(Num) 51AST_LEAF(Num)
49AST_END(Num) 52AST_END(Num)
diff --git a/src/MoonP/parser.cpp b/src/MoonP/parser.cpp
index 07f6e41..8dc2ff9 100644
--- a/src/MoonP/parser.cpp
+++ b/src/MoonP/parser.cpp
@@ -170,8 +170,7 @@ public:
170 void do_parse_procs(void* d) const { 170 void do_parse_procs(void* d) const {
171 for(_match_vector::const_iterator it = m_matches.begin(); 171 for(_match_vector::const_iterator it = m_matches.begin();
172 it != m_matches.end(); 172 it != m_matches.end();
173 ++it) 173 ++it) {
174 {
175 const _match &m = *it; 174 const _match &m = *it;
176 parse_proc p = _private::get_parse_proc(*m.m_rule); 175 parse_proc p = _private::get_parse_proc(*m.m_rule);
177 p(m.m_begin, m.m_end, d); 176 p(m.m_begin, m.m_end, d);
@@ -266,8 +265,7 @@ private:
266 //parse the string 265 //parse the string
267 bool _parse(_context& con) const { 266 bool _parse(_context& con) const {
268 for(input::const_iterator it = m_string.begin(), 267 for(input::const_iterator it = m_string.begin(),
269 end = m_string.end();;) 268 end = m_string.end();;) {
270 {
271 if (it == end) return true; 269 if (it == end) return true;
272 if (con.end()) break; 270 if (con.end()) break;
273 if (con.symbol() != *it) break; 271 if (con.symbol() != *it) break;
@@ -955,8 +953,7 @@ bool _context::parse_term(rule& r) {
955 r.m_state = old_state; 953 r.m_state = old_state;
956 throw _lr_ok(r.this_ptr()); 954 throw _lr_ok(r.this_ptr());
957 } 955 }
958 } 956 } else {
959 else {
960 try { 957 try {
961 ok = _parse_term(r); 958 ok = _parse_term(r);
962 } 959 }
@@ -978,8 +975,7 @@ bool _context::parse_term(rule& r) {
978 case rule::_REJECT: 975 case rule::_REJECT:
979 if (lr) { 976 if (lr) {
980 ok = false; 977 ok = false;
981 } 978 } else {
982 else {
983 r.m_state.m_mode = rule::_PARSE; 979 r.m_state.m_mode = rule::_PARSE;
984 ok = _parse_term(r); 980 ok = _parse_term(r);
985 r.m_state.m_mode = rule::_REJECT; 981 r.m_state.m_mode = rule::_REJECT;
@@ -990,8 +986,7 @@ bool _context::parse_term(rule& r) {
990 case rule::_ACCEPT: 986 case rule::_ACCEPT:
991 if (lr) { 987 if (lr) {
992 ok = true; 988 ok = true;
993 } 989 } else {
994 else {
995 r.m_state.m_mode = rule::_PARSE; 990 r.m_state.m_mode = rule::_PARSE;
996 ok = _parse_term(r); 991 ok = _parse_term(r);
997 r.m_state.m_mode = rule::_ACCEPT; 992 r.m_state.m_mode = rule::_ACCEPT;
@@ -1015,8 +1010,7 @@ bool _context::_parse_non_term(rule& r) {
1015 if (ok) { 1010 if (ok) {
1016 m_matches.push_back(_match(r.this_ptr(), b, m_pos)); 1011 m_matches.push_back(_match(r.this_ptr(), b, m_pos));
1017 } 1012 }
1018 } 1013 } else {
1019 else {
1020 ok = _private::get_expr(r)->parse_non_term(*this); 1014 ok = _private::get_expr(r)->parse_non_term(*this);
1021 } 1015 }
1022 return ok; 1016 return ok;
@@ -1032,8 +1026,7 @@ bool _context::_parse_term(rule& r) {
1032 if (ok) { 1026 if (ok) {
1033 m_matches.push_back(_match(r.this_ptr(), b, m_pos)); 1027 m_matches.push_back(_match(r.this_ptr(), b, m_pos));
1034 } 1028 }
1035 } 1029 } else {
1036 else {
1037 ok = _private::get_expr(r)->parse_term(*this); 1030 ok = _private::get_expr(r)->parse_term(*this);
1038 } 1031 }
1039 return ok; 1032 return ok;
@@ -1404,8 +1397,7 @@ bool parse(input& i, rule& g, error_list& el, void* d, void* ud) {
1404 if (!con.end()) { 1397 if (!con.end()) {
1405 if (con.m_error_pos.m_it < con.m_end) { 1398 if (con.m_error_pos.m_it < con.m_end) {
1406 el.push_back(_syntax_error(con)); 1399 el.push_back(_syntax_error(con));
1407 } 1400 } else {
1408 else {
1409 el.push_back(_eof_error(con)); 1401 el.push_back(_eof_error(con));
1410 } 1402 }
1411 return false; 1403 return false;