Added definition duplicates check.

This commit is contained in:
yhirose 2015-06-13 00:57:45 -04:00
parent 67de659288
commit c2e19cfd01
2 changed files with 39 additions and 13 deletions

View File

@ -1517,6 +1517,7 @@ private:
std::shared_ptr<Grammar> grammar;
std::string start;
MatchAction match_action;
std::vector<std::pair<std::string, const char*>> duplicates;
std::unordered_map<std::string, const char*> references;
size_t capture_count;
@ -1693,7 +1694,9 @@ private:
const auto& name = sv[baseId].val.get<std::string>();
auto ope = sv[baseId + 2].val.get<std::shared_ptr<Ope>>();
auto& rule = (*data.grammar)[name];
auto& grammar = *data.grammar;
if (grammar.find(name) == grammar.end()) {
auto& rule = grammar[name];
rule <= ope;
rule.name = name;
rule.ignoreSemanticValue = ignore;
@ -1701,6 +1704,9 @@ private:
if (data.start.empty()) {
data.start = name;
}
} else {
data.duplicates.push_back(std::make_pair(name, sv.s));
}
};
g["Expression"] = [&](const SemanticValues& sv) {
@ -1868,9 +1874,19 @@ private:
}
}
// Check missing definitions
bool ret = true;
// Check duplicated definitions
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) {
const auto& name = x.first;
auto ptr = x.second;

View File

@ -565,6 +565,16 @@ TEST_CASE("Ignore semantic value of 'and' predicate test", "[general]")
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]")
{
peg parser(