Skip to content

Commit 36c4a81

Browse files
Adapt to Contour's best code quality & maintenance practice.
Signed-off-by: Christian Parpart <[email protected]>
1 parent d14604d commit 36c4a81

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1332
-3373
lines changed

.github/workflows/build.yml

+3
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ jobs:
410410
run: cmake --build build/ -- -j3
411411
- name: "test: crispy"
412412
run: ./build/src/crispy/crispy_test
413+
- name: "test: regex_dfa"
414+
run: ./build/src/regex_dfa/regex_dfa_test
413415
- name: "test: vtparser"
414416
run: ./build/src/vtparser/vtparser_test
415417
- name: "test: libterminal"
@@ -421,6 +423,7 @@ jobs:
421423
name: contour-ubuntu2204-tests
422424
path: |
423425
build/src/crispy/crispy_test
426+
build/src/regex_dfa/regex_dfa_test
424427
build/src/vtparser/vtparser_test
425428
build/src/vtbackend/vtbackend_test
426429
build/src/vtbackend/bench-headless

src/regex_dfa/Alphabet.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ namespace regex_dfa
3232

3333
void Alphabet::insert(Symbol ch)
3434
{
35-
if (alphabet_.find(ch) == alphabet_.end())
35+
if (_alphabet.find(ch) == _alphabet.end())
3636
{
3737
DEBUG("Alphabet: insert '{:}'", prettySymbol(ch));
38-
alphabet_.insert(ch);
38+
_alphabet.insert(ch);
3939
}
4040
}
4141

@@ -45,7 +45,7 @@ string Alphabet::to_string() const
4545

4646
sstr << '{';
4747

48-
for (Symbol c: alphabet_)
48+
for (Symbol c: _alphabet)
4949
sstr << prettySymbol(c);
5050

5151
sstr << '}';

src/regex_dfa/Alphabet.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ class Alphabet
2525
using set_type = std::set<Symbol>;
2626
using iterator = set_type::iterator;
2727

28-
size_t size() const noexcept { return alphabet_.size(); }
28+
[[nodiscard]] size_t size() const noexcept { return _alphabet.size(); }
2929

3030
void insert(Symbol ch);
3131

32-
std::string to_string() const;
32+
[[nodiscard]] std::string to_string() const;
3333

34-
const iterator begin() const { return alphabet_.begin(); }
35-
const iterator end() const { return alphabet_.end(); }
34+
[[nodiscard]] iterator begin() const { return _alphabet.begin(); }
35+
[[nodiscard]] iterator end() const { return _alphabet.end(); }
3636

3737
private:
38-
set_type alphabet_;
38+
set_type _alphabet;
3939
};
4040

4141
} // namespace regex_dfa

src/regex_dfa/CMakeLists.txt

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ target_include_directories(regex_dfa PUBLIC ${PROJECT_SOURCE_DIR}/src ${CMAKE_SO
2121
target_link_libraries(regex_dfa PUBLIC fmt::fmt-header-only)
2222

2323
# ----------------------------------------------------------------------------
24-
if(TESTS)
24+
option(REGEX_DFA_TESTING "Enables building of unittests for regex_dfa library [default: ON]" ON)
25+
if(REGEX_DFA_TESTING)
26+
enable_testing()
2527
add_executable(regex_dfa_test
2628
regex_dfa_test.cpp
2729
DFABuilder_test.cpp
@@ -33,9 +35,9 @@ if(TESTS)
3335
State_test.cpp
3436
Symbols_test.cpp
3537
util/iterator_test.cpp
36-
util/testing.cpp
3738
)
3839

3940
target_link_libraries(regex_dfa_test PUBLIC regex_dfa)
41+
target_link_libraries(regex_dfa_test PUBLIC Catch2::Catch2)
4042
target_link_libraries(regex_dfa_test PUBLIC fmt::fmt-header-only)
41-
endif(TESTS)
43+
endif(REGEX_DFA_TESTING)

src/regex_dfa/CharStream.h

+15-15
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,41 @@ class CharStream
2727
class StringStream: public CharStream
2828
{
2929
public:
30-
explicit StringStream(std::string&& s): source_ { std::move(s) } {}
30+
explicit StringStream(std::string&& s): _source { std::move(s) } {}
3131

32-
[[nodiscard]] bool isEof() const noexcept override { return pos_ >= source_.size(); }
33-
char get() override { return source_[pos_++]; }
34-
void rollback(int count) override { pos_ -= count; }
35-
void rewind() override { pos_ = 0; }
32+
[[nodiscard]] bool isEof() const noexcept override { return _pos >= _source.size(); }
33+
char get() override { return _source[_pos++]; }
34+
void rollback(int count) override { _pos -= count; }
35+
void rewind() override { _pos = 0; }
3636

3737
private:
38-
std::string source_;
39-
size_t pos_ = 0;
38+
std::string _source;
39+
size_t _pos = 0;
4040
};
4141

4242
class StandardStream: public CharStream
4343
{
4444
public:
4545
explicit StandardStream(std::istream* source);
4646

47-
[[nodiscard]] bool isEof() const noexcept override { return !source_->good(); }
48-
char get() override { return static_cast<char>(source_->get()); }
47+
[[nodiscard]] bool isEof() const noexcept override { return !_source->good(); }
48+
char get() override { return static_cast<char>(_source->get()); }
4949

5050
void rollback(int count) override
5151
{
52-
source_->clear();
53-
source_->seekg(-count, std::ios::cur);
52+
_source->clear();
53+
_source->seekg(-count, std::ios::cur);
5454
}
5555

5656
void rewind() override
5757
{
58-
source_->clear();
59-
source_->seekg(initialOffset_, std::ios::beg);
58+
_source->clear();
59+
_source->seekg(_initialOffset, std::ios::beg);
6060
}
6161

6262
private:
63-
std::istream* source_;
64-
std::streamoff initialOffset_;
63+
std::istream* _source;
64+
std::streamoff _initialOffset;
6565
};
6666

6767
} // namespace regex_dfa

src/regex_dfa/Compiler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ DFA Compiler::compileMinimalDFA()
134134

135135
LexerDef Compiler::compile()
136136
{
137-
return generateTables(compileMinimalDFA(), containsBeginOfLine_, move(names_));
137+
return generateTables(compileMinimalDFA(), containsBeginOfLine_, std::move(names_));
138138
}
139139

140140
LexerDef Compiler::compileMulti(OvershadowMap* overshadows)

src/regex_dfa/Compiler.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ class Compiler
4848
*/
4949
void declareAll(RuleList rules);
5050

51-
const RuleList& rules() const noexcept { return rules_; }
52-
const TagNameMap& names() const noexcept { return names_; }
53-
size_t size() const;
51+
[[nodiscard]] const RuleList& rules() const noexcept { return rules_; }
52+
[[nodiscard]] const TagNameMap& names() const noexcept { return names_; }
53+
[[nodiscard]] size_t size() const;
5454

5555
/**
5656
* Compiles all previousely parsed rules into a DFA.
@@ -84,9 +84,9 @@ class Compiler
8484
static LexerDef generateTables(const DFA& dfa, bool requiresBeginOfLine, const TagNameMap& names);
8585
static LexerDef generateTables(const MultiDFA& dfa, bool requiresBeginOfLine, const TagNameMap& names);
8686

87-
const std::map<std::string, NFA>& automata() const { return fa_; }
87+
[[nodiscard]] const std::map<std::string, NFA>& automata() const { return fa_; }
8888

89-
bool containsBeginOfLine() const noexcept { return containsBeginOfLine_; }
89+
[[nodiscard]] bool containsBeginOfLine() const noexcept { return containsBeginOfLine_; }
9090

9191
private:
9292
/**

src/regex_dfa/DFA.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Alphabet DFA::alphabet() const
3737
{
3838
Alphabet alphabet;
3939
for (const State& state: states_)
40-
for (const pair<Symbol, StateId>& t: state.transitions)
40+
for (pair<Symbol, StateId> const t: state.transitions)
4141
alphabet.insert(t.first);
4242

4343
return alphabet;
@@ -118,12 +118,12 @@ void DFA::prepareStateIds(StateId baseId, StateId q0)
118118
AcceptMap remapped;
119119
for (auto& a: acceptTags_)
120120
remapped[transformId(a.first)] = a.second;
121-
acceptTags_ = move(remapped);
121+
acceptTags_ = std::move(remapped);
122122

123123
BacktrackingMap backtracking;
124124
for (const auto& bt: backtrackStates_)
125125
backtracking[transformId(bt.first)] = transformId(bt.second);
126-
backtrackStates_ = move(backtracking);
126+
backtrackStates_ = std::move(backtracking);
127127

128128
initialState_ = q0;
129129
}

src/regex_dfa/DFA.h

+15-15
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ class DFA
5757
}
5858

5959
//! Retrieves the alphabet of this finite automaton.
60-
Alphabet alphabet() const;
60+
[[nodiscard]] Alphabet alphabet() const;
6161

6262
//! Retrieves the initial state.
63-
StateId initialState() const { return initialState_; }
63+
[[nodiscard]] StateId initialState() const { return initialState_; }
6464

6565
//! Retrieves the list of available states.
66-
const StateVec& states() const { return states_; }
67-
StateVec& states() { return states_; }
66+
[[nodiscard]] const StateVec& states() const { return states_; }
67+
[[nodiscard]] StateVec& states() { return states_; }
6868

69-
StateIdVec stateIds() const
69+
[[nodiscard]] StateIdVec stateIds() const
7070
{
7171
StateIdVec v;
7272
v.reserve(states_.size());
@@ -76,7 +76,7 @@ class DFA
7676
}
7777

7878
//! Retrieves the list of accepting states.
79-
std::vector<StateId> acceptStates() const;
79+
[[nodiscard]] std::vector<StateId> acceptStates() const;
8080

8181
/**
8282
* Traverses all states and edges in this NFA and calls @p visitor for each state & edge.
@@ -89,39 +89,39 @@ class DFA
8989

9090
void setInitialState(StateId state);
9191

92-
const TransitionMap& stateTransitions(StateId id) const
92+
[[nodiscard]] const TransitionMap& stateTransitions(StateId id) const
9393
{
9494
return states_[static_cast<size_t>(id)].transitions;
9595
}
9696

9797
// {{{ backtracking (for lookahead)
9898
void setBacktrack(StateId from, StateId to) { backtrackStates_[from] = to; }
9999

100-
std::optional<StateId> backtrack(StateId acceptState) const
100+
[[nodiscard]] std::optional<StateId> backtrack(StateId acceptState) const
101101
{
102102
if (auto i = backtrackStates_.find(acceptState); i != backtrackStates_.end())
103103
return i->second;
104104

105105
return std::nullopt;
106106
}
107107

108-
const BacktrackingMap& backtracking() const noexcept { return backtrackStates_; }
108+
[[nodiscard]] const BacktrackingMap& backtracking() const noexcept { return backtrackStates_; }
109109
// }}}
110110

111111
//! Flags given state as accepting-state with given Tag @p acceptTag.
112112
void setAccept(StateId state, Tag acceptTag) { acceptTags_[state] = acceptTag; }
113113

114-
bool isAccepting(StateId s) const { return acceptTags_.find(s) != acceptTags_.end(); }
114+
[[nodiscard]] bool isAccepting(StateId s) const { return acceptTags_.find(s) != acceptTags_.end(); }
115115

116-
std::optional<Tag> acceptTag(StateId s) const
116+
[[nodiscard]] std::optional<Tag> acceptTag(StateId s) const
117117
{
118118
if (auto i = acceptTags_.find(s); i != acceptTags_.end())
119119
return i->second;
120120

121121
return std::nullopt;
122122
}
123123

124-
std::optional<StateId> delta(StateId state, Symbol symbol) const
124+
[[nodiscard]] std::optional<StateId> delta(StateId state, Symbol symbol) const
125125
{
126126
const auto& T = states_[state].transitions;
127127
if (auto i = T.find(symbol); i != T.end())
@@ -133,7 +133,7 @@ class DFA
133133
void setTransition(StateId from, Symbol symbol, StateId to);
134134
void removeTransition(StateId from, Symbol symbol);
135135

136-
StateIdVec nonAcceptStates() const
136+
[[nodiscard]] StateIdVec nonAcceptStates() const
137137
{
138138
StateIdVec result;
139139
result.reserve(
@@ -146,9 +146,9 @@ class DFA
146146
return result;
147147
}
148148

149-
bool isAcceptor(Tag t) const
149+
[[nodiscard]] bool isAcceptor(Tag t) const
150150
{
151-
for (const std::pair<StateId, Tag>& p: acceptTags_)
151+
for (std::pair<StateId, Tag> p: acceptTags_)
152152
if (p.second == t)
153153
return true;
154154

src/regex_dfa/DFABuilder.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ DFA DFABuilder::constructDFA(const vector<StateIdVec>& Q,
166166

167167
// observe mapping from q_i to d_i
168168
for (auto const& [q_i, branch]: T.transitions)
169-
for (auto const [c, t_i]: branch)
169+
for (auto && [c, t_i]: branch)
170170
dfa.setTransition(q_i, c, t_i);
171171

172172
// q_0 becomes d_0 (initial state)

src/regex_dfa/DFABuilder.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,20 @@ class DFABuilder
3232
* @param overshadows if not nullptr, it will be used to store semantic information about
3333
* which rule tags have been overshadowed by which.
3434
*/
35-
DFA construct(OvershadowMap* overshadows = nullptr);
35+
[[nodiscard]] DFA construct(OvershadowMap* overshadows = nullptr);
3636

3737
private:
3838
struct TransitionTable;
3939

40-
DFA constructDFA(const std::vector<StateIdVec>& Q,
41-
const TransitionTable& T,
42-
OvershadowMap* overshadows) const;
40+
[[nodiscard]] DFA constructDFA(const std::vector<StateIdVec>& Q,
41+
const TransitionTable& T,
42+
OvershadowMap* overshadows) const;
4343

4444
/**
4545
* Finds @p t in @p Q and returns its offset (aka configuration number) or -1 if not found.
4646
*/
47-
static std::optional<StateId> configurationNumber(const std::vector<StateIdVec>& Q, const StateIdVec& t);
47+
[[nodiscard]] static std::optional<StateId> configurationNumber(const std::vector<StateIdVec>& Q,
48+
const StateIdVec& t);
4849

4950
/**
5051
* Determines the tag to use for the deterministic state representing @p q from non-deterministic FA @p
@@ -54,7 +55,7 @@ class DFABuilder
5455
*
5556
* @returns the determined tag or std::nullopt if none
5657
*/
57-
std::optional<Tag> determineTag(const StateIdVec& q, std::map<Tag, Tag>* overshadows) const;
58+
[[nodiscard]] std::optional<Tag> determineTag(const StateIdVec& q, std::map<Tag, Tag>* overshadows) const;
5859

5960
private:
6061
const NFA nfa_;

src/regex_dfa/DFABuilder_test.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
#include <regex_dfa/DFABuilder.h>
1111
#include <regex_dfa/MultiDFA.h>
1212

13+
#include <catch2/catch.hpp>
14+
1315
#include <memory>
1416
#include <sstream>
1517

16-
#include <klex/util/testing.h>
17-
1818
using namespace regex_dfa;
1919

20-
TEST(regex_DFABuilder, shadowing)
20+
TEST_CASE("regex_DFABuilder.shadowing")
2121
{
2222
Compiler cc;
2323
cc.parse(std::make_unique<std::stringstream>(R"(
@@ -27,7 +27,7 @@ TEST(regex_DFABuilder, shadowing)
2727
// rule 2 is overshadowed by rule 1
2828
Compiler::OvershadowMap overshadows;
2929
DFA dfa = cc.compileDFA(&overshadows);
30-
ASSERT_EQ(1, overshadows.size());
31-
EXPECT_EQ(2, overshadows[0].first); // overshadowee
32-
EXPECT_EQ(1, overshadows[0].second); // overshadower
30+
REQUIRE(1 == overshadows.size());
31+
CHECK(2 == overshadows[0].first); // overshadowee
32+
CHECK(1 == overshadows[0].second); // overshadower
3333
}

0 commit comments

Comments
 (0)