From 3ad4f18e1a5920fc6a78d8f9aae43e7e80d146d1 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 28 Feb 2017 16:25:04 +0300 Subject: [PATCH] Update grammar to Kotlin 1.1 Also drop obsolete comments and inline some trivial rules --- .../kotlin/parsing/KotlinParsing.java | 20 ++++++----- .../src/{attributes.grm => annotations.grm} | 6 ++-- grammar/src/class_members.grm | 18 +++++----- grammar/src/enum.grm | 6 ++-- grammar/src/expressions.grm | 34 ++++--------------- grammar/src/lexical.grm | 11 ++---- grammar/src/modifiers.grm | 18 ++++++---- grammar/src/toplevel.grm | 10 +++--- grammar/src/types.grm | 18 ++++------ grammar/src/when.grm | 5 ++- 10 files changed, 57 insertions(+), 89 deletions(-) rename grammar/src/{attributes.grm => annotations.grm} (92%) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java b/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java index f0f381df4e1..feaf2b9a919 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java @@ -570,7 +570,7 @@ public class KotlinParsing extends AbstractKotlinParsing { * : "@" (annotationUseSiteTarget ":")? "[" unescapedAnnotation+ "]" * ; * - * annotationUseSiteTarget + * annotationUseSiteTarget * : "file" * : "field" * : "property" @@ -1234,7 +1234,8 @@ public class KotlinParsing extends AbstractKotlinParsing { * * property * : modifiers ("val" | "var") - * typeParameters? (type "." | annotations)? + * typeParameters? + * (type ".")? * ("(" variableDeclarationEntry{","} ")" | variableDeclarationEntry) * typeConstraints * ("by" | "=" expression SEMI?)? @@ -1505,7 +1506,7 @@ public class KotlinParsing extends AbstractKotlinParsing { /* * function * : modifiers "fun" typeParameters? - * (type "." | annotations)? + * (type ".")? * SimpleName * typeParameters? functionParameters (":" type)? * typeConstraints @@ -1887,10 +1888,10 @@ public class KotlinParsing extends AbstractKotlinParsing { /* * type - * : annotations typeDescriptor + * : typeModifiers typeReference + * ; * - * typeDescriptor - * : selfType + * typeReference * : functionType * : userType * : nullableType @@ -1898,7 +1899,8 @@ public class KotlinParsing extends AbstractKotlinParsing { * ; * * nullableType - * : typeDescriptor "?" + * : typeReference "?" + * ; */ void parseTypeRef() { parseTypeRef(TokenSet.EMPTY); @@ -2165,7 +2167,7 @@ public class KotlinParsing extends AbstractKotlinParsing { /* * functionType - * : "(" (parameter | modifiers type){","}? ")" "->" type? + * : (type ".")? "(" parameter{","}? ")" "->" type? * ; */ private void parseFunctionType() { @@ -2188,7 +2190,7 @@ public class KotlinParsing extends AbstractKotlinParsing { /* * functionParameters - * : "(" functionParameter{","}? ")" // default values + * : "(" functionParameter{","}? ")" * ; * * functionParameter diff --git a/grammar/src/attributes.grm b/grammar/src/annotations.grm similarity index 92% rename from grammar/src/attributes.grm rename to grammar/src/annotations.grm index f7ff0627023..14547bc4561 100644 --- a/grammar/src/attributes.grm +++ b/grammar/src/annotations.grm @@ -15,15 +15,17 @@ annotationList ; annotationUseSiteTarget - : "file" : "field" + : "file" : "property" : "get" : "set" + : "receiver" : "param" : "setparam" + : "delegate" ; unescapedAnnotation : SimpleName{"."} typeArguments? valueArguments? - ; \ No newline at end of file + ; diff --git a/grammar/src/class_members.grm b/grammar/src/class_members.grm index af972588c7d..50d2f184201 100644 --- a/grammar/src/class_members.grm +++ b/grammar/src/class_members.grm @@ -42,28 +42,25 @@ anonymousInitializer ; companionObject - : modifiers "companion" "object" SimpleName (":" delegationSpecifier{","})? classBody? + : modifiers "companion" "object" SimpleName? (":" delegationSpecifier{","})? classBody? ; valueParameters - : "(" functionParameter{","}? ")" // default values + : "(" functionParameter{","}? ")" ; functionParameter : modifiers ("val" | "var")? parameter ("=" expression)? ; -initializer - : annotations constructorInvocation // type parameters may (must?) be omitted - ; - block : "{" statements "}" ; function - : modifiers "fun" typeParameters? - (type "." | annotations/*for receiver type*/)? + : modifiers "fun" + typeParameters? + (type ".")? SimpleName typeParameters? valueParameters (":" type)? typeConstraints @@ -85,7 +82,8 @@ multipleVariableDeclarations property : modifiers ("val" | "var") - typeParameters? (type "." | annotations)? + typeParameters? + (type ".")? (multipleVariableDeclarations | variableDeclarationEntry) typeConstraints ("by" | "=" expression SEMI?)? @@ -110,7 +108,7 @@ parameter ; object - : "object" SimpleName primaryConstructor? (":" delegationSpecifier{","})? classBody? // Class body can be optional: this is a declaration + : "object" SimpleName primaryConstructor? (":" delegationSpecifier{","})? classBody? secondaryConstructor : modifiers "constructor" valueParameters (":" constructorDelegationCall)? block diff --git a/grammar/src/enum.grm b/grammar/src/enum.grm index ad48a2ce996..e0ed2b502ba 100644 --- a/grammar/src/enum.grm +++ b/grammar/src/enum.grm @@ -8,10 +8,10 @@ enumClassBody : "{" enumEntries (";" members)? "}" ; -enumEntries (used by enumClassBody) - : (enumEntry{","} ","?)? +enumEntries + : (enumEntry{","} ","? ";"?)? ; enumEntry - : modifiers SimpleName ((":" initializer) | ("(" arguments ")"))? classBody? + : modifiers SimpleName ("(" arguments ")")? classBody? ; diff --git a/grammar/src/expressions.grm b/grammar/src/expressions.grm index f989652de81..c38df0b12a2 100644 --- a/grammar/src/expressions.grm +++ b/grammar/src/expressions.grm @@ -66,7 +66,7 @@ comparison namedInfix : elvisExpression (inOperation elvisExpression)* - : elvisExpression (isOperation isRHS)? + : elvisExpression (isOperation type)? ; elvisExpression @@ -102,7 +102,7 @@ postfixUnaryExpression : callableReference postfixUnaryOperation* ; -// TODO: callSuffix is forbidden after callableReference, since parentheses will be used to provide parameter types +// TODO: update this rule to include class literals and bound callable references callableReference : (userType "?"*)? "::" SimpleName typeArguments? ; @@ -121,7 +121,6 @@ atomicExpression : jump : loop : SimpleName - : FieldName ; labelReference @@ -158,15 +157,11 @@ longTemplate : "${" expression "}" ; -isRHS - : type - ; - declaration : function : property : class -// : typeAlias + : typeAlias : object ; @@ -215,8 +210,8 @@ assignmentOperator prefixUnaryOperation : "-" : "+" : "++" : "--" - : "!" // No ~ - : annotations // mandatory + : "!" + : annotations : labelDefinition ; @@ -255,7 +250,6 @@ jump // yield ? ; -// one can use "it" as a parameter name functionLiteral : "{" statements "}" : "{" lambdaParameter{","} "->" statements "}" @@ -279,21 +273,5 @@ arrayAccess ; objectLiteral - : "object" (":" delegationSpecifier{","})? classBody // Cannot make class body optional: foo(object : F, A) + : "object" (":" delegationSpecifier{","})? classBody ; - -/* Factory methods: - -objectLiteral - : "object" delegationSpecifier{","} ("{" objectLiteralMember{","} "}")? - ; - -objectLiteralMember - : memberDeclaration - : factoryMethod - ; - -factoryMethod - : accessModifier? SimpleName typeParameters? functionParameters functionBody - ; -*/ diff --git a/grammar/src/lexical.grm b/grammar/src/lexical.grm index 0c7a1087faa..4194b344265 100644 --- a/grammar/src/lexical.grm +++ b/grammar/src/lexical.grm @@ -7,7 +7,7 @@ Digit : ["0".."9"]; IntegerLiteral - : Digit+? + : Digit (Digit | "_")* FloatLiteral : ; @@ -17,7 +17,7 @@ HexDigit : Digit | ["A".."F", "a".."f"]; HexadecimalLiteral - : "0x" HexDigit+; + : "0x" HexDigit (HexDigit | "_")*; CharacterLiteral : ; @@ -61,13 +61,6 @@ SimpleName See [Java interoperability](java-interop.html) */ -FieldName - : "$" SimpleName; - -/* -See [Properties And Fields](properties.html) -*/ - LabelName : "@" SimpleName; diff --git a/grammar/src/modifiers.grm b/grammar/src/modifiers.grm index ae5c3ef04e3..8ab29b0dd6e 100644 --- a/grammar/src/modifiers.grm +++ b/grammar/src/modifiers.grm @@ -3,14 +3,14 @@ */ modifiers - : modifier* + : (modifier | annotations)* + ; + +typeModifiers + : (suspendModifier | annotations)* ; modifier - : modifierKeyword - ; - -modifierKeyword : classModifier : accessModifier : varianceAnnotation @@ -19,7 +19,6 @@ modifierKeyword : typeParameterModifier : functionModifier : propertyModifier - : annotations ; classModifier @@ -68,8 +67,13 @@ functionModifier : "infix" : "inline" : "external" + : suspendModifier ; propertyModifier : "const" - ; \ No newline at end of file + ; + +suspendModifier + : "suspend" + ; diff --git a/grammar/src/toplevel.grm b/grammar/src/toplevel.grm index 716d233b6a2..a2c92503a58 100644 --- a/grammar/src/toplevel.grm +++ b/grammar/src/toplevel.grm @@ -7,7 +7,7 @@ Relevant pages: [Packages](packages.html) [start] kotlinFile - : preamble toplevelObject* + : preamble topLevelObject* ; [start] @@ -43,16 +43,14 @@ import See [Imports](packages.html#imports) */ -toplevelObject +topLevelObject : class : object : function : property -// : typeAlias + : typeAlias ; -/* typeAlias - : modifiers "typealias" SimpleName (typeParameters typeConstraints)? "=" type + : modifiers "typealias" SimpleName typeParameters? "=" type ; -*/ diff --git a/grammar/src/types.grm b/grammar/src/types.grm index 402e141401e..163ea9314e0 100644 --- a/grammar/src/types.grm +++ b/grammar/src/types.grm @@ -13,13 +13,12 @@ Foo, T, Object> // user type */ type - : annotations typeDescriptor + : typeModifiers typeReference ; -// IF YOU CHANGE THIS, please, update TYPE_FIRST in JetParsing -typeDescriptor - : "(" typeDescriptor ")" -// : selfType +// If you change this, consider updating TYPE_REF_FIRST in KotlinParsing +typeReference + : "(" typeReference ")" : functionType : userType : nullableType @@ -27,13 +26,8 @@ typeDescriptor ; nullableType - : typeDescriptor "?" - -/* -selfType - : "This" + : typeReference "?" ; -*/ userType : simpleUserType{"."} @@ -48,5 +42,5 @@ optionalProjection ; functionType - : (type ".")? "(" (parameter | modifiers /*lazy out ref*/ type){","} ")" "->" type? + : (type ".")? "(" parameter{","}? ")" "->" type? ; diff --git a/grammar/src/when.grm b/grammar/src/when.grm index e8d01b78957..8b6ac5d0395 100644 --- a/grammar/src/when.grm +++ b/grammar/src/when.grm @@ -1,5 +1,5 @@ /** -#### Pattern matching +#### When-expression See [When-expression](control-flow.html#when-expression) */ @@ -10,7 +10,6 @@ when "}" ; -// TODO : consider empty after -> whenEntry : whenCondition{","} "->" controlStructureBody SEMI : "else" "->" controlStructureBody SEMI @@ -19,5 +18,5 @@ whenEntry whenCondition : expression : ("in" | "!in") expression - : ("is" | "!is") isRHS + : ("is" | "!is") type ;