@@ -2,6 +2,8 @@ parser grammar KScrParser;
22
33options { tokenVocab = KScrLexer; }
44
5+ file : packageDecl imports classDecl* EOF ;
6+
57// file-related
68packageDecl : PACKAGE id SEMICOLON ;
79
@@ -20,10 +22,11 @@ modifier
2022 | STATIC #modStatic
2123 | FINAL #modFinal
2224 | ABSTRACT #modAbstract
23- | SYNCHRONIZED #modSyncronized
2425 | NATIVE #modNative
26+ | SERVE #modServe
27+ | SYNCHRONIZED #modSyncronized
2528 ;
26- modifiers : modifier* ;
29+ modifiers : modifier+ ;
2730
2831classType
2932 : CLASS #ctClass
@@ -34,22 +37,44 @@ classType
3437
3538genericTypeUses : LESSER (n=NUMLIT | first=type) (COMMA type)* GREATER ;
3639
37- type
38- : idPart #importedTypeName
39- | rawType genericTypeUses? #normalTypeUse
40- | rawType genericTypeUses? (indexerEmpty | ELIPSES )? #arrayTypeUse
40+ num : NUMIDENT #numTypeLit
41+ | BOOL #numTypeLitBool
42+ | BYTE #numTypeLitByte
43+ | SHORT #numTypeLitShort
44+ | INT #numTypeLitInt
45+ | LONG #numTypeLitLong
46+ | FLOAT #numTypeLitFloat
47+ | DOUBLE #numTypeLitDouble
48+ ;
49+ primitiveTypeLit
50+ : OBJECT #typeLitObject
51+ | ARRAYIDENT #typeLitArray
52+ | TUPLEIDENT #typeLitTuple
53+ | num #typeLitNum
54+ | TYPE #typeLitType
55+ | ENUM #typeLitEnum
56+ | VOID #typeLitVoid
57+ ;
58+ primitiveLit
59+ : THIS #varThis
60+ | SUPER #varSuper
61+ | NUMLIT #varLitNum
62+ | TRUE #varLitTrue
63+ | FALSE #varLitFalse
64+ | STRLIT #varLitStr
65+ | STDIOLIT #varLitStdio
66+ | ENDLLIT #varLitEndl
67+ | NULL #varLitNull
4168 ;
42-
4369rawType
4470 : primitiveTypeLit
71+ | idPart
4572 | id
4673 ;
74+ type : rawType genericTypeUses? nullable=QUESTION ? (indexerEmpty | ELIPSES )? nullableArray=QUESTION ?;
4775
4876genericTypeDef : idPart elp=ELIPSES ? (EXTENDS ext=type | SUPER sup=type)? (ASSIGN (defN=NUMLIT | def=type))?;
49- genericTypeDefs : LESSER (NUMLIT | genericTypeDef) (COMMA genericTypeDef)* GREATER ;
50-
51- objectExtends : EXTENDS type (COMMA type)*;
52- objectImplements : IMPLEMENTS type (COMMA type)*;
77+ genericDefs : LESSER (NUMLIT | genericTypeDef) (COMMA genericTypeDef)* GREATER ;
5378
5479parameter : FINAL ? type idPart (ASSIGN expr)?;
5580parameters : LPAREN (parameter (COMMA parameter)*)? RPAREN ;
@@ -63,7 +88,7 @@ memberExpr: REQARROW uniformBlock;
6388lambdaBlock : RDASHARROW (uniformBlock | normalBlock);
6489caseBlock
6590 : COLON statements BREAK SEMICOLON #caseStmtBlock
66- | memberExpr COMMA #caseExprBlock
91+ | REQARROW memberExpr COMMA #caseExprBlock
6792 ;
6893memberBlock
6994 : normalBlock #memberNormalBlock
@@ -77,40 +102,46 @@ codeBlock
77102 ;
78103
79104initDecl : STATIC memberBlock;
80- subConstructorCall : type arguments;
105+ subConstructorCall : ( THIS | type) arguments;
81106subConstructorCalls : COLON subConstructorCall (COMMA subConstructorCall)*?;
82- constructorDecl : annotation* modifiers type parameters subConstructorCalls? memberBlock;
83- methodDecl : annotation* modifiers genericTypeDefs? type idPart parameters memberBlock;
84- indexerMemberDecl : annotation* modifiers genericTypeDefs? type THIS indexerDecl propBlock;
85-
86- propGetter : GET memberBlock;
87- propSetter : SET memberBlock;
88- propInit : INIT memberBlock;
107+ constructorDecl : annotation* modifiers? (WHERE condition=expr)? (SELECT supplier=lambda)? (ELSE (NULL | fallback=lambda))?
108+ type parameters subConstructorCalls? memberBlock;
109+ methodDecl : annotation* modifiers? (WHERE condition=expr)? (SELECT supplier=lambda)? (ELSE (NULL | fallback=lambda))?
110+ genericDefs? type idPart parameters genericSpecifiers? memberBlock;
111+ indexerMemberDecl : annotation* modifiers? (WHERE condition=expr)? (SELECT supplier=lambda)? (ELSE (NULL | fallback=lambda))?
112+ genericDefs? type THIS indexerDecl genericSpecifiers? propBlock;
113+
114+ propGetter : GET modifiers? (WHERE condition=expr)? (SELECT supplier=lambda)? (ELSE (NULL | fallback=lambda))? memberBlock;
115+ propSetter : SET modifiers? (WHERE condition=expr)? (SELECT supplier=lambda)? (ELSE (NULL | fallback=lambda))? memberBlock;
116+ propInit : INIT modifiers? (WHERE condition=expr)? (SELECT supplier=lambda)? (ELSE (NULL | fallback=lambda))? memberBlock;
89117propBlock
90118 : memberBlock #propComputed
91119 | LBRACE propGetter propSetter? propInit? RBRACE #propAccessors
92120 | (ASSIGN expr)? SEMICOLON #propFieldStyle
93121 ;
94- propertyDecl : annotation* modifiers type idPart propBlock;
122+ propertyDecl : annotation* modifiers? (WHERE condition=expr)? (SELECT supplier=lambda)? (ELSE (NULL | fallback=lambda))?
123+ type idPart propBlock;
95124
96125member
97- : propertyDecl #memProp
98- | classDecl #memCls
99- | initDecl catchBlocks? #memInit
100- | constructorDecl catchBlocks? #memCtor
101- | methodDecl catchBlocks? #memMtd
102- | indexerMemberDecl #memIdx
126+ : propertyDecl #memProp
127+ | classDecl #memCls
128+ | initDecl catchBlocks? #memInit
129+ | constructorDecl catchBlocks? #memCtor
130+ | methodDecl catchBlocks? #memMtd
131+ | indexerMemberDecl #memIdx
132+ | idPart arguments? ASSIGN expr (COMMA | SEMICOLON ?) #memEnumConst
103133 ;
134+ genericSpecifier : idPart (superclassesDef | (ASSIGN (type | expr)) | superclassesDef (ASSIGN (type | expr)));
135+ genericSpecifiers : WHERE genericSpecifier (COMMA genericSpecifier)*;
136+ superclassesDef : COLON type (COMMA type)*;
104137
105- classDecl : annotation* modifiers classType idPart genericTypeDefs? objectExtends? objectImplements? (LBRACE member* RBRACE | SEMICOLON );
106-
107- file : packageDecl imports classDecl* EOF ;
138+ classDecl : annotation* modifiers? classType idPart genericDefs? superclassesDef? genericSpecifiers? (LBRACE member* RBRACE | SEMICOLON );
108139
109140inferType : VOID | VAR ;
110141
111- indexerEmpty : LSQUAR RSQUAR ;
112- indexerDecl : LSQUAR type idPart (COMMA type idPart)* RSQUAR ;
113- indexerExpr : LSQUAR expr (COMMA expr)* RSQUAR ;
142+ indexerEmpty : LSQUAR COMMA * RSQUAR ;
143+ indexerDecl : ( LSQUAR ) type idPart (COMMA type idPart)* ( RSQUAR ) ;
144+ indexerExpr : ( LSQUAR ) expr (COMMA expr)* ( RSQUAR ) ;
114145cast : LPAREN type COLON expr RPAREN ;
115146declaration : type idPart (ASSIGN expr)?;
116147mutation : (binaryop | binaryop_late)? ASSIGN expr;
@@ -120,7 +151,7 @@ newArray: NEW type indexerExpr;
120151newListedArray : NEW type indexerEmpty LBRACE (expr (COMMA expr)*)? RBRACE ;
121152label : idPart COLON WS ;
122153lambda
123- : label? type COLON idPart #methodRef
154+ : label? ( type COLON )? idPart #methodRef
124155 | label? tupleExpr lambdaBlock #lambdaExpr
125156 ;
126157
@@ -153,6 +184,9 @@ statement
153184 | left=expr mutation SEMICOLON #stmtAssign
154185 | left=tupleExpr ASSIGN right=tupleExpr #stmtAssignTuple
155186 | left=expr DOT idPart arguments? #stmtCallMember
187+ | pipe=expr (RREQARROW lambda)+ SEMICOLON #stmtPipeListen
188+ | pipe=expr (RRDASHARROW expr)+ SEMICOLON #stmtPipeRead
189+ | pipe=expr (LLDASHARROW expr)+ SEMICOLON #stmtPipeWrite
156190 | returnStatement SEMICOLON #stmtReturn
157191 | throwStatement SEMICOLON #stmtThrow
158192 | markStatement #stmtMark
@@ -165,17 +199,12 @@ statement
165199 | forStatement catchBlocks? #stmtFor
166200 | foreachStatement catchBlocks? #stmtForeach
167201 | switchStatement catchBlocks? #stmtSwitch
168- | pipe=expr (RREQARROW lambda)+ SEMICOLON #stmtPipeListen
169- | pipe=expr (RRDASHARROW expr)+ SEMICOLON #stmtPipeRead
170- | pipe=expr (LLDASHARROW expr)+ SEMICOLON #stmtPipeWrite
171202 | SEMICOLON #stmtEmpty
172203 ;
173204typedExpr : type? expr;
174205expr
175- // simply a variable
176- : idPart #varValue
177206 // operators
178- | prefixop expr #opPrefix
207+ : prefixop expr #opPrefix
179208 | left=expr binaryop right=expr #opBinary
180209 | expr postfixop #opPostfix
181210 // `is` keyword
186215 | LPAREN expr RPAREN #parens
187216 | cond=expr QUESTION left=expr COLON right=expr #ternary
188217 | cast #exprCast
218+ // simply a variable
219+ | idPart #varValue
189220 | newArray #newArrayValue
190221 | newListedArray #newListedArrayValue
191222 | primitiveLit #nativeLitValue
204235 | left=expr SHORTELIPSES right=expr #rangeInvoc
205236 // pipe operators
206237 | pipe=expr (RREQARROW lambda)+ #exprPipeListen
238+ | pipe=expr (RRDASHARROW expr)+ #exprPipeRead
207239 // tuple expressions
208240 | tupleExpr #exprTuple
209241 | left=expr binaryop_late right=expr #opBinaryLate
@@ -258,35 +290,3 @@ idPart
258290 : ID
259291 | ANNOTATION
260292 ;
261-
262-
263- array : ARRAYIDENT genericTypeUses?;
264- tuple : TUPLEIDENT genericTypeUses?;
265- num : (NUMIDENT | INT ) genericTypeUses? #numTypeLitTuple
266- | BYTE #numTypeLitByte
267- | SHORT #numTypeLitShort
268- | INT #numTypeLitInt
269- | LONG #numTypeLitLong
270- | FLOAT #numTypeLitFloat
271- | DOUBLE #numTypeLitDouble
272- ;
273- primitiveTypeLit
274- : OBJECT #typeLitObject
275- | array #typeLitArray
276- | tuple #typeLitTuple
277- | num #typeLitNum
278- | TYPE #typeLitType
279- | ENUM #typeLitEnum
280- | VOID #typeLitVoid
281- ;
282- primitiveLit
283- : THIS #varThis
284- | SUPER #varSuper
285- | NUMLIT #varLitNum
286- | TRUE #varLitTrue
287- | FALSE #varLitFalse
288- | STRLIT #varLitStr
289- | STDIOLIT #varLitStdio
290- | ENDLLIT #varLitEndl
291- | NULL #varLitNull
292- ;
0 commit comments