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 Aug 13, 2023
1 parent 4ae771e commit 808f5ee
Show file tree
Hide file tree
Showing 59 changed files with 1,397 additions and 3,430 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ jobs:
cmake -DCMAKE_BUILD_TYPE=Debug -DLIBTERMINAL_BUILD_BENCH_HEADLESS=ON -DCONTOUR_QT_VERSION=6 -S . -B build
cmake --build build/ -j2
./build/src/crispy/crispy_test
./build/src/regex_dfa/regex_dfa_test
./build/src/vtparser/vtparser_test
./build/src/vtbackend/vtbackend_test
rm -rf _deps build
Expand Down Expand Up @@ -257,6 +258,8 @@ jobs:
run: cmake --build build/
- name: "test: crispy"
run: ./build/src/crispy/crispy_test
- name: "test: regex_dfa"
run: ./build/src/regex_dfa/regex_dfa_test
- name: "test: vtparser"
run: ./build/src/vtparser/vtparser_test
- name: "test: vtbackend"
Expand Down Expand Up @@ -326,6 +329,8 @@ jobs:
run: cmake --build build/ --config Release
- name: "test: crispy"
run: .\build\src\crispy\Release\crispy_test.exe
- name: "test: regex_dfa"
run: .\build\src\regex_dfa\Release\regex_dfa_test.exe
- name: "test: vtparser"
run: .\build\src\vtparser\Release\vtparser_test.exe
- name: "test: vtbackend"
Expand Down Expand Up @@ -450,6 +455,8 @@ jobs:
run: cmake --build build/ -- -j3
- name: "test: crispy"
run: ./build/src/crispy/crispy_test
- name: "test: regex_dfa"
run: ./build/src/regex_dfa/regex_dfa_test
- name: "test: vtparser"
run: ./build/src/vtparser/vtparser_test
- name: "test: vtbackend"
Expand All @@ -461,6 +468,7 @@ jobs:
name: contour-ubuntu2204-tests
path: |
build/src/crispy/crispy_test
build/src/regex_dfa/regex_dfa_test
build/src/vtparser/vtparser_test
build/src/vtbackend/vtbackend_test
build/src/vtbackend/bench-headless
Expand Down Expand Up @@ -516,6 +524,8 @@ jobs:
# run: cmake --build build/ -- -j3
# - name: "test: crispy"
# run: ./build/src/crispy/crispy_test
# - name: "test: regex_dfa"
# run: ./build/src/regex_dfa/regex_dfa_test
# - name: "test: vtparser"
# run: ./build/src/vtparser/vtparser_test
# - name: "test: vtbackend"
Expand Down Expand Up @@ -664,6 +674,8 @@ jobs:
run: cmake --build build/ -- -j3
- name: "test: crispy"
run: ./build/src/crispy/crispy_test
- name: "test: regex_dfa"
run: ./build/src/regex_dfa/regex_dfa_test
- name: "test: vtparser"
run: ./build/src/vtparser/vtparser_test
- name: "test: vtbackend"
Expand Down Expand Up @@ -728,6 +740,8 @@ jobs:
valgrind
- name: "test: crispy (via valgrind)"
run: valgrind --error-exitcode=64 ./build/src/crispy/crispy_test
- name: "test: regex_dfa"
run: valgrind --error-exitcode=64 ./build/src/regex_dfa/regex_dfa_test
- name: "test: vtparser (via valgrind)"
run: valgrind --error-exitcode=64 ./build/src/vtparser/vtparser_test
- name: "test: vtbackend (via valgrind)"
Expand Down
6 changes: 3 additions & 3 deletions src/regex_dfa/Alphabet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ namespace regex_dfa

void Alphabet::insert(Symbol ch)
{
if (alphabet_.find(ch) == alphabet_.end())
if (_alphabet.find(ch) == _alphabet.end())
{
DEBUG("Alphabet: insert '{:}'", prettySymbol(ch));
alphabet_.insert(ch);
_alphabet.insert(ch);
}
}

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

sstr << '{';

for (Symbol c: alphabet_)
for (Symbol c: _alphabet)
sstr << prettySymbol(c);

sstr << '}';
Expand Down
12 changes: 6 additions & 6 deletions src/regex_dfa/Alphabet.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ class Alphabet
using set_type = std::set<Symbol>;
using iterator = set_type::iterator;

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

void insert(Symbol ch);

std::string to_string() const;
[[nodiscard]] std::string to_string() const;

const iterator begin() const { return alphabet_.begin(); }
const iterator end() const { return alphabet_.end(); }
[[nodiscard]] iterator begin() const { return _alphabet.begin(); }
[[nodiscard]] iterator end() const { return _alphabet.end(); }

private:
set_type alphabet_;
set_type _alphabet;
};

} // namespace regex_dfa
Expand All @@ -54,7 +54,7 @@ struct formatter<regex_dfa::Alphabet>
template <typename FormatContext>
constexpr auto format(const regex_dfa::Alphabet& v, FormatContext& ctx)
{
return format_to(ctx.out(), "{}", v.to_string());
return fmt::format_to(ctx.out(), "{}", v.to_string());
}
};
} // namespace fmt
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)
30 changes: 15 additions & 15 deletions src/regex_dfa/CharStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,41 @@ class CharStream
class StringStream: public CharStream
{
public:
explicit StringStream(std::string&& s): source_ { std::move(s) } {}
explicit StringStream(std::string&& s): _source { std::move(s) } {}

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

private:
std::string source_;
size_t pos_ = 0;
std::string _source;
size_t _pos = 0;
};

class StandardStream: public CharStream
{
public:
explicit StandardStream(std::istream* source);

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

void rollback(int count) override
{
source_->clear();
source_->seekg(-count, std::ios::cur);
_source->clear();
_source->seekg(-count, std::ios::cur);
}

void rewind() override
{
source_->clear();
source_->seekg(initialOffset_, std::ios::beg);
_source->clear();
_source->seekg(_initialOffset, std::ios::beg);
}

private:
std::istream* source_;
std::streamoff initialOffset_;
std::istream* _source;
std::streamoff _initialOffset;
};

} // namespace regex_dfa
26 changes: 12 additions & 14 deletions src/regex_dfa/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ namespace regex_dfa

void Compiler::parse(string text)
{
parse(make_unique<stringstream>(move(text)));
parse(make_unique<stringstream>(std::move(text)));
}

void Compiler::parse(unique_ptr<istream> stream)
{
declareAll(RuleParser { move(stream) }.parseRules());
declareAll(RuleParser { std::move(stream) }.parseRules());
}

void Compiler::declareAll(RuleList rules)
Expand Down Expand Up @@ -81,7 +81,7 @@ void Compiler::declareAll(RuleList rules)
else
names_[rule.tag] = rule.name;

rules_.emplace_back(move(rule));
rules_.emplace_back(std::move(rule));
}
}

Expand Down Expand Up @@ -118,7 +118,7 @@ MultiDFA Compiler::compileMultiDFA(OvershadowMap* overshadows)
for (const auto& fa: fa_)
dfaMap[fa.first] = DFABuilder { fa.second.clone() }.construct(overshadows);

return constructMultiDFA(move(dfaMap));
return constructMultiDFA(std::move(dfaMap));
}

DFA Compiler::compileDFA(OvershadowMap* overshadows)
Expand All @@ -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 All @@ -144,7 +144,7 @@ LexerDef Compiler::compileMulti(OvershadowMap* overshadows)
return generateTables(multiDFA, containsBeginOfLine_, names());
}

LexerDef Compiler::generateTables(const DFA& dfa, bool requiresBeginOfLine, const map<Tag, string>& names)
LexerDef Compiler::generateTables(const DFA& dfa, bool requiresBeginOfLine, map<Tag, string> names)
{
const Alphabet alphabet = dfa.alphabet();
TransitionMap transitionMap;
Expand All @@ -161,15 +161,13 @@ LexerDef Compiler::generateTables(const DFA& dfa, bool requiresBeginOfLine, cons
// TODO: many initial states !
return LexerDef { { { "INITIAL", dfa.initialState() } },
requiresBeginOfLine,
move(transitionMap),
move(acceptStates),
std::move(transitionMap),
std::move(acceptStates),
dfa.backtracking(),
move(names) };
std::move(names) };
}

LexerDef Compiler::generateTables(const MultiDFA& multiDFA,
bool requiresBeginOfLine,
const map<Tag, string>& names)
LexerDef Compiler::generateTables(const MultiDFA& multiDFA, bool requiresBeginOfLine, map<Tag, string> names)
{
const Alphabet alphabet = multiDFA.dfa.alphabet();
TransitionMap transitionMap;
Expand All @@ -184,8 +182,8 @@ LexerDef Compiler::generateTables(const MultiDFA& multiDFA,
acceptStates.emplace(s, *multiDFA.dfa.acceptTag(s));

// TODO: many initial states !
return LexerDef { multiDFA.initialStates, requiresBeginOfLine, move(transitionMap),
move(acceptStates), multiDFA.dfa.backtracking(), move(names) };
return LexerDef { multiDFA.initialStates, requiresBeginOfLine, std::move(transitionMap),
std::move(acceptStates), multiDFA.dfa.backtracking(), std::move(names) };
}

} // namespace regex_dfa
14 changes: 7 additions & 7 deletions src/regex_dfa/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class Compiler
*/
void declareAll(RuleList rules);

const RuleList& rules() const noexcept { return rules_; }
const TagNameMap& names() const noexcept { return names_; }
size_t size() const;
[[nodiscard]] const RuleList& rules() const noexcept { return rules_; }
[[nodiscard]] const TagNameMap& names() const noexcept { return names_; }
[[nodiscard]] size_t size() const;

/**
* Compiles all previousely parsed rules into a DFA.
Expand Down Expand Up @@ -81,12 +81,12 @@ class Compiler
*
* @see Lexer
*/
static LexerDef generateTables(const DFA& dfa, bool requiresBeginOfLine, const TagNameMap& names);
static LexerDef generateTables(const MultiDFA& dfa, bool requiresBeginOfLine, const TagNameMap& names);
static LexerDef generateTables(const DFA& dfa, bool requiresBeginOfLine, TagNameMap names);
static LexerDef generateTables(const MultiDFA& dfa, bool requiresBeginOfLine, TagNameMap names);

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

bool containsBeginOfLine() const noexcept { return containsBeginOfLine_; }
[[nodiscard]] bool containsBeginOfLine() const noexcept { return containsBeginOfLine_; }

private:
/**
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
Loading

0 comments on commit 808f5ee

Please sign in to comment.