Grammar
shebangLine
(used by
kotlinFile, script)
fileAnnotation
(used by
kotlinFile, script)
See Packages
packageHeader
(used by
kotlinFile, script)
See Imports
importList
(used by
kotlinFile, script)
importHeader
(used by
importList)
importAlias
(used by
importHeader)
topLevelObject
(used by
kotlinFile)
typeAlias
(used by
declaration)
declaration
Classes
classDeclaration
(used by
declaration)
: modifiers? ('class' | ('fun'? 'interface'))
simpleIdentifier typeParameters?
primaryConstructor?
(':' delegationSpecifiers)?
typeConstraints?
(classBody | enumClassBody)?
;
simpleIdentifier typeParameters?
primaryConstructor?
(':' delegationSpecifiers)?
typeConstraints?
(classBody | enumClassBody)?
;
primaryConstructor
(used by
classDeclaration)
classBody
classParameters
(used by
primaryConstructor)
classParameter
(used by
classParameters)
delegationSpecifiers
delegationSpecifier
(used by
annotatedDelegationSpecifier)
constructorInvocation
(used by
delegationSpecifier, unescapedAnnotation)
annotatedDelegationSpecifier
(used by
delegationSpecifiers)
explicitDelegation
(used by
delegationSpecifier)
See Generic classes
typeParameters
typeParameter
(used by
typeParameters)
typeConstraints
typeConstraint
(used by
typeConstraints)
Class members
classMemberDeclarations
(used by
classBody, enumClassBody)
classMemberDeclaration
(used by
classMemberDeclarations)
anonymousInitializer
(used by
classMemberDeclaration)
companionObject
(used by
classMemberDeclaration)
: modifiers? 'companion' 'data'? 'object'
simpleIdentifier?
(':' delegationSpecifiers)? classBody?
;
simpleIdentifier?
(':' delegationSpecifiers)? classBody?
;
functionValueParameters
(used by
functionDeclaration, secondaryConstructor)
functionValueParameter
(used by
functionValueParameters)
functionDeclaration
(used by
declaration)
: modifiers? 'fun' typeParameters?
(receiverType '.')?
simpleIdentifier functionValueParameters
(':' type)? typeConstraints?
functionBody?
;
(receiverType '.')?
simpleIdentifier functionValueParameters
(':' type)? typeConstraints?
functionBody?
;
functionBody
variableDeclaration
multiVariableDeclaration
propertyDeclaration
(used by
declaration)
: modifiers? ('val' | 'var') typeParameters?
(receiverType '.')?
(multiVariableDeclaration | variableDeclaration)
typeConstraints?
(('=' expression) | propertyDelegate)? ';'?
((getter? (semi? setter)?) | (setter? (semi? getter)?))
;
(receiverType '.')?
(multiVariableDeclaration | variableDeclaration)
typeConstraints?
(('=' expression) | propertyDelegate)? ';'?
((getter? (semi? setter)?) | (setter? (semi? getter)?))
;
propertyDelegate
(used by
propertyDeclaration)
getter
(used by
propertyDeclaration)
setter
(used by
propertyDeclaration)
: modifiers? 'set' ('(' functionValueParameterWithOptionalType ','? ')' (':' type)? functionBody)?
;
;
parametersWithOptionalType
(used by
anonymousFunction)
: '('
(functionValueParameterWithOptionalType (',' functionValueParameterWithOptionalType)* ','?)? ')'
;
(functionValueParameterWithOptionalType (',' functionValueParameterWithOptionalType)* ','?)? ')'
;
functionValueParameterWithOptionalType
(used by
setter, parametersWithOptionalType)
parameterWithOptionalType
(used by
functionValueParameterWithOptionalType)
parameter
(used by
functionValueParameter, functionTypeParameters)
objectDeclaration
(used by
declaration)
secondaryConstructor
(used by
classMemberDeclaration)
constructorDelegationCall
(used by
secondaryConstructor)
Enum classes
See Enum classes
enumClassBody
(used by
classDeclaration)
enumEntries
(used by
enumClassBody)
enumEntry
(used by
enumEntries)
Types
See Types
type
(used by
typeAlias, classParameter, typeParameter, typeConstraint, functionDeclaration, variableDeclaration, getter, setter, parameterWithOptionalType, parameter, typeProjection, functionType, functionTypeParameters, parenthesizedType, infixOperation, asExpression, lambdaParameter, anonymousFunction, superExpression, typeTest, catchBlock)
typeReference
nullableType
(used by
type, receiverType)
quest
(used by
nullableType)
userType
simpleUserType
(used by
userType)
typeProjection
(used by
typeArguments)
typeProjectionModifiers
(used by
typeProjection)
typeProjectionModifier
(used by
typeProjectionModifiers)
functionType
functionTypeParameters
(used by
functionType)
parenthesizedType
receiverType
parenthesizedUserType
(used by
parenthesizedUserType, definitelyNonNullableType)
definitelyNonNullableType
(used by
type)
: typeModifiers? (userType | parenthesizedUserType) '&' typeModifiers?
(userType | parenthesizedUserType)
;
(userType | parenthesizedUserType)
;
Statements
statement
label
controlStructureBody
block
loopStatement
(used by
statement)
forStatement
(used by
loopStatement)
: 'for'
'(' annotation* (variableDeclaration | multiVariableDeclaration) 'in' expression ')'
controlStructureBody?
;
'(' annotation* (variableDeclaration | multiVariableDeclaration) 'in' expression ')'
controlStructureBody?
;
whileStatement
(used by
loopStatement)
doWhileStatement
(used by
loopStatement)
assignment
(used by
statement)
semi
:
;
;
semis
:
;
;
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 | = , += , -= , *= , /= , %= |
expression
(used by
classParameter, explicitDelegation, functionValueParameter, functionBody, propertyDeclaration, propertyDelegate, functionValueParameterWithOptionalType, statement, forStatement, whileStatement, doWhileStatement, assignment, indexingSuffix, valueArgument, parenthesizedExpression, collectionLiteral, lineStringExpression, multiLineStringExpression, ifExpression, whenSubject, whenCondition, rangeTest, jumpExpression)
disjunction
(used by
expression)
conjunction
(used by
disjunction)
equality
(used by
conjunction)
comparison
(used by
equality)
genericCallLikeComparison
(used by
comparison)
infixOperation
(used by
genericCallLikeComparison)
elvisExpression
(used by
infixOperation)
elvis
(used by
elvisExpression)
: '?' ':'
;
;
infixFunctionCall
(used by
elvisExpression)
rangeExpression
(used by
infixFunctionCall)
additiveExpression
(used by
rangeExpression)
multiplicativeExpression
(used by
additiveExpression)
asExpression
(used by
multiplicativeExpression)
prefixUnaryExpression
(used by
asExpression, assignableExpression)
unaryPrefix
(used by
prefixUnaryExpression)
postfixUnaryExpression
postfixUnarySuffix
(used by
postfixUnaryExpression)
directlyAssignableExpression
parenthesizedDirectlyAssignableExpression
(used by
directlyAssignableExpression)
assignableExpression
(used by
assignment, parenthesizedAssignableExpression)
parenthesizedAssignableExpression
(used by
assignableExpression)
assignableSuffix
(used by
directlyAssignableExpression)
indexingSuffix
(used by
postfixUnarySuffix, assignableSuffix)
(used by
postfixUnarySuffix, assignableSuffix)
callSuffix
(used by
genericCallLikeComparison, postfixUnarySuffix)
annotatedLambda
(used by
callSuffix)
typeArguments
valueArguments
valueArgument
(used by
valueArguments)
primaryExpression
(used by
postfixUnaryExpression)
parenthesizedExpression
(used by
navigationSuffix, primaryExpression)
collectionLiteral
(used by
primaryExpression)
literalConstant
(used by
primaryExpression)
: BooleanLiteral
| IntegerLiteral
| HexLiteral
| BinLiteral
| CharacterLiteral
| RealLiteral
| 'null'
| LongLiteral
| UnsignedLiteral
;
| IntegerLiteral
| HexLiteral
| BinLiteral
| CharacterLiteral
| RealLiteral
| 'null'
| LongLiteral
| UnsignedLiteral
;
stringLiteral
(used by
primaryExpression)
lineStringLiteral
(used by
stringLiteral)
multiLineStringLiteral
(used by
stringLiteral)
lineStringContent
(used by
lineStringLiteral)
lineStringExpression
(used by
lineStringLiteral)
multiLineStringContent
(used by
multiLineStringLiteral)
multiLineStringExpression
(used by
multiLineStringLiteral)
lambdaLiteral
(used by
annotatedLambda, functionLiteral)
lambdaParameters
(used by
lambdaLiteral)
lambdaParameter
(used by
lambdaParameters)
anonymousFunction
(used by
functionLiteral)
: 'suspend'? 'fun' (type '.')?
parametersWithOptionalType (':' type)?
typeConstraints? functionBody?
;
parametersWithOptionalType (':' type)?
typeConstraints? functionBody?
;
functionLiteral
(used by
primaryExpression)
objectLiteral
(used by
primaryExpression)
thisExpression
(used by
primaryExpression)
superExpression
(used by
primaryExpression)
ifExpression
(used by
primaryExpression)
: 'if' '(' expression ')'
(controlStructureBody | (controlStructureBody? ';'? 'else' (controlStructureBody | ';')) | ';')
;
(controlStructureBody | (controlStructureBody? ';'? 'else' (controlStructureBody | ';')) | ';')
;
whenSubject
(used by
whenExpression)
whenExpression
(used by
primaryExpression)
whenEntry
(used by
whenExpression)
: whenCondition (',' whenCondition)* ','? '->' controlStructureBody semi?
| 'else' '->' controlStructureBody semi?
;
| 'else' '->' controlStructureBody semi?
;
whenCondition
(used by
whenEntry)
rangeTest
(used by
whenCondition)
typeTest
(used by
whenCondition)
tryExpression
(used by
primaryExpression)
catchBlock
(used by
tryExpression)
finallyBlock
(used by
tryExpression)
jumpExpression
(used by
primaryExpression)
: 'throw' expression
| ('return' | RETURN_AT) expression?
| 'continue'
| CONTINUE_AT
| 'break'
| BREAK_AT
;
| ('return' | RETURN_AT) expression?
| 'continue'
| CONTINUE_AT
| 'break'
| BREAK_AT
;
callableReference
(used by
primaryExpression)
assignmentAndOperator
(used by
assignment)
: '+='
| '-='
| '*='
| '/='
| '%='
;
| '-='
| '*='
| '/='
| '%='
;
equalityOperator
(used by
equality)
: '!='
| '!=='
| '=='
| '==='
;
| '!=='
| '=='
| '==='
;
comparisonOperator
(used by
comparison)
: '<'
| '>'
| '<='
| '>='
;
| '>'
| '<='
| '>='
;
inOperator
(used by
infixOperation, rangeTest)
isOperator
(used by
infixOperation, typeTest)
additiveOperator
(used by
additiveExpression)
: '+'
| '-'
;
| '-'
;
multiplicativeOperator
(used by
multiplicativeExpression)
: '*'
| '/'
| '%'
;
| '/'
| '%'
;
asOperator
(used by
asExpression)
: 'as'
| 'as?'
;
| 'as?'
;
prefixUnaryOperator
(used by
unaryPrefix)
postfixUnaryOperator
(used by
postfixUnarySuffix)
excl
(used by
prefixUnaryOperator, postfixUnaryOperator)
memberAccessOperator
(used by
navigationSuffix)
(used by
memberAccessOperator)
: '?' '.'
;
;
Modifiers
modifiers
parameterModifiers
modifier
(used by
modifiers)
typeModifiers
typeModifier
(used by
typeModifiers)
classModifier
(used by
modifier)
: 'enum'
| 'sealed'
| 'annotation'
| 'data'
| 'inner'
| 'value'
;
| 'sealed'
| 'annotation'
| 'data'
| 'inner'
| 'value'
;
memberModifier
(used by
modifier)
: 'override'
| 'lateinit'
;
| 'lateinit'
;
visibilityModifier
(used by
modifier)
: 'public'
| 'private'
| 'internal'
| 'protected'
;
| 'private'
| 'internal'
| 'protected'
;
varianceModifier
(used by
typeProjectionModifier, typeParameterModifier)
: 'in'
| 'out'
;
| 'out'
;
typeParameterModifiers
(used by
typeParameter)
typeParameterModifier
(used by
typeParameterModifiers)
functionModifier
(used by
modifier)
: 'tailrec'
| 'operator'
| 'infix'
| 'inline'
| 'external'
| 'suspend'
;
| 'operator'
| 'infix'
| 'inline'
| 'external'
| 'suspend'
;
propertyModifier
(used by
modifier)
: 'const'
;
;
inheritanceModifier
(used by
modifier)
: 'abstract'
| 'final'
| 'open'
;
| 'final'
| 'open'
;
parameterModifier
(used by
parameterModifiers, modifier)
: 'vararg'
| 'noinline'
| 'crossinline'
;
| 'noinline'
| 'crossinline'
;
reificationModifier
(used by
typeParameterModifier)
: 'reified'
;
;
platformModifier
(used by
modifier)
: 'expect'
| 'actual'
;
| 'actual'
;
Annotations
annotation
singleAnnotation
(used by
annotation)
multiAnnotation
(used by
annotation)
annotationUseSiteTarget
(used by
singleAnnotation, multiAnnotation)
: ('@' | AT_PRE_WS)
('field' | 'property' | 'get' | 'set' | 'receiver' | 'param' | 'setparam' | 'delegate') ':'
;
('field' | 'property' | 'get' | 'set' | 'receiver' | 'param' | 'setparam' | 'delegate') ':'
;
unescapedAnnotation
Identifiers
simpleIdentifier
(used by
importAlias, typeAlias, classDeclaration, classParameter, typeParameter, typeConstraint, companionObject, functionDeclaration, variableDeclaration, parameterWithOptionalType, parameter, objectDeclaration, enumEntry, simpleUserType, label, infixFunctionCall, directlyAssignableExpression, navigationSuffix, valueArgument, primaryExpression, superExpression, catchBlock, callableReference, identifier)
: Identifier
| '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'
| 'field'
| 'property'
| 'receiver'
| 'param'
| 'setparam'
| 'delegate'
| 'file'
| 'expect'
| 'actual'
| 'const'
| 'suspend'
| 'value'
;
| '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'
| 'field'
| 'property'
| 'receiver'
| 'param'
| 'setparam'
| 'delegate'
| 'file'
| 'expect'
| 'actual'
| 'const'
| 'suspend'
| 'value'
;
identifier
(used by
packageHeader, importHeader)
Lexical grammar
General
ShebangLine
(used by
shebangLine)
: '#!' ~[\r\n]*
;
;
DelimitedComment
(used by
DelimitedComment, Hidden)
LineComment
(used by
Hidden)
: ('//' ~[\r\n]*)
;
;
WS
(used by
Hidden)
: [\u0020\u0009\u000C]
;
;
helper
Hidden
Separators and operations
RESERVED
: '...'
;
;
DOUBLE_ARROW
: '=>'
;
;
DOUBLE_SEMICOLON
: ';;'
;
;
HASH
: '#'
;
;
AT_PRE_WS
SINGLE_QUOTE
: '\''
;
;
Keywords
RETURN_AT
(used by
jumpExpression)
CONTINUE_AT
(used by
jumpExpression)
BREAK_AT
(used by
jumpExpression)
THIS_AT
(used by
thisExpression)
SUPER_AT
(used by
superExpression)
TYPEOF
: 'typeof'
;
;
NOT_IS
(used by
isOperator)
NOT_IN
(used by
inOperator)
Literals
helper
DecDigits
RealLiteral
(used by
literalConstant)
FloatLiteral
(used by
RealLiteral)
DoubleLiteral
(used by
RealLiteral, FloatLiteral)
IntegerLiteral
HexLiteral
BinLiteral
UnsignedLiteral
(used by
literalConstant)
LongLiteral
(used by
literalConstant)
BooleanLiteral
(used by
literalConstant)
: 'true'
| 'false'
;
| 'false'
;
CharacterLiteral
(used by
literalConstant)
Identifiers
Identifier
(used by
simpleIdentifier, RETURN_AT, CONTINUE_AT, BREAK_AT, THIS_AT, SUPER_AT, IdentifierOrSoftKey)
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)
~
(
[\r\n]
|
'`'
|
'.'
|
';'
|
':'
|
'\'
|
'/'
|
'['
|
']'
|
'<'
|
'>'
)
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)
~
(
[\r\n]
|
'`'
)
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)
~
(
[\r\n]
|
'`'
)
IdentifierOrSoftKey
(used by
FieldIdentifier)
: Identifier
| 'abstract'
| 'annotation'
| 'by'
| 'catch'
| 'companion'
| 'constructor'
| 'crossinline'
| 'data'
| 'dynamic'
| 'enum'
| 'external'
| 'final'
| 'finally'
| 'import'
| 'infix'
| 'init'
| 'inline'
| 'inner'
| 'internal'
| 'lateinit'
| 'noinline'
| 'open'
| 'operator'
| 'out'
| 'override'
| 'private'
| 'protected'
| 'public'
| 'reified'
| 'sealed'
| 'tailrec'
| 'vararg'
| 'where'
| 'get'
| 'set'
| 'field'
| 'property'
| 'receiver'
| 'param'
| 'setparam'
| 'delegate'
| 'file'
| 'expect'
| 'actual'
| 'value'
| 'const'
| 'suspend'
;
| 'abstract'
| 'annotation'
| 'by'
| 'catch'
| 'companion'
| 'constructor'
| 'crossinline'
| 'data'
| 'dynamic'
| 'enum'
| 'external'
| 'final'
| 'finally'
| 'import'
| 'infix'
| 'init'
| 'inline'
| 'inner'
| 'internal'
| 'lateinit'
| 'noinline'
| 'open'
| 'operator'
| 'out'
| 'override'
| 'private'
| 'protected'
| 'public'
| 'reified'
| 'sealed'
| 'tailrec'
| 'vararg'
| 'where'
| 'get'
| 'set'
| 'field'
| 'property'
| 'receiver'
| 'param'
| 'setparam'
| 'delegate'
| 'file'
| 'expect'
| 'actual'
| 'value'
| 'const'
| 'suspend'
;
FieldIdentifier
(used by
LineStrRef, MultiLineStrRef)
helper
UniCharacterLiteral
(used by
EscapeSeq, LineStrEscapedChar)
helper
EscapedIdentifier
(used by
EscapeSeq, LineStrEscapedChar)
: '\\' ('t' | 'b' | 'r' | 'n' | '\'' | '"' | '\\' | '$')
;
;
Characters
helper
Letter
(used by
Identifier)
Strings
LineStrRef
(used by
lineStringContent)
See String templates
LineStrText
(used by
lineStringContent)
: ~('\\' | '"' | '$')+
| '$'
;
| '$'
;
LineStrEscapedChar
(used by
lineStringContent)
TRIPLE_QUOTE_CLOSE
(used by
multiLineStringLiteral)
: ('"'? '"""')
;
;
MultiLineStrRef
(used by
multiLineStringContent)
MultiLineStrText
(used by
multiLineStringContent)
: ~('"' | '$')+
| '$'
;
| '$'
;
ErrorCharacter
: .
;
;
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.