Skip to content

Commit 3ad4f18

Browse files
committed
Update grammar to Kotlin 1.1
Also drop obsolete comments and inline some trivial rules
1 parent 875fdef commit 3ad4f18

File tree

10 files changed

+56
-88
lines changed

10 files changed

+56
-88
lines changed

compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ boolean parseAnnotations(AnnotationParsingMode mode) {
570570
* : "@" (annotationUseSiteTarget ":")? "[" unescapedAnnotation+ "]"
571571
* ;
572572
*
573-
* annotationUseSiteTarget
573+
* annotationUseSiteTarget
574574
* : "file"
575575
* : "field"
576576
* : "property"
@@ -1234,7 +1234,8 @@ IElementType parseTypeAlias() {
12341234
*
12351235
* property
12361236
* : modifiers ("val" | "var")
1237-
* typeParameters? (type "." | annotations)?
1237+
* typeParameters?
1238+
* (type ".")?
12381239
* ("(" variableDeclarationEntry{","} ")" | variableDeclarationEntry)
12391240
* typeConstraints
12401241
* ("by" | "=" expression SEMI?)?
@@ -1505,7 +1506,7 @@ IElementType parseFunction() {
15051506
/*
15061507
* function
15071508
* : modifiers "fun" typeParameters?
1508-
* (type "." | annotations)?
1509+
* (type ".")?
15091510
* SimpleName
15101511
* typeParameters? functionParameters (":" type)?
15111512
* typeConstraints
@@ -1887,18 +1888,19 @@ private void parseTypeParameter() {
18871888

18881889
/*
18891890
* type
1890-
* : annotations typeDescriptor
1891+
* : typeModifiers typeReference
1892+
* ;
18911893
*
1892-
* typeDescriptor
1893-
* : selfType
1894+
* typeReference
18941895
* : functionType
18951896
* : userType
18961897
* : nullableType
18971898
* : "dynamic"
18981899
* ;
18991900
*
19001901
* nullableType
1901-
* : typeDescriptor "?"
1902+
* : typeReference "?"
1903+
* ;
19021904
*/
19031905
void parseTypeRef() {
19041906
parseTypeRef(TokenSet.EMPTY);
@@ -2165,7 +2167,7 @@ boolean tryParseTypeArgumentList(TokenSet extraRecoverySet) {
21652167

21662168
/*
21672169
* functionType
2168-
* : "(" (parameter | modifiers type){","}? ")" "->" type?
2170+
* : (type ".")? "(" parameter{","}? ")" "->" type?
21692171
* ;
21702172
*/
21712173
private void parseFunctionType() {
@@ -2188,7 +2190,7 @@ private PsiBuilder.Marker parseFunctionTypeContents() {
21882190

21892191
/*
21902192
* functionParameters
2191-
* : "(" functionParameter{","}? ")" // default values
2193+
* : "(" functionParameter{","}? ")"
21922194
* ;
21932195
*
21942196
* functionParameter

grammar/src/attributes.grm renamed to grammar/src/annotations.grm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ annotationList
1515
;
1616

1717
annotationUseSiteTarget
18-
: "file"
1918
: "field"
19+
: "file"
2020
: "property"
2121
: "get"
2222
: "set"
23+
: "receiver"
2324
: "param"
2425
: "setparam"
26+
: "delegate"
2527
;
2628

2729
unescapedAnnotation
2830
: SimpleName{"."} typeArguments? valueArguments?
29-
;
31+
;

grammar/src/class_members.grm

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,25 @@ anonymousInitializer
4242
;
4343

4444
companionObject
45-
: modifiers "companion" "object" SimpleName (":" delegationSpecifier{","})? classBody?
45+
: modifiers "companion" "object" SimpleName? (":" delegationSpecifier{","})? classBody?
4646
;
4747

4848
valueParameters
49-
: "(" functionParameter{","}? ")" // default values
49+
: "(" functionParameter{","}? ")"
5050
;
5151

5252
functionParameter
5353
: modifiers ("val" | "var")? parameter ("=" expression)?
5454
;
5555

56-
initializer
57-
: annotations constructorInvocation // type parameters may (must?) be omitted
58-
;
59-
6056
block
6157
: "{" statements "}"
6258
;
6359

6460
function
65-
: modifiers "fun" typeParameters?
66-
(type "." | annotations/*for receiver type*/)?
61+
: modifiers "fun"
62+
typeParameters?
63+
(type ".")?
6764
SimpleName
6865
typeParameters? valueParameters (":" type)?
6966
typeConstraints
@@ -85,7 +82,8 @@ multipleVariableDeclarations
8582

8683
property
8784
: modifiers ("val" | "var")
88-
typeParameters? (type "." | annotations)?
85+
typeParameters?
86+
(type ".")?
8987
(multipleVariableDeclarations | variableDeclarationEntry)
9088
typeConstraints
9189
("by" | "=" expression SEMI?)?
@@ -110,7 +108,7 @@ parameter
110108
;
111109

112110
object
113-
: "object" SimpleName primaryConstructor? (":" delegationSpecifier{","})? classBody? // Class body can be optional: this is a declaration
111+
: "object" SimpleName primaryConstructor? (":" delegationSpecifier{","})? classBody?
114112

115113
secondaryConstructor
116114
: modifiers "constructor" valueParameters (":" constructorDelegationCall)? block

grammar/src/enum.grm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ enumClassBody
88
: "{" enumEntries (";" members)? "}"
99
;
1010

11-
enumEntries (used by enumClassBody)
12-
: (enumEntry{","} ","?)?
11+
enumEntries
12+
: (enumEntry{","} ","? ";"?)?
1313
;
1414

1515
enumEntry
16-
: modifiers SimpleName ((":" initializer) | ("(" arguments ")"))? classBody?
16+
: modifiers SimpleName ("(" arguments ")")? classBody?
1717
;

grammar/src/expressions.grm

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ comparison
6666

6767
namedInfix
6868
: elvisExpression (inOperation elvisExpression)*
69-
: elvisExpression (isOperation isRHS)?
69+
: elvisExpression (isOperation type)?
7070
;
7171

7272
elvisExpression
@@ -102,7 +102,7 @@ postfixUnaryExpression
102102
: callableReference postfixUnaryOperation*
103103
;
104104

105-
// TODO: callSuffix is forbidden after callableReference, since parentheses will be used to provide parameter types
105+
// TODO: update this rule to include class literals and bound callable references
106106
callableReference
107107
: (userType "?"*)? "::" SimpleName typeArguments?
108108
;
@@ -121,7 +121,6 @@ atomicExpression
121121
: jump
122122
: loop
123123
: SimpleName
124-
: FieldName
125124
;
126125

127126
labelReference
@@ -158,15 +157,11 @@ longTemplate
158157
: "${" expression "}"
159158
;
160159

161-
isRHS
162-
: type
163-
;
164-
165160
declaration
166161
: function
167162
: property
168163
: class
169-
// : typeAlias
164+
: typeAlias
170165
: object
171166
;
172167

@@ -215,8 +210,8 @@ assignmentOperator
215210
prefixUnaryOperation
216211
: "-" : "+"
217212
: "++" : "--"
218-
: "!" // No ~
219-
: annotations // mandatory
213+
: "!"
214+
: annotations
220215
: labelDefinition
221216
;
222217

@@ -255,7 +250,6 @@ jump
255250
// yield ?
256251
;
257252

258-
// one can use "it" as a parameter name
259253
functionLiteral
260254
: "{" statements "}"
261255
: "{" lambdaParameter{","} "->" statements "}"
@@ -279,21 +273,5 @@ arrayAccess
279273
;
280274

281275
objectLiteral
282-
: "object" (":" delegationSpecifier{","})? classBody // Cannot make class body optional: foo(object : F, A)
283-
;
284-
285-
/* Factory methods:
286-
287-
objectLiteral
288-
: "object" delegationSpecifier{","} ("{" objectLiteralMember{","} "}")?
276+
: "object" (":" delegationSpecifier{","})? classBody
289277
;
290-
291-
objectLiteralMember
292-
: memberDeclaration
293-
: factoryMethod
294-
;
295-
296-
factoryMethod
297-
: accessModifier? SimpleName typeParameters? functionParameters functionBody
298-
;
299-
*/

grammar/src/lexical.grm

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Digit
77
: ["0".."9"];
88

99
IntegerLiteral
10-
: Digit+?
10+
: Digit (Digit | "_")*
1111

1212
FloatLiteral
1313
: <Java double literal>;
@@ -17,7 +17,7 @@ HexDigit
1717
: Digit | ["A".."F", "a".."f"];
1818

1919
HexadecimalLiteral
20-
: "0x" HexDigit+;
20+
: "0x" HexDigit (HexDigit | "_")*;
2121

2222
CharacterLiteral
2323
: <character as in Java>;
@@ -61,13 +61,6 @@ SimpleName
6161
See [Java interoperability](java-interop.html)
6262
*/
6363

64-
FieldName
65-
: "$" SimpleName;
66-
67-
/*
68-
See [Properties And Fields](properties.html)
69-
*/
70-
7164
LabelName
7265
: "@" SimpleName;
7366

grammar/src/modifiers.grm

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
*/
44

55
modifiers
6-
: modifier*
6+
: (modifier | annotations)*
77
;
88

9-
modifier
10-
: modifierKeyword
9+
typeModifiers
10+
: (suspendModifier | annotations)*
1111
;
1212

13-
modifierKeyword
13+
modifier
1414
: classModifier
1515
: accessModifier
1616
: varianceAnnotation
@@ -19,7 +19,6 @@ modifierKeyword
1919
: typeParameterModifier
2020
: functionModifier
2121
: propertyModifier
22-
: annotations
2322
;
2423

2524
classModifier
@@ -68,8 +67,13 @@ functionModifier
6867
: "infix"
6968
: "inline"
7069
: "external"
70+
: suspendModifier
7171
;
7272

7373
propertyModifier
7474
: "const"
75-
;
75+
;
76+
77+
suspendModifier
78+
: "suspend"
79+
;

grammar/src/toplevel.grm

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Relevant pages: [Packages](packages.html)
77

88
[start]
99
kotlinFile
10-
: preamble toplevelObject*
10+
: preamble topLevelObject*
1111
;
1212

1313
[start]
@@ -43,16 +43,14 @@ import
4343
See [Imports](packages.html#imports)
4444
*/
4545

46-
toplevelObject
46+
topLevelObject
4747
: class
4848
: object
4949
: function
5050
: property
51-
// : typeAlias
51+
: typeAlias
5252
;
5353

54-
/*
5554
typeAlias
56-
: modifiers "typealias" SimpleName (typeParameters typeConstraints)? "=" type
55+
: modifiers "typealias" SimpleName typeParameters? "=" type
5756
;
58-
*/

grammar/src/types.grm

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,21 @@ Foo<Bar<X>, T, Object> // user type
1313
*/
1414

1515
type
16-
: annotations typeDescriptor
16+
: typeModifiers typeReference
1717
;
1818

19-
// IF YOU CHANGE THIS, please, update TYPE_FIRST in JetParsing
20-
typeDescriptor
21-
: "(" typeDescriptor ")"
22-
// : selfType
19+
// If you change this, consider updating TYPE_REF_FIRST in KotlinParsing
20+
typeReference
21+
: "(" typeReference ")"
2322
: functionType
2423
: userType
2524
: nullableType
2625
: "dynamic"
2726
;
2827

2928
nullableType
30-
: typeDescriptor "?"
31-
32-
/*
33-
selfType
34-
: "This"
29+
: typeReference "?"
3530
;
36-
*/
3731

3832
userType
3933
: simpleUserType{"."}
@@ -48,5 +42,5 @@ optionalProjection
4842
;
4943

5044
functionType
51-
: (type ".")? "(" (parameter | modifiers /*lazy out ref*/ type){","} ")" "->" type?
45+
: (type ".")? "(" parameter{","}? ")" "->" type?
5246
;

0 commit comments

Comments
 (0)