From 51d11cc142245745483e55410726750f60ea9143 Mon Sep 17 00:00:00 2001 From: Sharaf Mohamed Date: Wed, 8 Jan 2025 15:25:55 -0500 Subject: [PATCH] refactor: Remove redundant `this` when referring to member variables. (#60) Co-authored-by: Lin Zhihao <59785146+LinZhihao-723@users.noreply.github.com> --- src/log_surgeon/Lalr1Parser.hpp | 10 ++-- src/log_surgeon/Lalr1Parser.tpp | 52 +++++++++----------- src/log_surgeon/finite_automata/Dfa.hpp | 2 +- src/log_surgeon/finite_automata/RegexAST.hpp | 16 +++--- 4 files changed, 38 insertions(+), 42 deletions(-) diff --git a/src/log_surgeon/Lalr1Parser.hpp b/src/log_surgeon/Lalr1Parser.hpp index d2c2029..d800968 100644 --- a/src/log_surgeon/Lalr1Parser.hpp +++ b/src/log_surgeon/Lalr1Parser.hpp @@ -120,7 +120,7 @@ struct Production { * has nothing on its LHS (i.e., HEAD -> {}) * @return bool */ - [[nodiscard]] auto is_epsilon() const -> bool { return this->m_body.empty(); } + [[nodiscard]] auto is_epsilon() const -> bool { return m_body.empty(); } uint32_t m_index; uint32_t m_head; @@ -158,16 +158,14 @@ struct Item { * @return bool */ [[nodiscard]] auto has_dot_at_end() const -> bool { - return this->m_dot == this->m_production->m_body.size(); + return m_dot == m_production->m_body.size(); } /** * Returns the next unmatched symbol in the production based on the dot. * @return uint32_t */ - [[nodiscard]] auto next_symbol() const -> uint32_t { - return this->m_production->m_body.at(this->m_dot); - } + [[nodiscard]] auto next_symbol() const -> uint32_t { return m_production->m_body.at(m_dot); } Production* m_production; uint32_t m_dot; @@ -395,6 +393,8 @@ class Lalr1Parser : public Parser { auto symbol_is_token(uint32_t s) -> bool { return m_terminals.find(s) != m_terminals.end(); } + using Parser::m_lexer; + std::set m_terminals; std::set m_nullable; std::map, std::unique_ptr> m_lr0_item_sets; diff --git a/src/log_surgeon/Lalr1Parser.tpp b/src/log_surgeon/Lalr1Parser.tpp index 2c817d1..79bfc47 100644 --- a/src/log_surgeon/Lalr1Parser.tpp +++ b/src/log_surgeon/Lalr1Parser.tpp @@ -71,7 +71,7 @@ void Lalr1Parser::add_rule( std::unique_ptr> rule ) { Parser::add_rule(name, std::move(rule)); - m_terminals.insert(this->m_lexer.m_symbol_id[name]); + m_terminals.insert(m_lexer.m_symbol_id[name]); } template @@ -115,9 +115,9 @@ auto Lalr1Parser::add_production( std::vector const& body, SemanticRule semantic_rule ) -> uint32_t { - if (this->m_lexer.m_symbol_id.find(head) == this->m_lexer.m_symbol_id.end()) { - this->m_lexer.m_symbol_id[head] = this->m_lexer.m_symbol_id.size(); - this->m_lexer.m_id_symbol[this->m_lexer.m_symbol_id[head]] = head; + if (m_lexer.m_symbol_id.find(head) == m_lexer.m_symbol_id.end()) { + m_lexer.m_symbol_id[head] = m_lexer.m_symbol_id.size(); + m_lexer.m_id_symbol[m_lexer.m_symbol_id[head]] = head; } uint32_t n = m_productions.size(); auto it = m_productions_map.find(head); @@ -131,13 +131,13 @@ auto Lalr1Parser::add_production( } std::unique_ptr p(new Production); p->m_index = n; - p->m_head = this->m_lexer.m_symbol_id[head]; + p->m_head = m_lexer.m_symbol_id[head]; for (std::string const& symbol_string : body) { - if (this->m_lexer.m_symbol_id.find(symbol_string) == this->m_lexer.m_symbol_id.end()) { - this->m_lexer.m_symbol_id[symbol_string] = this->m_lexer.m_symbol_id.size(); - this->m_lexer.m_id_symbol[this->m_lexer.m_symbol_id[symbol_string]] = symbol_string; + if (m_lexer.m_symbol_id.find(symbol_string) == m_lexer.m_symbol_id.end()) { + m_lexer.m_symbol_id[symbol_string] = m_lexer.m_symbol_id.size(); + m_lexer.m_id_symbol[m_lexer.m_symbol_id[symbol_string]] = symbol_string; } - p->m_body.push_back(this->m_lexer.m_symbol_id[symbol_string]); + p->m_body.push_back(m_lexer.m_symbol_id[symbol_string]); } p->m_semantic_rule = std::move(semantic_rule); m_non_terminals.insert(std::pair>(p->m_head, {})); @@ -152,7 +152,7 @@ auto Lalr1Parser::add_production( template void Lalr1Parser::generate() { - this->m_lexer.generate(); + m_lexer.generate(); assert(!m_productions.empty()); generate_lr0_kernels(); generate_first_sets(); @@ -204,7 +204,7 @@ auto Lalr1Parser::lr_closure_helper( return true; } *next_symbol = item->next_symbol(); - if (this->symbol_is_token(*next_symbol)) { + if (symbol_is_token(*next_symbol)) { return true; } return false; @@ -435,7 +435,7 @@ void Lalr1Parser::generate_lalr1_action() { for (std::map, std::unique_ptr>::value_type const& kv : m_lr1_item_sets) { ItemSet* item_set_ptr = kv.second.get(); - item_set_ptr->m_actions.resize(this->m_lexer.m_symbol_id.size(), false); + item_set_ptr->m_actions.resize(m_lexer.m_symbol_id.size(), false); for (Item const& item : item_set_ptr->m_closure) { if (!item.has_dot_at_end()) { if (m_terminals.find(item.next_symbol()) == m_terminals.end() @@ -453,7 +453,7 @@ void Lalr1Parser::generate_lalr1_action() { } std::string conflict_msg{}; conflict_msg += "For symbol "; - conflict_msg += this->m_lexer.m_id_symbol[item.next_symbol()]; + conflict_msg += m_lexer.m_id_symbol[item.next_symbol()]; conflict_msg += ", adding shift to "; conflict_msg += std::to_string(item_set_ptr->m_next[item.next_symbol()]->m_index); @@ -464,11 +464,10 @@ void Lalr1Parser::generate_lalr1_action() { conflict_msg += "\n"; } else { conflict_msg += "shift-reduce conflict with reduction "; - conflict_msg - += this->m_lexer.m_id_symbol[std::get(action)->m_head]; + conflict_msg += m_lexer.m_id_symbol[std::get(action)->m_head]; conflict_msg += "-> {"; for (uint32_t symbol : std::get(action)->m_body) { - conflict_msg += this->m_lexer.m_id_symbol[symbol] + ","; + conflict_msg += m_lexer.m_id_symbol[symbol] + ","; } conflict_msg += "}\n"; } @@ -486,12 +485,12 @@ void Lalr1Parser::generate_lalr1_action() { if (!std::holds_alternative(action)) { std::string conflict_msg{}; conflict_msg += "For symbol "; - conflict_msg += this->m_lexer.m_id_symbol[item.m_lookahead]; + conflict_msg += m_lexer.m_id_symbol[item.m_lookahead]; conflict_msg += ", adding reduction "; - conflict_msg += this->m_lexer.m_id_symbol[item.m_production->m_head]; + conflict_msg += m_lexer.m_id_symbol[item.m_production->m_head]; conflict_msg += "-> {"; for (uint32_t symbol : item.m_production->m_body) { - conflict_msg += this->m_lexer.m_id_symbol[symbol] + ","; + conflict_msg += m_lexer.m_id_symbol[symbol] + ","; } conflict_msg += "} causes "; if (std::holds_alternative(action)) { @@ -501,11 +500,10 @@ void Lalr1Parser::generate_lalr1_action() { } else { conflict_msg += "reduce-reduce conflict with reduction "; conflict_msg - += this->m_lexer - .m_id_symbol[std::get(action)->m_head]; + += m_lexer.m_id_symbol[std::get(action)->m_head]; conflict_msg += "-> {"; for (uint32_t symbol : std::get(action)->m_body) { - conflict_msg += this->m_lexer.m_id_symbol[symbol] + ","; + conflict_msg += m_lexer.m_id_symbol[symbol] + ","; } conflict_msg += "}\n"; } @@ -605,12 +603,12 @@ auto Lalr1Parser::report_error() -> std::string { error_type += "'"; if (auto* regex_ast_literal = dynamic_cast*>( - this->m_lexer.get_rule(i) + m_lexer.get_rule(i) )) { error_type += unescape(char(regex_ast_literal->get_character())); } else { - error_type += this->m_lexer.m_id_symbol[i]; + error_type += m_lexer.m_id_symbol[i]; } error_type += "',"; } @@ -660,16 +658,14 @@ void Lalr1Parser::reset() { m_parse_stack_matches.pop(); } m_input_buffer.reset(); - this->m_lexer.reset(); + m_lexer.reset(); } template auto Lalr1Parser::get_next_symbol() -> Token { if (m_next_token == std::nullopt) { Token token; - if (ErrorCode error = this->m_lexer.scan(m_input_buffer, token); - ErrorCode::Success != error) - { + if (ErrorCode error = m_lexer.scan(m_input_buffer, token); ErrorCode::Success != error) { throw std::runtime_error("Error scanning in lexer."); } return token; diff --git a/src/log_surgeon/finite_automata/Dfa.hpp b/src/log_surgeon/finite_automata/Dfa.hpp index 531fb58..5d425dd 100644 --- a/src/log_surgeon/finite_automata/Dfa.hpp +++ b/src/log_surgeon/finite_automata/Dfa.hpp @@ -55,7 +55,7 @@ auto Dfa::get_intersect(Dfa const* dfa_in) const -> std::set schema_types; std::set> unvisited_pairs; std::set> visited_pairs; - unvisited_pairs.emplace(this->get_root(), dfa_in->get_root()); + unvisited_pairs.emplace(get_root(), dfa_in->get_root()); // TODO: Handle UTF-8 (multi-byte transitions) as well while (false == unvisited_pairs.empty()) { auto current_pair_it = unvisited_pairs.begin(); diff --git a/src/log_surgeon/finite_automata/RegexAST.hpp b/src/log_surgeon/finite_automata/RegexAST.hpp index 7ff88bd..bb55f62 100644 --- a/src/log_surgeon/finite_automata/RegexAST.hpp +++ b/src/log_surgeon/finite_automata/RegexAST.hpp @@ -605,7 +605,7 @@ class RegexASTMultiplication : public RegexAST { [[nodiscard]] auto serialize() const -> std::u32string override; - [[nodiscard]] auto is_infinite() const -> bool { return this->m_max == 0; } + [[nodiscard]] auto is_infinite() const -> bool { return m_max == 0; } [[nodiscard]] auto get_operand() const -> std::unique_ptr> const& { return m_operand; @@ -847,26 +847,26 @@ void RegexASTMultiplication::add_to_nfa( TypedNfaState* end_state ) const { TypedNfaState* saved_root = nfa->get_root(); - if (this->m_min == 0) { + if (m_min == 0) { nfa->get_root()->add_epsilon_transition(end_state); } else { - for (uint32_t i = 1; i < this->m_min; i++) { + for (uint32_t i = 1; i < m_min; i++) { TypedNfaState* intermediate_state = nfa->new_state(); m_operand->add_to_nfa_with_negative_tags(nfa, intermediate_state); nfa->set_root(intermediate_state); } m_operand->add_to_nfa_with_negative_tags(nfa, end_state); } - if (this->is_infinite()) { + if (is_infinite()) { nfa->set_root(end_state); m_operand->add_to_nfa_with_negative_tags(nfa, end_state); - } else if (this->m_max > this->m_min) { - if (this->m_min != 0) { + } else if (m_max > m_min) { + if (m_min != 0) { TypedNfaState* intermediate_state = nfa->new_state(); m_operand->add_to_nfa_with_negative_tags(nfa, intermediate_state); nfa->set_root(intermediate_state); } - for (uint32_t i = this->m_min + 1; i < this->m_max; ++i) { + for (uint32_t i = m_min + 1; i < m_max; ++i) { m_operand->add_to_nfa_with_negative_tags(nfa, end_state); TypedNfaState* intermediate_state = nfa->new_state(); m_operand->add_to_nfa_with_negative_tags(nfa, intermediate_state); @@ -1065,7 +1065,7 @@ void RegexASTGroup::add_to_nfa(Nfa* nfa, TypedNfaS auto merged_ranges = m_ranges; std::sort(merged_ranges.begin(), merged_ranges.end()); merged_ranges = merge(merged_ranges); - if (this->m_negate) { + if (m_negate) { merged_ranges = complement(merged_ranges); } for (auto const& [begin, end] : merged_ranges) {