mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 11:55:30 +00:00
Add error message support associated with definition rule
This commit is contained in:
parent
b64b53aeef
commit
8c240f25cf
11
peglib.h
11
peglib.h
@ -2735,6 +2735,12 @@ inline size_t Holder::parse_core(const char *s, size_t n, SemanticValues &vs,
|
|||||||
c.error_info.message = msg;
|
c.error_info.message = msg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (c.log && !outer_->error_message.empty() &&
|
||||||
|
c.error_info.message_pos < s) {
|
||||||
|
c.error_info.message_pos = s;
|
||||||
|
c.error_info.message = outer_->error_message;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -3382,9 +3388,8 @@ private:
|
|||||||
opt(seq(g["InstructionItem"], zom(seq(g["InstructionItemSeparator"],
|
opt(seq(g["InstructionItem"], zom(seq(g["InstructionItemSeparator"],
|
||||||
g["InstructionItem"])))),
|
g["InstructionItem"])))),
|
||||||
g["EndBlacket"]);
|
g["EndBlacket"]);
|
||||||
g["InstructionItem"] <= cho(g["PrecedenceClimbing"], g["ErrorMessage"],
|
g["InstructionItem"] <=
|
||||||
g["NoAstOpt"]
|
cho(g["PrecedenceClimbing"], g["ErrorMessage"], g["NoAstOpt"]);
|
||||||
);
|
|
||||||
~g["InstructionItemSeparator"] <= seq(chr(';'), g["Spacing"]);
|
~g["InstructionItemSeparator"] <= seq(chr(';'), g["Spacing"]);
|
||||||
|
|
||||||
~g["SpacesZom"] <= zom(g["Space"]);
|
~g["SpacesZom"] <= zom(g["Space"]);
|
||||||
|
@ -2051,3 +2051,39 @@ SkipToRCUR ← (!RCUR (LCUR SkipToRCUR / .))* RCUR
|
|||||||
)",
|
)",
|
||||||
ast_to_s(ast));
|
ast_to_s(ast));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(ErrorTest, Error_message_with_rule_instruction) {
|
||||||
|
parser pg(R"(
|
||||||
|
START <- (LINE (ln LINE)*)? ln?
|
||||||
|
|
||||||
|
LINE <- STR '=' CODE
|
||||||
|
|
||||||
|
CODE <- HEX / DEC { message 'code format error...' }
|
||||||
|
HEX <- < [a-f0-9]+ 'h' >
|
||||||
|
DEC <- < [0-9]+ >
|
||||||
|
|
||||||
|
STR <- < [a-z0-9]+ >
|
||||||
|
|
||||||
|
~ln <- [\r\n]+
|
||||||
|
%whitespace <- [ \t]*
|
||||||
|
)");
|
||||||
|
|
||||||
|
EXPECT_TRUE(!!pg);
|
||||||
|
|
||||||
|
std::vector<std::string> errors{
|
||||||
|
R"(2:5: code format error...)",
|
||||||
|
};
|
||||||
|
|
||||||
|
size_t i = 0;
|
||||||
|
pg.log = [&](size_t ln, size_t col, const std::string &msg) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << ln << ":" << col << ": " << msg;
|
||||||
|
EXPECT_EQ(errors[i++], ss.str());
|
||||||
|
};
|
||||||
|
|
||||||
|
EXPECT_FALSE(pg.parse(R"(1 = ah
|
||||||
|
2 = b
|
||||||
|
3 = ch
|
||||||
|
)"));
|
||||||
|
EXPECT_EQ(i, errors.size());
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user