Version 1.9-rfc+0.1
kotlin.Any
kotlin.Nothing
kotlin.Unit
kotlin.Boolean
kotlin.Char
kotlin.String
kotlin.Enum
kotlin.Throwable
kotlin.Comparable
kotlin.Function
kotlin.annotation.Retention
kotlin.annotation.Target
kotlin.annotation.Repeatable
kotlin.RequiresOptIn
/ kotlin.OptIn
kotlin.Deprecated
/ kotlin.ReplaceWith
kotlin.Suppress
kotlin.SinceKotlin
kotlin.UnsafeVariance
kotlin.DslMarker
kotlin.PublishedApi
kotlin.BuilderInference
kotlin.RestrictSuspension
kotlin.OverloadResolutionByLambdaReturnType
This section uses a BNF-based notation similar to EBNF with the following conventions:
Rule names starting with capital letters denote lexical rules, while rule names starting with lowercase letters denote syntactic rules.
Note: this notation is similar to ISO EBNF as per standard ISO/IEC 14977, but does not employ any special symbols for concatenation or termination and does not use some of the additional notation symbols
'#!'
{<any character excluding CR and LF >}
'/*'
{ DelimitedComment | <any character> } '*/'
'//'
{<any character excluding CR and LF >}
'...'
'.'
','
'('
')'
'['
']'
'{'
'}'
'*'
'%'
'/'
'+'
'-'
'++'
'--'
'&&'
'||'
'!'
Hidden
'!'
':'
';'
'='
'+='
'-='
'*='
'/='
'%='
'->'
'=>'
'..'
'::'
';;'
'#'
'@'
'?'
Hidden
'?'
'<'
'>'
'<='
'>='
'!='
'!=='
'as?'
'=='
'==='
'\''
'return@'
Identifier
'continue@'
Identifier
'break@'
Identifier
'this@'
Identifier
'super@'
Identifier
'file'
'field'
'property'
'get'
'set'
'receiver'
'param'
'setparam'
'delegate'
'package'
'import'
'class'
'interface'
'fun'
'object'
'val'
'var'
'typealias'
'constructor'
'by'
'companion'
'init'
'this'
'super'
'typeof'
'where'
'if'
'else'
'when'
'try'
'catch'
'finally'
'for'
'do'
'while'
'throw'
'return'
'continue'
'break'
'as'
'is'
'in'
'out'
'dynamic'
'public'
'private'
'protected'
'internal'
'enum'
'sealed'
'annotation'
'data'
'inner'
'tailrec'
'operator'
'inline'
'infix'
'external'
'suspend'
'override'
'abstract'
'final'
'open'
'const'
'lateinit'
'vararg'
'noinline'
'crossinline'
'reified'
'expect'
'actual'
'1'
| '2'
| '3'
| '4'
| '5'
| '6'
| '7'
| '8'
| '9'
'0'
| '1'
| '2'
| '3'
| '4'
| '5'
| '6'
| '7'
| '8'
| '9'
'_'
'e'
| 'E'
) [('+'
| '-'
)] DecDigits
'f'
| 'F'
)'f'
| 'F'
)
'.'
DecDigits [DoubleExponent]'A'
| 'B'
| 'C'
| 'D'
| 'E'
| 'F'
'a'
| 'b'
| 'c'
| 'd'
| 'e'
| 'f'
'_'
'0'
('x'
| 'X'
) HexDigit {HexDigitOrSeparator} HexDigit'0'
('x'
| 'X'
) HexDigit
'0'
| '1'
'_'
'0'
('b'
| 'B'
) BinDigit {BinDigitOrSeparator} BinDigit'0'
('b'
| 'B'
) BinDigit
'u'
| 'U'
) ['L'
]
'L'
'true'
| 'false'
'null'
'''
(EscapeSeq | <any character excluding CR, LF, '''
or '\'
>) '''
'\'
('t'
| 'b'
| 'r'
| 'n'
| '''
| '"'
| '\'
| '$'
)
'`'
>
'_'
) {Letter | '_'
| UnicodeDigit}'`'
QuotedSymbol {QuotedSymbol} '`'
Kotlin supports escaping identifiers by enclosing any sequence of characters into backtick (`
) characters, allowing to use any name as an identifier. This allows not only using non-alphanumeric characters (like @
or #
) in names, but also using keywords like if
or when
as identifiers. Actual set of characters that is allowed to be escaped may, however, be a subject to platform restrictions. Consult particular platform sections for details.
Note: for example, the following characters are not allowed in identifiers used as declaration names on the JVM platform even when escaped due to JVM restrictions:
.
,;
,[
,]
,/
,<
,>
,:
,\\
.
Escaped identifiers are treated the same as corresponding non-escaped identifier if it is allowed. For example, an escaped identifier `foo`
and non-escaped identifier foo
may be used interchangeably and refer to the same program entity.
Some of the keywords used in Kotlin are allowed to be used as identifiers even when not escaped. Such keywords are called soft keywords. You can see the complete list of soft keyword in the rule above. All other keywords are considered hard keywords and may only be used as identifiers if escaped.
Note: for example, this is a valid property declaration in Kotlin:
val field = 2
even though
field
is a keyword
'"'
'"""'
'$'
IdentifierOrSoftKey
Opening a double quote (QUOTE_OPEN) rule puts the lexical grammar into the special “line string” mode with the following rules. Closing double quote (QUOTE_CLOSE) rule exits this mode.
'"'
'\'
, '"'
or '$'
>} | '$'
'${'
Opening a triple double quote (TRIPLE_QUOTE_OPEN) rule puts the lexical grammar into the special “multiline string” mode with the following rules. Closing triple double quote (TRIPLE_QUOTE_CLOSE) rule exits this mode.
'"""'
'"""'
{'"'
}
'"'
or '$'
>} | '$'
'${'
These are all the valid tokens in one rule. Note that syntax grammar ignores tokens DelimitedComment, LineComment and WS.
The grammar below replaces some lexical grammar rules with explicit literals (where such replacement in trivial and always correct, for example, for keywords) for better readability.
'file'
':'
'['
(unescapedAnnotation {unescapedAnnotation}) ']'
) | unescapedAnnotation)'package'
identifier [semi]]
'import'
identifier [('.'
'*'
) | importAlias] [semi]
'as'
simpleIdentifier
'typealias'
'='
'class'
| (['fun'
{NL}] 'interface'
))':'
{NL} delegationSpecifiers]'constructor'
{NL}] classParameters
'{'
'}'
'('
','
{NL} classParameter} [{NL} ','
]]')'
'val'
| 'var'
]':'
'='
{NL} expression]
','
{NL} annotatedDelegationSpecifier}
'suspend'
{NL} functionType)
'by'
'<'
','
{NL} typeParameter}','
]'>'
':'
{NL} type]
'where'
{NL} typeConstraint {{NL} ','
{NL} typeConstraint}
':'
'companion'
'data'
]'object'
':'
{NL} delegationSpecifiers]'('
','
{NL} functionValueParameter} [{NL} ','
]]')'
'='
{NL} expression]
'fun'
'.'
]':'
{NL} type]'='
{NL} expression)
':'
{NL} type]
'('
','
{NL} variableDeclaration}','
]')'
'val'
| 'var'
)'.'
]'='
{NL} expression) | propertyDelegate)]';'
]'by'
{NL} expression
'('
','
{NL} functionValueParameterWithOptionalType} [{NL} ','
]]')'
'='
{NL} expression]
':'
{NL} type]
':'
'object'
':'
{NL} delegationSpecifiers]'constructor'
':'
{NL} constructorDelegationCall]'this'
| 'super'
) {NL} valueArguments
'{'
';'
{NL} classMemberDeclarations]'}'
'dynamic'
'.'
{NL} simpleUserType}
'*'
'.'
{NL}]'->'
'('
')'
'&'
'{'
'}'
'for'
'('
'in'
')'
'while'
'('
')'
';'
)
'do'
'while'
'('
')'
'='
) | (assignableExpression assignmentAndOperator)) {NL} expression
'||'
{NL} conjunction}
':'
'..'
| '..<'
) {NL} additiveExpression}
'('
')'
'('
')'
'['
','
{NL} expression}','
]']'
'<'
','
{NL} typeProjection}','
]'>'
'('
{NL} [valueArgument {{NL} ','
{NL} valueArgument} [{NL} ','
] {NL}] ')'
'='
{NL}]'*'
]'('
')'
'['
{NL} [expression {{NL} ','
{NL} expression} [{NL} ','
] {NL}] ']'
'null'
'"'
{lineStringContent | lineStringExpression} '"'
'"""'
{multiLineStringContent | multiLineStringExpression | '"'
} TRIPLE_QUOTE_CLOSE
'${'
'}'
'"'
'${'
'}'
'{'
'->'
{NL}]'}'
','
{NL} lambdaParameter} [{NL} ','
]
':'
{NL} type])
'suspend'
]'fun'
'.'
]':'
{NL} type]'this'
'if'
'('
')'
';'
] {NL} 'else'
{NL} (controlStructureBody | ';'
)) | ';'
)
'('
[{annotation} {NL} 'val'
{NL} variableDeclaration {NL} '='
{NL}] expression ')'
','
{NL} whenCondition} [{NL} ','
] {NL} '->'
{NL} controlStructureBody [semi])'else'
{NL} '->'
{NL} controlStructureBody [semi])
'try'
{NL} block ((({NL} catchBlock {{NL} catchBlock}) [{NL} finallyBlock]) | ({NL} finallyBlock))
'catch'
'('
':'
','
]')'
'throw'
{NL} expression)'return'
| RETURN_AT) [expression])'continue'
'break'
'::'
{NL} (simpleIdentifier | 'class'
)
'+='
'-='
'*='
'/='
'%='
'!='
'!=='
'=='
'==='
'<'
'>'
'<='
'>='
'in'
'is'
'+'
'-'
'*'
'/'
'%'
'as'
'as?'
'++'
'--'
'-'
'+'
'++'
'--'
'!'
excl)
'!'
'suspend'
{NL})
'enum'
'sealed'
'annotation'
'data'
'inner'
'value'
'override'
'lateinit'
'public'
'private'
'internal'
'protected'
'in'
'out'
'tailrec'
'operator'
'infix'
'inline'
'external'
'suspend'
'const'
'abstract'
'final'
'open'
'vararg'
'noinline'
'crossinline'
'reified'
'expect'
'actual'
'['
(unescapedAnnotation {unescapedAnnotation}) ']'
'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'
'.'
simpleIdentifier}