solidity/docs/grammar.txt

157 lines
7.5 KiB
Plaintext
Raw Normal View History

2016-08-19 17:57:21 +00:00
SourceUnit = (PragmaDirective | ImportDirective | ContractDefinition)*
2016-07-27 14:01:30 +00:00
2016-08-19 17:57:21 +00:00
// Pragma actually parses anything up to the trailing ';' to be fully forward-compatible.
PragmaDirective = 'pragma' Identifier ([^;]+) ';'
2016-07-20 17:17:03 +00:00
ImportDirective = 'import' StringLiteral ('as' Identifier)? ';'
| 'import' ('*' | Identifier) ('as' Identifier)? 'from' StringLiteral ';'
| 'import' '{' Identifier ('as' Identifier)? ( ',' Identifier ('as' Identifier)? )* '}' 'from' StringLiteral ';'
2016-07-20 14:14:23 +00:00
2017-06-14 23:41:00 +00:00
ContractDefinition = ( 'contract' | 'library' | 'interface' ) Identifier
2016-08-19 17:57:21 +00:00
( 'is' InheritanceSpecifier (',' InheritanceSpecifier )* )?
'{' ContractPart* '}'
2016-07-27 14:03:40 +00:00
ContractPart = StateVariableDeclaration | UsingForDeclaration
2016-07-20 17:31:05 +00:00
| StructDefinition | ModifierDefinition | FunctionDefinition | EventDefinition | EnumDefinition
2016-12-28 02:08:57 +00:00
InheritanceSpecifier = UserDefinedTypeName ( '(' Expression ( ',' Expression )* ')' )?
2016-07-20 17:31:05 +00:00
StateVariableDeclaration = TypeName ( 'public' | 'internal' | 'private' | 'constant' )? Identifier ('=' Expression)? ';'
2016-07-27 14:03:40 +00:00
UsingForDeclaration = 'using' Identifier 'for' ('*' | TypeName) ';'
StructDefinition = 'struct' Identifier '{'
( VariableDeclaration ';' (VariableDeclaration ';')* )? '}'
ModifierDefinition = 'modifier' Identifier ParameterList? Block
ModifierInvocation = Identifier ( '(' ExpressionList? ')' )?
2016-08-09 09:53:38 +00:00
FunctionDefinition = 'function' Identifier? ParameterList
( ModifierInvocation | StateMutability | 'external' | 'public' | 'internal' | 'private' )*
2016-12-27 01:57:00 +00:00
( 'returns' ParameterList )? ( ';' | Block )
EventDefinition = 'event' Identifier IndexedParameterList 'anonymous'? ';'
2016-07-20 13:59:00 +00:00
2015-02-13 21:56:46 +00:00
EnumValue = Identifier
EnumDefinition = 'enum' Identifier '{' EnumValue? (',' EnumValue)* '}'
2016-07-20 00:26:33 +00:00
IndexedParameterList = '(' ( TypeName 'indexed'? Identifier? (',' TypeName 'indexed'? Identifier?)* )? ')'
ParameterList = '(' ( TypeName Identifier? (',' TypeName Identifier?)* )? ')'
2016-11-08 10:48:28 +00:00
TypeNameList = '(' ( TypeName (',' TypeName )* )? ')'
2016-07-30 13:34:46 +00:00
// semantic restriction: mappings and structs (recursively) containing mappings
// are not allowed in argument lists
VariableDeclaration = TypeName StorageLocation? Identifier
2016-12-28 02:08:57 +00:00
TypeName = ElementaryTypeName
| UserDefinedTypeName
2016-12-28 02:08:57 +00:00
| Mapping
| ArrayTypeName
| FunctionTypeName
UserDefinedTypeName = Identifier ( '.' Identifier )*
Mapping = 'mapping' '(' ElementaryTypeName '=>' TypeName ')'
ArrayTypeName = TypeName '[' Expression? ']'
FunctionTypeName = 'function' TypeNameList ( 'internal' | 'external' | StateMutability )*
2016-11-08 10:48:28 +00:00
( 'returns' TypeNameList )?
2016-07-25 15:28:30 +00:00
StorageLocation = 'memory' | 'storage'
2017-08-16 19:53:46 +00:00
StateMutability = 'pure' | 'constant' | 'view' | 'payable'
Block = '{' Statement* '}'
2016-12-01 15:24:43 +00:00
Statement = IfStatement | WhileStatement | ForStatement | Block | InlineAssemblyStatement |
2016-11-11 08:33:19 +00:00
( DoWhileStatement | PlaceholderStatement | Continue | Break | Return |
Throw | SimpleStatement ) ';'
ExpressionStatement = Expression
IfStatement = 'if' '(' Expression ')' Statement ( 'else' Statement )?
WhileStatement = 'while' '(' Expression ')' Statement
2016-07-25 15:48:49 +00:00
PlaceholderStatement = '_'
SimpleStatement = VariableDefinition | ExpressionStatement
2016-07-27 14:42:33 +00:00
ForStatement = 'for' '(' (SimpleStatement)? ';' (Expression)? ';' (ExpressionStatement)? ')' Statement
2017-06-14 23:43:19 +00:00
InlineAssemblyStatement = 'assembly' StringLiteral? InlineAssemblyBlock
2016-11-11 08:33:19 +00:00
DoWhileStatement = 'do' Statement 'while' '(' Expression ')'
Continue = 'continue'
Break = 'break'
Return = 'return' Expression?
Throw = 'throw'
VariableDefinition = ('var' IdentifierList | VariableDeclaration) ( '=' Expression )?
IdentifierList = '(' ( Identifier? ',' )* Identifier? ')'
2016-07-23 14:15:01 +00:00
// Precedence by order (see github.com/ethereum/solidity/pull/732)
Expression
= Expression ('++' | '--')
| NewExpression
| IndexAccess
2017-06-15 01:59:58 +00:00
| MemberAccess
| FunctionCall
| '(' Expression ')'
2016-08-10 20:25:17 +00:00
| ('!' | '~' | 'delete' | '++' | '--' | '+' | '-') Expression
2016-07-23 14:15:01 +00:00
| Expression '**' Expression
| Expression ('*' | '/' | '%') Expression
| Expression ('+' | '-') Expression
| Expression ('<<' | '>>') Expression
2016-07-23 14:15:01 +00:00
| Expression '&' Expression
| Expression '^' Expression
| Expression '|' Expression
| Expression ('<' | '>' | '<=' | '>=') Expression
| Expression ('==' | '!=') Expression
| Expression '&&' Expression
| Expression '||' Expression
| Expression '?' Expression ':' Expression
2016-07-27 14:42:33 +00:00
| Expression ('=' | '|=' | '^=' | '&=' | '<<=' | '>>=' | '+=' | '-=' | '*=' | '/=' | '%=') Expression
2016-07-22 23:45:10 +00:00
| PrimaryExpression
2017-06-15 01:59:58 +00:00
PrimaryExpression = BooleanLiteral
| NumberLiteral
| HexLiteral
| StringLiteral
2017-06-19 20:16:45 +00:00
| TupleExpression
2017-06-15 01:59:58 +00:00
| Identifier
| ElementaryTypeNameExpression
2016-07-14 21:41:32 +00:00
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'
2016-12-24 16:10:51 +00:00
NumberLiteral = ( HexNumber | DecimalNumber ) (' ' NumberUnit)?
NumberUnit = 'wei' | 'szabo' | 'finney' | 'ether'
2016-07-20 14:49:14 +00:00
| 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'years'
HexLiteral = 'hex' ('"' ([0-9a-fA-F]{2})* '"' | '\'' ([0-9a-fA-F]{2})* '\'')
2016-07-23 00:35:20 +00:00
StringLiteral = '"' ([^"\r\n\\] | '\\' .)* '"'
2017-01-20 18:29:43 +00:00
Identifier = [a-zA-Z_$] [a-zA-Z_$0-9]*
2016-07-20 14:49:14 +00:00
2016-12-24 16:10:51 +00:00
HexNumber = '0x' [0-9a-fA-F]+
DecimalNumber = [0-9]+
2017-06-19 20:16:45 +00:00
TupleExpression = '(' ( Expression ( ',' Expression )* )? ')'
| '[' ( Expression ( ',' Expression )* )? ']'
ElementaryTypeNameExpression = ElementaryTypeName
2016-07-27 14:42:33 +00:00
ElementaryTypeName = 'address' | 'bool' | 'string' | 'var'
| 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'
2017-07-27 13:36:56 +00:00
Fixed = 'fixed' | ( 'fixed' DecimalNumber 'x' DecimalNumber )
2017-07-27 13:36:56 +00:00
Uixed = 'ufixed' | ( 'ufixed' DecimalNumber 'x' DecimalNumber )
2016-12-01 15:24:43 +00:00
InlineAssemblyBlock = '{' AssemblyItem* '}'
2017-06-14 23:40:01 +00:00
AssemblyItem = Identifier | FunctionalAssemblyExpression | InlineAssemblyBlock | AssemblyLocalBinding | AssemblyAssignment | AssemblyLabel | NumberLiteral | StringLiteral | HexLiteral
2016-12-01 15:24:43 +00:00
AssemblyLocalBinding = 'let' Identifier ':=' FunctionalAssemblyExpression
2017-06-14 23:39:46 +00:00
AssemblyAssignment = ( Identifier ':=' FunctionalAssemblyExpression ) | ( '=:' Identifier )
2017-06-14 23:40:01 +00:00
AssemblyLabel = Identifier ':'
2016-12-01 15:24:43 +00:00
FunctionalAssemblyExpression = Identifier '(' AssemblyItem? ( ',' AssemblyItem )* ')'