libyul: Adds Comment as new AST node type, to be used for pretty printing (and preserving) comments in yul-format.

This commit is contained in:
Christian Parpart 2019-05-27 15:20:37 +02:00
parent cfbbb194d1
commit ee572ee138
16 changed files with 50 additions and 2 deletions

View File

@ -116,6 +116,10 @@ public:
boost::apply_visitor(*this, s);
}
void operator()(yul::Comment const&)
{
}
private:
void checkInstruction(SourceLocation _location, dev::eth::Instruction _instruction)
{

View File

@ -94,6 +94,7 @@ public:
bool operator()(Break const&);
bool operator()(Continue const&);
bool operator()(Block const& _block);
bool operator()(Comment const& _comment) { return true; }
private:
/// Visits the statement and expects it to deposit one item onto the stack.

View File

@ -82,6 +82,7 @@ struct ForLoop { langutil::SourceLocation location; Block pre; std::unique_ptr<E
struct Break { langutil::SourceLocation location; };
/// Continue statement (valid within for loop)
struct Continue { langutil::SourceLocation location; };
struct Comment { langutil::SourceLocation location; std::string text; };
struct LocationExtractor: boost::static_visitor<langutil::SourceLocation>
{

View File

@ -45,10 +45,11 @@ struct Break;
struct Continue;
struct ExpressionStatement;
struct Block;
struct Comment;
struct TypedName;
using Expression = boost::variant<FunctionalInstruction, FunctionCall, Identifier, Literal>;
using Statement = boost::variant<ExpressionStatement, Instruction, Label, StackAssignment, Assignment, VariableDeclaration, FunctionDefinition, If, Switch, ForLoop, Break, Continue, Block>;
using Statement = boost::variant<ExpressionStatement, Instruction, Label, StackAssignment, Assignment, VariableDeclaration, FunctionDefinition, If, Switch, ForLoop, Break, Continue, Block, Comment>;
}

View File

@ -84,8 +84,22 @@ Block Parser::parseBlock()
RecursionGuard recursionGuard(*this);
Block block = createWithLocation<Block>();
expectToken(Token::LBrace);
while (currentToken() != Token::RBrace)
{
if (!m_scanner->currentCommentLiteral().empty())
block.statements.emplace_back(
Comment{m_scanner->currentCommentLocation(), m_scanner->currentCommentLiteral()}
);
block.statements.emplace_back(parseStatement());
}
if (!m_scanner->currentCommentLiteral().empty())
block.statements.emplace_back(
Comment{m_scanner->currentCommentLocation(), m_scanner->currentCommentLiteral()}
);
block.location.end = endPosition();
advance();
return block;

View File

@ -258,6 +258,12 @@ string AsmPrinter::operator()(Block const& _block) const
}
}
std::string AsmPrinter::operator()(Comment const& _comment) const
{
// Leading newline for better readability of comments.
return "\n// " + _comment.text;
}
string AsmPrinter::formatTypedName(TypedName _variable) const
{
solAssert(!_variable.name.empty(), "Invalid variable name.");

View File

@ -53,6 +53,7 @@ public:
std::string operator()(Break const& _break) const;
std::string operator()(Continue const& _continue) const;
std::string operator()(Block const& _block) const;
std::string operator()(Comment const& _comment) const;
private:
std::string formatTypedName(TypedName _variable) const;

View File

@ -66,6 +66,7 @@ public:
bool operator()(Break const&) { return true; }
bool operator()(Continue const&) { return true; }
bool operator()(Block const& _block);
bool operator()(Comment const&) { return true; }
private:
bool registerVariable(

View File

@ -188,6 +188,7 @@ public:
void operator()(Break const&);
void operator()(Continue const&);
void operator()(Block const& _block);
void operator()(Comment const&) {}
private:
AbstractAssembly::LabelID labelFromIdentifier(Identifier const& _identifier);

View File

@ -264,7 +264,9 @@ vector<wasm::Expression> EWasmCodeTransform::visit(vector<yul::Statement> const&
{
vector<wasm::Expression> ret;
for (auto const& s: _statements)
ret.emplace_back(visit(s));
if (s.type() != typeid(yul::Comment))
ret.emplace_back(visit(s));
return ret;
}

View File

@ -54,6 +54,7 @@ public:
wasm::Expression operator()(yul::Break const&);
wasm::Expression operator()(yul::Continue const&);
wasm::Expression operator()(yul::Block const& _block);
wasm::Expression operator()(yul::Comment const&) { return {}; }
private:
EWasmCodeTransform(

View File

@ -153,6 +153,11 @@ Statement ASTCopier::operator ()(Block const& _block)
return translate(_block);
}
Statement ASTCopier::operator()(Comment const& _comment)
{
return Comment{_comment.location, _comment.text};
}
Expression ASTCopier::translate(Expression const& _expression)
{
return _expression.apply_visitor(static_cast<ExpressionCopier&>(*this));

View File

@ -61,6 +61,7 @@ public:
virtual Statement operator()(Break const&) = 0;
virtual Statement operator()(Continue const&) = 0;
virtual Statement operator()(Block const& _block) = 0;
virtual Statement operator()(Comment const& _comment) = 0;
};
/**
@ -88,6 +89,7 @@ public:
Statement operator()(Break const&) override;
Statement operator()(Continue const&) override;
Statement operator()(Block const& _block) override;
Statement operator()(Comment const& _comment) override;
virtual Expression translate(Expression const& _expression);
virtual Statement translate(Statement const& _statement);

View File

@ -59,6 +59,7 @@ public:
virtual void operator()(Break const&) {}
virtual void operator()(Continue const&) {}
virtual void operator()(Block const& _block);
virtual void operator()(Comment const&) {}
virtual void visit(Statement const& _st);
virtual void visit(Expression const& _e);
@ -96,6 +97,7 @@ public:
virtual void operator()(Break&);
virtual void operator()(Continue&);
virtual void operator()(Block& _block);
virtual void operator()(Comment&) {}
virtual void visit(Statement& _st);
virtual void visit(Expression& _e);

View File

@ -181,6 +181,11 @@ bool SyntacticallyEqual::statementEqual(Block const& _lhs, Block const& _rhs)
});
}
bool SyntacticallyEqual::statementEqual(Comment const& _lhs, Comment const& _rhs)
{
return _lhs.text == _rhs.text;
}
bool SyntacticallyEqual::visitDeclaration(TypedName const& _lhs, TypedName const& _rhs)
{
if (_lhs.type != _rhs.type)

View File

@ -58,6 +58,7 @@ public:
bool statementEqual(Break const&, Break const&) { return true; }
bool statementEqual(Continue const&, Continue const&) { return true; }
bool statementEqual(Block const& _lhs, Block const& _rhs);
bool statementEqual(Comment const& _lhs, Comment const& _rhs);
private:
bool statementEqual(Instruction const& _lhs, Instruction const& _rhs);
bool statementEqual(Label const& _lhs, Label const& _rhs);