diff options
author | Christian <c@ethdev.com> | 2014-12-04 01:52:28 +0800 |
---|---|---|
committer | Christian <c@ethdev.com> | 2014-12-04 01:52:28 +0800 |
commit | d2cf34548322598ae067434a61a171bd190fc2c9 (patch) | |
tree | 9f89db707b8435f4774aea1115249e076fb37b74 | |
parent | 254df50feab6bb4c9f013257591b73919e4013a5 (diff) | |
download | dexon-solidity-d2cf34548322598ae067434a61a171bd190fc2c9.tar dexon-solidity-d2cf34548322598ae067434a61a171bd190fc2c9.tar.gz dexon-solidity-d2cf34548322598ae067434a61a171bd190fc2c9.tar.bz2 dexon-solidity-d2cf34548322598ae067434a61a171bd190fc2c9.tar.lz dexon-solidity-d2cf34548322598ae067434a61a171bd190fc2c9.tar.xz dexon-solidity-d2cf34548322598ae067434a61a171bd190fc2c9.tar.zst dexon-solidity-d2cf34548322598ae067434a61a171bd190fc2c9.zip |
Improved external interface for multi-source and multi-contract compilation.
-rw-r--r-- | BaseTypes.h | 7 | ||||
-rw-r--r-- | CompilerStack.cpp | 12 | ||||
-rw-r--r-- | CompilerStack.h | 3 | ||||
-rw-r--r-- | Exceptions.h | 1 | ||||
-rw-r--r-- | Parser.cpp | 11 | ||||
-rw-r--r-- | Parser.h | 1 | ||||
-rw-r--r-- | Scanner.cpp | 10 | ||||
-rw-r--r-- | Scanner.h | 3 | ||||
-rw-r--r-- | SourceReferenceFormatter.cpp | 41 | ||||
-rw-r--r-- | SourceReferenceFormatter.h | 4 |
10 files changed, 50 insertions, 43 deletions
diff --git a/BaseTypes.h b/BaseTypes.h index 627aa7df..a8fd77c8 100644 --- a/BaseTypes.h +++ b/BaseTypes.h @@ -37,14 +37,13 @@ namespace solidity */ struct Location { - Location(int _start, int _end): start(_start), end(_end) { } + Location(int _start, int _end, std::shared_ptr<std::string const> _sourceName): + start(_start), end(_end), sourceName(_sourceName) { } Location(): start(-1), end(-1) { } - bool IsValid() const { return !!sourceName && start >= 0 && end >= start; } - - std::shared_ptr<std::string> sourceName; int start; int end; + std::shared_ptr<std::string const> sourceName; }; /// Stream output for Location (used e.g. in boost exceptions). diff --git a/CompilerStack.cpp b/CompilerStack.cpp index 49ac3b64..198ded09 100644 --- a/CompilerStack.cpp +++ b/CompilerStack.cpp @@ -81,10 +81,20 @@ void CompilerStack::parse(string const& _sourceCode) parse(); } -void CompilerStack::compile(bool _optimize) +vector<string> CompilerStack::getContractNames() { if (!m_parseSuccessful) BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful.")); + vector<string> contractNames; + for (auto const& contract: m_contracts) + contractNames.push_back(contract.first); + return contractNames; +} + +void CompilerStack::compile(bool _optimize) +{ + if (!m_parseSuccessful) + parse(); for (Source const* source: m_sourceOrder) for (ASTPointer<ASTNode> const& node: source->ast->getNodes()) if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get())) diff --git a/CompilerStack.h b/CompilerStack.h index 0520f2b1..a5b8ec41 100644 --- a/CompilerStack.h +++ b/CompilerStack.h @@ -54,6 +54,9 @@ public: void parse(); /// Sets the given source code as the only source unit and parses it. void parse(std::string const& _sourceCode); + /// Returns a list of the contract names in the sources. + std::vector<std::string> getContractNames(); + /// Compiles the source units that were prevously added and parsed. void compile(bool _optimize = false); /// Parses and compiles the given source code. diff --git a/Exceptions.h b/Exceptions.h index 1903c1dc..ffd8a72d 100644 --- a/Exceptions.h +++ b/Exceptions.h @@ -37,7 +37,6 @@ struct DeclarationError: virtual Exception {}; struct CompilerError: virtual Exception {}; struct InternalCompilerError: virtual Exception {}; -typedef boost::error_info<struct tag_sourcePosition, int> errinfo_sourcePosition; typedef boost::error_info<struct tag_sourceLocation, Location> errinfo_sourceLocation; } @@ -39,7 +39,8 @@ namespace solidity class Parser::ASTNodeFactory { public: - ASTNodeFactory(Parser const& _parser): m_parser(_parser), m_location(_parser.getPosition(), -1) {} + ASTNodeFactory(Parser const& _parser): + m_parser(_parser), m_location(_parser.getPosition(), -1, _parser.getSourceName()) {} void markEndPosition() { m_location.end = m_parser.getEndPosition(); } void setLocationEmpty() { m_location.end = m_location.start; } @@ -81,6 +82,11 @@ ASTPointer<SourceUnit> Parser::parse(shared_ptr<Scanner> const& _scanner) return nodeFactory.createNode<SourceUnit>(nodes); } +std::shared_ptr<const string> const& Parser::getSourceName() const +{ + return m_scanner->getSourceName(); +} + int Parser::getPosition() const { return m_scanner->getCurrentLocation().start; @@ -579,7 +585,8 @@ ASTPointer<ASTString> Parser::getLiteralAndAdvance() ParserError Parser::createParserError(string const& _description) const { - return ParserError() << errinfo_sourcePosition(getPosition()) << errinfo_comment(_description); + return ParserError() << errinfo_sourceLocation(Location(getPosition(), getPosition(), getSourceName())) + << errinfo_comment(_description); } @@ -35,6 +35,7 @@ class Parser { public: ASTPointer<SourceUnit> parse(std::shared_ptr<Scanner> const& _scanner); + std::shared_ptr<std::string const> const& getSourceName() const; private: class ASTNodeFactory; diff --git a/Scanner.cpp b/Scanner.cpp index e155f550..08bf744d 100644 --- a/Scanner.cpp +++ b/Scanner.cpp @@ -143,16 +143,10 @@ private: }; // end of LiteralScope class -void Scanner::reset(CharStream const& _source, std::string const& _sourceName) +void Scanner::reset(CharStream const& _source, string const& _sourceName) { m_source = _source; - - shared_ptr<string> sourceName = make_shared<string>(_sourceName); - m_currentToken.location.sourceName = sourceName; - m_nextToken.location.sourceName = sourceName; - m_skippedComment.location.sourceName = sourceName; - m_nextSkippedComment.location.sourceName = sourceName; - + m_sourceName = make_shared<string const>(_sourceName); reset(); } @@ -142,6 +142,8 @@ public: std::string const& peekLiteral() const { return m_nextToken.literal; } ///@} + std::shared_ptr<std::string const> const& getSourceName() const { return m_sourceName; } + ///@{ ///@name Error printing helper functions /// Functions that help pretty-printing parse errors @@ -206,6 +208,7 @@ private: TokenDesc m_nextToken; // desc for next token (one token look-ahead) CharStream m_source; + std::shared_ptr<std::string const> m_sourceName; /// one character look-ahead, equals 0 at end of input char m_char; diff --git a/SourceReferenceFormatter.cpp b/SourceReferenceFormatter.cpp index d3f2152a..1a7d12a9 100644 --- a/SourceReferenceFormatter.cpp +++ b/SourceReferenceFormatter.cpp @@ -21,6 +21,7 @@ */ #include <libsolidity/SourceReferenceFormatter.h> +#include <libsolidity/CompilerStack.h> #include <libsolidity/Scanner.h> #include <libsolidity/Exceptions.h> @@ -38,7 +39,6 @@ void SourceReferenceFormatter::printSourceLocation(ostream& _stream, int startLine; int startColumn; tie(startLine, startColumn) = _scanner.translatePositionToLineColumn(_location.start); - _stream << "starting at line " << (startLine + 1) << ", column " << (startColumn + 1) << "\n"; int endLine; int endColumn; tie(endLine, endColumn) = _scanner.translatePositionToLineColumn(_location.end); @@ -58,37 +58,28 @@ void SourceReferenceFormatter::printSourceLocation(ostream& _stream, << "Spanning multiple lines.\n"; } -void SourceReferenceFormatter::printSourcePosition(ostream& _stream, - int _position, - const Scanner& _scanner) -{ - int line; - int column; - tie(line, column) = _scanner.translatePositionToLineColumn(_position); - _stream << "at line " << (line + 1) << ", column " << (column + 1) << endl - << _scanner.getLineAtPosition(_position) << endl - << string(column, ' ') << "^" << endl; -} - void SourceReferenceFormatter::printExceptionInformation(ostream& _stream, Exception const& _exception, string const& _name, - Scanner const& _scanner) + CompilerStack& _compiler) { - _stream << _name; - if (string const* description = boost::get_error_info<errinfo_comment>(_exception)) - _stream << ": " << *description; + Location const* location = boost::get_error_info<errinfo_sourceLocation>(_exception); + Scanner const* scanner; - if (int const* position = boost::get_error_info<errinfo_sourcePosition>(_exception)) + if (location) { - _stream << " "; - printSourcePosition(_stream, *position, _scanner); - } - if (Location const* location = boost::get_error_info<errinfo_sourceLocation>(_exception)) - { - _stream << " "; - printSourceLocation(_stream, *location, _scanner); + scanner = &_compiler.getScanner(*location->sourceName); + int startLine; + int startColumn; + tie(startLine, startColumn) = scanner->translatePositionToLineColumn(location->start); + _stream << *location->sourceName << ":" << (startLine + 1) << ":" << (startColumn + 1) << ": "; } + _stream << _name; + if (string const* description = boost::get_error_info<errinfo_comment>(_exception)) + _stream << ": " << *description << endl; + + if (location) + printSourceLocation(_stream, *location, *scanner); } } diff --git a/SourceReferenceFormatter.h b/SourceReferenceFormatter.h index 4736066f..9b556704 100644 --- a/SourceReferenceFormatter.h +++ b/SourceReferenceFormatter.h @@ -34,14 +34,14 @@ namespace solidity { class Scanner; // forward +class CompilerStack; // forward struct SourceReferenceFormatter { public: static void printSourceLocation(std::ostream& _stream, Location const& _location, Scanner const& _scanner); - static void printSourcePosition(std::ostream& _stream, int _position, Scanner const& _scanner); static void printExceptionInformation(std::ostream& _stream, Exception const& _exception, - std::string const& _name, Scanner const& _scanner); + std::string const& _name, CompilerStack& _compiler); }; } |