MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
nfa.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4
5#include <vector>
6
7#include <absl/container/flat_hash_map.h>
8
10
11namespace automaton {
12class NFANode {
13public:
14 NFANode() = default;
15
16 void add_transition(const NFANode* to, std::uint16_t c);
17 std::vector<const NFANode*> get_transitions(std::uint16_t c) const;
18
19 // F: void(const NFANode*)
20 template<class F> void for_transitions(F&& f, std::uint16_t c) const {
21 if (erroring_) return;
22 if (auto it = transitions_.find(c); it != transitions_.end())
23 for (const auto& to : it->second) std::forward<F>(f)(to);
24 }
25
26 // F: void(std::uint16_t, const NFANode*)
27 template<class F> void for_transitions(F&& f) const {
28 if (erroring_) return;
29 for (auto& [c, tos] : transitions_)
30 for (const auto& to : tos) std::forward<F>(f)(c, to);
31 }
32
33 bool is_accepting() const { return accepting_; }
34 void set_accepting(bool accepting) {
35 assert(!(accepting && erroring_) && "state cannot be accepting and erroring");
36 accepting_ = accepting;
37 }
38
39 bool is_erroring() const noexcept { return erroring_; }
40 void set_erroring(bool erroring) noexcept {
41 assert(!(accepting_ && erroring) && "state cannot be accepting and erroring");
42 erroring_ = erroring;
43 }
44
45 friend std::ostream& operator<<(std::ostream& os, const NFANode& node);
46
47private:
48 absl::flat_hash_map<std::uint16_t, std::vector<const NFANode*>> transitions_;
49 bool accepting_ = false;
50 bool erroring_ = false;
51};
52
53extern template class AutomatonBase<NFANode>;
54
56public:
57 NFA() = default;
58 NFA(const NFA&) = delete;
59 NFA& operator=(const NFA&) = delete;
60
61 enum SpecialTransitons : std::uint16_t {
62 EPSILON = 0x8001,
63 };
64};
65
66} // namespace automaton
bool is_erroring() const noexcept
Definition nfa.h:39
void set_erroring(bool erroring) noexcept
Definition nfa.h:40
bool is_accepting() const
Definition nfa.h:33
void for_transitions(F &&f) const
Definition nfa.h:27
friend std::ostream & operator<<(std::ostream &os, const NFANode &node)
Definition nfa.cpp:25
void set_accepting(bool accepting)
Definition nfa.h:34
void for_transitions(F &&f, std::uint16_t c) const
Definition nfa.h:20
std::vector< const NFANode * > get_transitions(std::uint16_t c) const
Definition nfa.cpp:14
void add_transition(const NFANode *to, std::uint16_t c)
Definition nfa.cpp:7
NFA & operator=(const NFA &)=delete
SpecialTransitons
Definition nfa.h:61
NFA(const NFA &)=delete
NFA()=default