Add version pragma parsing to Parser

This commit is contained in:
Alex Beregszaszi 2018-12-12 15:45:17 +00:00
parent c60ec958ab
commit c2ec6d71b4
4 changed files with 36 additions and 1 deletions

View File

@ -114,7 +114,7 @@ bool SyntaxChecker::visit(PragmaDirective const& _pragma)
vector<string> literals(_pragma.literals().begin() + 1, _pragma.literals().end()); vector<string> literals(_pragma.literals().begin() + 1, _pragma.literals().end());
SemVerMatchExpressionParser parser(tokens, literals); SemVerMatchExpressionParser parser(tokens, literals);
auto matchExpression = parser.parse(); auto matchExpression = parser.parse();
SemVerVersion currentVersion{string(VersionString)}; static SemVerVersion const currentVersion{string(VersionString)};
if (!matchExpression.matches(currentVersion)) if (!matchExpression.matches(currentVersion))
m_errorReporter.syntaxError( m_errorReporter.syntaxError(
_pragma.location(), _pragma.location(),

View File

@ -22,6 +22,8 @@
#include <libsolidity/parsing/Parser.h> #include <libsolidity/parsing/Parser.h>
#include <libsolidity/analysis/SemVerHandler.h>
#include <libsolidity/interface/Version.h>
#include <libyul/AsmParser.h> #include <libyul/AsmParser.h>
#include <libyul/backends/evm/EVMDialect.h> #include <libyul/backends/evm/EVMDialect.h>
#include <liblangutil/ErrorReporter.h> #include <liblangutil/ErrorReporter.h>
@ -106,6 +108,20 @@ ASTPointer<SourceUnit> Parser::parse(shared_ptr<Scanner> const& _scanner)
} }
} }
void Parser::parsePragmaVersion(vector<Token> const& tokens, vector<string> const& literals)
{
SemVerMatchExpressionParser parser(tokens, literals);
auto matchExpression = parser.parse();
static SemVerVersion const currentVersion{string(VersionString)};
// FIXME: only match for major version incompatibility
if (!matchExpression.matches(currentVersion))
fatalParserError(
"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<PragmaDirective> Parser::parsePragmaDirective() ASTPointer<PragmaDirective> Parser::parsePragmaDirective()
{ {
RecursionGuard recursionGuard(*this); RecursionGuard recursionGuard(*this);
@ -134,6 +150,15 @@ ASTPointer<PragmaDirective> Parser::parsePragmaDirective()
while (m_scanner->currentToken() != Token::Semicolon && m_scanner->currentToken() != Token::EOS); while (m_scanner->currentToken() != Token::Semicolon && m_scanner->currentToken() != Token::EOS);
nodeFactory.markEndPosition(); nodeFactory.markEndPosition();
expectToken(Token::Semicolon); expectToken(Token::Semicolon);
if (literals.size() >= 2 && literals[0] == "solidity")
{
parsePragmaVersion(
vector<Token>(tokens.begin() + 1, tokens.end()),
vector<string>(literals.begin() + 1, literals.end())
);
}
return nodeFactory.createNode<PragmaDirective>(tokens, literals); return nodeFactory.createNode<PragmaDirective>(tokens, literals);
} }

View File

@ -70,6 +70,7 @@ private:
///@{ ///@{
///@name Parsing functions for the AST nodes ///@name Parsing functions for the AST nodes
void parsePragmaVersion(std::vector<Token> const& tokens, std::vector<std::string> const& literals);
ASTPointer<PragmaDirective> parsePragmaDirective(); ASTPointer<PragmaDirective> parsePragmaDirective();
ASTPointer<ImportDirective> parseImportDirective(); ASTPointer<ImportDirective> parseImportDirective();
ContractDefinition::ContractKind parseContractKind(); ContractDefinition::ContractKind parseContractKind();

View File

@ -113,6 +113,15 @@ while(0)
BOOST_AUTO_TEST_SUITE(SolidityParser) BOOST_AUTO_TEST_SUITE(SolidityParser)
BOOST_AUTO_TEST_CASE(unsatisfied_version_followed_by_invalid_syntax)
{
char const* text = R"(
pragma solidity ^99.99.0;
this is surely invalid
)";
CHECK_PARSE_ERROR(text, "Source file requires different compiler version");
}
BOOST_AUTO_TEST_CASE(function_natspec_documentation) BOOST_AUTO_TEST_CASE(function_natspec_documentation)
{ {
char const* text = R"( char const* text = R"(