Skip to content
2 changes: 1 addition & 1 deletion src/log_surgeon/LexicalRule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class LexicalRule {
template <typename TypedNfaState>
void LexicalRule<TypedNfaState>::add_to_nfa(finite_automata::Nfa<TypedNfaState>* nfa) const {
auto* end_state = nfa->new_accepting_state(m_variable_id);
m_regex->add_to_nfa_with_negative_captures(nfa, end_state);
m_regex->add_to_nfa_with_negative_captures(nfa, end_state, false);
}
} // namespace log_surgeon

Expand Down
27 changes: 20 additions & 7 deletions src/log_surgeon/finite_automata/Nfa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,20 @@ class Nfa {
/**
* @param captures A vector containing the captures of all alternate paths.
* @param dest_state The destination state to arrive at after negating the captures.
* @param multi_valued If the tag needs to store multiple positions to track the capture.
* @return A pointer to the newly created NFA state with a spontaneous transition to
* `dest_state`negating all the tags associated with `captures`.
*/
[[nodiscard]] auto new_state_from_negative_captures(
std::vector<Capture const*> const& captures,
TypedNfaState const* dest_state
TypedNfaState const* dest_state,
bool multi_valued
) -> TypedNfaState*;

/**
* @param capture The positive capture to be tracked.
* @param dest_state The destination state to arrive at after tracking the capture.
* @param multi_valued If the tag needs to store multiple positions to track the capture.
* @return A pair of pointers to the two newly created NFA states:
* - A state arrived at from a spontaneous transition out of `m_root` that sets a tag to track
* the capture's start position.
Expand All @@ -72,7 +75,8 @@ class Nfa {
*/
[[nodiscard]] auto new_start_and_end_states_from_positive_capture(
Capture const* capture,
TypedNfaState const* dest_state
TypedNfaState const* dest_state,
bool multi_valued
) -> std::pair<TypedNfaState*, TypedNfaState*>;

/**
Expand Down Expand Up @@ -160,7 +164,8 @@ auto Nfa<TypedNfaState>::new_accepting_state(uint32_t const matching_variable_id
template <typename TypedNfaState>
auto Nfa<TypedNfaState>::new_state_from_negative_captures(
std::vector<Capture const*> const& captures,
TypedNfaState const* dest_state
TypedNfaState const* dest_state,
bool const multi_valued
) -> TypedNfaState* {
std::vector<tag_id_t> tags;
for (auto const* capture : captures) {
Expand All @@ -173,24 +178,32 @@ auto Nfa<TypedNfaState>::new_state_from_negative_captures(
m_state_id_generator.generate_id(),
TagOperationType::Negate,
std::move(tags),
dest_state
dest_state,
multi_valued
));
return m_states.back().get();
}

template <typename TypedNfaState>
auto Nfa<TypedNfaState>::new_start_and_end_states_from_positive_capture(
Capture const* capture,
TypedNfaState const* dest_state
TypedNfaState const* dest_state,
bool const multi_valued
) -> std::pair<TypedNfaState*, TypedNfaState*> {
auto const [start_tag, end_tag]{get_or_create_capture_tag_pair(capture)};
auto* start_state{new_state()};
m_root->add_spontaneous_transition(TagOperationType::Set, {start_tag}, start_state);
m_root->add_spontaneous_transition(
TagOperationType::Set,
{start_tag},
start_state,
multi_valued
);
m_states.emplace_back(std::make_unique<TypedNfaState>(
m_state_id_generator.generate_id(),
TagOperationType::Set,
std::vector{end_tag},
dest_state
dest_state,
multi_valued
));
auto* end_state{m_states.back().get()};
return {start_state, end_state};
Expand Down
10 changes: 6 additions & 4 deletions src/log_surgeon/finite_automata/NfaState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ class NfaState {
state_id_t const id,
TagOperationType const op_type,
std::vector<tag_id_t> const& tag_ids,
NfaState const* dest_state
NfaState const* dest_state,
bool const multi_valued
)
: m_id{id} {
add_spontaneous_transition(op_type, tag_ids, dest_state);
add_spontaneous_transition(op_type, tag_ids, dest_state, multi_valued);
}

auto add_spontaneous_transition(NfaState const* dest_state) -> void {
Expand All @@ -63,12 +64,13 @@ class NfaState {
auto add_spontaneous_transition(
TagOperationType const op_type,
std::vector<tag_id_t> const& tag_ids,
NfaState const* dest_state
NfaState const* dest_state,
bool const multi_valued
) -> void {
std::vector<TagOperation> tag_ops;
tag_ops.reserve(tag_ids.size());
for (auto const tag_id : tag_ids) {
tag_ops.emplace_back(tag_id, op_type);
tag_ops.emplace_back(tag_id, op_type, multi_valued);
}
m_spontaneous_transitions.emplace_back(std::move(tag_ops), dest_state);
}
Expand Down
Loading
Loading