Experimental standard library

Change import syntax and cover with tests
This commit is contained in:
Nikola Matic
2023-06-06 17:16:23 +02:00
parent f1d2eda795
commit 47969adf91
17 changed files with 157 additions and 22 deletions
+12
View File
@@ -59,6 +59,8 @@
#include <libsolidity/codegen/ir/Common.h>
#include <libsolidity/codegen/ir/IRGenerator.h>
#include <libstdlib/stdlib.h>
#include <libyul/YulString.h>
#include <libyul/AsmPrinter.h>
#include <libyul/AsmJsonConverter.h>
@@ -95,6 +97,7 @@ using namespace std;
using namespace solidity;
using namespace solidity::langutil;
using namespace solidity::frontend;
using namespace solidity::stdlib;
using solidity::util::errinfo_comment;
@@ -369,6 +372,15 @@ bool CompilerStack::parse()
for (auto const& import: ASTNode::filteredNodes<ImportDirective>(source.ast->nodes()))
{
solAssert(!import->path().empty(), "Import path cannot be empty.");
// Check whether the import directive is for the standard library,
// and if yes, add specified file to source units to be parsed.
auto it = stdlib::sources.find(import->path());
if (it != stdlib::sources.end())
{
auto [name, content] = *it;
m_sources[name].charStream = make_unique<CharStream>(content, name);
sourcesToParse.push_back(name);
}
// The current value of `path` is the absolute path as seen from this source file.
// We first have to apply remappings before we can store the actual absolute path
+25 -4
View File
@@ -271,9 +271,9 @@ ASTPointer<ImportDirective> Parser::parseImportDirective()
SourceLocation unitAliasLocation{};
ImportDirective::SymbolAliasList symbolAliases;
if (m_scanner->currentToken() == Token::StringLiteral)
if (isQuotedPath() || isStdlibPath())
{
path = getLiteralAndAdvance();
path = isQuotedPath() ? getLiteralAndAdvance() : getStdlibImportPathAndAdvance();
if (m_scanner->currentToken() == Token::As)
{
advance();
@@ -315,9 +315,9 @@ ASTPointer<ImportDirective> Parser::parseImportDirective()
if (m_scanner->currentToken() != Token::Identifier || m_scanner->currentLiteral() != "from")
fatalParserError(8208_error, "Expected \"from\".");
advance();
if (m_scanner->currentToken() != Token::StringLiteral)
if (!isQuotedPath() && !isStdlibPath())
fatalParserError(6845_error, "Expected import path.");
path = getLiteralAndAdvance();
path = isQuotedPath() ? getLiteralAndAdvance() : getStdlibImportPathAndAdvance();
}
if (path->empty())
fatalParserError(6326_error, "Import path cannot be empty.");
@@ -2486,4 +2486,25 @@ ASTPointer<ASTString> Parser::getLiteralAndAdvance()
return identifier;
}
bool Parser::isQuotedPath() const
{
return m_scanner->currentToken() == Token::StringLiteral;
}
bool Parser::isStdlibPath() const
{
return m_experimentalSolidityEnabledInCurrentSourceUnit
&& m_scanner->currentToken() == Token::Identifier
&& m_scanner->currentLiteral() == "std";
}
ASTPointer<ASTString> Parser::getStdlibImportPathAndAdvance()
{
ASTPointer<ASTString> std = expectIdentifierToken();
if (m_scanner->currentToken() == Token::Period)
advance();
ASTPointer<ASTString> library = expectIdentifierToken();
return make_shared<ASTString>(*std + "." + *library);
}
}
+5
View File
@@ -219,6 +219,11 @@ private:
ASTPointer<ASTString> getLiteralAndAdvance();
///@}
bool isQuotedPath() const;
bool isStdlibPath() const;
ASTPointer<ASTString> getStdlibImportPathAndAdvance();
/// Creates an empty ParameterList at the current location (used if parameters can be omitted).
ASTPointer<ParameterList> createEmptyParameterList();