Parse @ast-id annotation.

This commit is contained in:
chriseth
2021-09-16 17:18:48 +02:00
parent 42739b73b1
commit 05d20446bb
4 changed files with 107 additions and 9 deletions
+13 -5
View File
@@ -38,14 +38,22 @@ using Type = YulString;
struct DebugData
{
explicit DebugData(langutil::SourceLocation _location): location(std::move(_location)) {}
explicit DebugData(langutil::SourceLocation _location, std::optional<int64_t> _astID = {}):
location(std::move(_location)),
astID(std::move(_astID))
{}
static std::shared_ptr<DebugData const> create(
langutil::SourceLocation _location = {},
std::optional<int64_t> _astID = {}
)
{
return std::make_shared<DebugData const>(std::move(_location), std::move(_astID));
}
langutil::SourceLocation location;
/// ID in the (Solidity) source AST.
std::optional<int64_t> astID;
static std::shared_ptr<DebugData const> create(langutil::SourceLocation _location = {})
{
return std::make_shared<DebugData const>(_location);
}
};
struct TypedName { std::shared_ptr<DebugData const> debugData; YulString name; Type type; };
+43 -2
View File
@@ -103,7 +103,7 @@ unique_ptr<Block> Parser::parseInline(std::shared_ptr<Scanner> const& _scanner)
try
{
m_scanner = _scanner;
if (m_sourceNames)
if (m_useSourceLocationFrom == UseSourceLocationFrom::Comments)
fetchDebugDataFromComment();
return make_unique<Block>(parseBlock());
}
@@ -136,6 +136,8 @@ void Parser::fetchDebugDataFromComment()
match_results<string_view::const_iterator> match;
langutil::SourceLocation sourceLocation = m_debugDataOverride->location;
// Empty for each new node.
optional<int> astID;
while (regex_search(commentLiteral.cbegin(), commentLiteral.cend(), match, tagRegex))
{
@@ -149,12 +151,19 @@ void Parser::fetchDebugDataFromComment()
else
break;
}
else if (match[1] == "@ast-id")
{
if (auto parseResult = parseASTIDComment(commentLiteral, m_scanner->currentCommentLocation()))
tie(commentLiteral, astID) = *parseResult;
else
break;
}
else
// Ignore unrecognized tags.
continue;
}
m_debugDataOverride = DebugData::create(sourceLocation);
m_debugDataOverride = DebugData::create(sourceLocation, astID);
}
optional<pair<string_view, SourceLocation>> Parser::parseSrcComment(
@@ -222,6 +231,38 @@ optional<pair<string_view, SourceLocation>> Parser::parseSrcComment(
return {{tail, SourceLocation{}}};
}
optional<pair<string_view, optional<int>>> Parser::parseASTIDComment(
string_view _arguments,
langutil::SourceLocation const& _commentLocation
)
{
static regex const argRegex = regex(
R"~~(^(\d+)(?:\s|$))~~",
regex_constants::ECMAScript | regex_constants::optimize
);
match_results<string_view::const_iterator> match;
optional<int> astID;
bool matched = regex_search(_arguments.cbegin(), _arguments.cend(), match, argRegex);
string_view tail = _arguments;
if (matched)
{
solAssert(match.size() == 2, "");
tail = _arguments.substr(static_cast<size_t>(match.position() + match.length()));
astID = toInt(match[1].str());
}
if (!matched || !astID || *astID < 0 || static_cast<int64_t>(*astID) != *astID)
{
m_errorReporter.syntaxError(1749_error, _commentLocation, "Invalid argument for @ast-id.");
astID = nullopt;
}
if (matched)
return {{_arguments, astID}};
else
return nullopt;
}
Block Parser::parseBlock()
{
RecursionGuard recursionGuard(*this);
+6 -2
View File
@@ -108,8 +108,12 @@ protected:
void fetchDebugDataFromComment();
std::optional<std::pair<std::string_view, langutil::SourceLocation>>
parseSrcComment(
std::optional<std::pair<std::string_view, langutil::SourceLocation>> parseSrcComment(
std::string_view _arguments,
langutil::SourceLocation const& _commentLocation
);
std::optional<std::pair<std::string_view, std::optional<int>>> parseASTIDComment(
std::string_view _arguments,
langutil::SourceLocation const& _commentLocation
);