Browse Source

Addressing styling and miscellaneous issue with Natspec

cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
67da8798cf
  1. 20
      libsolidity/CompilerStack.cpp
  2. 4
      libsolidity/CompilerStack.h
  3. 49
      libsolidity/InterfaceHandler.cpp
  4. 18
      libsolidity/InterfaceHandler.h

20
libsolidity/CompilerStack.cpp

@ -36,7 +36,7 @@ namespace dev
namespace solidity namespace solidity
{ {
CompilerStack::CompilerStack():m_interfaceHandler(make_shared<InterfaceHandler>()){} CompilerStack::CompilerStack(): m_interfaceHandler(make_shared<InterfaceHandler>()) {}
void CompilerStack::setSource(string const& _sourceCode) void CompilerStack::setSource(string const& _sourceCode)
{ {
@ -84,33 +84,31 @@ void CompilerStack::streamAssembly(ostream& _outStream)
m_compiler->streamAssembly(_outStream); m_compiler->streamAssembly(_outStream);
} }
std::string const* CompilerStack::getJsonDocumentation(enum documentationType _type) std::string const* CompilerStack::getJsonDocumentation(enum DocumentationType _type)
{ {
if (!m_parseSuccessful) if (!m_parseSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful.")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
auto createOrReturnDoc = [this, _type](std::unique_ptr<string>& _doc) auto createDocIfNotThere = [this, _type](std::unique_ptr<string>& _doc)
{ {
if(!_doc) if (!_doc)
{
_doc = m_interfaceHandler->getDocumentation(m_contractASTNode, _type); _doc = m_interfaceHandler->getDocumentation(m_contractASTNode, _type);
}
}; };
switch (_type) switch (_type)
{ {
case NATSPEC_USER: case NATSPEC_USER:
createOrReturnDoc(m_userDocumentation); createDocIfNotThere(m_userDocumentation);
return m_userDocumentation.get(); return m_userDocumentation.get();
case NATSPEC_DEV: case NATSPEC_DEV:
createOrReturnDoc(m_devDocumentation); createDocIfNotThere(m_devDocumentation);
return m_devDocumentation.get(); return m_devDocumentation.get();
case ABI_INTERFACE: case ABI_INTERFACE:
createOrReturnDoc(m_interface); createDocIfNotThere(m_interface);
return m_interface.get(); return m_interface.get();
} }
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Internal error"));
return nullptr; BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Illegal documentation type."));
} }
bytes CompilerStack::staticCompile(std::string const& _sourceCode, bool _optimize) bytes CompilerStack::staticCompile(std::string const& _sourceCode, bool _optimize)

4
libsolidity/CompilerStack.h

@ -37,7 +37,7 @@ class Compiler;
class GlobalContext; class GlobalContext;
class InterfaceHandler; class InterfaceHandler;
enum documentationType: unsigned short enum DocumentationType: unsigned short
{ {
NATSPEC_USER = 1, NATSPEC_USER = 1,
NATSPEC_DEV, NATSPEC_DEV,
@ -74,7 +74,7 @@ public:
/// Prerequisite: Successful call to parse or compile. /// Prerequisite: Successful call to parse or compile.
/// @param type The type of the documentation to get. /// @param type The type of the documentation to get.
/// Can be one of 3 types defined at @c documentation_type /// Can be one of 3 types defined at @c documentation_type
std::string const* getJsonDocumentation(enum documentationType type); std::string const* getJsonDocumentation(enum DocumentationType type);
/// Returns the previously used scanner, useful for counting lines during error reporting. /// Returns the previously used scanner, useful for counting lines during error reporting.
Scanner const& getScanner() const { return *m_scanner; } Scanner const& getScanner() const { return *m_scanner; }

49
libsolidity/InterfaceHandler.cpp

@ -3,8 +3,10 @@
#include <libsolidity/AST.h> #include <libsolidity/AST.h>
#include <libsolidity/CompilerStack.h> #include <libsolidity/CompilerStack.h>
namespace dev { namespace dev
namespace solidity { {
namespace solidity
{
/* -- public -- */ /* -- public -- */
@ -14,7 +16,7 @@ InterfaceHandler::InterfaceHandler()
} }
std::unique_ptr<std::string> InterfaceHandler::getDocumentation(std::shared_ptr<ContractDefinition> _contractDef, std::unique_ptr<std::string> InterfaceHandler::getDocumentation(std::shared_ptr<ContractDefinition> _contractDef,
enum documentationType _type) enum DocumentationType _type)
{ {
switch(_type) switch(_type)
{ {
@ -34,8 +36,7 @@ std::unique_ptr<std::string> InterfaceHandler::getABIInterface(std::shared_ptr<C
{ {
Json::Value methods(Json::arrayValue); Json::Value methods(Json::arrayValue);
std::vector<FunctionDefinition const*> exportedFunctions = _contractDef->getInterfaceFunctions(); for (FunctionDefinition const* f: _contractDef->getInterfaceFunctions())
for (FunctionDefinition const* f: exportedFunctions)
{ {
Json::Value method; Json::Value method;
Json::Value inputs(Json::arrayValue); Json::Value inputs(Json::arrayValue);
@ -107,9 +108,8 @@ std::unique_ptr<std::string> InterfaceHandler::getDevDocumentation(std::shared_p
method["details"] = Json::Value(m_dev); method["details"] = Json::Value(m_dev);
Json::Value params(Json::objectValue); Json::Value params(Json::objectValue);
for (auto const& pair: m_params) for (auto const& pair: m_params)
{
params[pair.first] = pair.second; params[pair.first] = pair.second;
}
if (!m_params.empty()) if (!m_params.empty())
method["params"] = params; method["params"] = params;
if (!m_return.empty()) if (!m_return.empty())
@ -145,12 +145,12 @@ size_t skipLineOrEOS(std::string const& _string, size_t _nlPos)
size_t InterfaceHandler::parseDocTagLine(std::string const& _string, size_t InterfaceHandler::parseDocTagLine(std::string const& _string,
std::string& _tagString, std::string& _tagString,
size_t _pos, size_t _pos,
enum docTagType _tagType) enum DocTagType _tagType)
{ {
size_t nlPos = _string.find("\n", _pos); size_t nlPos = _string.find('\n', _pos);
_tagString += _string.substr(_pos, _tagString += _string.substr(_pos,
nlPos == std::string::npos ? nlPos == std::string::npos ?
_string.length() : _string.length() - _pos:
nlPos - _pos); nlPos - _pos);
m_lastTag = _tagType; m_lastTag = _tagType;
return skipLineOrEOS(_string, nlPos); return skipLineOrEOS(_string, nlPos);
@ -159,20 +159,18 @@ size_t InterfaceHandler::parseDocTagLine(std::string const& _string,
size_t InterfaceHandler::parseDocTagParam(std::string const& _string, size_t _startPos) size_t InterfaceHandler::parseDocTagParam(std::string const& _string, size_t _startPos)
{ {
// find param name // find param name
size_t currPos = _string.find(" ", _startPos); size_t currPos = _string.find(' ', _startPos);
if (currPos == std::string::npos) if (currPos == std::string::npos)
{
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("End of param name not found")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("End of param name not found"));
return currPos; //no end of tag found
}
auto paramName = _string.substr(_startPos, currPos - _startPos); auto paramName = _string.substr(_startPos, currPos - _startPos);
currPos += 1; currPos += 1;
size_t nlPos = _string.find("\n", currPos); size_t nlPos = _string.find('\n', currPos);
auto paramDesc = _string.substr(currPos, auto paramDesc = _string.substr(currPos,
nlPos == std::string::npos ? nlPos == std::string::npos ?
_string.length() : _string.length() - currPos :
nlPos - currPos); nlPos - currPos);
m_params.push_back(std::make_pair(paramName, paramDesc)); m_params.push_back(std::make_pair(paramName, paramDesc));
@ -184,13 +182,13 @@ size_t InterfaceHandler::parseDocTagParam(std::string const& _string, size_t _st
size_t InterfaceHandler::appendDocTagParam(std::string const& _string, size_t _startPos) size_t InterfaceHandler::appendDocTagParam(std::string const& _string, size_t _startPos)
{ {
// Should never be called with an empty vector // Should never be called with an empty vector
assert(!m_params.empty()); if (asserts(!m_params.empty()))
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Internal: Tried to append to empty parameter"));
auto pair = m_params.back(); auto pair = m_params.back();
size_t nlPos = _string.find("\n", _startPos); size_t nlPos = _string.find('\n', _startPos);
pair.second += _string.substr(_startPos, pair.second += _string.substr(_startPos,
nlPos == std::string::npos ? nlPos == std::string::npos ?
_string.length() : _string.length() - _startPos :
nlPos - _startPos); nlPos - _startPos);
m_params.at(m_params.size() - 1) = pair; m_params.at(m_params.size() - 1) = pair;
@ -227,7 +225,7 @@ size_t InterfaceHandler::parseDocTag(std::string const& _string, std::string con
size_t InterfaceHandler::appendDocTag(std::string const& _string, size_t _startPos) size_t InterfaceHandler::appendDocTag(std::string const& _string, size_t _startPos)
{ {
size_t newPos = _startPos; size_t newPos = _startPos;
switch(m_lastTag) switch (m_lastTag)
{ {
case DOCTAG_DEV: case DOCTAG_DEV:
m_dev += " "; m_dev += " ";
@ -254,18 +252,15 @@ void InterfaceHandler::parseDocString(std::string const& _string, size_t _startP
{ {
size_t pos2; size_t pos2;
size_t newPos = _startPos; size_t newPos = _startPos;
size_t tagPos = _string.find("@", _startPos); size_t tagPos = _string.find('@', _startPos);
size_t nlPos = _string.find("\n", _startPos); size_t nlPos = _string.find('\n', _startPos);
if (tagPos != std::string::npos && tagPos < nlPos) if (tagPos != std::string::npos && tagPos < nlPos)
{ {
// we found a tag // we found a tag
pos2 = _string.find(" ", tagPos); pos2 = _string.find(' ', tagPos);
if (pos2 == std::string::npos) if (pos2 == std::string::npos)
{
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("End of tag not found")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("End of tag not found"));
return; //no end of tag found
}
newPos = parseDocTag(_string, _string.substr(tagPos + 1, pos2 - tagPos - 1), pos2 + 1); newPos = parseDocTag(_string, _string.substr(tagPos + 1, pos2 - tagPos - 1), pos2 + 1);
} }

18
libsolidity/InterfaceHandler.h

@ -30,14 +30,16 @@
#include <memory> #include <memory>
#include <jsonrpc/json/json.h> #include <jsonrpc/json/json.h>
namespace dev { namespace dev
namespace solidity { {
namespace solidity
{
// Forward declarations // Forward declarations
class ContractDefinition; class ContractDefinition;
enum documentationType: unsigned short; enum DocumentationType: unsigned short;
enum docTagType enum DocTagType
{ {
DOCTAG_NONE = 0, DOCTAG_NONE = 0,
DOCTAG_DEV, DOCTAG_DEV,
@ -54,11 +56,11 @@ public:
/// Get the given type of documentation /// Get the given type of documentation
/// @param _contractDef The contract definition /// @param _contractDef The contract definition
/// @param _type The type of the documentation. Can be one of the /// @param _type The type of the documentation. Can be one of the
/// types provided by @c documentation_type /// types provided by @c DocumentationType
/// @return A unique pointer contained string with the json /// @return A unique pointer contained string with the json
/// representation of provided type /// representation of provided type
std::unique_ptr<std::string> getDocumentation(std::shared_ptr<ContractDefinition> _contractDef, std::unique_ptr<std::string> getDocumentation(std::shared_ptr<ContractDefinition> _contractDef,
enum documentationType _type); enum DocumentationType _type);
/// Get the ABI Interface of the contract /// Get the ABI Interface of the contract
/// @param _contractDef The contract definition /// @param _contractDef The contract definition
/// @return A unique pointer contained string with the json /// @return A unique pointer contained string with the json
@ -79,7 +81,7 @@ private:
void resetUser(); void resetUser();
void resetDev(); void resetDev();
size_t parseDocTagLine(std::string const& _string, std::string& _tagString, size_t _pos, enum docTagType _tagType); size_t parseDocTagLine(std::string const& _string, std::string& _tagString, size_t _pos, enum DocTagType _tagType);
size_t parseDocTagParam(std::string const& _string, size_t _startPos); size_t parseDocTagParam(std::string const& _string, size_t _startPos);
size_t appendDocTagParam(std::string const& _string, size_t _startPos); size_t appendDocTagParam(std::string const& _string, size_t _startPos);
void parseDocString(std::string const& _string, size_t _startPos = 0); void parseDocString(std::string const& _string, size_t _startPos = 0);
@ -89,7 +91,7 @@ private:
Json::StyledWriter m_writer; Json::StyledWriter m_writer;
// internal state // internal state
enum docTagType m_lastTag; enum DocTagType m_lastTag;
std::string m_notice; std::string m_notice;
std::string m_dev; std::string m_dev;
std::string m_return; std::string m_return;

Loading…
Cancel
Save