diff options
author | chriseth <c@ethdev.com> | 2016-07-01 16:14:50 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2016-07-21 01:45:43 +0800 |
commit | c55584d3e2da49674993972a129ef478ba6b4914 (patch) | |
tree | 419a4abda107edef1ed3df15398b6030b43bd1a8 /libsolidity/ast | |
parent | 980abfe52aac3161dc4f25574da83bfc6be977bf (diff) | |
download | dexon-solidity-c55584d3e2da49674993972a129ef478ba6b4914.tar.gz dexon-solidity-c55584d3e2da49674993972a129ef478ba6b4914.tar.zst dexon-solidity-c55584d3e2da49674993972a129ef478ba6b4914.zip |
Source location as part of AST.
Diffstat (limited to 'libsolidity/ast')
-rw-r--r-- | libsolidity/ast/ASTJsonConverter.cpp | 121 | ||||
-rw-r--r-- | libsolidity/ast/ASTJsonConverter.h | 18 |
2 files changed, 83 insertions, 56 deletions
diff --git a/libsolidity/ast/ASTJsonConverter.cpp b/libsolidity/ast/ASTJsonConverter.cpp index 89d0bf35..6b459da4 100644 --- a/libsolidity/ast/ASTJsonConverter.cpp +++ b/libsolidity/ast/ASTJsonConverter.cpp @@ -42,12 +42,17 @@ void ASTJsonConverter::addKeyValue(Json::Value& _obj, string const& _key, string _obj[_key] = _val; } -void ASTJsonConverter::addJsonNode(string const& _nodeName, - initializer_list<pair<string const, string const>> _list, - bool _hasChildren = false) +void ASTJsonConverter::addJsonNode( + ASTNode const& _node, + string const& _nodeName, + initializer_list<pair<string const, string const>> _list, + bool _hasChildren = false +) { Json::Value node; + node["id"] = reinterpret_cast<Json::UInt64>(&_node); + node["src"] = sourceLocationToString(_node.location()); node["name"] = _nodeName; if (_list.size() != 0) { @@ -68,7 +73,21 @@ void ASTJsonConverter::addJsonNode(string const& _nodeName, } } -ASTJsonConverter::ASTJsonConverter(ASTNode const& _ast): m_ast(&_ast) +string ASTJsonConverter::sourceLocationToString(SourceLocation const& _location) const +{ + int sourceIndex{-1}; + if (_location.sourceName && m_sourceIndices.count(*_location.sourceName)) + sourceIndex = m_sourceIndices.at(*_location.sourceName); + int length = -1; + if (_location.start >= 0 && _location.end >= 0) + length = _location.end - _location.start; + return std::to_string(_location.start) + ":" + std::to_string(length) + ":" + std::to_string(sourceIndex); +} + +ASTJsonConverter::ASTJsonConverter( + ASTNode const& _ast, + map<string, unsigned> const& _sourceIndices +): m_ast(&_ast), m_sourceIndices(_sourceIndices) { Json::Value children(Json::arrayValue); @@ -91,31 +110,31 @@ Json::Value const& ASTJsonConverter::json() bool ASTJsonConverter::visit(ImportDirective const& _node) { - addJsonNode("Import", { make_pair("file", _node.path())}); + addJsonNode(_node, "Import", { make_pair("file", _node.path())}); return true; } bool ASTJsonConverter::visit(ContractDefinition const& _node) { - addJsonNode("Contract", { make_pair("name", _node.name()) }, true); + addJsonNode(_node, "Contract", { make_pair("name", _node.name()) }, true); return true; } bool ASTJsonConverter::visit(StructDefinition const& _node) { - addJsonNode("Struct", { make_pair("name", _node.name()) }, true); + addJsonNode(_node, "Struct", { make_pair("name", _node.name()) }, true); return true; } -bool ASTJsonConverter::visit(ParameterList const&) +bool ASTJsonConverter::visit(ParameterList const& _node) { - addJsonNode("ParameterList", {}, true); + addJsonNode(_node, "ParameterList", {}, true); return true; } bool ASTJsonConverter::visit(FunctionDefinition const& _node) { - addJsonNode("Function", + addJsonNode(_node, "Function", { make_pair("name", _node.name()), make_pair("public", boost::lexical_cast<std::string>(_node.isPublic())), make_pair("const", boost::lexical_cast<std::string>(_node.isDeclaredConst())) }, @@ -125,7 +144,7 @@ bool ASTJsonConverter::visit(FunctionDefinition const& _node) bool ASTJsonConverter::visit(VariableDeclaration const& _node) { - addJsonNode("VariableDeclaration", { + addJsonNode(_node, "VariableDeclaration", { make_pair("name", _node.name()), make_pair("name", _node.name()), }, true); @@ -139,114 +158,114 @@ bool ASTJsonConverter::visit(TypeName const&) bool ASTJsonConverter::visit(ElementaryTypeName const& _node) { - addJsonNode("ElementaryTypeName", { make_pair("name", _node.typeName().toString()) }); + addJsonNode(_node, "ElementaryTypeName", { make_pair("name", _node.typeName().toString()) }); return true; } bool ASTJsonConverter::visit(UserDefinedTypeName const& _node) { - addJsonNode("UserDefinedTypeName", { + addJsonNode(_node, "UserDefinedTypeName", { make_pair("name", boost::algorithm::join(_node.namePath(), ".")) }); return true; } -bool ASTJsonConverter::visit(Mapping const&) +bool ASTJsonConverter::visit(Mapping const& _node) { - addJsonNode("Mapping", {}, true); + addJsonNode(_node, "Mapping", {}, true); return true; } -bool ASTJsonConverter::visit(InlineAssembly const&) +bool ASTJsonConverter::visit(InlineAssembly const& _node) { - addJsonNode("InlineAssembly", {}, true); + addJsonNode(_node, "InlineAssembly", {}, true); return true; } -bool ASTJsonConverter::visit(Block const&) +bool ASTJsonConverter::visit(Block const& _node) { - addJsonNode("Block", {}, true); + addJsonNode(_node, "Block", {}, true); return true; } -bool ASTJsonConverter::visit(IfStatement const&) +bool ASTJsonConverter::visit(IfStatement const& _node) { - addJsonNode("IfStatement", {}, true); + addJsonNode(_node, "IfStatement", {}, true); return true; } -bool ASTJsonConverter::visit(WhileStatement const&) +bool ASTJsonConverter::visit(WhileStatement const& _node) { - addJsonNode("WhileStatement", {}, true); + addJsonNode(_node, "WhileStatement", {}, true); return true; } -bool ASTJsonConverter::visit(ForStatement const&) +bool ASTJsonConverter::visit(ForStatement const& _node) { - addJsonNode("ForStatement", {}, true); + addJsonNode(_node, "ForStatement", {}, true); return true; } -bool ASTJsonConverter::visit(Continue const&) +bool ASTJsonConverter::visit(Continue const& _node) { - addJsonNode("Continue", {}); + addJsonNode(_node, "Continue", {}); return true; } -bool ASTJsonConverter::visit(Break const&) +bool ASTJsonConverter::visit(Break const& _node) { - addJsonNode("Break", {}); + addJsonNode(_node, "Break", {}); return true; } -bool ASTJsonConverter::visit(Return const&) +bool ASTJsonConverter::visit(Return const& _node) { - addJsonNode("Return", {}, true);; + addJsonNode(_node, "Return", {}, true);; return true; } -bool ASTJsonConverter::visit(Throw const&) +bool ASTJsonConverter::visit(Throw const& _node) { - addJsonNode("Throw", {}, true);; + addJsonNode(_node, "Throw", {}, true);; return true; } -bool ASTJsonConverter::visit(VariableDeclarationStatement const&) +bool ASTJsonConverter::visit(VariableDeclarationStatement const& _node) { - addJsonNode("VariableDefinition", {}, true); + addJsonNode(_node, "VariableDefinition", {}, true); return true; } -bool ASTJsonConverter::visit(ExpressionStatement const&) +bool ASTJsonConverter::visit(ExpressionStatement const& _node) { - addJsonNode("ExpressionStatement", {}, true); + addJsonNode(_node, "ExpressionStatement", {}, true); return true; } -bool ASTJsonConverter::visit(Conditional const&) +bool ASTJsonConverter::visit(Conditional const& _node) { - addJsonNode("Conditional", {}, true); + addJsonNode(_node, "Conditional", {}, true); return true; } bool ASTJsonConverter::visit(Assignment const& _node) { - addJsonNode("Assignment", + addJsonNode(_node, "Assignment", { make_pair("operator", Token::toString(_node.assignmentOperator())), make_pair("type", type(_node)) }, true); return true; } -bool ASTJsonConverter::visit(TupleExpression const&) +bool ASTJsonConverter::visit(TupleExpression const& _node) { - addJsonNode("TupleExpression",{}, true); + addJsonNode(_node, "TupleExpression",{}, true); return true; } bool ASTJsonConverter::visit(UnaryOperation const& _node) { - addJsonNode("UnaryOperation", + addJsonNode(_node, "UnaryOperation", { make_pair("prefix", boost::lexical_cast<std::string>(_node.isPrefixOperation())), make_pair("operator", Token::toString(_node.getOperator())), make_pair("type", type(_node)) }, @@ -256,7 +275,7 @@ bool ASTJsonConverter::visit(UnaryOperation const& _node) bool ASTJsonConverter::visit(BinaryOperation const& _node) { - addJsonNode("BinaryOperation", { + addJsonNode(_node, "BinaryOperation", { make_pair("operator", Token::toString(_node.getOperator())), make_pair("type", type(_node)) }, true); @@ -265,7 +284,7 @@ bool ASTJsonConverter::visit(BinaryOperation const& _node) bool ASTJsonConverter::visit(FunctionCall const& _node) { - addJsonNode("FunctionCall", { + addJsonNode(_node, "FunctionCall", { make_pair("type_conversion", boost::lexical_cast<std::string>(_node.annotation().isTypeConversion)), make_pair("type", type(_node)) }, true); @@ -274,13 +293,13 @@ bool ASTJsonConverter::visit(FunctionCall const& _node) bool ASTJsonConverter::visit(NewExpression const& _node) { - addJsonNode("NewExpression", { make_pair("type", type(_node)) }, true); + addJsonNode(_node, "NewExpression", { make_pair("type", type(_node)) }, true); return true; } bool ASTJsonConverter::visit(MemberAccess const& _node) { - addJsonNode("MemberAccess", + addJsonNode(_node, "MemberAccess", { make_pair("member_name", _node.memberName()), make_pair("type", type(_node)) }, true); @@ -289,20 +308,20 @@ bool ASTJsonConverter::visit(MemberAccess const& _node) bool ASTJsonConverter::visit(IndexAccess const& _node) { - addJsonNode("IndexAccess", { make_pair("type", type(_node)) }, true); + addJsonNode(_node, "IndexAccess", { make_pair("type", type(_node)) }, true); return true; } bool ASTJsonConverter::visit(Identifier const& _node) { - addJsonNode("Identifier", + addJsonNode(_node, "Identifier", { make_pair("value", _node.name()), make_pair("type", type(_node)) }); return true; } bool ASTJsonConverter::visit(ElementaryTypeNameExpression const& _node) { - addJsonNode("ElementaryTypenameExpression", + addJsonNode(_node, "ElementaryTypenameExpression", { make_pair("value", _node.typeName().toString()), make_pair("type", type(_node)) }); return true; } @@ -310,7 +329,7 @@ bool ASTJsonConverter::visit(ElementaryTypeNameExpression const& _node) bool ASTJsonConverter::visit(Literal const& _node) { char const* tokenString = Token::toString(_node.token()); - addJsonNode("Literal", + addJsonNode(_node, "Literal", { make_pair("string", (tokenString) ? tokenString : "null"), make_pair("value", _node.value()), make_pair("type", type(_node)) }); diff --git a/libsolidity/ast/ASTJsonConverter.h b/libsolidity/ast/ASTJsonConverter.h index 91ee72e1..2e3046f1 100644 --- a/libsolidity/ast/ASTJsonConverter.h +++ b/libsolidity/ast/ASTJsonConverter.h @@ -42,7 +42,11 @@ class ASTJsonConverter: public ASTConstVisitor { public: /// Create a converter to JSON for the given abstract syntax tree. - explicit ASTJsonConverter(ASTNode const& _ast); + /// @a _sourceIndices is used to abbreviate source names in source locations. + explicit ASTJsonConverter( + ASTNode const& _ast, + std::map<std::string, unsigned> const& _sourceIndices = std::map<std::string, unsigned>() + ); /// Output the json representation of the AST to _stream. void print(std::ostream& _stream); Json::Value const& json(); @@ -118,9 +122,13 @@ public: private: void process(); void addKeyValue(Json::Value& _obj, std::string const& _key, std::string const& _val); - void addJsonNode(std::string const& _nodeName, - std::initializer_list<std::pair<std::string const, std::string const>> _list, - bool _hasChildren); + void addJsonNode( + ASTNode const& _node, + std::string const& _nodeName, + std::initializer_list<std::pair<std::string const, std::string const>> _list, + bool _hasChildren + ); + std::string sourceLocationToString(SourceLocation const& _location) const; std::string type(Expression const& _expression); std::string type(VariableDeclaration const& _varDecl); inline void goUp() @@ -132,8 +140,8 @@ private: bool processed = false; Json::Value m_astJson; std::stack<Json::Value*> m_jsonNodePtrs; - std::string m_source; ASTNode const* m_ast; + std::map<std::string, unsigned> const& m_sourceIndices; }; } |