From 27e0f69843f412f25703c2c9165dbc1a0c6d6218 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Mon, 17 Feb 2020 17:56:05 +0800 Subject: change method to generate unique type id. --- src/MoonP/moon_ast.h | 17 ++++++++++------- src/MoonP/parser.cpp | 24 ++++++++---------------- 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; namespace MoonP { -// str hash helper functions (djb2) -inline constexpr size_t hash(char const* input) { - return *input ? *input + 33ull * hash(input + 1) : 5381; -} +template struct Counter { enum { value = Counter::value }; }; +template<> struct Counter<0> { enum { value = 0 }; }; + +#define COUNTER_READ Counter<__LINE__>::value +#define COUNTER_INC template<> struct Counter<__LINE__> { enum { value = Counter<__LINE__-1>::value + 1}; } template constexpr typename std::enable_if::value,size_t>::type id() { return 0; } #define AST_LEAF(type) \ +COUNTER_INC;\ class type##_t : public ast_node \ { \ public: \ virtual int get_type() override { return ast_type(); } \ - virtual size_t getId() const override { return hash(#type); } + virtual size_t getId() const override { return COUNTER_READ; } #define AST_NODE(type) \ +COUNTER_INC;\ class type##_t : public ast_container \ { \ public: \ virtual int get_type() override { return ast_type(); } \ - virtual size_t getId() const override { return hash(#type); } \ + virtual size_t getId() const override { return COUNTER_READ; } \ #define AST_MEMBER(type, ...) \ type##_t() { \ @@ -43,7 +46,7 @@ public: \ #define AST_END(type) \ }; \ -template<> constexpr size_t id() { return hash(#type); } +template<> constexpr size_t id() { return COUNTER_READ; } AST_LEAF(Num) AST_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: void do_parse_procs(void* d) const { for(_match_vector::const_iterator it = m_matches.begin(); it != m_matches.end(); - ++it) - { + ++it) { const _match &m = *it; parse_proc p = _private::get_parse_proc(*m.m_rule); p(m.m_begin, m.m_end, d); @@ -266,8 +265,7 @@ private: //parse the string bool _parse(_context& con) const { for(input::const_iterator it = m_string.begin(), - end = m_string.end();;) - { + end = m_string.end();;) { if (it == end) return true; if (con.end()) break; if (con.symbol() != *it) break; @@ -955,8 +953,7 @@ bool _context::parse_term(rule& r) { r.m_state = old_state; throw _lr_ok(r.this_ptr()); } - } - else { + } else { try { ok = _parse_term(r); } @@ -978,8 +975,7 @@ bool _context::parse_term(rule& r) { case rule::_REJECT: if (lr) { ok = false; - } - else { + } else { r.m_state.m_mode = rule::_PARSE; ok = _parse_term(r); r.m_state.m_mode = rule::_REJECT; @@ -990,8 +986,7 @@ bool _context::parse_term(rule& r) { case rule::_ACCEPT: if (lr) { ok = true; - } - else { + } else { r.m_state.m_mode = rule::_PARSE; ok = _parse_term(r); r.m_state.m_mode = rule::_ACCEPT; @@ -1015,8 +1010,7 @@ bool _context::_parse_non_term(rule& r) { if (ok) { m_matches.push_back(_match(r.this_ptr(), b, m_pos)); } - } - else { + } else { ok = _private::get_expr(r)->parse_non_term(*this); } return ok; @@ -1032,8 +1026,7 @@ bool _context::_parse_term(rule& r) { if (ok) { m_matches.push_back(_match(r.this_ptr(), b, m_pos)); } - } - else { + } else { ok = _private::get_expr(r)->parse_term(*this); } return ok; @@ -1404,8 +1397,7 @@ bool parse(input& i, rule& g, error_list& el, void* d, void* ud) { if (!con.end()) { if (con.m_error_pos.m_it < con.m_end) { el.push_back(_syntax_error(con)); - } - else { + } else { el.push_back(_eof_error(con)); } return false; -- cgit v1.2.3-55-g6feb