语言语法

grammarSolidity

Solidity is a statically typed, contract-oriented, high-level language for implementing smart contracts on the Ethereum platform.rulesource-unit

On top level, Solidity allows pragmas, import directives, and definitions of contracts, interfaces, libraries, structs, enums and constants.

pragmapragma-token;import-directivecontract-definitioninterface-definitionlibrary-definitionfunction-definitionconstant-variable-declarationstruct-definitionenum-definitioneofruleimport-directive

Import directives import identifiers from different files.

importpathasidentifiersymbol-aliasesfrompath*asidentifierfrompath;rulepath

Path of a file to be imported.

non-empty-string-literalrulesymbol-aliases

List of aliases for symbols to be imported.

{identifierasidentifier,}rulecontract-definition

Top-level definition of a contract.

abstractcontractidentifierisinheritance-specifier,{contract-body-element}ruleinterface-definition

Top-level definition of an interface.

interfaceidentifierisinheritance-specifier,{contract-body-element}rulelibrary-definition

Top-level definition of a library.

libraryidentifier{contract-body-element}ruleinheritance-specifier

Inheritance specifier for contracts and interfaces. Can optionally supply base constructor arguments.

identifier-pathcall-argument-listrulecontract-body-element

Declarations that can be used in contracts, interfaces and libraries.

Note that interfaces and libraries may not contain constructors, interfaces may not contain state variables and libraries may not contain fallback, receive functions nor non-constant state variables.

constructor-definitionfunction-definitionmodifier-definitionfallback-function-definitionreceive-function-definitionstruct-definitionenum-definitionstate-variable-declarationevent-definitionusing-directiverulecall-argument-list

Arguments when calling a function or a similar callable object. The arguments are either given as comma separated list or as map of named arguments.

(expression,{identifier:expression,})ruleidentifier-path

Qualified name.

identifier.rulemodifier-invocation

Call to a modifier. If the modifier takes no arguments, the argument list can be skipped entirely (including opening and closing parentheses).

identifier-pathcall-argument-listrulevisibility

Visibility for functions and function types.

internalexternalprivatepublicruleparameter-list

A list of parameters, such as function arguments or return values.

type-namedata-locationidentifier,ruleconstructor-definition

Definition of a constructor. Must always supply an implementation. Note that specifying internal or public visibility is deprecated.

constructor(parameter-list)modifier-invocationpayableinternalpublicblockrulestate-mutability

State mutability for function types. The default mutability ‘non-payable’ is assumed if no mutability is specified.

pureviewpayableruleoverride-specifier

An override specifier used for functions, modifiers or state variables. In cases where there are ambiguous declarations in several base contracts being overridden, a complete list of base contracts has to be given.

override(identifier-path,)rulefunction-definition

The definition of contract, library and interface functions. Depending on the context in which the function is defined, further restrictions may apply, e.g. functions in interfaces have to be unimplemented, i.e. may not contain a body block.

functionidentifierfallbackreceive(parameter-list)visibilitystate-mutabilitymodifier-invocationvirtualoverride-specifierreturns(parameter-list);blockrulemodifier-definition

The definition of a modifier. Note that within the body block of a modifier, the underscore cannot be used as identifier, but is used as placeholder statement for the body of a function to which the modifier is applied.

modifieridentifier(parameter-list)virtualoverride-specifier;blockrulefallback-function-definition

Definition of the special fallback function.

fallback(parameter-list)externalstate-mutabilitymodifier-invocationvirtualoverride-specifierreturns(parameter-list);blockrulereceive-function-definition

Definition of the special receive function.

receive()externalpayablemodifier-invocationvirtualoverride-specifier;blockrulestruct-definition

Definition of a struct. Can occur at top-level within a source unit or within a contract, library or interface.

structidentifier{struct-member}rulestruct-member

The declaration of a named struct member.

type-nameidentifier;ruleenum-definition

Definition of an enum. Can occur at top-level within a source unit or within a contract, library or interface.

enumidentifier{identifier,}rulestate-variable-declaration

The declaration of a state variable.

type-namepublicprivateinternalconstantoverride-specifierimmutableidentifier=expression;ruleconstant-variable-declaration

The declaration of a constant variable.

type-nameconstantidentifier=expression;ruleevent-parameter

Parameter of an event.

type-nameindexedidentifierruleevent-definition

Definition of an event. Can occur in contracts, libraries or interfaces.

eventidentifier(event-parameter,)anonymous;ruleusing-directive

Using directive to bind library functions to types. Can occur within contracts and libraries.

usingidentifier-pathfor*type-name;ruletype-name

A type name can be an elementary type, a function type, a mapping type, a user-defined type (e.g. a contract or struct) or an array type.

elementary-type-namefunction-type-namemapping-typeidentifier-pathtype-name[expression]ruleelementary-type-name

addressaddresspayableboolstringbytessigned-integer-typeunsigned-integer-typefixed-bytesfixedufixedrulefunction-type-name

function(parameter-list)visibilitystate-mutabilityreturns(parameter-list)rulevariable-declaration

The declaration of a single variable.

type-namedata-locationidentifierruledata-location

memorystoragecalldataruleexpression

Complex expression. Can be an index access, an index range access, a member access, a function call (with optional function call options), a type conversion, an unary or binary expression, a comparison or assignment, a ternary expression, a new-expression (i.e. a contract creation or the allocation of a dynamic memory array), a tuple, an inline array or a primary expression (i.e. an identifier, literal or type name).

expression[expression]expression[expression:expression]expression.identifieraddressexpression{identifier:expression,}expressioncall-argument-listpayablecall-argument-listtype(type-name)++–!~delete-expressionexpression++–expression**expressionexpression*/%expressionexpression+-expressionexpression<<>>>>>expressionexpression&expressionexpression^expressionexpression|expressionexpression<><=>=expressionexpression==!=expressionexpression&&expressionexpression||expressionexpression?expression:expressionexpression=|=^=&=<<=>>=>>>=+=-=*=/=%=expressionnewtype-nametuple-expressioninline-array-expressionidentifierliteralelementary-type-nameruletuple-expression

(expression,)ruleinline-array-expression

An inline array expression denotes a statically sized array of the common type of the contained expressions.

[expression,]ruleidentifier

Besides regular non-keyword Identifiers, the ‘from’ keyword can also occur as identifier outside of import statements.

identifierfromruleliteral

string-literalnumber-literalboolean-literalhex-string-literalunicode-string-literalruleboolean-literal

truefalserulestring-literal

A full string literal consists of either one or several consecutive quoted strings.

string-literalrulehex-string-literal

A full hex string literal that consists of either one or several consecutive hex strings.

hex-stringruleunicode-string-literal

A full unicode string literal that consists of either one or several consecutive unicode strings.

unicode-string-literalrulenumber-literal

Number literals can be decimal or hexadecimal numbers with an optional unit.

decimal-numberhex-numbernumber-unitruleblock

A curly-braced block of statements. Opens its own scope.

{statementunchecked-block}ruleunchecked-block

uncheckedblockrulestatement

blockvariable-declaration-statementexpression-statementif-statementfor-statementwhile-statementdo-while-statementcontinue-statementbreak-statementtry-statementreturn-statementemit-statementassembly-statementruleif-statement

If statement with optional else part.

if(expression)statementelsestatementrulefor-statement

For statement with optional init, condition and post-loop part.

for(variable-declaration-statementexpression-statement;expression-statement;expression)statementrulewhile-statement

while(expression)statementruledo-while-statement

dostatementwhile(expression);rulecontinue-statement

A continue statement. Only allowed inside for, while or do-while loops.

continue;rulebreak-statement

A break statement. Only allowed inside for, while or do-while loops.

break;ruletry-statement

A try statement. The contained expression needs to be an external function call or a contract creation.

tryexpressionreturns(parameter-list)blockcatch-clauserulecatch-clause

The catch clause of a try statement.

catchidentifier(parameter-list)blockrulereturn-statement

returnexpression;ruleemit-statement

An emit statement. The contained expression needs to refer to an event.

emitexpressioncall-argument-list;ruleassembly-statement

An inline assembly block. The contents of an inline assembly block use a separate scanner/lexer, i.e. the set of keywords and allowed identifiers is different inside an inline assembly block.

assembly'”evmasm”‘{yul-statement}rulevariable-declaration-tuple

A tuple of variable names to be used in variable declarations. May contain empty fields.

(,variable-declaration,variable-declaration)rulevariable-declaration-statement

A variable declaration statement. A single variable may be declared without initial value, whereas a tuple of variables can only be declared with initial value.

variable-declaration=expressionvariable-declaration-tuple=expression;ruleexpression-statement

expression;rulemapping-type

mapping(mapping-key-type=>type-name)rulemapping-key-type

Only elementary types or user defined types are viable as mapping keys.

elementary-type-nameidentifier-pathruleyul-statement

A Yul statement within an inline assembly block. continue and break statements are only valid within for loops. leave statements are only valid within function bodies.

yul-blockyul-variable-declarationyul-assignmentyul-function-callyul-if-statementyul-for-statementyul-switch-statementleavebreakcontinueyul-function-definitionruleyul-block

{yul-statement}ruleyul-variable-declaration

The declaration of one or more Yul variables with optional initial value. If multiple variables are declared, only a function call is a valid initial value.

letyul-identifier:=yul-expressionletyul-identifier,:=yul-function-callruleyul-assignment

Any expression can be assigned to a single Yul variable, whereas multi-assignments require a function call on the right-hand side.

yul-path:=yul-expressionyul-path,yul-path:=yul-function-callruleyul-if-statement

ifyul-expressionyul-blockruleyul-for-statement

foryul-blockyul-expressionyul-blockyul-blockruleyul-switch-statement

A Yul switch statement can consist of only a default-case (deprecated) or one or more non-default cases optionally followed by a default-case.

switchyul-expressioncaseyul-literalyul-blockdefaultyul-blockdefaultyul-blockruleyul-function-definition

functionyul-identifier(yul-identifier,)->yul-identifier,yul-blockruleyul-path

While only identifiers without dots can be declared within inline assembly, paths containing dots can refer to declarations outside the inline assembly block.

yul-identifier.ruleyul-function-call

A call to a function with return values can only occur as right-hand side of an assignment or a variable declaration.

yul-identifieryul-evm-builtin(yul-expression,)ruleyul-boolean

truefalseruleyul-literal

yul-decimal-numberyul-string-literalyul-hex-numberyul-booleanruleyul-expression

yul-pathyul-function-callyul-literallexer grammarSolidityLexerrulefixed-bytes

Bytes types of fixed length.

‘bytes1”bytes2”bytes3”bytes4”bytes5”bytes6”bytes7”bytes8”bytes9”bytes10”bytes11”bytes12”bytes13”bytes14”bytes15”bytes16”bytes17”bytes18”bytes19”bytes20”bytes21”bytes22”bytes23”bytes24”bytes25”bytes26”bytes27”bytes28”bytes29”bytes30”bytes31”bytes32’rulenumber-unit

Unit denomination for numbers.

‘wei”gwei”ether”seconds”minutes”hours”days”weeks”years’rulesigned-integer-type

Sized signed integer types. int is an alias of int256.

‘int”int8”int16”int24”int32”int40”int48”int56”int64”int72”int80”int88”int96”int104”int112”int120”int128”int136”int144”int152”int160”int168”int176”int184”int192”int200”int208”int216”int224”int232”int240”int248”int256’ruleunsigned-integer-type

Sized unsigned integer types. uint is an alias of uint256.

‘uint”uint8”uint16”uint24”uint32”uint40”uint48”uint56”uint64”uint72”uint80”uint88”uint96”uint104”uint112”uint120”uint128”uint136”uint144”uint152”uint160”uint168”uint176”uint184”uint192”uint200”uint208”uint216”uint224”uint232”uint240”uint248”uint256’rulestring-literal

A single quoted string literal restricted to printable characters.

‘”‘double-quoted-printableescape-sequence‘””\”single-quoted-printableescape-sequence‘\”rulenon-empty-string-literal

A single non-empty quoted string literal.

‘”‘double-quoted-printableescape-sequence‘””\”single-quoted-printableescape-sequence‘\”rulesingle-quoted-printable

Any printable character except single quote or back slash.

[\u0020-\u0026\u0028-\u005B\u005D-\u007E]ruledouble-quoted-printable

Any printable character except double quote or back slash.

[\u0020-\u0021\u0023-\u005B\u005D-\u007E]ruleescape-sequence

Escape sequence. Apart from common single character escape sequences, line breaks can be escaped as well as four hex digit unicode escapes \uXXXX and two digit hex escape sequences \xXX are allowed.

‘\\'[‘”\\nrt\n\r]’u'[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]’x'[0-9A-Fa-f][0-9A-Fa-f]ruleunicode-string-literal

A single quoted string literal allowing arbitrary unicode characters.

‘unicode”‘~[“\r\n\\]escape-sequence‘””unicode\”~[‘\r\n\\]escape-sequence‘\”rulehex-string

Hex strings need to consist of an even number of hex digits that may be grouped using underscores.

‘hex””‘[0-9A-Fa-f][0-9A-Fa-f]’_”””\”[0-9A-Fa-f][0-9A-Fa-f]’_”\”rulehex-number

Hex numbers consist of a prefix and an arbitrary number of hex digits that may be delimited by underscores.

‘0”x'[0-9A-Fa-f]’_’ruledecimal-number

A decimal number literal consists of decimal digits that may be delimited by underscores and an optional positive or negative exponent. If the digits contain a decimal point, the literal has fixed point type.

[0-9]’_'[0-9]’_”.'[0-9]’_'[eE]’-‘[0-9]’_’ruleidentifier

An identifier in solidity has to start with a letter, a dollar-sign or an underscore and may additionally contain numbers after the first symbol.

[a-zA-Z$_][a-zA-Z0-9$_]ruleyul-evm-builtin

Builtin functions in the EVM Yul dialect.

‘stop”add”sub”mul”div”sdiv”mod”smod”exp”not”lt”gt”slt”sgt”eq”iszero”and”or”xor”byte”shl”shr”sar”addmod”mulmod”signextend”keccak256”pop”mload”mstore”mstore8”sload”sstore”msize”gas”address”balance”selfbalance”caller”callvalue”calldataload”calldatasize”calldatacopy”extcodesize”extcodecopy”returndatasize”returndatacopy”extcodehash”create”create2”call”callcode”delegatecall”staticcall”return”revert”selfdestruct”invalid”log0”log1”log2”log3”log4”chainid”origin”gasprice”blockhash”coinbase”timestamp”number”difficulty”gaslimit’ruleyul-identifier

Yul identifiers consist of letters, dollar signs, underscores and numbers, but may not start with a number. In inline assembly there cannot be dots in user-defined identifiers. Instead see yulPath for expressions consisting of identifiers with dots.

[a-zA-Z$_][a-zA-Z0-9$_]ruleyul-hex-number

Hex literals in Yul consist of a prefix and one or more hexadecimal digits.

‘0”x'[0-9a-fA-F]ruleyul-decimal-number

Decimal literals in Yul may be zero or any sequence of decimal digits without leading zeroes.

‘0’[1-9][0-9]ruleyul-string-literal

String literals in Yul consist of one or more double-quoted or single-quoted strings that may contain escape sequences and printable characters except unescaped line breaks or unescaped double-quotes or single-quotes, respectively.

‘”‘double-quoted-printableescape-sequence‘””\”single-quoted-printableescape-sequence‘\”rulepragma-token

Pragma token. Can contain any kind of symbol except a semicolon. Note that currently the solidity parser only allows a subset of this.Next  Previous

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索