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