Skip to content

Commit dc61221

Browse files
ldirrycorepointer
authored andcommitted
[DAPHNE-#773] Undetected invalid script args
- Fixed invalid script arguments that try to evaulate an expression - Fixed invalid script arguments that were not literals - Added testcases for single script arguments - Added testcases where Daphne fails in case of an invalid script arguments
1 parent e6cd72a commit dc61221

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

src/parser/daphnedsl/DaphneDSLVisitor.cpp

+27-13
Original file line numberDiff line numberDiff line change
@@ -794,27 +794,41 @@ antlrcpp::Any DaphneDSLVisitor::visitArgExpr(DaphneDSLGrammarParser::ArgExprCont
794794
"argument " + arg + " referenced, but not provided as a command line argument"
795795
);
796796

797-
bool hasMinus;
798-
std::string litStr;
799-
if(!it->second.empty() && it->second[0] == '-') {
797+
std::string argValue = it->second;
798+
bool hasMinus = false;
799+
if (!argValue.empty() && argValue[0] == '-') {
800800
hasMinus = true;
801-
litStr = it->second.substr(1);
802-
}
803-
else {
804-
hasMinus = false;
805-
litStr = it->second;
801+
argValue = argValue.substr(1);
806802
}
807803

808-
// Parse the string that was passed as the value for this argument on the
809-
// command line as a DaphneDSL literal.
804+
// Parse the argument value as a literal
810805
// TODO: fix for string literals when " are not escaped or not present
811-
std::istringstream stream(litStr);
806+
std::istringstream stream(argValue);
812807
antlr4::ANTLRInputStream input(stream);
813-
input.name = "argument"; // TODO Does this make sense?
808+
input.name = "argument";
814809
DaphneDSLGrammarLexer lexer(&input);
815810
antlr4::CommonTokenStream tokens(&lexer);
816811
DaphneDSLGrammarParser parser(&tokens);
817-
DaphneDSLGrammarParser::LiteralContext * literalCtx = parser.literal();
812+
813+
CancelingErrorListener errorListener;
814+
lexer.removeErrorListeners();
815+
lexer.addErrorListener(&errorListener);
816+
parser.removeErrorListeners();
817+
parser.addErrorListener(&errorListener);
818+
819+
DaphneDSLGrammarParser::LiteralContext * literalCtx = nullptr;
820+
try {
821+
literalCtx = parser.literal();
822+
if (tokens.LA(1) != antlr4::Token::EOF) {
823+
// Ensure entire input is consumed; if not, it's not a valid literal
824+
throw std::runtime_error("Extra input after literal");
825+
}
826+
}
827+
catch (std::exception & e) {
828+
throw ErrorHandler::compilerError(utils.getLoc(ctx->start), "DSLVisitor",
829+
"Invalid literal value for argument '" + arg + "': " + argValue
830+
);
831+
}
818832

819833
mlir::Value lit = visitLiteral(literalCtx);
820834
if(!hasMinus)

test/api/cli/scriptargs/ScriptArgsTest.cpp

+15-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ TEST_CASE("Print single script argument", TAG_SCRIPTARGS) {
3131
compareDaphneToStr("-123.45\n" , scriptPath.c_str(), "--args", "foo=-123.45");
3232
compareDaphneToStr("1\n" , scriptPath.c_str(), "--args", "foo=true");
3333
compareDaphneToStr("hello world\n", scriptPath.c_str(), "--args", "foo=\"hello world\"");
34+
compareDaphneToStr("nan\n", scriptPath.c_str(), "--args", "foo=nan");
35+
compareDaphneToStr("inf\n", scriptPath.c_str(), "--args", "foo=inf");
36+
compareDaphneToStr("-inf\n", scriptPath.c_str(), "--args", "foo=-inf");
37+
compareDaphneToStr("12.34\n", scriptPath.c_str(), "--args", "foo=12.34f");
38+
compareDaphneToStr("120000\n", scriptPath.c_str(), "--args", "foo=1.2e5f");
39+
compareDaphneToStr("1e-14\n", scriptPath.c_str(), "--args", "foo=1E-14");
40+
compareDaphneToStr("1\n", scriptPath.c_str(), "--args", "foo=true");
41+
compareDaphneToStr("0\n", scriptPath.c_str(), "--args", "foo=false");
3442
}
3543

3644
TEST_CASE("Missing script argument", TAG_SCRIPTARGS) {
@@ -89,13 +97,15 @@ TEST_CASE("Ways of specifying script arguments", TAG_SCRIPTARGS) {
8997
CHECK(err.str() == "");
9098
}
9199

92-
// TODO While DAPHNE does not evaluate the expressions provided as script arguments,
93-
// these invalid script arguments are not properly detected, either. DAPHNE succeeds
94-
// with unexpected results (see #773).
95-
#if 0
100+
96101
TEST_CASE("Don't support general expressions as script arguments", TAG_SCRIPTARGS) {
97102
const std::string scriptPath = dirPath + "printSingleArg.daphne";
98103
checkDaphneFails(scriptPath.c_str(), "--args", "foo=10+10");
99104
checkDaphneFails(scriptPath.c_str(), "--args", "foo=sin(1.23)");
100105
}
101-
#endif
106+
107+
TEST_CASE("Don't support literal mixtures ", TAG_SCRIPTARGS) {
108+
const std::string scriptPath = dirPath + "printSingleArg.daphne";
109+
checkDaphneFails(scriptPath.c_str(), "--args", "foo=10xyz23");
110+
checkDaphneFails(scriptPath.c_str(), "--args", "foo=-0.0mo");
111+
}

0 commit comments

Comments
 (0)