语法
See Packages
See Imports
Classes
simpleIdentifier typeParameters?
primaryConstructor?
(':' delegationSpecifiers)?
typeConstraints?
(classBody | enumClassBody)?
;
See Generic classes
Class members
(receiverType '.')?
simpleIdentifier functionValueParameters
(':' type)? typeConstraints?
functionBody?
;
(receiverType '.')?
(multiVariableDeclaration | variableDeclaration)
typeConstraints?
(('=' expression) | propertyDelegate)? ';'?
((getter? (semi? setter)?) | (setter? (semi? getter)?))
;
| modifiers? 'set' '(' (annotation | parameterModifier)* setterParameter ')'
(':' type)?
functionBody
;
Enum classes
See Enum classes
Types
See Types
Statements
'(' annotation* (variableDeclaration | multiVariableDeclaration) 'in' expression ')'
controlStructureBody?
;
;
;
Expressions
Precedence | Title | Symbols |
---|---|---|
Highest | Postfix | ++ , -- , . , ?. , ? |
Prefix | - , + , ++ , -- , ! , label |
|
Type RHS | : , as , as? |
|
Multiplicative | * , / , % |
|
Additive | + , - |
|
Range | .. |
|
Infix function | simpleIdentifier |
|
Elvis | ?: |
|
Named checks | in , !in , is , !is |
|
Comparison | < , > , <= , >= |
|
Equality | == , !== |
|
Conjunction | && |
|
Disjunction | || |
|
Spread operator | * |
|
Lowest | Assignment | = , += , -= , *= , /= , %= |
;
| IntegerLiteral
| HexLiteral
| BinLiteral
| CharacterLiteral
| RealLiteral
| 'null'
| LongLiteral
| UnsignedLiteral
;
(controlStructureBody | ';')
| 'if' '(' expression ')'
controlStructureBody? ';'? 'else' (controlStructureBody | ';')
;
| 'else' '->' controlStructureBody semi?
;
| ('return' | RETURN_AT) expression?
| 'continue'
| CONTINUE_AT
| 'break'
| BREAK_AT
;
| '-='
| '*='
| '/='
| '%='
;
| '!=='
| '=='
| '==='
;
| '>'
| '<='
| '>='
;
| '-'
;
| '/'
| '%'
;
| 'as?'
;
;
Modifiers
| 'sealed'
| 'annotation'
| 'data'
| 'inner'
;
| 'lateinit'
;
| 'private'
| 'internal'
| 'protected'
;
| 'out'
;
| 'operator'
| 'infix'
| 'inline'
| 'external'
| 'suspend'
;
;
| 'final'
| 'open'
;
;
| 'actual'
;
Annotations
Identifiers
| 'abstract'
| 'annotation'
| 'by'
| 'catch'
| 'companion'
| 'constructor'
| 'crossinline'
| 'data'
| 'dynamic'
| 'enum'
| 'external'
| 'final'
| 'finally'
| 'get'
| 'import'
| 'infix'
| 'init'
| 'inline'
| 'inner'
| 'internal'
| 'lateinit'
| 'noinline'
| 'open'
| 'operator'
| 'out'
| 'override'
| 'private'
| 'protected'
| 'public'
| 'reified'
| 'sealed'
| 'tailrec'
| 'set'
| 'vararg'
| 'where'
| 'expect'
| 'actual'
| 'const'
| 'suspend'
;
Lexical grammar
General
;
;
;
Separators and operations
;
;
;
;
;
Keywords
;
;
;
;
;
;
;
;
;
;
Literals
| 'false'
;
Identifiers
| '`' ~([\r\n] | '`' | '.' | ';' | ':' | '\\' | '/' | '[' | ']' | '<' | '>')+ '`'
;
Depending on the target and publicity of the declaration, the set of allowed symbols in identifiers is different. This rule contains the union of allowed symbols from all targets. Thus, the code for any target can be parsed using the grammar.
The allowed symbols in identifiers corresponding to the target and publicity of the declaration are given below.
Kotlin/JVM (any declaration publicity)
Kotlin/Android (any declaration publicity)
The allowed symbols are different from allowed symbols for Kotlin/JVM and correspond to the Dalvik Executable format.
Kotlin/JS (private declarations)
Kotlin/JS (public declarations)
The allowed symbols for public declarations correspond to the ECMA specification (section 7.6) except that ECMA reserved words is allowed.
Kotlin/Native (any declaration publicity)
| 'abstract'
| 'annotation'
| 'by'
| 'catch'
| 'companion'
| 'constructor'
| 'crossinline'
| 'data'
| 'dynamic'
| 'enum'
| 'external'
| 'final'
| 'finally'
| 'get'
| 'import'
| 'infix'
| 'init'
| 'inline'
| 'inner'
| 'internal'
| 'lateinit'
| 'noinline'
| 'open'
| 'operator'
| 'out'
| 'override'
| 'private'
| 'protected'
| 'public'
| 'reified'
| 'sealed'
| 'tailrec'
| 'set'
| 'vararg'
| 'where'
| 'expect'
| 'actual'
| 'const'
| 'suspend'
;
;
Characters
Strings
See String templates
| '$'
;
;
| '$'
;
;
Description
Notation
The notation used on this page corresponds to the ANTLR 4 notation with a few exceptions for better readability:
Short description:
|
denotes alternative,*
denotes iteration (zero or more),+
denotes iteration (one or more),?
denotes option (zero or one),..
denotes range (from left to right),~
denotes negation.Grammar source files
Kotlin grammar source files (in ANTLR format) are located in the Kotlin specification repository:
The grammar on this page corresponds to the grammar files above.
Symbols and naming
Terminal symbol names start with an uppercase letter, e.g. Identifier.
Non-terminal symbol names start with a lowercase letter, e.g. kotlinFile.
Symbol definitions may be documented with attributes:
start
attribute denotes a symbol that represents the whole source file (see kotlinFile and script),helper
attribute denotes a lexer fragment rule (used only inside other terminal symbols).Also for better readability some simplifications are made:
Scope
The grammar corresponds to the latest stable version of the Kotlin compiler excluding lexer and parser rules for experimental features that are disabled by default.