Remove parser error recovery mode

This commit is contained in:
Kamil Śliwak
2023-08-22 12:00:11 +02:00
parent c96db51013
commit 9adbced98e
44 changed files with 142 additions and 585 deletions
+1 -8
View File
@@ -159,14 +159,7 @@ bool SyntaxChecker::visit(PragmaDirective const& _pragma)
SemVerMatchExpressionParser parser(tokens, literals);
SemVerMatchExpression matchExpression = parser.parse();
static SemVerVersion const currentVersion{std::string(VersionString)};
if (!matchExpression.matches(currentVersion))
m_errorReporter.syntaxError(
3997_error,
_pragma.location(),
"Source file requires different compiler version (current compiler is " +
std::string(VersionString) + ") - note that nightly builds are considered to be "
"strictly less than the released version"
);
solAssert(matchExpression.matches(currentVersion));
m_versionPragmaFound = true;
}
catch (SemVerError const&)
+2 -2
View File
@@ -352,7 +352,7 @@ bool CompilerStack::parse()
if (SemVerVersion{std::string(VersionString)}.isPrerelease())
m_errorReporter.warning(3805_error, "This is a pre-release compiler version, please do not use it in production.");
Parser parser{m_errorReporter, m_evmVersion, m_parserErrorRecovery};
Parser parser{m_errorReporter, m_evmVersion};
std::vector<std::string> sourcesToParse;
for (auto const& s: m_sources)
@@ -1104,7 +1104,7 @@ SourceUnit const& CompilerStack::ast(std::string const& _sourceName) const
{
if (m_stackState < Parsed)
solThrow(CompilerError, "Parsing not yet performed.");
if (!source(_sourceName).ast && !m_parserErrorRecovery)
if (!source(_sourceName).ast)
solThrow(CompilerError, "Parsing was not successful.");
return *source(_sourceName).ast;
-9
View File
@@ -158,14 +158,6 @@ public:
/// Sets whether to strip revert strings, add additional strings or do nothing at all.
void setRevertStringBehaviour(RevertStrings _revertStrings);
/// Set whether or not parser error is desired.
/// When called without an argument it will revert to the default.
/// Must be set before parsing.
void setParserErrorRecovery(bool _wantErrorRecovery = false)
{
m_parserErrorRecovery = _wantErrorRecovery;
}
/// Sets the pipeline to go through the Yul IR or not.
/// Must be set before parsing.
void setViaIR(bool _viaIR);
@@ -511,7 +503,6 @@ private:
bool m_metadataLiteralSources = false;
MetadataHash m_metadataHash = MetadataHash::IPFS;
langutil::DebugInfoSelection m_debugInfoSelection = langutil::DebugInfoSelection::Default();
bool m_parserErrorRecovery = false;
State m_stackState = Empty;
CompilationSourceType m_compilationSourceType = CompilationSourceType::Solidity;
MetadataFormat m_metadataFormat = defaultMetadataFormat();
+1 -9
View File
@@ -423,7 +423,7 @@ std::optional<Json::Value> checkAuxiliaryInputKeys(Json::Value const& _input)
std::optional<Json::Value> checkSettingsKeys(Json::Value const& _input)
{
static std::set<std::string> keys{"parserErrorRecovery", "debug", "evmVersion", "libraries", "metadata", "modelChecker", "optimizer", "outputSelection", "remappings", "stopAfter", "viaIR"};
static std::set<std::string> keys{"debug", "evmVersion", "libraries", "metadata", "modelChecker", "optimizer", "outputSelection", "remappings", "stopAfter", "viaIR"};
return checkKeys(_input, keys, "settings");
}
@@ -773,13 +773,6 @@ std::variant<StandardCompiler::InputsAndSettings, Json::Value> StandardCompiler:
ret.stopAfter = CompilerStack::State::Parsed;
}
if (settings.isMember("parserErrorRecovery"))
{
if (!settings["parserErrorRecovery"].isBool())
return formatFatalError(Error::Type::JSONError, "\"settings.parserErrorRecovery\" must be a Boolean.");
ret.parserErrorRecovery = settings["parserErrorRecovery"].asBool();
}
if (settings.isMember("viaIR"))
{
if (!settings["viaIR"].isBool())
@@ -1166,7 +1159,6 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
compilerStack.addSMTLib2Response(smtLib2Response.first, smtLib2Response.second);
compilerStack.setViaIR(_inputsAndSettings.viaIR);
compilerStack.setEVMVersion(_inputsAndSettings.evmVersion);
compilerStack.setParserErrorRecovery(_inputsAndSettings.parserErrorRecovery);
compilerStack.setRemappings(std::move(_inputsAndSettings.remappings));
compilerStack.setOptimiserSettings(std::move(_inputsAndSettings.optimiserSettings));
compilerStack.setRevertStringBehaviour(_inputsAndSettings.revertStrings);
-1
View File
@@ -72,7 +72,6 @@ private:
{
std::string language;
Json::Value errors;
bool parserErrorRecovery = false;
CompilerStack::State stopAfter = CompilerStack::State::CompilationSuccessful;
std::map<std::string, std::string> sources;
std::map<util::h256, std::string> smtLib2Responses;
+118 -169
View File
@@ -173,16 +173,13 @@ void Parser::parsePragmaVersion(SourceLocation const& _location, std::vector<Tok
static SemVerVersion const currentVersion{std::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 " +
std::string(VersionString) + ") - note that nightly builds are considered to be "
"strictly less than the released version"
);
m_errorReporter.fatalParserError(
5333_error,
_location,
"Source file requires different compiler version (current compiler is " +
std::string(VersionString) + ") - note that nightly builds are considered to be "
"strictly less than the released version"
);
}
catch (SemVerError const& matchError)
{
@@ -364,78 +361,62 @@ ASTPointer<ContractDefinition> Parser::parseContractDefinition()
std::vector<ASTPointer<InheritanceSpecifier>> baseContracts;
std::vector<ASTPointer<ASTNode>> subNodes;
std::pair<ContractKind, bool> contractKind{};
try
{
documentation = parseStructuredDocumentation();
contractKind = parseContractKind();
tie(name, nameLocation) = expectIdentifierWithLocation();
if (m_scanner->currentToken() == Token::Is)
do
{
advance();
baseContracts.push_back(parseInheritanceSpecifier());
}
while (m_scanner->currentToken() == Token::Comma);
expectToken(Token::LBrace);
while (true)
documentation = parseStructuredDocumentation();
contractKind = parseContractKind();
std::tie(name, nameLocation) = expectIdentifierWithLocation();
if (m_scanner->currentToken() == Token::Is)
do
{
Token currentTokenValue = m_scanner->currentToken();
if (currentTokenValue == Token::RBrace)
break;
else if (
(currentTokenValue == Token::Function && m_scanner->peekNextToken() != Token::LParen) ||
currentTokenValue == Token::Constructor ||
currentTokenValue == Token::Receive ||
currentTokenValue == Token::Fallback
)
subNodes.push_back(parseFunctionDefinition());
else if (currentTokenValue == Token::Struct)
subNodes.push_back(parseStructDefinition());
else if (currentTokenValue == Token::Enum)
subNodes.push_back(parseEnumDefinition());
else if (currentTokenValue == Token::Type)
subNodes.push_back(parseUserDefinedValueTypeDefinition());
else if (
// Workaround because `error` is not a keyword.
currentTokenValue == Token::Identifier &&
currentLiteral() == "error" &&
m_scanner->peekNextToken() == Token::Identifier &&
m_scanner->peekNextNextToken() == Token::LParen
)
subNodes.push_back(parseErrorDefinition());
else if (variableDeclarationStart())
{
VarDeclParserOptions options;
options.kind = VarDeclKind::State;
options.allowInitialValue = true;
subNodes.push_back(parseVariableDeclaration(options));
expectToken(Token::Semicolon);
}
else if (currentTokenValue == Token::Modifier)
subNodes.push_back(parseModifierDefinition());
else if (currentTokenValue == Token::Event)
subNodes.push_back(parseEventDefinition());
else if (currentTokenValue == Token::Using)
subNodes.push_back(parseUsingDirective());
else
fatalParserError(9182_error, "Function, variable, struct or modifier declaration expected.");
advance();
baseContracts.push_back(parseInheritanceSpecifier());
}
}
catch (FatalError const&)
while (m_scanner->currentToken() == Token::Comma);
expectToken(Token::LBrace);
while (true)
{
if (
!m_errorReporter.hasErrors() ||
!m_parserErrorRecovery ||
m_errorReporter.hasExcessiveErrors()
Token currentTokenValue = m_scanner->currentToken();
if (currentTokenValue == Token::RBrace)
break;
else if (
(currentTokenValue == Token::Function && m_scanner->peekNextToken() != Token::LParen) ||
currentTokenValue == Token::Constructor ||
currentTokenValue == Token::Receive ||
currentTokenValue == Token::Fallback
)
BOOST_THROW_EXCEPTION(FatalError()); /* Don't try to recover here. */
m_inParserRecovery = true;
subNodes.push_back(parseFunctionDefinition());
else if (currentTokenValue == Token::Struct)
subNodes.push_back(parseStructDefinition());
else if (currentTokenValue == Token::Enum)
subNodes.push_back(parseEnumDefinition());
else if (currentTokenValue == Token::Type)
subNodes.push_back(parseUserDefinedValueTypeDefinition());
else if (
// Workaround because `error` is not a keyword.
currentTokenValue == Token::Identifier &&
currentLiteral() == "error" &&
m_scanner->peekNextToken() == Token::Identifier &&
m_scanner->peekNextNextToken() == Token::LParen
)
subNodes.push_back(parseErrorDefinition());
else if (variableDeclarationStart())
{
VarDeclParserOptions options;
options.kind = VarDeclKind::State;
options.allowInitialValue = true;
subNodes.push_back(parseVariableDeclaration(options));
expectToken(Token::Semicolon);
}
else if (currentTokenValue == Token::Modifier)
subNodes.push_back(parseModifierDefinition());
else if (currentTokenValue == Token::Event)
subNodes.push_back(parseEventDefinition());
else if (currentTokenValue == Token::Using)
subNodes.push_back(parseUsingDirective());
else
fatalParserError(9182_error, "Function, variable, struct or modifier declaration expected.");
}
nodeFactory.markEndPosition();
if (m_inParserRecovery)
expectTokenOrConsumeUntil(Token::RBrace, "ContractDefinition");
else
expectToken(Token::RBrace);
expectToken(Token::RBrace);
return nodeFactory.createNode<ContractDefinition>(
name,
nameLocation,
@@ -1295,26 +1276,10 @@ ASTPointer<Block> Parser::parseBlock(bool _allowUnchecked, ASTPointer<ASTString>
}
expectToken(Token::LBrace);
std::vector<ASTPointer<Statement>> statements;
try
{
while (m_scanner->currentToken() != Token::RBrace)
statements.push_back(parseStatement(true));
nodeFactory.markEndPosition();
}
catch (FatalError const&)
{
if (
!m_errorReporter.hasErrors() ||
!m_parserErrorRecovery ||
m_errorReporter.hasExcessiveErrors()
)
BOOST_THROW_EXCEPTION(FatalError()); /* Don't try to recover here. */
m_inParserRecovery = true;
}
if (m_inParserRecovery)
expectTokenOrConsumeUntil(Token::RBrace, "Block");
else
expectToken(Token::RBrace);
while (m_scanner->currentToken() != Token::RBrace)
statements.push_back(parseStatement(true));
nodeFactory.markEndPosition();
expectToken(Token::RBrace);
return nodeFactory.createNode<Block>(_docString, unchecked, statements);
}
@@ -1323,86 +1288,70 @@ ASTPointer<Statement> Parser::parseStatement(bool _allowUnchecked)
RecursionGuard recursionGuard(*this);
ASTPointer<ASTString> docString;
ASTPointer<Statement> statement;
try
if (m_scanner->currentCommentLiteral() != "")
docString = std::make_shared<ASTString>(m_scanner->currentCommentLiteral());
switch (m_scanner->currentToken())
{
if (m_scanner->currentCommentLiteral() != "")
docString = std::make_shared<ASTString>(m_scanner->currentCommentLiteral());
switch (m_scanner->currentToken())
{
case Token::If:
return parseIfStatement(docString);
case Token::While:
return parseWhileStatement(docString);
case Token::Do:
return parseDoWhileStatement(docString);
case Token::For:
return parseForStatement(docString);
case Token::Unchecked:
case Token::LBrace:
return parseBlock(_allowUnchecked, docString);
case Token::Continue:
statement = ASTNodeFactory(*this).createNode<Continue>(docString);
advance();
break;
case Token::Break:
statement = ASTNodeFactory(*this).createNode<Break>(docString);
advance();
break;
case Token::Return:
{
ASTNodeFactory nodeFactory(*this);
ASTPointer<Expression> expression;
if (advance() != Token::Semicolon)
{
expression = parseExpression();
nodeFactory.setEndPositionFromNode(expression);
}
statement = nodeFactory.createNode<Return>(docString, expression);
break;
}
case Token::Throw:
{
statement = ASTNodeFactory(*this).createNode<Throw>(docString);
advance();
break;
}
case Token::Try:
return parseTryStatement(docString);
case Token::Assembly:
return parseInlineAssembly(docString);
case Token::Emit:
statement = parseEmitStatement(docString);
break;
case Token::Identifier:
if (m_scanner->currentLiteral() == "revert" && m_scanner->peekNextToken() == Token::Identifier)
statement = parseRevertStatement(docString);
else if (m_insideModifier && m_scanner->currentLiteral() == "_")
case Token::If:
return parseIfStatement(docString);
case Token::While:
return parseWhileStatement(docString);
case Token::Do:
return parseDoWhileStatement(docString);
case Token::For:
return parseForStatement(docString);
case Token::Unchecked:
case Token::LBrace:
return parseBlock(_allowUnchecked, docString);
case Token::Continue:
statement = ASTNodeFactory(*this).createNode<Continue>(docString);
advance();
break;
case Token::Break:
statement = ASTNodeFactory(*this).createNode<Break>(docString);
advance();
break;
case Token::Return:
{
ASTNodeFactory nodeFactory(*this);
ASTPointer<Expression> expression;
if (advance() != Token::Semicolon)
{
statement = ASTNodeFactory(*this).createNode<PlaceholderStatement>(docString);
advance();
expression = parseExpression();
nodeFactory.setEndPositionFromNode(expression);
}
else
statement = parseSimpleStatement(docString);
statement = nodeFactory.createNode<Return>(docString, expression);
break;
default:
statement = parseSimpleStatement(docString);
break;
}
}
catch (FatalError const&)
case Token::Throw:
{
if (
!m_errorReporter.hasErrors() ||
!m_parserErrorRecovery ||
m_errorReporter.hasExcessiveErrors()
)
BOOST_THROW_EXCEPTION(FatalError()); /* Don't try to recover here. */
m_inParserRecovery = true;
statement = ASTNodeFactory(*this).createNode<Throw>(docString);
advance();
break;
}
if (m_inParserRecovery)
expectTokenOrConsumeUntil(Token::Semicolon, "Statement");
else
expectToken(Token::Semicolon);
case Token::Try:
return parseTryStatement(docString);
case Token::Assembly:
return parseInlineAssembly(docString);
case Token::Emit:
statement = parseEmitStatement(docString);
break;
case Token::Identifier:
if (m_scanner->currentLiteral() == "revert" && m_scanner->peekNextToken() == Token::Identifier)
statement = parseRevertStatement(docString);
else if (m_insideModifier && m_scanner->currentLiteral() == "_")
{
statement = ASTNodeFactory(*this).createNode<PlaceholderStatement>(docString);
advance();
}
else
statement = parseSimpleStatement(docString);
break;
default:
statement = parseSimpleStatement(docString);
break;
}
expectToken(Token::Semicolon);
return statement;
}
+2 -3
View File
@@ -40,10 +40,9 @@ class Parser: public langutil::ParserBase
public:
explicit Parser(
langutil::ErrorReporter& _errorReporter,
langutil::EVMVersion _evmVersion,
bool _errorRecovery = false
langutil::EVMVersion _evmVersion
):
ParserBase(_errorReporter, _errorRecovery),
ParserBase(_errorReporter),
m_evmVersion(_evmVersion)
{}