mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2025-01-10 09:35:30 +00:00
Added definition duplicates check.
This commit is contained in:
parent
67de659288
commit
c2e19cfd01
42
peglib.h
42
peglib.h
@ -1514,11 +1514,12 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Data {
|
struct Data {
|
||||||
std::shared_ptr<Grammar> grammar;
|
std::shared_ptr<Grammar> grammar;
|
||||||
std::string start;
|
std::string start;
|
||||||
MatchAction match_action;
|
MatchAction match_action;
|
||||||
std::unordered_map<std::string, const char*> references;
|
std::vector<std::pair<std::string, const char*>> duplicates;
|
||||||
size_t capture_count;
|
std::unordered_map<std::string, const char*> references;
|
||||||
|
size_t capture_count;
|
||||||
|
|
||||||
Data()
|
Data()
|
||||||
: grammar(std::make_shared<Grammar>())
|
: grammar(std::make_shared<Grammar>())
|
||||||
@ -1693,13 +1694,18 @@ private:
|
|||||||
const auto& name = sv[baseId].val.get<std::string>();
|
const auto& name = sv[baseId].val.get<std::string>();
|
||||||
auto ope = sv[baseId + 2].val.get<std::shared_ptr<Ope>>();
|
auto ope = sv[baseId + 2].val.get<std::shared_ptr<Ope>>();
|
||||||
|
|
||||||
auto& rule = (*data.grammar)[name];
|
auto& grammar = *data.grammar;
|
||||||
rule <= ope;
|
if (grammar.find(name) == grammar.end()) {
|
||||||
rule.name = name;
|
auto& rule = grammar[name];
|
||||||
rule.ignoreSemanticValue = ignore;
|
rule <= ope;
|
||||||
|
rule.name = name;
|
||||||
|
rule.ignoreSemanticValue = ignore;
|
||||||
|
|
||||||
if (data.start.empty()) {
|
if (data.start.empty()) {
|
||||||
data.start = name;
|
data.start = name;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
data.duplicates.push_back(std::make_pair(name, sv.s));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1868,9 +1874,19 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check missing definitions
|
// Check duplicated definitions
|
||||||
bool ret = true;
|
bool ret = data.duplicates.empty();;
|
||||||
|
|
||||||
|
for (const auto& x: data.duplicates) {
|
||||||
|
if (log) {
|
||||||
|
const auto& name = x.first;
|
||||||
|
auto ptr = x.second;
|
||||||
|
auto line = line_info(s, ptr);
|
||||||
|
log(line.first, line.second, "'" + name + "' is already defined.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check missing definitions
|
||||||
for (const auto& x : data.references) {
|
for (const auto& x : data.references) {
|
||||||
const auto& name = x.first;
|
const auto& name = x.first;
|
||||||
auto ptr = x.second;
|
auto ptr = x.second;
|
||||||
|
10
test/test.cc
10
test/test.cc
@ -565,6 +565,16 @@ TEST_CASE("Ignore semantic value of 'and' predicate test", "[general]")
|
|||||||
REQUIRE(ast->nodes[0]->name == "HELLO_WORLD");
|
REQUIRE(ast->nodes[0]->name == "HELLO_WORLD");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Definition duplicates test", "[general]")
|
||||||
|
{
|
||||||
|
peg parser(
|
||||||
|
" A <- ''"
|
||||||
|
" A <- ''"
|
||||||
|
);
|
||||||
|
|
||||||
|
REQUIRE(parser == false);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("Left recursive test", "[left recursive]")
|
TEST_CASE("Left recursive test", "[left recursive]")
|
||||||
{
|
{
|
||||||
peg parser(
|
peg parser(
|
||||||
|
Loading…
Reference in New Issue
Block a user