Improve Error Reporting of SemVer Parser

This commit is contained in:
Vinay
2022-11-25 13:09:09 -03:00
committed by Matheus Aguiar
parent 10f81425a3
commit 9e7b85ac4b
20 changed files with 135 additions and 64 deletions
+22 -16
View File
@@ -153,22 +153,28 @@ bool SyntaxChecker::visit(PragmaDirective const& _pragma)
}
else if (_pragma.literals()[0] == "solidity")
{
vector<Token> tokens(_pragma.tokens().begin() + 1, _pragma.tokens().end());
vector<string> literals(_pragma.literals().begin() + 1, _pragma.literals().end());
SemVerMatchExpressionParser parser(tokens, literals);
auto matchExpression = parser.parse();
// An unparsable version pragma is an unrecoverable fatal error in the parser.
solAssert(matchExpression.has_value(), "");
static SemVerVersion const currentVersion{string(VersionString)};
if (!matchExpression->matches(currentVersion))
m_errorReporter.syntaxError(
3997_error,
_pragma.location(),
"Source file requires different compiler version (current compiler is " +
string(VersionString) + ") - note that nightly builds are considered to be "
"strictly less than the released version"
);
m_versionPragmaFound = true;
try
{
vector<Token> tokens(_pragma.tokens().begin() + 1, _pragma.tokens().end());
vector<string> literals(_pragma.literals().begin() + 1, _pragma.literals().end());
SemVerMatchExpressionParser parser(tokens, literals);
SemVerMatchExpression matchExpression = parser.parse();
static SemVerVersion const currentVersion{string(VersionString)};
if (!matchExpression.matches(currentVersion))
m_errorReporter.syntaxError(
3997_error,
_pragma.location(),
"Source file requires different compiler version (current compiler is " +
string(VersionString) + ") - note that nightly builds are considered to be "
"strictly less than the released version"
);
m_versionPragmaFound = true;
}
catch (SemVerError const&)
{
// An unparsable version pragma is an unrecoverable fatal error in the parser.
solAssert(false);
}
}
else
m_errorReporter.syntaxError(4936_error, _pragma.location(), "Unknown pragma \"" + _pragma.literals()[0] + "\"");
+21 -17
View File
@@ -160,27 +160,31 @@ ASTPointer<SourceUnit> Parser::parse(CharStream& _charStream)
void Parser::parsePragmaVersion(SourceLocation const& _location, vector<Token> const& _tokens, vector<string> const& _literals)
{
SemVerMatchExpressionParser parser(_tokens, _literals);
auto matchExpression = parser.parse();
if (!matchExpression.has_value())
try
{
SemVerMatchExpression matchExpression = parser.parse();
static SemVerVersion const currentVersion{string(VersionString)};
// FIXME: only match for major version incompatibility
if (!matchExpression.matches(currentVersion))
// If m_parserErrorRecovery is true, the same message will appear from SyntaxChecker::visit(),
// so we don't need to report anything here.
if (!m_parserErrorRecovery)
m_errorReporter.fatalParserError(
5333_error,
_location,
"Source file requires different compiler version (current compiler is " +
string(VersionString) + ") - note that nightly builds are considered to be "
"strictly less than the released version"
);
}
catch (SemVerError const& matchError)
{
m_errorReporter.fatalParserError(
1684_error,
_location,
"Found version pragma, but failed to parse it. "
"Please ensure there is a trailing semicolon."
"Invalid version pragma. "s + matchError.what()
);
static SemVerVersion const currentVersion{string(VersionString)};
// FIXME: only match for major version incompatibility
if (!matchExpression->matches(currentVersion))
// If m_parserErrorRecovery is true, the same message will appear from SyntaxChecker::visit(),
// so we don't need to report anything here.
if (!m_parserErrorRecovery)
m_errorReporter.fatalParserError(
5333_error,
_location,
"Source file requires different compiler version (current compiler is " +
string(VersionString) + ") - note that nightly builds are considered to be "
"strictly less than the released version"
);
}
}
ASTPointer<StructuredDocumentation> Parser::parseStructuredDocumentation()