美文网首页
附录5 solidity完整文法

附录5 solidity完整文法

作者: 智能合约大师兄 | 来源:发表于2019-12-01 12:45 被阅读0次

    由于solidity语言本身一直处于快速升级和迭代过程中,功能和语法不同版本间可能会有细微差别。因此在实际编程中如果对语法有疑问,最好参考官方solidity最新的在线版本相关语法说明。

    本附录收录了solidity 0.5.0版本的完整文法定义,这也是截止本书完成时的应用最为广泛和稳定的版本,可以作为日常工作的快速参考。

    SourceUnit = (PragmaDirective | ImportDirective | ContractDefinition)*
    
    // Pragma actually parses anything up to the trailing ';' to be fully forward-compatible.
    PragmaDirective = 'pragma' Identifier ([^;]+) ';'
    
    ImportDirective = 'import' StringLiteral ('as' Identifier)? ';'
            | 'import' ('*' | Identifier) ('as' Identifier)? 'from' StringLiteral ';'
            | 'import' '{' Identifier ('as' Identifier)? ( ',' Identifier ('as' Identifier)? )* '}' 'from' StringLiteral ';'
    
    ContractDefinition = ( 'contract' | 'library' | 'interface' ) Identifier
                         ( 'is' InheritanceSpecifier (',' InheritanceSpecifier )* )?
                         '{' ContractPart* '}'
    
    ContractPart = StateVariableDeclaration | UsingForDeclaration
                 | StructDefinition | ModifierDefinition | FunctionDefinition | EventDefinition | EnumDefinition
    
    InheritanceSpecifier = UserDefinedTypeName ( '(' Expression ( ',' Expression )* ')' )?
    
    StateVariableDeclaration = TypeName ( 'public' | 'internal' | 'private' | 'constant' )* Identifier ('=' Expression)? ';'
    UsingForDeclaration = 'using' Identifier 'for' ('*' | TypeName) ';'
    StructDefinition = 'struct' Identifier '{'
                         ( VariableDeclaration ';' (VariableDeclaration ';')* ) '}'
    
    ModifierDefinition = 'modifier' Identifier ParameterList? Block
    ModifierInvocation = Identifier ( '(' ExpressionList? ')' )?
    
    FunctionDefinition = 'function' Identifier? ParameterList
                         ( ModifierInvocation | StateMutability | 'external' | 'public' | 'internal' | 'private' )*
                         ( 'returns' ParameterList )? ( ';' | Block )
    EventDefinition = 'event' Identifier EventParameterList 'anonymous'? ';'
    
    EnumValue = Identifier
    EnumDefinition = 'enum' Identifier '{' EnumValue? (',' EnumValue)* '}'
    
    ParameterList = '(' ( Parameter (',' Parameter)* )? ')'
    Parameter = TypeName StorageLocation? Identifier?
    
    EventParameterList = '(' ( EventParameter (',' EventParameter )* )? ')'
    EventParameter = TypeName 'indexed'? Identifier?
    
    FunctionTypeParameterList = '(' ( FunctionTypeParameter (',' FunctionTypeParameter )* )? ')'
    FunctionTypeParameter = TypeName StorageLocation?
    
    // semantic restriction: mappings and structs (recursively) containing mappings
    // are not allowed in argument lists
    VariableDeclaration = TypeName StorageLocation? Identifier
    
    TypeName = ElementaryTypeName
             | UserDefinedTypeName
             | Mapping
             | ArrayTypeName
             | FunctionTypeName
             | ( 'address' 'payable' )
    
    UserDefinedTypeName = Identifier ( '.' Identifier )*
    
    Mapping = 'mapping' '(' ElementaryTypeName '=>' TypeName ')'
    ArrayTypeName = TypeName '[' Expression? ']'
    FunctionTypeName = 'function' FunctionTypeParameterList ( 'internal' | 'external' | StateMutability )*
                       ( 'returns' FunctionTypeParameterList )?
    StorageLocation = 'memory' | 'storage' | 'calldata'
    StateMutability = 'pure' | 'view' | 'payable'
    
    Block = '{' Statement* '}'
    Statement = IfStatement | WhileStatement | ForStatement | Block | InlineAssemblyStatement |
                ( DoWhileStatement | PlaceholderStatement | Continue | Break | Return |
                  Throw | EmitStatement | SimpleStatement ) ';'
    
    ExpressionStatement = Expression
    IfStatement = 'if' '(' Expression ')' Statement ( 'else' Statement )?
    WhileStatement = 'while' '(' Expression ')' Statement
    PlaceholderStatement = '_'
    SimpleStatement = VariableDefinition | ExpressionStatement
    ForStatement = 'for' '(' (SimpleStatement)? ';' (Expression)? ';' (ExpressionStatement)? ')' Statement
    InlineAssemblyStatement = 'assembly' StringLiteral? InlineAssemblyBlock
    DoWhileStatement = 'do' Statement 'while' '(' Expression ')'
    Continue = 'continue'
    Break = 'break'
    Return = 'return' Expression?
    Throw = 'throw'
    EmitStatement = 'emit' FunctionCall
    VariableDefinition = (VariableDeclaration | '(' VariableDeclaration? (',' VariableDeclaration? )* ')' ) ( '=' Expression )?
    
    // Precedence by order (see github.com/ethereum/solidity/pull/732)
    Expression
      = Expression ('++' | '--')
      | NewExpression
      | IndexAccess
      | MemberAccess
      | FunctionCall
      | '(' Expression ')'
      | ('!' | '~' | 'delete' | '++' | '--' | '+' | '-') Expression
      | Expression '**' Expression
      | Expression ('*' | '/' | '%') Expression
      | Expression ('+' | '-') Expression
      | Expression ('<<' | '>>') Expression
      | Expression '&' Expression
      | Expression '^' Expression
      | Expression '|' Expression
      | Expression ('<' | '>' | '<=' | '>=') Expression
      | Expression ('==' | '!=') Expression
      | Expression '&&' Expression
      | Expression '||' Expression
      | Expression '?' Expression ':' Expression
      | Expression ('=' | '|=' | '^=' | '&=' | '<<=' | '>>=' | '+=' | '-=' | '*=' | '/=' | '%=') Expression
      | PrimaryExpression
    
    PrimaryExpression = BooleanLiteral
                      | NumberLiteral
                      | HexLiteral
                      | StringLiteral
                      | TupleExpression
                      | Identifier
                      | ElementaryTypeNameExpression
    
    ExpressionList = Expression ( ',' Expression )*
    NameValueList = Identifier ':' Expression ( ',' Identifier ':' Expression )*
    
    FunctionCall = Expression '(' FunctionCallArguments ')'
    FunctionCallArguments = '{' NameValueList? '}'
                          | ExpressionList?
    
    NewExpression = 'new' TypeName
    MemberAccess = Expression '.' Identifier
    IndexAccess = Expression '[' Expression? ']'
    
    BooleanLiteral = 'true' | 'false'
    NumberLiteral = ( HexNumber | DecimalNumber ) (' ' NumberUnit)?
    NumberUnit = 'wei' | 'szabo' | 'finney' | 'ether'
               | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'years'
    HexLiteral = 'hex' ('"' ([0-9a-fA-F]{2})* '"' | '\'' ([0-9a-fA-F]{2})* '\'')
    StringLiteral = '"' ([^"\r\n\\] | '\\' .)* '"'
    Identifier = [a-zA-Z_$] [a-zA-Z_$0-9]*
    
    HexNumber = '0x' [0-9a-fA-F]+
    DecimalNumber = [0-9]+ ( '.' [0-9]* )? ( [eE] [0-9]+ )?
    
    TupleExpression = '(' ( Expression? ( ',' Expression? )*  )? ')'
                    | '[' ( Expression  ( ',' Expression  )*  )? ']'
    
    ElementaryTypeNameExpression = ElementaryTypeName
    
    ElementaryTypeName = 'address' | 'bool' | 'string' | Int | Uint | Byte | Fixed | Ufixed
    
    Int = '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'
    
    Uint = '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'
    
    Byte = 'byte' | 'bytes' | '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'
    
    Fixed = 'fixed' | ( 'fixed' [0-9]+ 'x' [0-9]+ )
    
    Ufixed = 'ufixed' | ( 'ufixed' [0-9]+ 'x' [0-9]+ )
    
    InlineAssemblyBlock = '{' AssemblyItem* '}'
    
    AssemblyItem = Identifier | FunctionalAssemblyExpression | InlineAssemblyBlock | AssemblyVariableDeclaration | AssemblyAssignment | AssemblyLabel | NumberLiteral | StringLiteral | HexLiteral
    AssemblyExpression = Identifier | FunctionalAssemblyExpression | NumberLiteral | StringLiteral | HexLiteral
    AssemblyVariableDeclaration = 'let' Identifier ':=' AssemblyExpression
    AssemblyAssignment = ( Identifier ':=' AssemblyExpression ) | ( '=:' Identifier )
    AssemblyLabel = Identifier ':'
    FunctionalAssemblyExpression = Identifier '(' AssemblyItem? ( ',' AssemblyItem )* ')'
    

    相关文章

      网友评论

          本文标题:附录5 solidity完整文法

          本文链接:https://www.haomeiwen.com/subject/evezwctx.html