Skip to content

Commit

Permalink
Adapt to Contour's best code quality & maintenance practice.
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Parpart <christian@parpart.family>
  • Loading branch information
christianparpart committed Apr 11, 2023
1 parent f253291 commit 6adefd7
Show file tree
Hide file tree
Showing 33 changed files with 951 additions and 1,932 deletions.
8 changes: 5 additions & 3 deletions src/regex_dfa/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ target_include_directories(regex_dfa PUBLIC ${PROJECT_SOURCE_DIR}/src ${CMAKE_SO
target_link_libraries(regex_dfa PUBLIC fmt::fmt-header-only)

# ----------------------------------------------------------------------------
if(TESTS)
option(REGEX_DFA_TESTING "Enables building of unittests for regex_dfa library [default: ON]" ON)
if(REGEX_DFA_TESTING)
enable_testing()
add_executable(regex_dfa_test
regex_dfa_test.cpp
DFABuilder_test.cpp
Expand All @@ -33,9 +35,9 @@ if(TESTS)
State_test.cpp
Symbols_test.cpp
util/iterator_test.cpp
util/testing.cpp
)

target_link_libraries(regex_dfa_test PUBLIC regex_dfa)
target_link_libraries(regex_dfa_test PUBLIC Catch2::Catch2)
target_link_libraries(regex_dfa_test PUBLIC fmt::fmt-header-only)
endif(TESTS)
endif(REGEX_DFA_TESTING)
2 changes: 1 addition & 1 deletion src/regex_dfa/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ DFA Compiler::compileMinimalDFA()

LexerDef Compiler::compile()
{
return generateTables(compileMinimalDFA(), containsBeginOfLine_, move(names_));
return generateTables(compileMinimalDFA(), containsBeginOfLine_, std::move(names_));
}

LexerDef Compiler::compileMulti(OvershadowMap* overshadows)
Expand Down
6 changes: 3 additions & 3 deletions src/regex_dfa/DFA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Alphabet DFA::alphabet() const
{
Alphabet alphabet;
for (const State& state: states_)
for (const pair<Symbol, StateId>& t: state.transitions)
for (pair<Symbol, StateId> const t: state.transitions)
alphabet.insert(t.first);

return alphabet;
Expand Down Expand Up @@ -118,12 +118,12 @@ void DFA::prepareStateIds(StateId baseId, StateId q0)
AcceptMap remapped;
for (auto& a: acceptTags_)
remapped[transformId(a.first)] = a.second;
acceptTags_ = move(remapped);
acceptTags_ = std::move(remapped);

BacktrackingMap backtracking;
for (const auto& bt: backtrackStates_)
backtracking[transformId(bt.first)] = transformId(bt.second);
backtrackStates_ = move(backtracking);
backtrackStates_ = std::move(backtracking);

initialState_ = q0;
}
Expand Down
30 changes: 15 additions & 15 deletions src/regex_dfa/DFA.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ class DFA
}

//! Retrieves the alphabet of this finite automaton.
Alphabet alphabet() const;
[[nodiscard]] Alphabet alphabet() const;

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

//! Retrieves the list of available states.
const StateVec& states() const { return states_; }
StateVec& states() { return states_; }
[[nodiscard]] const StateVec& states() const { return states_; }
[[nodiscard]] StateVec& states() { return states_; }

StateIdVec stateIds() const
[[nodiscard]] StateIdVec stateIds() const
{
StateIdVec v;
v.reserve(states_.size());
Expand All @@ -76,7 +76,7 @@ class DFA
}

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

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

void setInitialState(StateId state);

const TransitionMap& stateTransitions(StateId id) const
[[nodiscard]] const TransitionMap& stateTransitions(StateId id) const
{
return states_[static_cast<size_t>(id)].transitions;
}

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

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

return std::nullopt;
}

const BacktrackingMap& backtracking() const noexcept { return backtrackStates_; }
[[nodiscard]] const BacktrackingMap& backtracking() const noexcept { return backtrackStates_; }
// }}}

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

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

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

return std::nullopt;
}

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

StateIdVec nonAcceptStates() const
[[nodiscard]] StateIdVec nonAcceptStates() const
{
StateIdVec result;
result.reserve(
Expand All @@ -146,9 +146,9 @@ class DFA
return result;
}

bool isAcceptor(Tag t) const
[[nodiscard]] bool isAcceptor(Tag t) const
{
for (const std::pair<StateId, Tag>& p: acceptTags_)
for (std::pair<StateId, Tag> p: acceptTags_)
if (p.second == t)
return true;

Expand Down
12 changes: 6 additions & 6 deletions src/regex_dfa/DFABuilder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
#include <regex_dfa/DFABuilder.h>
#include <regex_dfa/MultiDFA.h>

#include <catch2/catch.hpp>

#include <memory>
#include <sstream>

#include <klex/util/testing.h>

using namespace regex_dfa;

TEST(regex_DFABuilder, shadowing)
TEST_CASE("regex_DFABuilder.shadowing")
{
Compiler cc;
cc.parse(std::make_unique<std::stringstream>(R"(
Expand All @@ -27,7 +27,7 @@ TEST(regex_DFABuilder, shadowing)
// rule 2 is overshadowed by rule 1
Compiler::OvershadowMap overshadows;
DFA dfa = cc.compileDFA(&overshadows);
ASSERT_EQ(1, overshadows.size());
EXPECT_EQ(2, overshadows[0].first); // overshadowee
EXPECT_EQ(1, overshadows[0].second); // overshadower
REQUIRE(1 == overshadows.size());
CHECK(2 == overshadows[0].first); // overshadowee
CHECK(1 == overshadows[0].second); // overshadower
}
10 changes: 5 additions & 5 deletions src/regex_dfa/DFAMinimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ DFAMinimizer::PartitionVec DFAMinimizer::split(const StateIdVec& S) const
{
DEBUG("split: {} on character '{}' into {} sets", to_string(S), (char) c, t_i.size());
PartitionVec result;
for (const pair<int, StateIdVec>& t: t_i)
for (auto&& t: t_i)
{
result.emplace_back(move(t.second));
result.emplace_back(std::move(t.second));
DEBUG(" partition {}: {}", t.first, t.second);
}
return result;
Expand All @@ -125,7 +125,7 @@ DFAMinimizer::PartitionVec DFAMinimizer::split(const StateIdVec& S) const
main.emplace_back(s);

if (!main.empty())
result.emplace_back(move(main));
result.emplace_back(std::move(main));
}
}

Expand Down Expand Up @@ -253,9 +253,9 @@ DFA DFAMinimizer::constructFromPartitions(const PartitionVec& P) const
for (const StateIdVec& p: P)
{
const StateId s = *p.begin();
for (const pair<Symbol, StateId>& transition: dfa_.stateTransitions(s))
for (pair<Symbol, StateId> const transition: dfa_.stateTransitions(s))
{
const int t_i = partitionId(transition.second);
auto const t_i = partitionId(transition.second);
DEBUG("map p{} --({})--> p{}", p_i, prettySymbol(transition.first), t_i);
dfamin.setTransition(p_i, transition.first, t_i);
}
Expand Down
2 changes: 1 addition & 1 deletion src/regex_dfa/DotWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void DotWriter::visitNode(StateId number, bool start, bool accept)
}
}

void DotWriter::visitEdge(StateId from, StateId to, Symbol s)
void DotWriter::visitEdge(StateId /*from*/, StateId to, Symbol s)
{
transitionGroups_[to].push_back(s);
}
Expand Down
12 changes: 5 additions & 7 deletions src/regex_dfa/DotWriter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

#include <sstream>

#include <klex/util/testing.h>
#include <catch2/catch.hpp>

using namespace std;
using namespace regex_dfa;

TEST(regex_DotWriter, simple)
TEST_CASE("regex_DotWriter.simple")
{
stringstream sstr;
DotWriter dw(sstr, "n");
Expand All @@ -33,12 +33,11 @@ TEST(regex_DotWriter, simple)
dw.endVisitEdge(1, 1);
dw.end();

log(sstr.str());
ASSERT_TRUE(!sstr.str().empty());
REQUIRE(!sstr.str().empty());
// just make sure it processes
}

TEST(regex_DotWriter, multidfa_simple)
TEST_CASE("regex_DotWriter.multidfa_simple")
{
stringstream sstr;
const MultiDFA::InitialStateMap mis { { "foo", 1 }, { "bar", 2 } };
Expand All @@ -63,7 +62,6 @@ TEST(regex_DotWriter, multidfa_simple)

dw.end();

log(sstr.str());
ASSERT_TRUE(!sstr.str().empty());
REQUIRE(!sstr.str().empty());
// just make sure it processes
}
48 changes: 25 additions & 23 deletions src/regex_dfa/Lexable.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ namespace regex_dfa
//! Runtime exception that is getting thrown when a word could not be recognized.
struct LexerError: public std::runtime_error
{
explicit LexerError(unsigned int _offset):
std::runtime_error { fmt::format("[{}] Failed to lexically recognize a word.", _offset) },
offset { _offset }
explicit LexerError(unsigned int offset):
std::runtime_error { fmt::format("[{}] Failed to lexically recognize a word.", offset) },
offset { offset }
{
}

Expand Down Expand Up @@ -72,7 +72,7 @@ class LexerIterator
/**
* Retrieves the default DFA machine that is used to recognize words.
*/
Machine defaultMachine() const noexcept;
[[nodiscard]] Machine defaultMachine() const noexcept;

/**
* Sets the active deterministic finite automaton to use for recognizing words.
Expand All @@ -82,11 +82,11 @@ class LexerIterator
*/
Machine setMachine(Machine machine);

const TokenInfo& operator*() const noexcept { return currentToken_; }
auto offset() const noexcept { return currentToken_.offset; }
auto literal() const noexcept -> const std::string& { return currentToken_.literal; }
auto token() const noexcept { return currentToken_.token; }
auto name() const noexcept { return name(token()); }
[[nodiscard]] const TokenInfo& operator*() const noexcept { return currentToken_; }
[[nodiscard]] auto offset() const noexcept { return currentToken_.offset; }
[[nodiscard]] auto literal() const noexcept -> const std::string& { return currentToken_.literal; }
[[nodiscard]] auto token() const noexcept { return currentToken_.token; }
[[nodiscard]] auto name() const noexcept { return name(token()); }

bool operator==(const LexerIterator& rhs) const noexcept;
bool operator!=(const LexerIterator& rhs) const noexcept;
Expand All @@ -96,15 +96,15 @@ class LexerIterator

private:
void recognize();
Token recognizeOne();
[[nodiscard]] Token recognizeOne();

// ---------------------------------------------------------------------------------
// state helpers

static constexpr StateId BadState = std::numeric_limits<StateId>::max();

StateId getInitialState() const noexcept;
bool isAcceptState(StateId state) const;
[[nodiscard]] StateId getInitialState() const noexcept;
[[nodiscard]] bool isAcceptState(StateId state) const;

/**
* Retrieves the next state for given input state and input symbol.
Expand All @@ -114,27 +114,27 @@ class LexerIterator
* state.
* @returns the next state to transition to.
*/
StateId delta(StateId currentState, Symbol inputSymbol) const;
[[nodiscard]] StateId delta(StateId currentState, Symbol inputSymbol) const;

// ---------------------------------------------------------------------------------
// stream helpers

int currentChar() const noexcept { return currentChar_; }
bool eof() const noexcept { return !source_->good(); }
[[nodiscard]] int currentChar() const noexcept { return currentChar_; }
[[nodiscard]] bool eof() const noexcept { return !source_->good(); }
Symbol nextChar();
void rollback();

// ---------------------------------------------------------------------------------
// debugging helpers

template <typename... Args>
void tracef(const char* msg, Args&&... args) const;
void tracef(fmt::format_string<Args...> msg, Args&&... args) const;

const std::string& name(Token t) const;
[[nodiscard]] const std::string& name(Token t) const;

std::string toString(const std::deque<StateId>& stack);
Token token(StateId s) const;
static std::string stateName(StateId s);
[[nodiscard]] std::string toString(const std::deque<StateId>& stack);
[[nodiscard]] Token token(StateId s) const;
[[nodiscard]] static std::string stateName(StateId s);

private:
const LexerDef* def_ = nullptr;
Expand Down Expand Up @@ -451,25 +451,27 @@ inline Symbol LexerIterator<Token, Machine, RequiresBeginOfLine, Trace>::nextCha
currentChar_ = ch;
buffered_.resize(buffered_.size() - 1);
if constexpr (Trace)
tracef("Lexer:{}: advance '{}'", offset_, prettySymbol(ch));
tracef("Lexer:{}: advance (buffered) '{}'", offset_, prettySymbol(ch));
offset_++;
return ch;
}

if (!source_->good())
{ // EOF or I/O error
if constexpr (Trace)
tracef("Lexer:{}: advance '{}'", offset_, "EOF");
tracef("Lexer:{}: advance '<<{}>>'", offset_, "EOF");
return Symbols::EndOfFile;
}

int ch = source_->get();
fmt::print("source.get: => {} (0x{:02X}, {})\n", ch, (uint8_t)ch, prettySymbol(ch));
if (ch < 0)
{
currentChar_ = Symbols::EndOfFile;
offset_++;
if constexpr (Trace)
tracef("Lexer:{}: advance '{}'", offset_, prettySymbol(ch));
fmt::print("EOF reached\n");
return currentChar_;
}

Expand All @@ -495,7 +497,7 @@ inline void LexerIterator<Token, Machine, RequiresBeginOfLine, Trace>::rollback(

template <typename Token, typename Machine, const bool RequiresBeginOfLine, const bool Trace>
template <typename... Args>
inline void LexerIterator<Token, Machine, RequiresBeginOfLine, Trace>::tracef(const char* msg,
inline void LexerIterator<Token, Machine, RequiresBeginOfLine, Trace>::tracef(fmt::format_string<Args...> msg,
Args&&... args) const
{
if constexpr (Trace)
Expand Down
Loading

0 comments on commit 6adefd7

Please sign in to comment.