Scanner hack.

This commit is contained in:
Daniel Kirchner 2023-06-13 20:42:59 +02:00 committed by Nikola Matic
parent 6347faa86f
commit 6ec4d3dd98
6 changed files with 30 additions and 2 deletions

View File

@ -1019,15 +1019,28 @@ std::tuple<Token, unsigned, unsigned> Scanner::scanIdentifierOrKeyword()
while (isIdentifierPart(m_char) || (m_char == '.' && m_kind == ScannerKind::Yul)) while (isIdentifierPart(m_char) || (m_char == '.' && m_kind == ScannerKind::Yul))
addLiteralCharAndAdvance(); addLiteralCharAndAdvance();
literal.complete(); literal.complete();
auto const token = TokenTraits::fromIdentifierOrKeyword(m_tokens[NextNext].literal); auto const token = TokenTraits::fromIdentifierOrKeyword(m_tokens[NextNext].literal);
if (m_kind == ScannerKind::Yul) switch (m_kind)
{ {
case ScannerKind::Solidity:
// Turn experimental Solidity keywords that are not keywords in legacy Solidity into identifiers.
if (TokenTraits::isExperimentalSolidityOnlyKeyword(std::get<0>(token)))
return std::make_tuple(Token::Identifier, 0, 0);
break;
case ScannerKind::Yul:
// Turn Solidity identifier into a Yul keyword // Turn Solidity identifier into a Yul keyword
if (m_tokens[NextNext].literal == "leave") if (m_tokens[NextNext].literal == "leave")
return std::make_tuple(Token::Leave, 0, 0); return std::make_tuple(Token::Leave, 0, 0);
// Turn non-Yul keywords into identifiers. // Turn non-Yul keywords into identifiers.
if (!TokenTraits::isYulKeyword(std::get<0>(token))) if (!TokenTraits::isYulKeyword(std::get<0>(token)))
return std::make_tuple(Token::Identifier, 0, 0); return std::make_tuple(Token::Identifier, 0, 0);
break;
case ScannerKind::ExperimentalSolidity:
// Turn Solidity keywords that are not keywords in experimental solidity into identifiers.
if (!TokenTraits::isExperimentalSolidityKeyword(std::get<0>(token)))
return std::make_tuple(Token::Identifier, 0, 0);
break;
} }
return token; return token;
} }

View File

@ -69,7 +69,8 @@ class ParserRecorder;
enum class ScannerKind enum class ScannerKind
{ {
Solidity, Solidity,
Yul Yul,
ExperimentalSolidity
}; };
enum class ScannerError enum class ScannerError

View File

@ -322,6 +322,14 @@ namespace TokenTraits
tok == Token::Default || tok == Token::For || tok == Token::Break || tok == Token::Continue || tok == Token::Leave || tok == Token::Default || tok == Token::For || tok == Token::Break || tok == Token::Continue || tok == Token::Leave ||
tok == Token::TrueLiteral || tok == Token::FalseLiteral || tok == Token::HexStringLiteral || tok == Token::Hex; tok == Token::TrueLiteral || tok == Token::FalseLiteral || tok == Token::HexStringLiteral || tok == Token::Hex;
} }
constexpr bool isExperimentalSolidityKeyword(Token tok)
{
return tok == Token::Assembly || tok == Token::Contract || tok == Token::External || tok == Token::Fallback;
}
constexpr bool isExperimentalSolidityOnlyKeyword(Token)
{
return false;
}
bool isYulKeyword(std::string const& _literal); bool isYulKeyword(std::string const& _literal);

View File

@ -503,6 +503,7 @@ private:
std::map<std::string, util::h160> m_libraries; std::map<std::string, util::h160> m_libraries;
ImportRemapper m_importRemapper; ImportRemapper m_importRemapper;
std::map<std::string const, Source> m_sources; std::map<std::string const, Source> m_sources;
std::optional<int64_t> m_maxAstId;
std::vector<std::string> m_unhandledSMTLib2Queries; std::vector<std::string> m_unhandledSMTLib2Queries;
std::map<util::h256, std::string> m_smtlib2Responses; std::map<util::h256, std::string> m_smtlib2Responses;
std::shared_ptr<GlobalContext> m_globalContext; std::shared_ptr<GlobalContext> m_globalContext;

View File

@ -100,6 +100,9 @@ ASTPointer<SourceUnit> Parser::parse(CharStream& _charStream)
while (m_scanner->currentToken() == Token::Pragma) while (m_scanner->currentToken() == Token::Pragma)
nodes.push_back(parsePragmaDirective(false)); nodes.push_back(parsePragmaDirective(false));
if (m_experimentalSolidityEnabledInCurrentSourceUnit)
m_scanner->setScannerMode(ScannerKind::ExperimentalSolidity);
while (m_scanner->currentToken() != Token::EOS) while (m_scanner->currentToken() != Token::EOS)
{ {
switch (m_scanner->currentToken()) switch (m_scanner->currentToken())

View File

@ -197,6 +197,8 @@ private:
/// Returns the next AST node ID /// Returns the next AST node ID
int64_t nextID() { return ++m_currentNodeID; } int64_t nextID() { return ++m_currentNodeID; }
/// Returns the maximal AST node ID assigned so far
int64_t maxID() const { return m_currentNodeID; }
std::pair<LookAheadInfo, IndexAccessedPath> tryParseIndexAccessedPath(); std::pair<LookAheadInfo, IndexAccessedPath> tryParseIndexAccessedPath();
/// Performs limited look-ahead to distinguish between variable declaration and expression statement. /// Performs limited look-ahead to distinguish between variable declaration and expression statement.