diff options
author | Alex Beregszaszi <alex@rtfs.hu> | 2018-12-12 23:45:17 +0800 |
---|---|---|
committer | Alex Beregszaszi <alex@rtfs.hu> | 2018-12-18 23:02:35 +0800 |
commit | c2ec6d71b4707dab9b7727347077904b7a90eaf2 (patch) | |
tree | e53763da22d68f1b97a5390f07c4d09c633baefa | |
parent | c60ec958abea4f63eea8783d67a5054ca04d0a64 (diff) | |
download | dexon-solidity-c2ec6d71b4707dab9b7727347077904b7a90eaf2.tar.gz dexon-solidity-c2ec6d71b4707dab9b7727347077904b7a90eaf2.tar.zst dexon-solidity-c2ec6d71b4707dab9b7727347077904b7a90eaf2.zip |
Add version pragma parsing to Parser
-rw-r--r-- | libsolidity/analysis/SyntaxChecker.cpp | 2 | ||||
-rw-r--r-- | libsolidity/parsing/Parser.cpp | 25 | ||||
-rw-r--r-- | libsolidity/parsing/Parser.h | 1 | ||||
-rw-r--r-- | test/libsolidity/SolidityParser.cpp | 9 |
4 files changed, 36 insertions, 1 deletions
diff --git a/libsolidity/analysis/SyntaxChecker.cpp b/libsolidity/analysis/SyntaxChecker.cpp index 12e14864..066b5004 100644 --- a/libsolidity/analysis/SyntaxChecker.cpp +++ b/libsolidity/analysis/SyntaxChecker.cpp @@ -114,7 +114,7 @@ bool SyntaxChecker::visit(PragmaDirective const& _pragma) vector<string> literals(_pragma.literals().begin() + 1, _pragma.literals().end()); SemVerMatchExpressionParser parser(tokens, literals); auto matchExpression = parser.parse(); - SemVerVersion currentVersion{string(VersionString)}; + static SemVerVersion const currentVersion{string(VersionString)}; if (!matchExpression.matches(currentVersion)) m_errorReporter.syntaxError( _pragma.location(), diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 9de2146e..bcb28988 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -22,6 +22,8 @@ #include <libsolidity/parsing/Parser.h> +#include <libsolidity/analysis/SemVerHandler.h> +#include <libsolidity/interface/Version.h> #include <libyul/AsmParser.h> #include <libyul/backends/evm/EVMDialect.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() { RecursionGuard recursionGuard(*this); @@ -134,6 +150,15 @@ ASTPointer<PragmaDirective> Parser::parsePragmaDirective() while (m_scanner->currentToken() != Token::Semicolon && m_scanner->currentToken() != Token::EOS); nodeFactory.markEndPosition(); 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); } diff --git a/libsolidity/parsing/Parser.h b/libsolidity/parsing/Parser.h index 15852096..bf02c626 100644 --- a/libsolidity/parsing/Parser.h +++ b/libsolidity/parsing/Parser.h @@ -70,6 +70,7 @@ private: ///@{ ///@name Parsing functions for the AST nodes + void parsePragmaVersion(std::vector<Token> const& tokens, std::vector<std::string> const& literals); ASTPointer<PragmaDirective> parsePragmaDirective(); ASTPointer<ImportDirective> parseImportDirective(); ContractDefinition::ContractKind parseContractKind(); diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp index d86d3d39..619d6a48 100644 --- a/test/libsolidity/SolidityParser.cpp +++ b/test/libsolidity/SolidityParser.cpp @@ -113,6 +113,15 @@ while(0) 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) { char const* text = R"( |