Skip to content

Commit

Permalink
refactor: Update NFA and DFA headers to align with the latest coding …
Browse files Browse the repository at this point in the history
…guidelines. (#58)

Co-authored-by: Lin Zhihao <[email protected]>
  • Loading branch information
SharafMohamed and LinZhihao-723 authored Jan 7, 2025
1 parent 081b20f commit c5017ab
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/log_surgeon/LALR1Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ ParserAST::~ParserAST() = default;
uint32_t NonTerminal::m_next_children_start = 0;

NonTerminal::NonTerminal(Production* p)
: m_children_start(NonTerminal::m_next_children_start),
: m_children_start(m_next_children_start),
m_production(p),
m_ast(nullptr) {
NonTerminal::m_next_children_start += p->m_body.size();
m_next_children_start += p->m_body.size();
}
} // namespace log_surgeon
4 changes: 2 additions & 2 deletions src/log_surgeon/LALR1Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class NonTerminal {
*/
[[nodiscard]] auto token_cast(uint32_t i) const -> Token* {
assert(i < cSizeOfAllChildren);
return &std::get<Token>(NonTerminal::m_all_children[m_children_start + i]);
return &std::get<Token>(m_all_children[m_children_start + i]);
}

/**
Expand All @@ -89,7 +89,7 @@ class NonTerminal {
*/
[[nodiscard]] auto non_terminal_cast(uint32_t i) const -> NonTerminal* {
assert(i < cSizeOfAllChildren);
return &std::get<NonTerminal>(NonTerminal::m_all_children[m_children_start + i]);
return &std::get<NonTerminal>(m_all_children[m_children_start + i]);
}

/**
Expand Down
21 changes: 11 additions & 10 deletions src/log_surgeon/finite_automata/RegexDFAState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class RegexDFAState;
using RegexDFAByteState = RegexDFAState<RegexDFAStateType::Byte>;
using RegexDFAUTF8State = RegexDFAState<RegexDFAStateType::UTF8>;

template <RegexDFAStateType stateType>
template <RegexDFAStateType state_type>
class RegexDFAState {
public:
using Tree = UnicodeIntervalTree<RegexDFAState<stateType>*>;
using Tree = UnicodeIntervalTree<RegexDFAState*>;

RegexDFAState() {
std::fill(std::begin(m_bytes_transition), std::end(m_bytes_transition), nullptr);
Expand All @@ -40,27 +40,28 @@ class RegexDFAState {
return false == m_matching_variable_ids.empty();
}

auto add_byte_transition(uint8_t const& byte, RegexDFAState<stateType>* dest_state) -> void {
auto add_byte_transition(uint8_t const& byte, RegexDFAState* dest_state) -> void {
m_bytes_transition[byte] = dest_state;
}

/**
* @param character The character (byte or utf8) to transition on.
* @return A pointer to the DFA state reached after transitioning on `character`.
*/
[[nodiscard]] auto next(uint32_t character) const -> RegexDFAState<stateType>*;
[[nodiscard]] auto next(uint32_t character) const -> RegexDFAState*;

private:
std::vector<uint32_t> m_matching_variable_ids;
RegexDFAState<stateType>* m_bytes_transition[cSizeOfByte];
// NOTE: We don't need m_tree_transitions for the `stateType == RegexDFAStateType::Byte` case,
RegexDFAState* m_bytes_transition[cSizeOfByte];
// NOTE: We don't need m_tree_transitions for the `state_type == RegexDFAStateType::Byte` case,
// so we use an empty class (`std::tuple<>`) in that case.
std::conditional_t<stateType == RegexDFAStateType::UTF8, Tree, std::tuple<>> m_tree_transitions;
std::conditional_t<state_type == RegexDFAStateType::UTF8, Tree, std::tuple<>>
m_tree_transitions;
};

template <RegexDFAStateType stateType>
auto RegexDFAState<stateType>::next(uint32_t character) const -> RegexDFAState<stateType>* {
if constexpr (RegexDFAStateType::Byte == stateType) {
template <RegexDFAStateType state_type>
auto RegexDFAState<state_type>::next(uint32_t character) const -> RegexDFAState* {
if constexpr (RegexDFAStateType::Byte == state_type) {
return m_bytes_transition[character];
} else {
if (character < cSizeOfByte) {
Expand Down
21 changes: 10 additions & 11 deletions src/log_surgeon/finite_automata/RegexDFAStatePair.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ class RegexDFAStatePair {
m_state2(state2) {};

/**
* Used for ordering in a set by considering the states' addresses
* @param rhs
* @return Whether m_state1 in lhs has a lower address than in rhs, or if they're equal,
* whether m_state2 in lhs has a lower address than in rhs
* @return Whether `m_state1` in lhs has a lower address than in rhs, or if they're equal,
* whether `m_state2` in lhs has a lower address than in rhs.
*/
auto operator<(RegexDFAStatePair const& rhs) const -> bool {
if (m_state1 == rhs.m_state1) {
Expand All @@ -41,13 +40,13 @@ class RegexDFAStatePair {

/**
* Generates all pairs reachable from the current pair via any string and store any reachable
* pair not previously visited in unvisited_pairs
* @param visited_pairs Previously visited pairs
* @param unvisited_pairs Set to add unvisited reachable pairs
* pair not previously visited in `unvisited_pairs`.
* @param visited_pairs Previously visited pairs.
* @param unvisited_pairs Set to add unvisited reachable pairs.
*/
auto get_reachable_pairs(
std::set<RegexDFAStatePair<DFAState>>& visited_pairs,
std::set<RegexDFAStatePair<DFAState>>& unvisited_pairs
std::set<RegexDFAStatePair>& visited_pairs,
std::set<RegexDFAStatePair>& unvisited_pairs
) const -> void;

[[nodiscard]] auto is_accepting() const -> bool {
Expand All @@ -65,15 +64,15 @@ class RegexDFAStatePair {

template <typename DFAState>
auto RegexDFAStatePair<DFAState>::get_reachable_pairs(
std::set<RegexDFAStatePair<DFAState>>& visited_pairs,
std::set<RegexDFAStatePair<DFAState>>& unvisited_pairs
std::set<RegexDFAStatePair>& visited_pairs,
std::set<RegexDFAStatePair>& unvisited_pairs
) const -> void {
// TODO: Handle UTF-8 (multi-byte transitions) as well
for (uint32_t i = 0; i < cSizeOfByte; i++) {
auto next_state1 = m_state1->next(i);
auto next_state2 = m_state2->next(i);
if (next_state1 != nullptr && next_state2 != nullptr) {
RegexDFAStatePair<DFAState> reachable_pair{next_state1, next_state2};
RegexDFAStatePair const reachable_pair{next_state1, next_state2};
if (visited_pairs.count(reachable_pair) == 0) {
unvisited_pairs.insert(reachable_pair);
}
Expand Down
3 changes: 0 additions & 3 deletions src/log_surgeon/finite_automata/RegexNFA.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@

#include <cstdint>
#include <memory>
#include <optional>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
Expand Down
5 changes: 2 additions & 3 deletions src/log_surgeon/finite_automata/RegexNFAState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <cstdint>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
Expand Down Expand Up @@ -89,8 +88,8 @@ class RegexNFAState {
auto get_tree_transitions() -> Tree const& { return m_tree_transitions; }

/**
Add dest_state to m_bytes_transitions if all values in interval are a byte, otherwise add
dest_state to m_tree_transitions
* Add `dest_state` to `m_bytes_transitions` if all values in interval are a byte, otherwise add
* `dest_state` to `m_tree_transitions`.
* @param interval
* @param dest_state
*/
Expand Down

0 comments on commit c5017ab

Please sign in to comment.