solidity/Token.h

388 lines
21 KiB
C
Raw Normal View History

// Copyright 2006-2012, the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Modifications as part of cpp-ethereum under the following license:
//
// cpp-ethereum 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.
//
// cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include <libdevcore/Common.h>
#include <libdevcore/Log.h>
#include <libsolidity/Utils.h>
2014-11-05 13:20:56 +00:00
#include <libsolidity/Exceptions.h>
#include <libdevcore/UndefMacros.h>
2014-10-16 12:08:54 +00:00
namespace dev
{
namespace solidity
{
// TOKEN_LIST takes a list of 3 macros M, all of which satisfy the
// same signature M(name, string, precedence), where name is the
// symbolic token name, string is the corresponding syntactic symbol
// (or NULL, for literals), and precedence is the precedence (or 0).
// The parameters are invoked for token categories as follows:
//
// T: Non-keyword tokens
// K: Keyword tokens
// IGNORE_TOKEN is a convenience macro that can be supplied as
// an argument (at any position) for a TOKEN_LIST call. It does
// nothing with tokens belonging to the respective category.
#define IGNORE_TOKEN(name, string, precedence)
#define TOKEN_LIST(T, K) \
/* End of source indicator. */ \
T(EOS, "EOS", 0) \
\
/* Punctuators (ECMA-262, section 7.7, page 15). */ \
2015-02-13 14:59:30 +00:00
T(LParen, "(", 0) \
T(RParen, ")", 0) \
T(LBrack, "[", 0) \
T(RBrack, "]", 0) \
T(LBrace, "{", 0) \
T(RBrace, "}", 0) \
T(Colon, ":", 0) \
T(Semicolon, ";", 0) \
T(Period, ".", 0) \
T(Conditional, "?", 3) \
T(Arrow, "=>", 0) \
\
/* Assignment operators. */ \
/* IsAssignmentOp() relies on this block of enum values being */ \
/* contiguous and sorted in the same order!*/ \
2015-02-13 14:59:30 +00:00
T(Assign, "=", 2) \
2014-10-13 16:22:15 +00:00
/* The following have to be in exactly the same order as the simple binary operators*/ \
2015-02-13 14:59:30 +00:00
T(AssignBitOr, "|=", 2) \
T(AssignBitXor, "^=", 2) \
T(AssignBitAnd, "&=", 2) \
T(AssignShl, "<<=", 2) \
T(AssignSar, ">>=", 2) \
T(AssignShr, ">>>=", 2) \
T(AssignAdd, "+=", 2) \
T(AssignSub, "-=", 2) \
T(AssignMul, "*=", 2) \
T(AssignDiv, "/=", 2) \
T(AssignMod, "%=", 2) \
2014-10-16 12:08:54 +00:00
\
2014-10-09 10:28:37 +00:00
/* Binary operators sorted by precedence. */ \
/* IsBinaryOp() relies on this block of enum values */ \
/* being contiguous and sorted in the same order! */ \
T(Comma, ",", 1) \
T(Or, "||", 4) \
T(And, "&&", 5) \
2015-02-13 14:59:30 +00:00
T(BitOr, "|", 8) \
T(BitXor, "^", 9) \
T(BitAnd, "&", 10) \
2014-10-09 10:28:37 +00:00
T(SHL, "<<", 11) \
T(SAR, ">>", 11) \
T(SHR, ">>>", 11) \
T(Add, "+", 12) \
T(Sub, "-", 12) \
T(Mul, "*", 13) \
T(Div, "/", 13) \
T(Mod, "%", 13) \
T(Exp, "**", 14) \
2014-10-16 12:08:54 +00:00
\
2014-10-09 10:28:37 +00:00
/* Compare operators sorted by precedence. */ \
/* IsCompareOp() relies on this block of enum values */ \
/* being contiguous and sorted in the same order! */ \
2015-02-13 14:59:30 +00:00
T(Equal, "==", 6) \
T(NotEqual, "!=", 6) \
T(LessThan, "<", 7) \
T(GreaterThan, ">", 7) \
T(LessThanOrEqual, "<=", 7) \
T(GreaterThanOrEqual, ">=", 7) \
K(In, "in", 7) \
2014-10-16 12:08:54 +00:00
\
2014-10-09 10:28:37 +00:00
/* Unary operators. */ \
/* IsUnaryOp() relies on this block of enum values */ \
/* being contiguous and sorted in the same order! */ \
T(Not, "!", 0) \
2015-02-13 14:59:30 +00:00
T(BitNot, "~", 0) \
T(Inc, "++", 0) \
T(Dec, "--", 0) \
K(Delete, "delete", 0) \
2014-10-16 12:08:54 +00:00
\
2014-10-17 10:52:39 +00:00
/* Keywords */ \
K(Break, "break", 0) \
K(Case, "case", 0) \
K(Const, "constant", 0) \
K(Continue, "continue", 0) \
K(Contract, "contract", 0) \
K(Default, "default", 0) \
K(Do, "do", 0) \
K(Else, "else", 0) \
K(Event, "event", 0) \
2015-02-13 23:43:02 +00:00
K(External, "external", 0) \
K(Is, "is", 0) \
K(Indexed, "indexed", 0) \
K(For, "for", 0) \
K(Function, "function", 0) \
K(If, "if", 0) \
K(Import, "import", 0) \
K(Mapping, "mapping", 0) \
K(Modifier, "modifier", 0) \
K(New, "new", 0) \
K(Public, "public", 0) \
K(Private, "private", 0) \
2015-03-04 16:35:23 +00:00
K(Internal, "internal", 0) \
K(Return, "return", 0) \
K(Returns, "returns", 0) \
K(Struct, "struct", 0) \
K(Switch, "switch", 0) \
K(Var, "var", 0) \
K(While, "while", 0) \
2015-02-13 14:59:30 +00:00
K(Enum, "enum", 0) \
2014-10-16 12:08:54 +00:00
\
2015-03-04 16:35:23 +00:00
/* Ether subdenominations */ \
K(SubWei, "wei", 0) \
K(SubSzabo, "szabo", 0) \
K(SubFinney, "finney", 0) \
K(SubEther, "ether", 0) \
K(SubSecond, "seconds", 0) \
K(SubMinute, "minutes", 0) \
K(SubHour, "hours", 0) \
K(SubDay, "days", 0) \
K(SubWeek, "weeks", 0) \
K(SubYear, "years", 0) \
K(After, "after", 0) \
2014-10-13 16:22:15 +00:00
/* type keywords, keep them in this order, keep int as first keyword
2015-02-09 17:45:00 +00:00
* the implementation in Types.cpp has to be synced to this here */\
K(Int, "int", 0) \
K(Int8, "int8", 0) \
K(Int16, "int16", 0) \
K(Int24, "int24", 0) \
K(Int32, "int32", 0) \
K(Int40, "int40", 0) \
2015-02-10 08:52:19 +00:00
K(Int48, "int48", 0) \
K(Int56, "int56", 0) \
K(Int64, "int64", 0) \
K(Int72, "int72", 0) \
K(Int80, "int80", 0) \
K(Int88, "int88", 0) \
K(Int96, "int96", 0) \
K(Int104, "int104", 0) \
K(Int112, "int112", 0) \
K(Int120, "int120", 0) \
K(Int128, "int128", 0) \
K(Int136, "int136", 0) \
K(Int144, "int144", 0) \
K(Int152, "int152", 0) \
K(Int160, "int160", 0) \
K(Int168, "int168", 0) \
K(Int176, "int178", 0) \
K(Int184, "int184", 0) \
K(Int192, "int192", 0) \
K(Int200, "int200", 0) \
K(Int208, "int208", 0) \
K(Int216, "int216", 0) \
K(Int224, "int224", 0) \
K(Int232, "int232", 0) \
K(Int240, "int240", 0) \
K(Int248, "int248", 0) \
K(Int256, "int256", 0) \
2015-02-10 08:52:19 +00:00
K(UInt, "uint", 0) \
K(UInt8, "uint8", 0) \
K(UInt16, "uint16", 0) \
K(UInt24, "uint24", 0) \
K(UInt32, "uint32", 0) \
K(UInt40, "uint40", 0) \
K(UInt48, "uint48", 0) \
K(UInt56, "uint56", 0) \
K(UInt64, "uint64", 0) \
K(UInt72, "uint72", 0) \
K(UInt80, "uint80", 0) \
K(UInt88, "uint88", 0) \
K(UInt96, "uint96", 0) \
K(UInt104, "uint104", 0) \
K(UInt112, "uint112", 0) \
K(UInt120, "uint120", 0) \
K(UInt128, "uint128", 0) \
K(UInt136, "uint136", 0) \
K(UInt144, "uint144", 0) \
K(UInt152, "uint152", 0) \
K(UInt160, "uint160", 0) \
K(UInt168, "uint168", 0) \
K(UInt176, "uint178", 0) \
K(UInt184, "uint184", 0) \
K(UInt192, "uint192", 0) \
K(UInt200, "uint200", 0) \
K(UInt208, "uint208", 0) \
K(UInt216, "uint216", 0) \
K(UInt224, "uint224", 0) \
K(UInt232, "uint232", 0) \
K(UInt240, "uint240", 0) \
K(UInt248, "uint248", 0) \
K(UInt256, "uint256", 0) \
2015-03-04 10:41:15 +00:00
K(Bytes8, "bytes8", 0) \
K(Bytes16, "bytes16", 0) \
K(Bytes24, "bytes24", 0) \
K(Bytes32, "bytes32", 0) \
K(Bytes40, "bytes40", 0) \
K(Bytes48, "bytes48", 0) \
K(Bytes56, "bytes56", 0) \
K(Bytes64, "bytes64", 0) \
K(Bytes72, "bytes72", 0) \
K(Bytes80, "bytes80", 0) \
K(Bytes88, "bytes88", 0) \
K(Bytes96, "bytes96", 0) \
K(Bytes104, "bytes104", 0) \
K(Bytes112, "bytes112", 0) \
K(Bytes120, "bytes120", 0) \
K(Bytes128, "bytes128", 0) \
K(Bytes136, "bytes136", 0) \
K(Bytes144, "bytes144", 0) \
K(Bytes152, "bytes152", 0) \
K(Bytes160, "bytes160", 0) \
K(Bytes168, "bytes168", 0) \
K(Bytes176, "bytes178", 0) \
K(Bytes184, "bytes184", 0) \
K(Bytes192, "bytes192", 0) \
K(Bytes200, "bytes200", 0) \
K(Bytes208, "bytes208", 0) \
K(Bytes216, "bytes216", 0) \
K(Bytes224, "bytes224", 0) \
K(Bytes232, "bytes232", 0) \
K(Bytes240, "bytes240", 0) \
K(Bytes248, "bytes248", 0) \
K(Bytes256, "bytes256", 0) \
K(Bytes, "bytes", 0) \
K(Address, "address", 0) \
K(Bool, "bool", 0) \
2015-02-09 17:45:00 +00:00
K(StringType, "string", 0) \
K(Real, "real", 0) \
K(UReal, "ureal", 0) \
2015-02-13 14:59:30 +00:00
T(TypesEnd, NULL, 0) /* used as type enum end marker */ \
2014-10-16 12:08:54 +00:00
\
2014-10-17 10:52:39 +00:00
/* Literals */ \
2015-02-13 14:59:30 +00:00
K(NullLiteral, "null", 0) \
K(TrueLiteral, "true", 0) \
K(FalseLiteral, "false", 0) \
T(Number, NULL, 0) \
2015-02-13 14:59:30 +00:00
T(StringLiteral, NULL, 0) \
T(CommentLiteral, NULL, 0) \
2014-10-16 12:08:54 +00:00
\
2014-10-09 10:28:37 +00:00
/* Identifiers (not keywords or future reserved words). */ \
T(Identifier, NULL, 0) \
2014-10-16 12:08:54 +00:00
\
2014-10-09 10:28:37 +00:00
/* Illegal token - not able to scan. */ \
T(Illegal, "ILLEGAL", 0) \
2014-10-16 12:08:54 +00:00
\
2014-10-09 10:28:37 +00:00
/* Scanner-internal use only. */ \
T(Whitespace, NULL, 0)
2014-10-16 12:08:54 +00:00
class Token
{
2014-10-09 10:28:37 +00:00
public:
// All token values.
2014-12-08 11:23:10 +00:00
// attention! msvc issue:
// http://stackoverflow.com/questions/9567868/compile-errors-after-adding-v8-to-my-project-c2143-c2059
// @todo: avoid TOKEN_LIST macro
#define T(name, string, precedence) name,
2014-10-16 12:08:54 +00:00
enum Value
{
2014-10-09 10:28:37 +00:00
TOKEN_LIST(T, T)
NUM_TOKENS
};
#undef T
2014-10-09 10:28:37 +00:00
// Returns a string corresponding to the C++ token name
// (e.g. "LT" for the token LT).
static char const* getName(Value tok)
2014-10-16 12:08:54 +00:00
{
solAssert(tok < NUM_TOKENS, "");
2014-10-09 10:28:37 +00:00
return m_name[tok];
}
// Predicates
static bool isElementaryTypeName(Value tok) { return Int <= tok && tok < TypesEnd; }
static bool isAssignmentOp(Value tok) { return Assign <= tok && tok <= AssignMod; }
static bool isBinaryOp(Value op) { return Comma <= op && op <= Exp; }
static bool isCommutativeOp(Value op) { return op == BitOr || op == BitXor || op == BitAnd ||
2015-02-10 08:52:19 +00:00
op == Add || op == Mul || op == Equal || op == NotEqual; }
static bool isArithmeticOp(Value op) { return Add <= op && op <= Exp; }
2015-02-10 08:52:19 +00:00
static bool isCompareOp(Value op) { return Equal <= op && op <= In; }
2014-10-09 10:28:37 +00:00
2014-10-16 12:08:54 +00:00
static Value AssignmentToBinaryOp(Value op)
{
solAssert(isAssignmentOp(op) && op != Assign, "");
2015-02-20 20:00:13 +00:00
return Value(op + (BitOr - AssignBitOr));
2014-10-13 16:22:15 +00:00
}
static bool isBitOp(Value op) { return (BitOr <= op && op <= SHR) || op == BitNot; }
2015-03-04 16:35:23 +00:00
static bool isUnaryOp(Value op) { return (Not <= op && op <= Delete) || op == Add || op == Sub || op == After; }
static bool isCountOp(Value op) { return op == Inc || op == Dec; }
2014-10-16 12:08:54 +00:00
static bool isShiftOp(Value op) { return (SHL <= op) && (op <= SHR); }
2015-02-13 23:43:02 +00:00
static bool isVisibilitySpecifier(Value op) { return isVariableVisibilitySpecifier(op) || op == External; }
2015-02-22 18:37:54 +00:00
static bool isVariableVisibilitySpecifier(Value op) { return op == Public || op == Private || op == Internal; }
2015-03-04 16:35:23 +00:00
static bool isEtherSubdenomination(Value op) { return op == SubWei || op == SubSzabo || op == SubFinney || op == SubEther; }
static bool isTimeSubdenomination(Value op) { return op == SubSecond || op == SubMinute || op == SubHour || op == SubDay || op == SubWeek || op == SubYear; }
2014-10-09 10:28:37 +00:00
// Returns a string corresponding to the JS token string
// (.e., "<" for the token LT) or NULL if the token doesn't
// have a (unique) string (e.g. an IDENTIFIER).
static char const* toString(Value tok)
2014-10-16 12:08:54 +00:00
{
solAssert(tok < NUM_TOKENS, "");
2014-10-09 10:28:37 +00:00
return m_string[tok];
}
// Returns the precedence > 0 for binary and compare
// operators; returns 0 otherwise.
2014-10-16 12:08:54 +00:00
static int precedence(Value tok)
{
solAssert(tok < NUM_TOKENS, "");
2014-10-09 10:28:37 +00:00
return m_precedence[tok];
}
static Token::Value fromIdentifierOrKeyword(std::string const& _name);
2014-10-09 10:28:37 +00:00
private:
static char const* const m_name[NUM_TOKENS];
static char const* const m_string[NUM_TOKENS];
static int8_t const m_precedence[NUM_TOKENS];
static char const m_tokenType[NUM_TOKENS];
};
2014-10-16 12:08:54 +00:00
}
}