1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
#include <libsolidity/InterfaceHandler.h>
#include <libsolidity/AST.h>
#include <libsolidity/CompilerStack.h>
namespace dev {
namespace solidity {
/* -- public -- */
InterfaceHandler::InterfaceHandler()
{
m_lastTag = DOCTAG_NONE;
}
std::unique_ptr<std::string> InterfaceHandler::getDocumentation(std::shared_ptr<ContractDefinition> _contractDef,
enum documentationType _type)
{
switch(_type)
{
case NATSPEC_USER:
return getUserDocumentation(_contractDef);
case NATSPEC_DEV:
return getDevDocumentation(_contractDef);
case ABI_INTERFACE:
return getABIInterface(_contractDef);
}
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Internal error"));
return nullptr;
}
std::unique_ptr<std::string> InterfaceHandler::getABIInterface(std::shared_ptr<ContractDefinition> _contractDef)
{
Json::Value methods(Json::arrayValue);
std::vector<FunctionDefinition const*> exportedFunctions = _contractDef->getInterfaceFunctions();
for (FunctionDefinition const* f: exportedFunctions)
{
Json::Value method;
Json::Value inputs(Json::arrayValue);
Json::Value outputs(Json::arrayValue);
auto populateParameters = [](std::vector<ASTPointer<VariableDeclaration>> const& _vars)
{
Json::Value params(Json::arrayValue);
for (ASTPointer<VariableDeclaration> const& var: _vars)
{
Json::Value input;
input["name"] = var->getName();
input["type"] = var->getType()->toString();
params.append(input);
}
return params;
};
method["name"] = f->getName();
method["inputs"] = populateParameters(f->getParameters());
method["outputs"] = populateParameters(f->getReturnParameters());
methods.append(method);
}
return std::unique_ptr<std::string>(new std::string(m_writer.write(methods)));
}
std::unique_ptr<std::string> InterfaceHandler::getUserDocumentation(std::shared_ptr<ContractDefinition> _contractDef)
{
Json::Value doc;
Json::Value methods(Json::objectValue);
for (FunctionDefinition const* f: _contractDef->getInterfaceFunctions())
{
Json::Value user;
auto strPtr = f->getDocumentation();
if (strPtr)
{
resetUser();
parseDocString(*strPtr);
user["notice"] = Json::Value(m_notice);
methods[f->getName()] = user;
}
}
doc["methods"] = methods;
return std::unique_ptr<std::string>(new std::string(m_writer.write(doc)));
}
std::unique_ptr<std::string> InterfaceHandler::getDevDocumentation(std::shared_ptr<ContractDefinition> _contractDef)
{
Json::Value doc;
Json::Value methods(Json::objectValue);
for (FunctionDefinition const* f: _contractDef->getInterfaceFunctions())
{
Json::Value method;
auto strPtr = f->getDocumentation();
if (strPtr)
{
resetDev();
parseDocString(*strPtr);
method["details"] = Json::Value(m_dev);
Json::Value params(Json::objectValue);
for (auto const& pair: m_params)
{
params[pair.first] = pair.second;
}
method["params"] = params;
methods[f->getName()] = method;
}
}
doc["methods"] = methods;
return std::unique_ptr<std::string>(new std::string(m_writer.write(doc)));
}
/* -- private -- */
void InterfaceHandler::resetUser()
{
m_notice.clear();
}
void InterfaceHandler::resetDev()
{
m_dev.clear();
m_params.clear();
}
size_t skipLineOrEOS(std::string const& _string, size_t _nlPos)
{
return (_nlPos == std::string::npos) ? _string.length() : _nlPos + 1;
}
size_t InterfaceHandler::parseDocTagLine(std::string const& _string,
std::string& _tagString,
size_t _pos,
enum docTagType _tagType)
{
size_t nlPos = _string.find("\n", _pos);
_tagString += _string.substr(_pos,
nlPos == std::string::npos ?
_string.length() :
nlPos - _pos);
m_lastTag = _tagType;
return skipLineOrEOS(_string, nlPos);
}
size_t InterfaceHandler::parseDocTagParam(std::string const& _string, size_t _startPos)
{
// find param name
size_t currPos = _string.find(" ", _startPos);
if (currPos == std::string::npos)
{
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);
currPos += 1;
size_t nlPos = _string.find("\n", currPos);
auto paramDesc = _string.substr(currPos,
nlPos == std::string::npos ?
_string.length() :
nlPos - currPos);
m_params.push_back(std::make_pair(paramName, paramDesc));
m_lastTag = DOCTAG_PARAM;
return skipLineOrEOS(_string, nlPos);
}
size_t InterfaceHandler::appendDocTagParam(std::string const& _string, size_t _startPos)
{
// Should never be called with an empty vector
assert(!m_params.empty());
auto pair = m_params.back();
size_t nlPos = _string.find("\n", _startPos);
pair.second += _string.substr(_startPos,
nlPos == std::string::npos ?
_string.length() :
nlPos - _startPos);
m_params.at(m_params.size() - 1) = pair;
return skipLineOrEOS(_string, nlPos);
}
size_t InterfaceHandler::parseDocTag(std::string const& _string, std::string const& _tag, size_t _pos)
{
//TODO: need to check for @(start of a tag) between here and the end of line
// for all cases
size_t nlPos = _pos;
if (m_lastTag == DOCTAG_NONE || _tag != "")
{
if (_tag == "dev")
nlPos = parseDocTagLine(_string, m_dev, _pos, DOCTAG_DEV);
else if (_tag == "notice")
nlPos = parseDocTagLine(_string, m_notice, _pos, DOCTAG_NOTICE);
else if (_tag == "param")
nlPos = parseDocTagParam(_string, _pos);
else
{
//TODO: Some form of warning
}
}
else
appendDocTag(_string, _pos);
return nlPos;
}
size_t InterfaceHandler::appendDocTag(std::string const& _string, size_t _startPos)
{
size_t newPos = _startPos;
switch(m_lastTag)
{
case DOCTAG_DEV:
m_dev += " ";
newPos = parseDocTagLine(_string, m_dev, _startPos, DOCTAG_DEV);
break;
case DOCTAG_NOTICE:
m_notice += " ";
newPos = parseDocTagLine(_string, m_notice, _startPos, DOCTAG_NOTICE);
break;
case DOCTAG_PARAM:
newPos = appendDocTagParam(_string, _startPos);
break;
default:
break;
}
return newPos;
}
void InterfaceHandler::parseDocString(std::string const& _string, size_t _startPos)
{
size_t pos2;
size_t newPos = _startPos;
size_t tagPos = _string.find("@", _startPos);
size_t nlPos = _string.find("\n", _startPos);
if (tagPos != std::string::npos && tagPos < nlPos)
{
// we found a tag
pos2 = _string.find(" ", tagPos);
if (pos2 == std::string::npos)
{
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);
}
else if (m_lastTag != DOCTAG_NONE) // continuation of the previous tag
newPos = appendDocTag(_string, _startPos + 1);
else // skip the line if a newline was found
{
if (newPos != std::string::npos)
newPos = nlPos + 1;
}
if (newPos == std::string::npos || newPos == _string.length())
return; // EOS
parseDocString(_string, newPos);
}
} //solidity NS
} // dev NS
|