2015-10-26 14:13:36 +00:00
|
|
|
|
|
|
|
#include <libsolidity/parsing/DocStringParser.h>
|
2017-05-11 13:26:35 +00:00
|
|
|
#include <libsolidity/interface/ErrorReporter.h>
|
2017-06-21 20:50:57 +00:00
|
|
|
#include <libsolidity/interface/Exceptions.h>
|
2015-10-26 14:13:36 +00:00
|
|
|
|
2017-01-27 10:19:48 +00:00
|
|
|
#include <boost/range/irange.hpp>
|
|
|
|
#include <boost/range/algorithm.hpp>
|
|
|
|
|
2015-10-26 14:13:36 +00:00
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::solidity;
|
|
|
|
|
|
|
|
|
2017-01-27 10:19:48 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
string::const_iterator skipLineOrEOS(
|
2015-10-26 14:13:36 +00:00
|
|
|
string::const_iterator _nlPos,
|
|
|
|
string::const_iterator _end
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return (_nlPos == _end) ? _end : ++_nlPos;
|
|
|
|
}
|
|
|
|
|
2017-01-27 10:19:48 +00:00
|
|
|
string::const_iterator firstSpaceOrTab(
|
2015-10-26 14:13:36 +00:00
|
|
|
string::const_iterator _pos,
|
|
|
|
string::const_iterator _end
|
|
|
|
)
|
|
|
|
{
|
2017-01-27 10:19:48 +00:00
|
|
|
return boost::range::find_first_of(make_pair(_pos, _end), " \t");
|
2015-10-26 14:13:36 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 10:19:48 +00:00
|
|
|
string::const_iterator firstWhitespaceOrNewline(
|
2016-11-30 14:04:07 +00:00
|
|
|
string::const_iterator _pos,
|
|
|
|
string::const_iterator _end
|
|
|
|
)
|
|
|
|
{
|
2017-01-27 10:19:48 +00:00
|
|
|
return boost::range::find_first_of(make_pair(_pos, _end), " \t\n");
|
2016-11-30 14:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-27 10:19:48 +00:00
|
|
|
string::const_iterator skipWhitespace(
|
2016-11-30 14:04:07 +00:00
|
|
|
string::const_iterator _pos,
|
|
|
|
string::const_iterator _end
|
|
|
|
)
|
|
|
|
{
|
|
|
|
auto currPos = _pos;
|
2017-01-27 10:19:48 +00:00
|
|
|
while (currPos != _end && (*currPos == ' ' || *currPos == '\t'))
|
2016-11-30 14:04:07 +00:00
|
|
|
currPos += 1;
|
|
|
|
return currPos;
|
|
|
|
}
|
|
|
|
|
2017-01-27 10:19:48 +00:00
|
|
|
}
|
2016-11-30 14:04:07 +00:00
|
|
|
|
2017-05-11 13:26:35 +00:00
|
|
|
bool DocStringParser::parse(string const& _docString, ErrorReporter& _errorReporter)
|
2015-10-26 14:13:36 +00:00
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter = &_errorReporter;
|
2015-10-26 14:13:36 +00:00
|
|
|
m_errorsOccurred = false;
|
|
|
|
m_lastTag = nullptr;
|
|
|
|
|
|
|
|
auto currPos = _docString.begin();
|
|
|
|
auto end = _docString.end();
|
|
|
|
|
|
|
|
while (currPos != end)
|
|
|
|
{
|
|
|
|
auto tagPos = find(currPos, end, '@');
|
|
|
|
auto nlPos = find(currPos, end, '\n');
|
|
|
|
|
|
|
|
if (tagPos != end && tagPos < nlPos)
|
|
|
|
{
|
|
|
|
// we found a tag
|
2017-01-26 23:09:00 +00:00
|
|
|
auto tagNameEndPos = firstWhitespaceOrNewline(tagPos, end);
|
2015-10-26 14:13:36 +00:00
|
|
|
if (tagNameEndPos == end)
|
|
|
|
{
|
|
|
|
appendError("End of tag " + string(tagPos, tagNameEndPos) + "not found");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
currPos = parseDocTag(tagNameEndPos + 1, end, string(tagPos + 1, tagNameEndPos));
|
|
|
|
}
|
|
|
|
else if (!!m_lastTag) // continuation of the previous tag
|
|
|
|
currPos = appendDocTag(currPos, end);
|
|
|
|
else if (currPos != end)
|
|
|
|
{
|
|
|
|
// if it begins without a tag then consider it as @notice
|
|
|
|
if (currPos == _docString.begin())
|
|
|
|
{
|
|
|
|
currPos = parseDocTag(currPos, end, "notice");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (nlPos == end) //end of text
|
|
|
|
break;
|
|
|
|
// else skip the line if a newline was found and we get here
|
|
|
|
currPos = nlPos + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !m_errorsOccurred;
|
|
|
|
}
|
|
|
|
|
|
|
|
DocStringParser::iter DocStringParser::parseDocTagLine(iter _pos, iter _end, bool _appending)
|
|
|
|
{
|
|
|
|
solAssert(!!m_lastTag, "");
|
|
|
|
auto nlPos = find(_pos, _end, '\n');
|
2016-11-30 14:04:07 +00:00
|
|
|
if (_appending && _pos < _end && *_pos != ' ' && *_pos != '\t')
|
2015-10-26 14:13:36 +00:00
|
|
|
m_lastTag->content += " ";
|
2016-12-01 09:14:19 +00:00
|
|
|
else if (!_appending)
|
|
|
|
_pos = skipWhitespace(_pos, _end);
|
2015-10-26 14:13:36 +00:00
|
|
|
copy(_pos, nlPos, back_inserter(m_lastTag->content));
|
|
|
|
return skipLineOrEOS(nlPos, _end);
|
|
|
|
}
|
|
|
|
|
|
|
|
DocStringParser::iter DocStringParser::parseDocTagParam(iter _pos, iter _end)
|
|
|
|
{
|
2016-11-30 14:04:07 +00:00
|
|
|
// find param name start
|
|
|
|
auto nameStartPos = skipWhitespace(_pos, _end);
|
|
|
|
if (nameStartPos == _end)
|
|
|
|
{
|
2017-01-27 10:19:48 +00:00
|
|
|
appendError("No param name given");
|
2016-11-30 14:04:07 +00:00
|
|
|
return _end;
|
|
|
|
}
|
|
|
|
auto nameEndPos = firstSpaceOrTab(nameStartPos, _end);
|
|
|
|
auto paramName = string(nameStartPos, nameEndPos);
|
2015-10-26 14:13:36 +00:00
|
|
|
|
2016-11-30 14:04:07 +00:00
|
|
|
auto descStartPos = skipWhitespace(nameEndPos, _end);
|
2018-03-14 08:55:04 +00:00
|
|
|
auto nlPos = find(descStartPos, _end, '\n');
|
|
|
|
|
|
|
|
if (descStartPos == nlPos)
|
2016-11-30 14:04:07 +00:00
|
|
|
{
|
2017-01-27 10:19:48 +00:00
|
|
|
appendError("No description given for param " + paramName);
|
2016-11-30 14:04:07 +00:00
|
|
|
return _end;
|
|
|
|
}
|
2015-10-26 14:13:36 +00:00
|
|
|
|
2016-11-30 14:04:07 +00:00
|
|
|
auto paramDesc = string(descStartPos, nlPos);
|
2015-10-26 14:13:36 +00:00
|
|
|
newTag("param");
|
|
|
|
m_lastTag->paramName = paramName;
|
|
|
|
m_lastTag->content = paramDesc;
|
|
|
|
|
|
|
|
return skipLineOrEOS(nlPos, _end);
|
|
|
|
}
|
|
|
|
|
|
|
|
DocStringParser::iter DocStringParser::parseDocTag(iter _pos, iter _end, string const& _tag)
|
|
|
|
{
|
|
|
|
// LTODO: need to check for @(start of a tag) between here and the end of line
|
|
|
|
// for all cases.
|
|
|
|
if (!m_lastTag || _tag != "")
|
|
|
|
{
|
|
|
|
if (_tag == "param")
|
|
|
|
return parseDocTagParam(_pos, _end);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newTag(_tag);
|
|
|
|
return parseDocTagLine(_pos, _end, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return appendDocTag(_pos, _end);
|
|
|
|
}
|
|
|
|
|
|
|
|
DocStringParser::iter DocStringParser::appendDocTag(iter _pos, iter _end)
|
|
|
|
{
|
|
|
|
solAssert(!!m_lastTag, "");
|
|
|
|
return parseDocTagLine(_pos, _end, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocStringParser::newTag(string const& _tagName)
|
|
|
|
{
|
|
|
|
m_lastTag = &m_docTags.insert(make_pair(_tagName, DocTag()))->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocStringParser::appendError(string const& _description)
|
|
|
|
{
|
|
|
|
m_errorsOccurred = true;
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter->docstringParsingError(_description);
|
2015-10-26 14:13:36 +00:00
|
|
|
}
|