The notation used on this page corresponds to the ANTLR 4 notation with a few exceptions for better readability:
- omitted lexer rule actions and commands,
- omitted lexical modes.
Short description:
- operator
|
denotes alternative, - operator
*
denotes iteration (zero or more), - operator
+
denotes iteration (one or more), - operator
?
denotes option (zero or one), - operator
..
denotes range (from left to right), - operator
~
denotes negation.
Kotlin grammar source files (in ANTLR format) are located in the Kotlin specification repository:
- KotlinLexer.g4 describes lexical structure;
- UnicodeClasses.g4 describes the characters that can be used in identifiers (these rules are omitted on this page for better readability);
- KotlinParser.g4 describes syntax.
The grammar on this page corresponds to the grammar files above.
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:
- lexer rules consisting of one string literal element are inlined to the use site,
- new line tokens are excluded (new lines are not allowed in some places, see source grammar files for details).
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.
Relevant pages: Packages
start
: shebangLine? fileAnnotation* packageHeader importList topLevelObject* EOF
;
start
: shebangLine? fileAnnotation* packageHeader importList ( statement semi) * EOF
;
(used by , kotlinFile, script)
: ShebangLine
;
(used by , kotlinFile, script)
: ANNOTATION_USE_SITE_TARGET_FILE ( ( '['
unescapedAnnotation+ ']'
) | unescapedAnnotation)
;
See Packages
(used by , kotlinFile, script)
: ( 'package'
identifier semi? ) ?
;
See Imports
(used by , kotlinFile, script)
: importHeader*
;
(used by , importList)
: 'import'
identifier ( ( '.'
'*'
) | importAlias) ? semi?
;
(used by , importHeader)
: 'as'
simpleIdentifier
;
(used by , kotlinFile)
: declaration semis?
;
(used by , declaration)
: modifiers? 'typealias'
simpleIdentifier typeParameters? '='
type
;
(used by , topLevelObject, classMemberDeclaration, statement)
: classDeclaration
| objectDeclaration
| functionDeclaration
| propertyDeclaration
| typeAlias
;
(used by , declaration)
: modifiers? ( 'class'
| 'interface'
)
simpleIdentifier typeParameters?
primaryConstructor?
( ':'
delegationSpecifiers) ?
typeConstraints?
( classBody | enumClassBody) ?
;
(used by , classDeclaration)
: ( modifiers? 'constructor'
) ? classParameters
;
(used by , classDeclaration, companionObject, objectDeclaration, enumEntry, objectLiteral)
: '{'
classMemberDeclarations '}'
;
(used by , primaryConstructor)
: '('
( classParameter ( ','
classParameter) * ) ? ')'
;
(used by , classParameters)
: modifiers? ( 'val'
| 'var'
) ? simpleIdentifier ':'
type ( '='
expression) ?
;
(used by , classDeclaration, companionObject, objectDeclaration, objectLiteral)
: annotatedDelegationSpecifier ( ','
annotatedDelegationSpecifier) *
;
(used by , annotatedDelegationSpecifier)
: constructorInvocation
| explicitDelegation
| userType
| functionType
;
(used by , delegationSpecifier, unescapedAnnotation)
: userType valueArguments
;
(used by , delegationSpecifiers)
: annotation* delegationSpecifier
;
(used by , delegationSpecifier)
: ( userType | functionType) 'by'
expression
;
See Generic classes
(used by , typeAlias, classDeclaration, functionDeclaration, propertyDeclaration)
: '<'
typeParameter ( ','
typeParameter) * '>'
;
(used by , typeParameters)
: typeParameterModifiers? simpleIdentifier ( ':'
type) ?
;
(used by , classDeclaration, functionDeclaration, propertyDeclaration, anonymousFunction)
: 'where'
typeConstraint ( ','
typeConstraint) *
;
(used by , typeConstraints)
: annotation* simpleIdentifier ':'
type
;
(used by , classBody, enumClassBody)
: ( classMemberDeclaration semis? ) *
;
(used by , classMemberDeclarations)
: declaration
| companionObject
| anonymousInitializer
| secondaryConstructor
;
(used by , classMemberDeclaration)
: 'init'
block
;
(used by , classMemberDeclaration)
: modifiers? 'companion'
'object'
simpleIdentifier?
( ':'
delegationSpecifiers) ?
classBody?
;
(used by , functionDeclaration, secondaryConstructor, anonymousFunction)
: '('
( functionValueParameter ( ','
functionValueParameter) * ) ? ')'
;
(used by , functionValueParameters)
: modifiers? parameter ( '='
expression) ?
;
(used by , declaration)
: modifiers? 'fun'
typeParameters?
( receiverType '.'
) ?
simpleIdentifier functionValueParameters
( ':'
type) ? typeConstraints?
functionBody?
;
(used by , functionDeclaration, getter, setter, anonymousFunction)
: block
| '='
expression
;
(used by , multiVariableDeclaration, propertyDeclaration, forStatement, lambdaParameter, whenSubject)
: annotation* simpleIdentifier ( ':'
type) ?
;
(used by , propertyDeclaration, forStatement, lambdaParameter)
: '('
variableDeclaration ( ','
variableDeclaration) * ')'
;
(used by , declaration)
: modifiers? ( 'val'
| 'var'
) typeParameters?
( receiverType '.'
) ?
( multiVariableDeclaration | variableDeclaration)
typeConstraints?
( ( '='
expression) | propertyDelegate) ? ';'
?
( ( getter? ( semi? setter) ? ) | ( setter? ( semi? getter) ? ) )
;
(used by , propertyDeclaration)
: 'by'
expression
;
(used by , propertyDeclaration)
: modifiers? 'get'
| modifiers? 'get'
'('
')'
( ':'
type) ?
functionBody
;
(used by , propertyDeclaration)
: modifiers? 'set'
| modifiers? 'set'
'('
( annotation | parameterModifier) * setterParameter ')'
( ':'
type) ?
functionBody
;
(used by , setter)
: simpleIdentifier ( ':'
type) ?
;
(used by , functionValueParameter, functionTypeParameters)
: simpleIdentifier ':'
type
;
See Object expressions and Declarations
(used by , declaration)
: modifiers? 'object'
simpleIdentifier ( ':'
delegationSpecifiers) ? classBody?
;
(used by , classMemberDeclaration)
: modifiers? 'constructor'
functionValueParameters
( ':'
constructorDelegationCall) ? block?
;
(used by , secondaryConstructor)
: 'this'
valueArguments
| 'super'
valueArguments
;
See Enum classes
(used by , classDeclaration)
: '{'
enumEntries? ( ';'
classMemberDeclarations) ? '}'
;
(used by , enumClassBody)
: enumEntry ( ','
enumEntry) * ','
?
;
(used by , enumEntries)
: modifiers? simpleIdentifier valueArguments? classBody?
;
See Types
(used by , typeAlias, classParameter, typeParameter, typeConstraint, functionDeclaration, variableDeclaration, getter, setter, setterParameter, parameter, typeProjection, functionType, functionTypeParameters, parenthesizedType, infixOperation, asExpression, lambdaParameter, anonymousFunction, superExpression, typeTest, catchBlock)
: typeModifiers? ( parenthesizedType | nullableType | typeReference | functionType)
;
(used by , type, nullableType, receiverType)
: userType
| 'dynamic'
;
(used by , type, receiverType)
: ( typeReference | parenthesizedType) quest+
;
(used by , nullableType)
: '?'
| QUEST_WS
;
(used by , delegationSpecifier, constructorInvocation, explicitDelegation, typeReference, parenthesizedUserType, unescapedAnnotation)
: simpleUserType ( '.'
simpleUserType) *
;
(used by , userType)
: simpleIdentifier typeArguments?
;
(used by , typeArguments)
: typeProjectionModifiers? type
| '*'
;
(used by , typeProjection)
: typeProjectionModifier+
;
(used by , typeProjectionModifiers)
: varianceModifier
| annotation
;
(used by , delegationSpecifier, explicitDelegation, type)
: ( receiverType '.'
) ? functionTypeParameters '->'
type
;
(used by , functionType)
: '('
( parameter | type) ? ( ','
( parameter | type) ) * ')'
;
(used by , type, nullableType, receiverType)
: '('
type ')'
;
(used by , functionDeclaration, propertyDeclaration, functionType, callableReference)
: typeModifiers? ( parenthesizedType | nullableType | typeReference)
;
(used by , parenthesizedUserType)
: '('
userType ')'
| '('
parenthesizedUserType ')'
;
(used by , block, lambdaLiteral)
: ( statement ( semis statement) * semis? ) ?
;
(used by , script, statements, controlStructureBody)
: ( label | annotation) * ( declaration | assignment | loopStatement | expression)
;
(used by , statement, unaryPrefix, annotatedLambda)
: IdentifierAt
;
(used by , forStatement, whileStatement, doWhileStatement, ifExpression, whenEntry)
: block
| statement
;
(used by , anonymousInitializer, functionBody, secondaryConstructor, controlStructureBody, tryExpression, catchBlock, finallyBlock)
: '{'
statements '}'
;
(used by , statement)
: forStatement
| whileStatement
| doWhileStatement
;
(used by , loopStatement)
: 'for'
'('
annotation* ( variableDeclaration | multiVariableDeclaration) 'in'
expression ')'
controlStructureBody?
;
(used by , loopStatement)
: 'while'
'('
expression ')'
controlStructureBody
| 'while'
'('
expression ')'
';'
;
(used by , loopStatement)
: 'do'
controlStructureBody? 'while'
'('
expression ')'
;
(used by , statement)
: directlyAssignableExpression '='
expression
| assignableExpression assignmentAndOperator expression
;
(used by , script, packageHeader, importHeader, propertyDeclaration, whenEntry)
: EOF
;
(used by , topLevelObject, classMemberDeclarations, statements)
: EOF
;
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 | = , += , -= , *= , /= , %= |
(used by , classParameter, explicitDelegation, functionValueParameter, functionBody, propertyDeclaration, propertyDelegate, statement, forStatement, whileStatement, doWhileStatement, assignment, indexingSuffix, valueArgument, parenthesizedExpression, collectionLiteral, lineStringExpression, multiLineStringExpression, ifExpression, whenSubject, whenCondition, rangeTest, jumpExpression)
: disjunction
;
(used by , expression)
: conjunction ( '||'
conjunction) *
;
(used by , disjunction)
: equality ( '&&'
equality) *
;
(used by , conjunction)
: comparison ( equalityOperator comparison) *
;
(used by , equality)
: infixOperation ( comparisonOperator infixOperation) ?
;
(used by , comparison)
: elvisExpression ( ( inOperator elvisExpression) | ( isOperator type) ) *
;
(used by , infixOperation)
: infixFunctionCall ( elvis infixFunctionCall) *
;
(used by , elvisExpression)
: '?'
':'
;
(used by , elvisExpression)
: rangeExpression ( simpleIdentifier rangeExpression) *
;
(used by , infixFunctionCall)
: additiveExpression ( '..'
additiveExpression) *
;
(used by , rangeExpression)
: multiplicativeExpression ( additiveOperator multiplicativeExpression) *
;
(used by , additiveExpression)
: asExpression ( multiplicativeOperator asExpression) *
;
(used by , multiplicativeExpression)
: prefixUnaryExpression ( asOperator type) ?
;
(used by , asExpression, assignableExpression)
: unaryPrefix* postfixUnaryExpression
;
(used by , prefixUnaryExpression)
: annotation
| label
| prefixUnaryOperator
;
(used by , prefixUnaryExpression, directlyAssignableExpression)
: primaryExpression
| primaryExpression postfixUnarySuffix+
;
(used by , postfixUnaryExpression)
: postfixUnaryOperator
| typeArguments
| callSuffix
| indexingSuffix
| navigationSuffix
;
(used by , assignment)
: postfixUnaryExpression assignableSuffix
| simpleIdentifier
;
(used by , assignment)
: prefixUnaryExpression
;
(used by , directlyAssignableExpression)
: typeArguments
| indexingSuffix
| navigationSuffix
;
(used by , postfixUnarySuffix, assignableSuffix)
: '['
expression ( ','
expression) * ']'
;
(used by , postfixUnarySuffix, assignableSuffix)
: memberAccessOperator ( simpleIdentifier | parenthesizedExpression | 'class'
)
;
(used by , postfixUnarySuffix)
: typeArguments? valueArguments? annotatedLambda
| typeArguments? valueArguments
;
(used by , callSuffix)
: annotation* label? lambdaLiteral
;
(used by , simpleUserType, postfixUnarySuffix, assignableSuffix, callSuffix)
: '<'
typeProjection ( ','
typeProjection) * '>'
;
(used by , constructorInvocation, constructorDelegationCall, enumEntry, callSuffix)
: '('
')'
| '('
valueArgument ( ','
valueArgument) * ')'
;
(used by , valueArguments)
: annotation? ( simpleIdentifier '='
) ? '*'
? expression
;
(used by , postfixUnaryExpression)
: parenthesizedExpression
| simpleIdentifier
| literalConstant
| stringLiteral
| callableReference
| functionLiteral
| objectLiteral
| collectionLiteral
| thisExpression
| superExpression
| ifExpression
| whenExpression
| tryExpression
| jumpExpression
;
(used by , navigationSuffix, primaryExpression)
: '('
expression ')'
;
(used by , primaryExpression)
: '['
expression ( ','
expression) * ']'
| '['
']'
;
(used by , primaryExpression)
: BooleanLiteral
| IntegerLiteral
| HexLiteral
| BinLiteral
| CharacterLiteral
| RealLiteral
| 'null'
| LongLiteral
| UnsignedLiteral
;
(used by , primaryExpression)
: lineStringLiteral
| multiLineStringLiteral
;
(used by , stringLiteral)
: '"'
( lineStringContent | lineStringExpression) * '"'
;
(used by , stringLiteral)
: '"""'
( multiLineStringContent | multiLineStringExpression | '"'
) *
TRIPLE_QUOTE_CLOSE
;
(used by , lineStringLiteral)
: LineStrText
| LineStrEscapedChar
| LineStrRef
;
(used by , lineStringLiteral)
: '${'
expression '}'
;
(used by , multiLineStringLiteral)
: MultiLineStrText
| '"'
| MultiLineStrRef
;
(used by , multiLineStringLiteral)
: '${'
expression '}'
;
(used by , annotatedLambda, functionLiteral)
: '{'
statements '}'
| '{'
lambdaParameters? '->'
statements '}'
;
(used by , lambdaLiteral)
: lambdaParameter ( ','
lambdaParameter) *
;
(used by , lambdaParameters)
: variableDeclaration
| multiVariableDeclaration ( ':'
type) ?
;
(used by , functionLiteral)
: 'fun'
( type '.'
) ? functionValueParameters
( ':'
type) ? typeConstraints?
functionBody?
;
(used by , primaryExpression)
: lambdaLiteral
| anonymousFunction
;
(used by , primaryExpression)
: 'object'
':'
delegationSpecifiers classBody
| 'object'
classBody
;
(used by , primaryExpression)
: 'this'
| THIS_AT
;
(used by , primaryExpression)
: 'super'
( '<'
type '>'
) ? ( '@'
simpleIdentifier) ?
| SUPER_AT
;
(used by , primaryExpression)
: 'if'
'('
expression ')'
( controlStructureBody | ';'
)
| 'if'
'('
expression ')'
controlStructureBody? ';'
? 'else'
( controlStructureBody | ';'
)
;
(used by , whenExpression)
: '('
( annotation* 'val'
variableDeclaration '='
) ? expression ')'
;
(used by , primaryExpression)
: 'when'
whenSubject? '{'
whenEntry* '}'
;
(used by , whenExpression)
: whenCondition ( ','
whenCondition) * '->'
controlStructureBody semi?
| 'else'
'->'
controlStructureBody semi?
;
(used by , whenEntry)
: expression
| rangeTest
| typeTest
;
(used by , whenCondition)
: inOperator expression
;
(used by , whenCondition)
: isOperator type
;
(used by , primaryExpression)
: 'try'
block ( ( catchBlock+ finallyBlock? ) | finallyBlock)
;
(used by , tryExpression)
: 'catch'
'('
annotation* simpleIdentifier ':'
type ')'
block
;
(used by , tryExpression)
: 'finally'
block
;
(used by , primaryExpression)
: 'throw'
expression
| ( 'return'
| RETURN_AT) expression?
| 'continue'
| CONTINUE_AT
| 'break'
| BREAK_AT
;
(used by , primaryExpression)
: ( receiverType? '::'
( simpleIdentifier | 'class'
) )
;
(used by , assignment)
: '+='
| '-='
| '*='
| '/='
| '%='
;
(used by , equality)
: '!='
| '!=='
| '=='
| '==='
;
(used by , comparison)
: '<'
| '>'
| '<='
| '>='
;
(used by , infixOperation, rangeTest)
: 'in'
| NOT_IN
;
(used by , infixOperation, typeTest)
: 'is'
| NOT_IS
;
(used by , additiveExpression)
: '+'
| '-'
;
(used by , multiplicativeExpression)
: '*'
| '/'
| '%'
;
(used by , asExpression)
: 'as'
| 'as?'
;
(used by , unaryPrefix)
: '++'
| '--'
| '-'
| '+'
| excl
;
(used by , postfixUnarySuffix)
: '++'
| '--'
| '!'
excl
;
(used by , prefixUnaryOperator, postfixUnaryOperator)
: '!'
| EXCL_WS
;
(used by , navigationSuffix)
: '.'
| safeNav
| '::'
;
(used by , memberAccessOperator)
: '?'
'.'
;
(used by , typeAlias, classDeclaration, primaryConstructor, classParameter, companionObject, functionValueParameter, functionDeclaration, propertyDeclaration, getter, setter, objectDeclaration, secondaryConstructor, enumEntry)
: annotation
| modifier+
;
(used by , modifiers)
: classModifier
| memberModifier
| visibilityModifier
| functionModifier
| propertyModifier
| inheritanceModifier
| parameterModifier
| platformModifier
;
(used by , type, receiverType)
: typeModifier+
;
(used by , typeModifiers)
: annotation
| 'suspend'
;
(used by , modifier)
: 'enum'
| 'sealed'
| 'annotation'
| 'data'
| 'inner'
;
(used by , modifier)
: 'override'
| 'lateinit'
;
(used by , modifier)
: 'public'
| 'private'
| 'internal'
| 'protected'
;
(used by , typeProjectionModifier, typeParameterModifier)
: 'in'
| 'out'
;
(used by , typeParameter)
: typeParameterModifier+
;
(used by , typeParameterModifiers)
: reificationModifier
| varianceModifier
| annotation
;
(used by , modifier)
: 'tailrec'
| 'operator'
| 'infix'
| 'inline'
| 'external'
| 'suspend'
;
(used by , modifier)
: 'const'
;
(used by , modifier)
: 'abstract'
| 'final'
| 'open'
;
(used by , setter, modifier)
: 'vararg'
| 'noinline'
| 'crossinline'
;
(used by , typeParameterModifier)
: 'reified'
;
(used by , modifier)
: 'expect'
| 'actual'
;
(used by , annotatedDelegationSpecifier, typeConstraint, variableDeclaration, setter, typeProjectionModifier, statement, forStatement, unaryPrefix, annotatedLambda, valueArgument, whenSubject, catchBlock, modifiers, typeModifier, typeParameterModifier)
: singleAnnotation
| multiAnnotation
;
(used by , annotation)
: annotationUseSiteTarget unescapedAnnotation
| '@'
unescapedAnnotation
;
(used by , annotation)
: annotationUseSiteTarget '['
unescapedAnnotation+ ']'
| '@'
'['
unescapedAnnotation+ ']'
;
(used by , singleAnnotation, multiAnnotation)
: ANNOTATION_USE_SITE_TARGET_FIELD
| ANNOTATION_USE_SITE_TARGET_PROPERTY
| ANNOTATION_USE_SITE_TARGET_GET
| ANNOTATION_USE_SITE_TARGET_SET
| ANNOTATION_USE_SITE_TARGET_RECEIVER
| ANNOTATION_USE_SITE_TARGET_PARAM
| ANNOTATION_USE_SITE_TARGET_SETPARAM
| ANNOTATION_USE_SITE_TARGET_DELEGATE
;
(used by , fileAnnotation, singleAnnotation, multiAnnotation)
: constructorInvocation
| userType
;
(used by , importAlias, typeAlias, classDeclaration, classParameter, typeParameter, typeConstraint, companionObject, functionDeclaration, variableDeclaration, setterParameter, parameter, objectDeclaration, enumEntry, simpleUserType, 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'
| 'expect'
| 'actual'
| 'const'
| 'suspend'
;
(used by , packageHeader, importHeader)
: simpleIdentifier ( '.'
simpleIdentifier) *
;
(used by , shebangLine)
: '#!'
~ [\r\n]
*
;
(used by , DelimitedComment, Hidden)
: ( '/*'
( DelimitedComment | .
) * ? '*/'
)
;
(used by , Hidden)
: ( '//'
~ [\r\n]
* )
;
(used by , Hidden)
: [\u0020\u0009\u000C]
;
helper
Hidden
(used by , EXCL_WS, QUEST_WS, NOT_IS, NOT_IN)
: DelimitedComment
| LineComment
| WS
;
: '...'
;
(used by , excl)
: '!'
Hidden
;
: '=>'
;
: ';;'
;
: '#'
;
(used by , quest)
: '?'
Hidden
;
: '\''
;
(used by , jumpExpression)
: 'return@'
Identifier
;
(used by , jumpExpression)
: 'continue@'
Identifier
;
(used by , jumpExpression)
: 'break@'
Identifier
;
(used by , thisExpression)
: 'this@'
Identifier
;
(used by , superExpression)
: 'super@'
Identifier
;
(used by , fileAnnotation)
: '@file'
':'
;
(used by , annotationUseSiteTarget)
: '@field'
':'
;
(used by , annotationUseSiteTarget)
: '@property'
':'
;
(used by , annotationUseSiteTarget)
: '@get'
':'
;
(used by , annotationUseSiteTarget)
: '@set'
':'
;
(used by , annotationUseSiteTarget)
: '@receiver'
':'
;
(used by , annotationUseSiteTarget)
: '@param'
':'
;
(used by , annotationUseSiteTarget)
: '@setparam'
':'
;
(used by , annotationUseSiteTarget)
: '@delegate'
':'
;
: 'typeof'
;
(used by , isOperator)
: '!is'
Hidden
;
(used by , inOperator)
: '!in'
Hidden
;
helper
(used by , DecDigitOrSeparator, DecDigits, IntegerLiteral)
: '0'
..
'9'
;
helper
(used by , IntegerLiteral)
: '1'
..
'9'
;
helper
(used by , DecDigits, IntegerLiteral)
: DecDigit
| '_'
;
helper
(used by , DoubleExponent, FloatLiteral, DoubleLiteral)
: DecDigit DecDigitOrSeparator* DecDigit
| DecDigit
;
helper
(used by , DoubleLiteral)
: [eE]
[+-]
? DecDigits
;
(used by , literalConstant)
: FloatLiteral
| DoubleLiteral
;
(used by , RealLiteral)
: DoubleLiteral [fF]
| DecDigits [fF]
;
(used by , RealLiteral, FloatLiteral)
: DecDigits? '.'
DecDigits DoubleExponent?
| DecDigits DoubleExponent
;
(used by , literalConstant, UnsignedLiteral, LongLiteral)
: DecDigitNoZero DecDigitOrSeparator* DecDigit
| DecDigit
;
helper
(used by , HexDigitOrSeparator, HexLiteral, UniCharacterLiteral)
: [0-9a-fA-F]
;
helper
(used by , HexLiteral)
: HexDigit
| '_'
;
(used by , literalConstant, UnsignedLiteral, LongLiteral)
: '0'
[xX]
HexDigit HexDigitOrSeparator* HexDigit
| '0'
[xX]
HexDigit
;
helper
(used by , BinDigitOrSeparator, BinLiteral)
: [01]
;
helper
(used by , BinLiteral)
: BinDigit
| '_'
;
(used by , literalConstant, UnsignedLiteral, LongLiteral)
: '0'
[bB]
BinDigit BinDigitOrSeparator* BinDigit
| '0'
[bB]
BinDigit
;
(used by , literalConstant)
: ( IntegerLiteral | HexLiteral | BinLiteral) [uU]
'L'
?
;
(used by , literalConstant)
: ( IntegerLiteral | HexLiteral | BinLiteral) 'L'
;
(used by , literalConstant)
: 'true'
| 'false'
;
(used by , literalConstant)
: '\''
( EscapeSeq | ~ [\n\r'\\]
) '\''
;
helper
(used by , Identifier)
: UNICODE_CLASS_ND
;
(used by , simpleIdentifier, RETURN_AT, CONTINUE_AT, BREAK_AT, THIS_AT, SUPER_AT, IdentifierOrSoftKey)
: ( Letter | '_'
) ( Letter | '_'
| UnicodeDigit) *
| '`'
~ ( [\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.
~
(
[\r\n]
|
'`'
|
'.'
|
';'
|
':'
|
'\'
|
'/'
|
'['
|
']'
|
'<'
|
'>'
)
The allowed symbols are different from allowed symbols for Kotlin/JVM and correspond to the Dalvik Executable format.
~
(
[\r\n]
|
'`'
)
The allowed symbols for public declarations correspond to the ECMA specification (section 7.6) except that ECMA reserved words is allowed.
~
(
[\r\n]
|
'`'
)
(used by , IdentifierAt, FieldIdentifier)
: 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'
| 'expect'
| 'actual'
| 'const'
| 'suspend'
;
(used by , label)
: IdentifierOrSoftKey '@'
;
(used by , LineStrRef, MultiLineStrRef)
: '$'
IdentifierOrSoftKey
;
helper
(used by , EscapeSeq, LineStrEscapedChar)
: '\\'
'u'
HexDigit HexDigit HexDigit HexDigit
;
helper
(used by , EscapeSeq, LineStrEscapedChar)
: '\\'
( 't'
| 'b'
| 'r'
| 'n'
| '\''
| '"'
| '\\'
| '$'
)
;
helper
(used by , CharacterLiteral)
: UniCharacterLiteral
| EscapedIdentifier
;
helper
(used by , Identifier)
: UNICODE_CLASS_LL
| UNICODE_CLASS_LM
| UNICODE_CLASS_LO
| UNICODE_CLASS_LT
| UNICODE_CLASS_LU
| UNICODE_CLASS_NL
;
(used by , lineStringContent)
: FieldIdentifier
;
See String templates
(used by , lineStringContent)
: ~ ( '\\'
| '"'
| '$'
) +
| '$'
;
(used by , lineStringContent)
: EscapedIdentifier
| UniCharacterLiteral
;
(used by , multiLineStringLiteral)
: ( '"'
? '"""'
)
;
(used by , multiLineStringContent)
: FieldIdentifier
;
(used by , multiLineStringContent)
: ~ ( '"'
| '$'
) +
| '$'
;
: .
;