Parse complex import directives.

This commit is contained in:
chriseth
2015-12-18 12:46:54 +01:00
parent fe23cc8226
commit d3c459b5a9
7 changed files with 114 additions and 28 deletions
+21 -5
View File
@@ -129,23 +129,39 @@ private:
/**
* Import directive for referencing other files / source objects.
* Example: import "abc.sol"
* Example: import "abc.sol" // imports all symbols of "abc.sol" into current scope
* Source objects are identified by a string which can be a file name but does not have to be.
* Other ways to use it:
* import "abc" as x; // creates symbol "x" that contains all symbols in "abc"
* import * as x from "abc"; // same as above
* import {a as b, c} from "abc"; // creates new symbols "b" and "c" referencing "a" and "c" in "abc", respectively.
*/
class ImportDirective: public ASTNode
{
public:
ImportDirective(SourceLocation const& _location, ASTPointer<ASTString> const& _identifier):
ASTNode(_location), m_identifier(_identifier) {}
ImportDirective(
SourceLocation const& _location,
ASTPointer<ASTString> const& _path,
ASTPointer<ASTString> const& _unitAlias,
std::vector<std::pair<ASTPointer<Identifier>, ASTPointer<ASTString>>>&& _symbolAliases
):
ASTNode(_location), m_path(_path), m_unitAlias(_unitAlias), m_symbolAliases(_symbolAliases) {}
virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override;
ASTString const& identifier() const { return *m_identifier; }
ASTString const& path() const { return *m_path; }
virtual ImportAnnotation& annotation() const override;
private:
ASTPointer<ASTString> m_identifier;
ASTPointer<ASTString> m_path;
/// The alias for the module itself. If present, import the whole unit under that name and
/// ignore m_symbolAlias.
ASTPointer<ASTString> m_unitAlias;
/// The aliases for the specific symbols to import. If non-empty import the specific symbols.
/// If the second component is empty, import the identifier unchanged.
/// If both m_unitAlias and m_symbolAlias are empty, import all symbols into the current scope.
std::vector<std::pair<ASTPointer<Identifier>, ASTPointer<ASTString>>> m_symbolAliases;
};
/**
+1 -1
View File
@@ -91,7 +91,7 @@ Json::Value const& ASTJsonConverter::json()
bool ASTJsonConverter::visit(ImportDirective const& _node)
{
addJsonNode("Import", { make_pair("file", _node.identifier())});
addJsonNode("Import", { make_pair("file", _node.path())});
return true;
}
+1 -1
View File
@@ -49,7 +49,7 @@ void ASTPrinter::print(ostream& _stream)
bool ASTPrinter::visit(ImportDirective const& _node)
{
writeLine("ImportDirective \"" + _node.identifier() + "\"");
writeLine("ImportDirective \"" + _node.path() + "\"");
printSourcePart(_node);
return goDeeper();
}