From bc187820d471e70166d362fe7aacc7c10fbe4063 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sat, 18 Dec 2021 21:35:33 +0100 Subject: [PATCH] [orx-kotlin-parser] Update kotlin antlr grammars --- .../src/main/antlr/KotlinLexer.g4 | 11 +- .../src/main/antlr/KotlinLexer.tokens | 152 +++++++-------- .../src/main/antlr/KotlinParser.g4 | 180 +++++++++--------- .../src/main/antlr/README.md | 1 + 4 files changed, 172 insertions(+), 172 deletions(-) create mode 100644 orx-jvm/orx-kotlin-parser/src/main/antlr/README.md diff --git a/orx-jvm/orx-kotlin-parser/src/main/antlr/KotlinLexer.g4 b/orx-jvm/orx-kotlin-parser/src/main/antlr/KotlinLexer.g4 index fe81474d..403f1e90 100755 --- a/orx-jvm/orx-kotlin-parser/src/main/antlr/KotlinLexer.g4 +++ b/orx-jvm/orx-kotlin-parser/src/main/antlr/KotlinLexer.g4 @@ -2,12 +2,8 @@ * Kotlin lexical grammar in ANTLR4 notation */ - - lexer grammar KotlinLexer; - - import UnicodeClasses; // SECTION: lexicalGeneral @@ -160,6 +156,7 @@ SEALED: 'sealed'; ANNOTATION: 'annotation'; DATA: 'data'; INNER: 'inner'; +VALUE: 'value'; TAILREC: 'tailrec'; OPERATOR: 'operator'; INLINE: 'inline'; @@ -229,11 +226,11 @@ BinLiteral ; UnsignedLiteral - : (IntegerLiteral | HexLiteral | BinLiteral) [uU] 'L'? + : (IntegerLiteral | HexLiteral | BinLiteral) [uU] [lL]? ; LongLiteral - : (IntegerLiteral | HexLiteral | BinLiteral) 'L' + : (IntegerLiteral | HexLiteral | BinLiteral) [lL] ; BooleanLiteral: 'true'| 'false'; @@ -300,6 +297,7 @@ IdentifierOrSoftKey | FILE | EXPECT | ACTUAL + | VALUE /* Strong keywords */ | CONST | SUSPEND @@ -488,6 +486,7 @@ Inside_SEALED: SEALED -> type(SEALED); Inside_ANNOTATION: ANNOTATION -> type(ANNOTATION); Inside_DATA: DATA -> type(DATA); Inside_INNER: INNER -> type(INNER); +Inside_VALUE: VALUE -> type(VALUE); Inside_TAILREC: TAILREC -> type(TAILREC); Inside_OPERATOR: OPERATOR -> type(OPERATOR); Inside_INLINE: INLINE -> type(INLINE); diff --git a/orx-jvm/orx-kotlin-parser/src/main/antlr/KotlinLexer.tokens b/orx-jvm/orx-kotlin-parser/src/main/antlr/KotlinLexer.tokens index 8e9b4a58..a207d69c 100644 --- a/orx-jvm/orx-kotlin-parser/src/main/antlr/KotlinLexer.tokens +++ b/orx-jvm/orx-kotlin-parser/src/main/antlr/KotlinLexer.tokens @@ -113,61 +113,62 @@ SEALED=112 ANNOTATION=113 DATA=114 INNER=115 -TAILREC=116 -OPERATOR=117 -INLINE=118 -INFIX=119 -EXTERNAL=120 -SUSPEND=121 -OVERRIDE=122 -ABSTRACT=123 -FINAL=124 -OPEN=125 -CONST=126 -LATEINIT=127 -VARARG=128 -NOINLINE=129 -CROSSINLINE=130 -REIFIED=131 -EXPECT=132 -ACTUAL=133 -RealLiteral=134 -FloatLiteral=135 -DoubleLiteral=136 -IntegerLiteral=137 -HexLiteral=138 -BinLiteral=139 -UnsignedLiteral=140 -LongLiteral=141 -BooleanLiteral=142 -NullLiteral=143 -CharacterLiteral=144 -Identifier=145 -IdentifierOrSoftKey=146 -FieldIdentifier=147 -QUOTE_OPEN=148 -TRIPLE_QUOTE_OPEN=149 -UNICODE_CLASS_LL=150 -UNICODE_CLASS_LM=151 -UNICODE_CLASS_LO=152 -UNICODE_CLASS_LT=153 -UNICODE_CLASS_LU=154 -UNICODE_CLASS_ND=155 -UNICODE_CLASS_NL=156 -QUOTE_CLOSE=157 -LineStrRef=158 -LineStrText=159 -LineStrEscapedChar=160 -LineStrExprStart=161 -TRIPLE_QUOTE_CLOSE=162 -MultiLineStringQuote=163 -MultiLineStrRef=164 -MultiLineStrText=165 -MultiLineStrExprStart=166 -Inside_Comment=167 -Inside_WS=168 -Inside_NL=169 -ErrorCharacter=170 +VALUE=116 +TAILREC=117 +OPERATOR=118 +INLINE=119 +INFIX=120 +EXTERNAL=121 +SUSPEND=122 +OVERRIDE=123 +ABSTRACT=124 +FINAL=125 +OPEN=126 +CONST=127 +LATEINIT=128 +VARARG=129 +NOINLINE=130 +CROSSINLINE=131 +REIFIED=132 +EXPECT=133 +ACTUAL=134 +RealLiteral=135 +FloatLiteral=136 +DoubleLiteral=137 +IntegerLiteral=138 +HexLiteral=139 +BinLiteral=140 +UnsignedLiteral=141 +LongLiteral=142 +BooleanLiteral=143 +NullLiteral=144 +CharacterLiteral=145 +Identifier=146 +IdentifierOrSoftKey=147 +FieldIdentifier=148 +QUOTE_OPEN=149 +TRIPLE_QUOTE_OPEN=150 +UNICODE_CLASS_LL=151 +UNICODE_CLASS_LM=152 +UNICODE_CLASS_LO=153 +UNICODE_CLASS_LT=154 +UNICODE_CLASS_LU=155 +UNICODE_CLASS_ND=156 +UNICODE_CLASS_NL=157 +QUOTE_CLOSE=158 +LineStrRef=159 +LineStrText=160 +LineStrEscapedChar=161 +LineStrExprStart=162 +TRIPLE_QUOTE_CLOSE=163 +MultiLineStringQuote=164 +MultiLineStrRef=165 +MultiLineStrText=166 +MultiLineStrExprStart=167 +Inside_Comment=168 +Inside_WS=169 +Inside_NL=170 +ErrorCharacter=171 '...'=6 '.'=7 ','=8 @@ -266,23 +267,24 @@ ErrorCharacter=170 'annotation'=113 'data'=114 'inner'=115 -'tailrec'=116 -'operator'=117 -'inline'=118 -'infix'=119 -'external'=120 -'suspend'=121 -'override'=122 -'abstract'=123 -'final'=124 -'open'=125 -'const'=126 -'lateinit'=127 -'vararg'=128 -'noinline'=129 -'crossinline'=130 -'reified'=131 -'expect'=132 -'actual'=133 -'null'=143 -'"""'=149 +'value'=116 +'tailrec'=117 +'operator'=118 +'inline'=119 +'infix'=120 +'external'=121 +'suspend'=122 +'override'=123 +'abstract'=124 +'final'=125 +'open'=126 +'const'=127 +'lateinit'=128 +'vararg'=129 +'noinline'=130 +'crossinline'=131 +'reified'=132 +'expect'=133 +'actual'=134 +'null'=144 +'"""'=150 diff --git a/orx-jvm/orx-kotlin-parser/src/main/antlr/KotlinParser.g4 b/orx-jvm/orx-kotlin-parser/src/main/antlr/KotlinParser.g4 index f994a84b..faffca3a 100644 --- a/orx-jvm/orx-kotlin-parser/src/main/antlr/KotlinParser.g4 +++ b/orx-jvm/orx-kotlin-parser/src/main/antlr/KotlinParser.g4 @@ -65,10 +65,10 @@ declaration classDeclaration : modifiers? (CLASS | (FUN NL*)? INTERFACE) NL* simpleIdentifier - (NL* typeParameters)? (NL* primaryConstructor)? - (NL* COLON NL* delegationSpecifiers)? - (NL* typeConstraints)? - (NL* classBody | NL* enumClassBody)? + (NL* typeParameters)? (NL* primaryConstructor)? + (NL* COLON NL* delegationSpecifiers)? + (NL* typeConstraints)? + (NL* classBody | NL* enumClassBody)? ; primaryConstructor @@ -145,9 +145,9 @@ anonymousInitializer companionObject : modifiers? COMPANION NL* OBJECT - (NL* simpleIdentifier)? - (NL* COLON NL* delegationSpecifiers)? - (NL* classBody)? + (NL* simpleIdentifier)? + (NL* COLON NL* delegationSpecifiers)? + (NL* classBody)? ; functionValueParameters @@ -160,11 +160,11 @@ functionValueParameter functionDeclaration : modifiers? - FUN (NL* typeParameters)? (NL* receiverType NL* DOT)? NL* simpleIdentifier - NL* functionValueParameters - (NL* COLON NL* type)? - (NL* typeConstraints)? - (NL* functionBody)? + FUN (NL* typeParameters)? (NL* receiverType NL* DOT)? NL* simpleIdentifier + NL* functionValueParameters + (NL* COLON NL* type)? + (NL* typeConstraints)? + (NL* functionBody)? ; functionBody @@ -182,12 +182,12 @@ multiVariableDeclaration propertyDeclaration : modifiers? (VAL | VAR) - (NL* typeParameters)? - (NL* receiverType NL* DOT)? - (NL* (multiVariableDeclaration | variableDeclaration)) - (NL* typeConstraints)? - (NL* (ASSIGNMENT NL* expression | propertyDelegate))? - (NL+ SEMICOLON)? NL* (getter? (NL* semi? setter)? | setter? (NL* semi? getter)?) + (NL* typeParameters)? + (NL* receiverType NL* DOT)? + (NL* (multiVariableDeclaration | variableDeclaration)) + (NL* typeConstraints)? + (NL* (ASSIGNMENT NL* expression | propertyDelegate))? + (NL+ SEMICOLON)? NL* (getter? (NL* semi? setter)? | setter? (NL* semi? getter)?) ; propertyDelegate @@ -196,20 +196,24 @@ propertyDelegate getter : modifiers? GET - | modifiers? GET NL* LPAREN NL* RPAREN (NL* COLON NL* type)? NL* functionBody + (NL* LPAREN NL* RPAREN (NL* COLON NL* type)? NL* functionBody)? ; setter : modifiers? SET - | modifiers? SET NL* LPAREN NL* parameterWithOptionalType (NL* COMMA)? NL* RPAREN (NL* COLON NL* type)? NL* functionBody + (NL* LPAREN NL* functionValueParameterWithOptionalType (NL* COMMA)? NL* RPAREN (NL* COLON NL* type)? NL* functionBody)? ; parametersWithOptionalType - : LPAREN NL* (parameterWithOptionalType (NL* COMMA NL* parameterWithOptionalType)* (NL* COMMA)?)? NL* RPAREN + : LPAREN NL* (functionValueParameterWithOptionalType (NL* COMMA NL* functionValueParameterWithOptionalType)* (NL* COMMA)?)? NL* RPAREN + ; + +functionValueParameterWithOptionalType + : parameterModifiers? parameterWithOptionalType (NL* ASSIGNMENT NL* expression)? ; parameterWithOptionalType - : parameterModifiers? simpleIdentifier NL* (COLON NL* type)? + : simpleIdentifier NL* (COLON NL* type)? ; parameter @@ -218,9 +222,9 @@ parameter objectDeclaration : modifiers? OBJECT - NL* simpleIdentifier - (NL* COLON NL* delegationSpecifiers)? - (NL* classBody)? + NL* simpleIdentifier + (NL* COLON NL* delegationSpecifiers)? + (NL* classBody)? ; secondaryConstructor @@ -228,8 +232,7 @@ secondaryConstructor ; constructorDelegationCall - : THIS NL* valueArguments - | SUPER NL* valueArguments + : (THIS | SUPER) NL* valueArguments ; // SECTION: enumClasses @@ -249,11 +252,7 @@ enumEntry // SECTION: types type - : typeModifiers? - ( parenthesizedType - | nullableType - | typeReference - | functionType) + : typeModifiers? (parenthesizedType | nullableType | typeReference | functionType) ; typeReference @@ -279,7 +278,8 @@ simpleUserType ; typeProjection - : typeProjectionModifiers? type | MULT + : typeProjectionModifiers? type + | MULT ; typeProjectionModifiers @@ -304,15 +304,11 @@ parenthesizedType ; receiverType - : typeModifiers? - ( parenthesizedType - | nullableType - | typeReference) + : typeModifiers? (parenthesizedType | nullableType | typeReference) ; parenthesizedUserType - : LPAREN NL* userType NL* RPAREN - | LPAREN NL* parenthesizedUserType NL* RPAREN + : LPAREN NL* (userType | parenthesizedUserType) NL* RPAREN ; // SECTION: statements @@ -322,11 +318,7 @@ statements ; statement - : (label | annotation)* - ( declaration - | assignment - | loopStatement - | expression) + : (label | annotation)* ( declaration | assignment | loopStatement | expression) ; label @@ -349,12 +341,12 @@ loopStatement ; forStatement - : FOR NL* LPAREN annotation* (variableDeclaration | multiVariableDeclaration) IN expression RPAREN NL* controlStructureBody? + : FOR NL* LPAREN annotation* (variableDeclaration | multiVariableDeclaration) + IN expression RPAREN NL* controlStructureBody? ; whileStatement - : WHILE NL* LPAREN expression RPAREN NL* controlStructureBody - | WHILE NL* LPAREN expression RPAREN NL* SEMICOLON + : WHILE NL* LPAREN expression RPAREN NL* (controlStructureBody | SEMICOLON) ; doWhileStatement @@ -362,13 +354,13 @@ doWhileStatement ; assignment - : directlyAssignableExpression ASSIGNMENT NL* expression - | assignableExpression assignmentAndOperator NL* expression + : (directlyAssignableExpression ASSIGNMENT | assignableExpression assignmentAndOperator) NL* expression ; semi : (SEMICOLON | NL) NL* - | EOF; + | EOF + ; semis : (SEMICOLON | NL)+ @@ -394,7 +386,11 @@ equality ; comparison - : infixOperation (comparisonOperator NL* infixOperation)? + : genericCallLikeComparison (comparisonOperator NL* genericCallLikeComparison)* + ; + +genericCallLikeComparison + : infixOperation callSuffix* ; infixOperation @@ -426,11 +422,7 @@ multiplicativeExpression ; asExpression - : comparisonWithLiteralRightSide (NL* asOperator NL* type)? - ; - -comparisonWithLiteralRightSide - : prefixUnaryExpression (NL* LANGLE NL* literalConstant NL* RANGLE NL* (expression | parenthesizedExpression))* + : prefixUnaryExpression (NL* asOperator NL* type)* ; prefixUnaryExpression @@ -444,8 +436,7 @@ unaryPrefix ; postfixUnaryExpression - : primaryExpression - | primaryExpression postfixUnarySuffix+ + : primaryExpression postfixUnarySuffix* ; postfixUnarySuffix @@ -467,7 +458,8 @@ parenthesizedDirectlyAssignableExpression ; assignableExpression - : prefixUnaryExpression | parenthesizedAssignableExpression + : prefixUnaryExpression + | parenthesizedAssignableExpression ; parenthesizedAssignableExpression @@ -485,12 +477,11 @@ indexingSuffix ; navigationSuffix - : NL* memberAccessOperator NL* (simpleIdentifier | parenthesizedExpression | CLASS) + : memberAccessOperator NL* (simpleIdentifier | parenthesizedExpression | CLASS) ; callSuffix - : typeArguments? valueArguments? annotatedLambda - | typeArguments? valueArguments + : typeArguments? (valueArguments? annotatedLambda | valueArguments) ; annotatedLambda @@ -502,8 +493,7 @@ typeArguments ; valueArguments - : LPAREN NL* RPAREN - | LPAREN NL* valueArgument (NL* COMMA NL* valueArgument)* (NL* COMMA)? NL* RPAREN + : LPAREN NL* (valueArgument (NL* COMMA NL* valueArgument)* (NL* COMMA)? NL*)? RPAREN ; valueArgument @@ -532,8 +522,7 @@ parenthesizedExpression ; collectionLiteral - : LSQUARE NL* expression (NL* COMMA NL* expression)* (NL* COMMA)? NL* RSQUARE - | LSQUARE NL* RSQUARE + : LSQUARE NL* (expression (NL* COMMA NL* expression)* (NL* COMMA)? NL*)? RSQUARE ; literalConstant @@ -568,7 +557,7 @@ lineStringContent ; lineStringExpression - : LineStrExprStart expression RCURL + : LineStrExprStart NL* expression NL* RCURL ; multiLineStringContent @@ -582,8 +571,7 @@ multiLineStringExpression ; lambdaLiteral - : LCURL NL* statements NL* RCURL - | LCURL NL* lambdaParameters? NL* ARROW NL* statements NL* RCURL + : LCURL NL* (lambdaParameters? NL* ARROW NL*)? statements NL* RCURL ; lambdaParameters @@ -597,11 +585,11 @@ lambdaParameter anonymousFunction : FUN - (NL* type NL* DOT)? - NL* parametersWithOptionalType - (NL* COLON NL* type)? - (NL* typeConstraints)? - (NL* functionBody)? + (NL* type NL* DOT)? + NL* parametersWithOptionalType + (NL* COLON NL* type)? + (NL* typeConstraints)? + (NL* functionBody)? ; functionLiteral @@ -610,8 +598,7 @@ functionLiteral ; objectLiteral - : OBJECT NL* COLON NL* delegationSpecifiers NL* classBody - | OBJECT NL* classBody + : OBJECT (NL* COLON NL* delegationSpecifiers NL*)? (NL* classBody)? ; thisExpression @@ -625,8 +612,10 @@ superExpression ; ifExpression - : IF NL* LPAREN NL* expression NL* RPAREN NL* (controlStructureBody | SEMICOLON) - | IF NL* LPAREN NL* expression NL* RPAREN NL* controlStructureBody? NL* SEMICOLON? NL* ELSE NL* (controlStructureBody | SEMICOLON) + : IF NL* LPAREN NL* expression NL* RPAREN NL* + ( controlStructureBody + | controlStructureBody? NL* SEMICOLON? NL* ELSE NL* (controlStructureBody | SEMICOLON) + | SEMICOLON) ; whenSubject @@ -671,12 +660,14 @@ finallyBlock jumpExpression : THROW NL* expression | (RETURN | RETURN_AT) expression? - | CONTINUE | CONTINUE_AT - | BREAK | BREAK_AT + | CONTINUE + | CONTINUE_AT + | BREAK + | BREAK_AT ; callableReference - : (receiverType? NL* COLONCOLON NL* (simpleIdentifier | CLASS)) + : receiverType? COLONCOLON NL* (simpleIdentifier | CLASS) ; assignmentAndOperator @@ -702,15 +693,18 @@ comparisonOperator ; inOperator - : IN | NOT_IN + : IN + | NOT_IN ; isOperator - : IS | NOT_IS + : IS + | NOT_IS ; additiveOperator - : ADD | SUB + : ADD + | SUB ; multiplicativeOperator @@ -744,7 +738,9 @@ excl ; memberAccessOperator - : DOT | safeNav | COLONCOLON + : NL* DOT + | NL* safeNav + | COLONCOLON ; safeNav @@ -777,7 +773,8 @@ typeModifiers ; typeModifier - : annotation | SUSPEND NL* + : annotation + | SUSPEND NL* ; classModifier @@ -786,6 +783,7 @@ classModifier | ANNOTATION | DATA | INNER + | VALUE ; memberModifier @@ -856,13 +854,11 @@ annotation ; singleAnnotation - : annotationUseSiteTarget NL* unescapedAnnotation - | (AT_NO_WS | AT_PRE_WS) unescapedAnnotation + : (annotationUseSiteTarget NL* | AT_NO_WS | AT_PRE_WS) unescapedAnnotation ; multiAnnotation - : annotationUseSiteTarget NL* LSQUARE unescapedAnnotation+ RSQUARE - | (AT_NO_WS | AT_PRE_WS) LSQUARE unescapedAnnotation+ RSQUARE + : (annotationUseSiteTarget NL* | AT_NO_WS | AT_PRE_WS) LSQUARE unescapedAnnotation+ RSQUARE ; annotationUseSiteTarget @@ -876,7 +872,8 @@ unescapedAnnotation // SECTION: identifiers -simpleIdentifier: Identifier +simpleIdentifier + : Identifier | ABSTRACT | ANNOTATION | BY @@ -923,6 +920,7 @@ simpleIdentifier: Identifier | ACTUAL | CONST | SUSPEND + | VALUE ; identifier diff --git a/orx-jvm/orx-kotlin-parser/src/main/antlr/README.md b/orx-jvm/orx-kotlin-parser/src/main/antlr/README.md new file mode 100644 index 00000000..690a6e4c --- /dev/null +++ b/orx-jvm/orx-kotlin-parser/src/main/antlr/README.md @@ -0,0 +1 @@ +Synced from https://github.com/Kotlin/kotlin-spec/ @ 648afef3b9a7fccec7fdaa4aabde6d114bcf9d69