/* This file is part of solidity. solidity is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. solidity is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with solidity. If not, see . */ /** * @author Christian * @date 2016 * Solidity inline assembly parser. */ #include #include #include #include #include using namespace std; using namespace dev; using namespace dev::solidity; using namespace dev::solidity::assembly; shared_ptr Parser::parse(std::shared_ptr const& _scanner) { try { m_scanner = _scanner; return make_shared(parseBlock()); } catch (FatalError const&) { if (m_errors.empty()) throw; // Something is weird here, rather throw again. } return nullptr; } assembly::Block Parser::parseBlock() { assembly::Block block = createWithLocation(); expectToken(Token::LBrace); while (m_scanner->currentToken() != Token::RBrace) block.statements.emplace_back(parseStatement()); block.location.end = endPosition(); m_scanner->next(); return block; } assembly::Statement Parser::parseStatement() { switch (m_scanner->currentToken()) { case Token::Let: return parseVariableDeclaration(); case Token::Function: return parseFunctionDefinition(); case Token::LBrace: return parseBlock(); case Token::Switch: { assembly::Switch _switch = createWithLocation(); m_scanner->next(); _switch.expression = make_shared(parseExpression()); if (_switch.expression->type() == typeid(assembly::Instruction)) fatalParserError("Instructions are not supported as expressions for switch."); while (m_scanner->currentToken() == Token::Case) _switch.cases.emplace_back(parseCase()); if (m_scanner->currentToken() == Token::Default) _switch.cases.emplace_back(parseCase()); if (m_scanner->currentToken() == Token::Default) fatalParserError("Only one default case allowed."); else if (m_scanner->currentToken() == Token::Case) fatalParserError("Case not allowed after default case."); if (_switch.cases.size() == 0) fatalParserError("Switch statement without any cases."); _switch.location.end = _switch.cases.back().body.location.end; return _switch; } case Token::Assign: { if (m_julia) break; assembly::StackAssignment assignment = createWithLocation(); m_scanner->next(); expectToken(Token::Colon); assignment.variableName.location = location(); assignment.variableName.name = m_scanner->currentLiteral(); if (!m_julia && instructions().count(assignment.variableName.name)) fatalParserError("Identifier expected, got instruction name."); assignment.location.end = endPosition(); expectToken(Token::Identifier); return assignment; } default: break; } // Options left: // Simple instruction (might turn into functional), // literal, // identifier (might turn into label or functional assignment) Statement statement(parseElementaryOperation(false)); switch (m_scanner->currentToken()) { case Token::LParen: return parseCall(std::move(statement)); case Token::Colon: { if (statement.type() != typeid(assembly::Identifier)) fatalParserError("Label name / variable name must precede \":\"."); assembly::Identifier const& identifier = boost::get(statement); m_scanner->next(); // identifier:=: should be parsed as identifier: =: (i.e. a label), // while identifier:= (being followed by a non-colon) as identifier := (assignment). if (m_scanner->currentToken() == Token::Assign && m_scanner->peekNextToken() != Token::Colon) { assembly::Assignment assignment = createWithLocation(identifier.location); if (!m_julia && instructions().count(identifier.name)) fatalParserError("Cannot use instruction names for identifier names."); m_scanner->next(); assignment.variableName = identifier; assignment.value.reset(new Statement(parseExpression())); assignment.location.end = locationOf(*assignment.value).end; return assignment; } else { // label if (m_julia) fatalParserError("Labels are not supported."); Label label = createWithLocation