2014-10-06 15:13:52 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2016-11-18 23:13:20 +00:00
|
|
|
// Modifications as part of solidity under the following license:
|
2014-10-06 15:13:52 +00:00
|
|
|
//
|
2016-11-18 23:13:20 +00:00
|
|
|
// solidity is free software: you can redistribute it and/or modify
|
2014-10-06 15:13:52 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2016-11-18 23:13:20 +00:00
|
|
|
// solidity is distributed in the hope that it will be useful,
|
2014-10-06 15:13:52 +00:00
|
|
|
// 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
|
2016-11-18 23:13:20 +00:00
|
|
|
// along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2014-10-06 15:13:52 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Common.h>
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
#include <liblangutil/UndefMacros.h>
|
2018-11-14 16:11:55 +00:00
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
#include <iosfwd>
|
2018-11-14 16:11:55 +00:00
|
|
|
#include <string>
|
|
|
|
#include <tuple>
|
2015-01-21 16:06:43 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::langutil
|
2014-10-16 12:08:54 +00:00
|
|
|
{
|
2014-10-06 15:13:52 +00:00
|
|
|
|
|
|
|
// 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
|
2018-11-23 11:57:31 +00:00
|
|
|
// (or nullptr, for literals), and precedence is the precedence (or 0).
|
2014-10-06 15:13:52 +00:00
|
|
|
// 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)
|
|
|
|
|
2015-02-09 14:24:36 +00:00
|
|
|
#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) \
|
|
|
|
\
|
2015-02-09 14:24:36 +00:00
|
|
|
/* 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) \
|
2015-02-09 13:00:12 +00:00
|
|
|
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! */ \
|
2015-02-09 13:00:12 +00:00
|
|
|
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) \
|
2015-02-09 13:00:12 +00:00
|
|
|
T(Add, "+", 12) \
|
|
|
|
T(Sub, "-", 12) \
|
|
|
|
T(Mul, "*", 13) \
|
|
|
|
T(Div, "/", 13) \
|
|
|
|
T(Mod, "%", 13) \
|
2015-02-08 11:23:17 +00:00
|
|
|
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) \
|
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! */ \
|
2015-02-09 13:00:12 +00:00
|
|
|
T(Not, "!", 0) \
|
2015-02-13 14:59:30 +00:00
|
|
|
T(BitNot, "~", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
T(Inc, "++", 0) \
|
|
|
|
T(Dec, "--", 0) \
|
|
|
|
K(Delete, "delete", 0) \
|
2014-10-16 12:08:54 +00:00
|
|
|
\
|
2019-02-18 14:07:15 +00:00
|
|
|
/* Inline Assembly Operators */ \
|
|
|
|
T(AssemblyAssign, ":=", 2) \
|
2014-10-17 10:52:39 +00:00
|
|
|
/* Keywords */ \
|
2019-11-11 16:09:31 +00:00
|
|
|
K(Abstract, "abstract", 0) \
|
2015-05-25 09:49:38 +00:00
|
|
|
K(Anonymous, "anonymous", 0) \
|
2016-08-16 14:28:45 +00:00
|
|
|
K(As, "as", 0) \
|
2016-03-09 16:23:05 +00:00
|
|
|
K(Assembly, "assembly", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(Break, "break", 0) \
|
2017-08-09 11:50:53 +00:00
|
|
|
K(Constant, "constant", 0) \
|
2018-06-27 10:29:03 +00:00
|
|
|
K(Constructor, "constructor", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(Continue, "continue", 0) \
|
|
|
|
K(Contract, "contract", 0) \
|
|
|
|
K(Do, "do", 0) \
|
|
|
|
K(Else, "else", 0) \
|
2015-05-25 09:49:38 +00:00
|
|
|
K(Enum, "enum", 0) \
|
2018-05-23 09:57:52 +00:00
|
|
|
K(Emit, "emit", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(Event, "event", 0) \
|
2015-02-13 23:43:02 +00:00
|
|
|
K(External, "external", 0) \
|
2019-09-09 16:22:02 +00:00
|
|
|
K(Fallback, "fallback", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(For, "for", 0) \
|
|
|
|
K(Function, "function", 0) \
|
2016-08-16 14:31:23 +00:00
|
|
|
K(Hex, "hex", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(If, "if", 0) \
|
2015-05-25 09:49:38 +00:00
|
|
|
K(Indexed, "indexed", 0) \
|
2017-02-07 22:12:38 +00:00
|
|
|
K(Interface, "interface", 0) \
|
2015-05-25 09:49:38 +00:00
|
|
|
K(Internal, "internal", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(Import, "import", 0) \
|
2015-05-25 09:49:38 +00:00
|
|
|
K(Is, "is", 0) \
|
2015-09-08 14:48:33 +00:00
|
|
|
K(Library, "library", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(Mapping, "mapping", 0) \
|
2015-06-05 09:07:50 +00:00
|
|
|
K(Memory, "memory", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(Modifier, "modifier", 0) \
|
|
|
|
K(New, "new", 0) \
|
2019-08-13 11:00:46 +00:00
|
|
|
K(Override, "override", 0) \
|
2016-06-18 10:11:55 +00:00
|
|
|
K(Payable, "payable", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(Public, "public", 0) \
|
2016-08-19 17:57:21 +00:00
|
|
|
K(Pragma, "pragma", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(Private, "private", 0) \
|
2017-08-15 01:26:24 +00:00
|
|
|
K(Pure, "pure", 0) \
|
2019-09-09 16:22:02 +00:00
|
|
|
K(Receive, "receive", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(Return, "return", 0) \
|
|
|
|
K(Returns, "returns", 0) \
|
2015-06-05 09:07:50 +00:00
|
|
|
K(Storage, "storage", 0) \
|
2018-05-23 04:31:20 +00:00
|
|
|
K(CallData, "calldata", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(Struct, "struct", 0) \
|
2015-09-15 14:33:14 +00:00
|
|
|
K(Throw, "throw", 0) \
|
2019-01-10 15:28:39 +00:00
|
|
|
K(Type, "type", 0) \
|
2016-08-16 14:28:45 +00:00
|
|
|
K(Using, "using", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(Var, "var", 0) \
|
2016-09-08 01:18:17 +00:00
|
|
|
K(View, "view", 0) \
|
2019-10-30 13:34:37 +00:00
|
|
|
K(Virtual, "virtual", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(While, "while", 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) \
|
2016-02-08 21:43:22 +00:00
|
|
|
/* type keywords*/ \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(Int, "int", 0) \
|
2015-02-10 08:52:19 +00:00
|
|
|
K(UInt, "uint", 0) \
|
2015-03-11 16:41:12 +00:00
|
|
|
K(Bytes, "bytes", 0) \
|
|
|
|
K(Byte, "byte", 0) \
|
2015-05-28 14:20:50 +00:00
|
|
|
K(String, "string", 0) \
|
2015-02-09 13:00:12 +00:00
|
|
|
K(Address, "address", 0) \
|
|
|
|
K(Bool, "bool", 0) \
|
2016-03-07 16:12:03 +00:00
|
|
|
K(Fixed, "fixed", 0) \
|
|
|
|
K(UFixed, "ufixed", 0) \
|
2016-03-30 03:32:40 +00:00
|
|
|
T(IntM, "intM", 0) \
|
|
|
|
T(UIntM, "uintM", 0) \
|
|
|
|
T(BytesM, "bytesM", 0) \
|
|
|
|
T(FixedMxN, "fixedMxN", 0) \
|
2016-03-07 16:12:03 +00:00
|
|
|
T(UFixedMxN, "ufixedMxN", 0) \
|
2018-11-23 11:57:31 +00:00
|
|
|
T(TypesEnd, nullptr, 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(TrueLiteral, "true", 0) \
|
|
|
|
K(FalseLiteral, "false", 0) \
|
2018-11-23 11:57:31 +00:00
|
|
|
T(Number, nullptr, 0) \
|
|
|
|
T(StringLiteral, nullptr, 0) \
|
2019-10-05 20:47:23 +00:00
|
|
|
T(HexStringLiteral, nullptr, 0) \
|
2018-11-23 11:57:31 +00:00
|
|
|
T(CommentLiteral, nullptr, 0) \
|
2014-10-16 12:08:54 +00:00
|
|
|
\
|
2014-10-09 10:28:37 +00:00
|
|
|
/* Identifiers (not keywords or future reserved words). */ \
|
2018-11-23 11:57:31 +00:00
|
|
|
T(Identifier, nullptr, 0) \
|
2014-10-16 12:08:54 +00:00
|
|
|
\
|
2015-09-08 14:48:33 +00:00
|
|
|
/* Keywords reserved for future use. */ \
|
2016-08-09 14:25:44 +00:00
|
|
|
K(After, "after", 0) \
|
2018-03-01 11:18:44 +00:00
|
|
|
K(Alias, "alias", 0) \
|
|
|
|
K(Apply, "apply", 0) \
|
|
|
|
K(Auto, "auto", 0) \
|
2015-03-25 17:33:20 +00:00
|
|
|
K(Case, "case", 0) \
|
2015-05-25 09:49:38 +00:00
|
|
|
K(Catch, "catch", 0) \
|
2018-03-01 11:18:44 +00:00
|
|
|
K(CopyOf, "copyof", 0) \
|
2016-12-01 14:37:45 +00:00
|
|
|
K(Default, "default", 0) \
|
2018-03-01 11:18:44 +00:00
|
|
|
K(Define, "define", 0) \
|
2015-05-25 09:49:38 +00:00
|
|
|
K(Final, "final", 0) \
|
2018-03-01 11:18:44 +00:00
|
|
|
K(Immutable, "immutable", 0) \
|
|
|
|
K(Implements, "implements", 0) \
|
2016-07-20 18:03:07 +00:00
|
|
|
K(In, "in", 0) \
|
2016-03-09 16:23:05 +00:00
|
|
|
K(Inline, "inline", 0) \
|
2015-05-25 09:49:38 +00:00
|
|
|
K(Let, "let", 0) \
|
2018-03-01 11:18:44 +00:00
|
|
|
K(Macro, "macro", 0) \
|
2015-05-25 09:49:38 +00:00
|
|
|
K(Match, "match", 0) \
|
2018-03-01 11:18:44 +00:00
|
|
|
K(Mutable, "mutable", 0) \
|
2016-12-01 14:37:45 +00:00
|
|
|
K(NullLiteral, "null", 0) \
|
2015-05-25 09:49:38 +00:00
|
|
|
K(Of, "of", 0) \
|
2018-03-01 11:18:44 +00:00
|
|
|
K(Partial, "partial", 0) \
|
|
|
|
K(Promise, "promise", 0) \
|
|
|
|
K(Reference, "reference", 0) \
|
2015-05-25 09:49:38 +00:00
|
|
|
K(Relocatable, "relocatable", 0) \
|
2018-03-01 11:18:44 +00:00
|
|
|
K(Sealed, "sealed", 0) \
|
|
|
|
K(Sizeof, "sizeof", 0) \
|
2016-08-16 14:28:45 +00:00
|
|
|
K(Static, "static", 0) \
|
2018-03-01 11:18:44 +00:00
|
|
|
K(Supports, "supports", 0) \
|
2015-03-25 17:33:20 +00:00
|
|
|
K(Switch, "switch", 0) \
|
|
|
|
K(Try, "try", 0) \
|
2018-03-01 11:18:44 +00:00
|
|
|
K(Typedef, "typedef", 0) \
|
2015-03-25 17:33:20 +00:00
|
|
|
K(TypeOf, "typeof", 0) \
|
2018-03-01 11:18:44 +00:00
|
|
|
K(Unchecked, "unchecked", 0) \
|
|
|
|
\
|
2014-10-09 10:28:37 +00:00
|
|
|
/* Illegal token - not able to scan. */ \
|
2015-02-09 13:00:12 +00:00
|
|
|
T(Illegal, "ILLEGAL", 0) \
|
2014-10-16 12:08:54 +00:00
|
|
|
\
|
2014-10-09 10:28:37 +00:00
|
|
|
/* Scanner-internal use only. */ \
|
2018-11-23 11:57:31 +00:00
|
|
|
T(Whitespace, nullptr, 0)
|
2014-10-06 15:13:52 +00:00
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
// All token values.
|
|
|
|
// attention! msvc issue:
|
|
|
|
// http://stackoverflow.com/questions/9567868/compile-errors-after-adding-v8-to-my-project-c2143-c2059
|
|
|
|
// @todo: avoid TOKEN_LIST macro
|
|
|
|
enum class Token : unsigned int {
|
2014-10-06 15:13:52 +00:00
|
|
|
#define T(name, string, precedence) name,
|
2018-10-22 14:48:21 +00:00
|
|
|
TOKEN_LIST(T, T)
|
|
|
|
NUM_TOKENS
|
2014-10-06 15:13:52 +00:00
|
|
|
#undef T
|
2018-10-22 14:48:21 +00:00
|
|
|
};
|
2014-10-06 15:13:52 +00:00
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
namespace TokenTraits
|
|
|
|
{
|
|
|
|
constexpr size_t count() { return static_cast<size_t>(Token::NUM_TOKENS); }
|
2014-10-09 10:28:37 +00:00
|
|
|
|
|
|
|
// Predicates
|
2018-10-22 14:48:21 +00:00
|
|
|
constexpr bool isElementaryTypeName(Token tok) { return Token::Int <= tok && tok < Token::TypesEnd; }
|
|
|
|
constexpr bool isAssignmentOp(Token tok) { return Token::Assign <= tok && tok <= Token::AssignMod; }
|
|
|
|
constexpr bool isBinaryOp(Token op) { return Token::Comma <= op && op <= Token::Exp; }
|
|
|
|
constexpr bool isCommutativeOp(Token op) { return op == Token::BitOr || op == Token::BitXor || op == Token::BitAnd ||
|
2019-02-13 15:56:46 +00:00
|
|
|
op == Token::Add || op == Token::Mul || op == Token::Equal || op == Token::NotEqual; }
|
2018-10-22 14:48:21 +00:00
|
|
|
constexpr bool isArithmeticOp(Token op) { return Token::Add <= op && op <= Token::Exp; }
|
|
|
|
constexpr bool isCompareOp(Token op) { return Token::Equal <= op && op <= Token::GreaterThanOrEqual; }
|
2014-10-09 10:28:37 +00:00
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
constexpr bool isBitOp(Token op) { return (Token::BitOr <= op && op <= Token::BitAnd) || op == Token::BitNot; }
|
|
|
|
constexpr bool isBooleanOp(Token op) { return (Token::Or <= op && op <= Token::And) || op == Token::Not; }
|
|
|
|
constexpr bool isUnaryOp(Token op) { return (Token::Not <= op && op <= Token::Delete) || op == Token::Add || op == Token::Sub; }
|
|
|
|
constexpr bool isCountOp(Token op) { return op == Token::Inc || op == Token::Dec; }
|
|
|
|
constexpr bool isShiftOp(Token op) { return (Token::SHL <= op) && (op <= Token::SHR); }
|
|
|
|
constexpr bool isVariableVisibilitySpecifier(Token op) { return op == Token::Public || op == Token::Private || op == Token::Internal; }
|
|
|
|
constexpr bool isVisibilitySpecifier(Token op) { return isVariableVisibilitySpecifier(op) || op == Token::External; }
|
|
|
|
constexpr bool isLocationSpecifier(Token op) { return op == Token::Memory || op == Token::Storage || op == Token::CallData; }
|
2014-10-13 16:22:15 +00:00
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
constexpr bool isStateMutabilitySpecifier(Token op, bool _allowConstant = true)
|
2018-09-06 16:59:50 +00:00
|
|
|
{
|
2018-10-22 14:48:21 +00:00
|
|
|
return (op == Token::Constant && _allowConstant)
|
|
|
|
|| op == Token::Pure || op == Token::View || op == Token::Payable;
|
2018-09-06 16:59:50 +00:00
|
|
|
}
|
2014-10-09 10:28:37 +00:00
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
constexpr bool isEtherSubdenomination(Token op) { return op == Token::SubWei || op == Token::SubSzabo || op == Token::SubFinney || op == Token::SubEther; }
|
|
|
|
constexpr bool isTimeSubdenomination(Token op) { return op == Token::SubSecond || op == Token::SubMinute || op == Token::SubHour || op == Token::SubDay || op == Token::SubWeek || op == Token::SubYear; }
|
2019-11-11 16:09:31 +00:00
|
|
|
constexpr bool isReservedKeyword(Token op) { return (Token::After <= op && op <= Token::Unchecked); }
|
2014-10-09 10:28:37 +00:00
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
inline Token AssignmentToBinaryOp(Token op)
|
2018-04-18 12:29:44 +00:00
|
|
|
{
|
2018-10-22 14:48:21 +00:00
|
|
|
solAssert(isAssignmentOp(op) && op != Token::Assign, "");
|
|
|
|
return static_cast<Token>(static_cast<int>(op) + (static_cast<int>(Token::BitOr) - static_cast<int>(Token::AssignBitOr)));
|
2018-04-18 12:29:44 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 16:23:05 +00:00
|
|
|
// @returns the precedence > 0 for binary and compare
|
2014-10-09 10:28:37 +00:00
|
|
|
// operators; returns 0 otherwise.
|
2018-10-22 14:48:21 +00:00
|
|
|
int precedence(Token tok);
|
2014-10-09 10:28:37 +00:00
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
std::tuple<Token, unsigned int, unsigned int> fromIdentifierOrKeyword(std::string const& _literal);
|
2015-01-12 11:46:52 +00:00
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
// @returns a string corresponding to the C++ token name
|
|
|
|
// (e.g. "LT" for the token LT).
|
|
|
|
char const* name(Token tok);
|
|
|
|
|
|
|
|
// @returns a string corresponding to the JS token string
|
2018-11-23 11:57:31 +00:00
|
|
|
// (.e., "<" for the token LT) or nullptr if the token doesn't
|
2018-10-22 14:48:21 +00:00
|
|
|
// have a (unique) string (e.g. an IDENTIFIER).
|
|
|
|
char const* toString(Token tok);
|
|
|
|
|
|
|
|
std::string friendlyName(Token tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::ostream& operator<<(std::ostream& os, Token token)
|
|
|
|
{
|
|
|
|
os << TokenTraits::friendlyName(token);
|
|
|
|
return os;
|
|
|
|
}
|
2014-10-06 15:13:52 +00:00
|
|
|
|
2016-02-08 21:43:22 +00:00
|
|
|
class ElementaryTypeNameToken
|
|
|
|
{
|
|
|
|
public:
|
2018-10-22 14:48:21 +00:00
|
|
|
ElementaryTypeNameToken(Token _token, unsigned const& _firstNumber, unsigned const& _secondNumber)
|
2016-02-08 21:43:22 +00:00
|
|
|
{
|
2016-02-15 16:34:45 +00:00
|
|
|
assertDetails(_token, _firstNumber, _secondNumber);
|
2016-02-08 21:43:22 +00:00
|
|
|
}
|
2016-02-09 21:43:23 +00:00
|
|
|
|
2016-02-15 16:34:45 +00:00
|
|
|
unsigned int firstNumber() const { return m_firstNumber; }
|
|
|
|
unsigned int secondNumber() const { return m_secondNumber; }
|
2018-10-22 14:48:21 +00:00
|
|
|
Token token() const { return m_token; }
|
|
|
|
|
2016-02-09 21:43:23 +00:00
|
|
|
///if tokValue is set to true, then returns the actual token type name, otherwise, returns full type
|
2018-05-23 04:31:20 +00:00
|
|
|
std::string toString(bool const& tokenValue = false) const
|
2016-02-15 16:34:45 +00:00
|
|
|
{
|
2018-10-22 14:48:21 +00:00
|
|
|
std::string name = TokenTraits::toString(m_token);
|
2016-02-15 16:34:45 +00:00
|
|
|
if (tokenValue || (firstNumber() == 0 && secondNumber() == 0))
|
|
|
|
return name;
|
2016-03-30 18:15:54 +00:00
|
|
|
solAssert(name.size() >= 3, "Token name size should be greater than 3. Should not reach here.");
|
|
|
|
if (m_token == Token::FixedMxN || m_token == Token::UFixedMxN)
|
2016-03-30 03:32:40 +00:00
|
|
|
return name.substr(0, name.size() - 3) + std::to_string(m_firstNumber) + "x" + std::to_string(m_secondNumber);
|
|
|
|
else
|
|
|
|
return name.substr(0, name.size() - 1) + std::to_string(m_firstNumber);
|
2016-02-15 16:34:45 +00:00
|
|
|
}
|
2016-02-08 21:43:22 +00:00
|
|
|
|
|
|
|
private:
|
2018-10-22 14:48:21 +00:00
|
|
|
Token m_token;
|
2016-02-09 21:43:23 +00:00
|
|
|
unsigned int m_firstNumber;
|
|
|
|
unsigned int m_secondNumber;
|
2016-02-12 21:01:27 +00:00
|
|
|
/// throws if type is not properly sized
|
2018-10-22 14:48:21 +00:00
|
|
|
void assertDetails(Token _baseType, unsigned const& _first, unsigned const& _second);
|
2016-02-08 21:43:22 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
}
|