1
+ /*
2
+ The MIT License (MIT)
3
+
4
+ Copyright (c) 2023 Ivan Kniazkov
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included
14
+ in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ /*
26
+ I. Unified AST description
27
+
28
+ 1. Literals
29
+ */
30
+
1
31
IntegerLiteral <- $int$, $String.valueOf(#)$, $Integer.parseInt(#)$, $NumberFormatException$;
2
32
Identifier <- $String$, $#$, $#$;
3
33
StringLiteral <- $String$, $#$, $#$;
4
34
Modifier <- $String$, $#$, $#$;
5
35
PrimitiveType <- $String$, $#$, $#$;
6
36
37
+ Name <- [composition@Name ], last@Identifier;
38
+
39
+ /*
40
+ 2. Program structure
41
+ */
42
+
7
43
Program <- {ProgramItem};
8
44
ProgramItem <- ClassDeclaration | Statement | ClassItem;
45
+ ClassDeclaration <- [modifiers@ModifierBlock], name@Identifier, [extendsbl@ExtendsBlock], [implementsbl@ImplementsBlock], body@ClassBody;
46
+ ModifierBlock <- {Modifier};
47
+ Parameter <- [modifiers@ModifierBlock], [datatype@TypeName], name@Identifier;
48
+ ParameterBlock <- {Parameter};
49
+ ExtendsBlock <- {ClassType};
50
+ ImplementsBlock <- {ClassType};
51
+ ThrowsBlock <- {Exception};
52
+ ClassBody <- {ClassItem};
53
+ ClassItem <- FunctionDeclaration | FieldDeclaration;
54
+ FunctionDeclaration <- [modifiers@ModifierBlock], [datatype@TypeName], name@Identifier, parameters@ParameterBlock, [throwsbl@ThrowsBlock], body@StatementBlock;
55
+ FieldDeclaration <- [modifiers@ModifierBlock], [datatype@TypeName], declarators@DeclaratorList;
56
+ DeclaratorList <- {Declarator};
57
+ Declarator <- name@Identifier, [value@Expression];
58
+ Exception <- name@ClassType;
9
59
60
+ /*
61
+ 3. Data types
62
+ */
63
+
64
+ TypeName <- ArrayType | PrimitiveType | ClassType | VoidType;
65
+ ArrayType <- base@TypeName, dimensions@DimensionList;
66
+ ClassType <- name@Name ;
67
+ VoidType <- 0 ;
68
+ DimensionList <- {Dimension};
69
+ Dimension <- [expression@Expression];
70
+
71
+ /*
72
+ 4. Statements
73
+ */
74
+
75
+ Statement <- Return | StatementBlock | VariableDeclaration | ExpressionStatement | IfElse;
76
+ Return <- [expression@Expression];
77
+ StatementBlock <- {Statement};
78
+ VariableDeclaration <- [modifiers@ModifierBlock], [datatype@TypeName], declarators@DeclaratorList;
79
+ ExpressionStatement <- expression@Expression;
80
+ IfElse <- condition@Expression, ifBranch@Statement, [elseBranch@Statement];
81
+
82
+ /*
83
+ 5. Expressions
84
+ */
85
+
86
+ ExpressionList <- {Expression};
10
87
Expression <- BinaryExpression | IntegerLiteral | This | StringLiteral | Identifier | NullLiteral
11
88
| FunctionCall | UnaryExpression | BitwiseExpression | LogicalExpression | AssignableExpression | Assignment
12
89
| ParenthesizedExpression | ObjectCreationExpression | ArrayInitializer;
13
90
ArithmeticExpression <- Addition | Subtraction | Multiplication | Division | Modulus;
14
91
BinaryExpression <- ArithmeticExpression | RelationalExpression;
15
92
RelationalExpression <- IsEqualTo | NotEqualTo | GreaterThan | LessThan | GreaterThanOrEqualTo | LessThanOrEqualTo;
16
- Statement <- Return | StatementBlock | VariableDeclaration | ExpressionStatement | IfElse;
17
- TypeName <- ArrayType | PrimitiveType | ClassType | VoidType;
18
93
UnaryExpression <- PreIncrement | PreDecrement | PostIncrement | PostDecrement | Positive | Negative;
19
94
BitwiseExpression <- BitwiseComplement | LeftShift | RightShift | UnsignedRightShift | BitwiseAnd | BitwiseOr | ExclusiveOr;
20
95
LogicalExpression <- LogicalAnd | LogicalOr | LogicalNot;
21
96
Assignment <- SimpleAssignment | AdditionAssignment | SubtractionAssignment | MultiplicationAssignment
22
97
| DivisionAssignment | ModulusAssignment | BitwiseAndAssignment | BitwiseOrAssignment | ExclusiveOrAssignment
23
98
| RightShiftAssignment | UnsignedRightShiftAssignment | LeftShiftAssignment;
24
99
AssignableExpression <- Variable | PropertyAccess;
25
-
26
- ExpressionStatement <- expression@Expression;
27
100
ParenthesizedExpression <- expression@Expression;
28
101
102
+ This <- 0 ;
103
+ NullLiteral <- 0 ;
104
+
29
105
Addition <- left@Expression, right@Expression;
30
106
Subtraction <- left@Expression, right@Expression;
31
107
Multiplication <- left@Expression, right@Expression;
@@ -71,46 +147,28 @@ UnsignedRightShiftAssignment <- left@AssignableExpression, right@Expression;
71
147
RightShiftAssignment <- left@AssignableExpression, right@Expression;
72
148
LeftShiftAssignment <- left@AssignableExpression, right@Expression;
73
149
74
- ClassType <- name@Name ;
75
- ArrayType <- base@TypeName, dimensions@DimensionList;
76
- DimensionList <- {Dimension};
77
- Dimension <- [expression@Expression];
78
- Return <- [expression@Expression];
79
- IfElse <- condition@Expression, ifbranch@Statement, [elsebranch@Statement];
80
- Name <- [composition@Name ], last@Identifier;
81
150
Variable <- Name ;
82
- StatementBlock <- {Statement};
83
- This <- 0 ;
84
- VoidType <- 0 ;
85
- NullLiteral <- 0 ;
86
- PropertyAccess <- left@Expression, right@Expression;
87
- ModifierBlock <- {Modifier};
88
- ExpressionList <- {Expression};
89
- ArrayInitializer <- {Expression};
90
151
FunctionCall <- [owner@Name ], name@Identifier, arguments@ExpressionList;
91
- Parameter <- [modifiers@ModifierBlock], [datatype@TypeName], name@Identifier;
92
- FunctionDeclaration <- [modifiers@ModifierBlock], [datatype@TypeName], name@Identifier, parameters@ParameterBlock, [throwsbl@ThrowsBlock], body@StatementBlock;
93
- ParameterBlock <- {Parameter};
94
- ClassDeclaration <- [modifiers@ModifierBlock], name@Identifier, [extendsbl@ExtendsBlock], [implementsbl@ImplementsBlock], body@ClassBody;
95
- ExtendsBlock <- {ClassType};
96
- ImplementsBlock <- {ClassType};
97
- ThrowsBlock <- {Exception};
98
- ClassBody <- {ClassItem};
99
- ClassItem <- FunctionDeclaration | FieldDeclaration;
100
- FieldDeclaration <- [modifiers@ModifierBlock], [datatype@TypeName], declarators@DeclaratorList;
101
- VariableDeclaration <- [modifiers@ModifierBlock], [datatype@TypeName], declarators@DeclaratorList;
102
- DeclaratorList <- {Declarator};
103
- Declarator <- name@Identifier, [value@Expression];
104
- Exception <- name@ClassType;
105
152
ObjectCreationExpression <- datatype@TypeName, [arguments@ExpressionList];
153
+ PropertyAccess <- left@Expression, right@Expression;
154
+ ArrayInitializer <- {Expression};
155
+
156
+ /*
157
+ II. Java
158
+
159
+ 1. Red nodes
160
+ */
106
161
107
162
java:
108
163
109
164
Synchronized <- expression@Expression, body@StatementBlock;
110
165
Statement <- & | Synchronized;
111
166
112
- IntegerLiteralExpr<#1 > -> IntegerLiteral<#1 >;
167
+ /*
168
+ 2. Transformation rules
169
+ */
113
170
171
+ IntegerLiteralExpr<#1 > -> IntegerLiteral<#1 >;
114
172
115
173
EnclosedExpr(#1 ) -> ParenthesizedExpression(#1 );
116
174
@@ -275,17 +333,11 @@ Modifier<#1> -> Modifier<#1>;
275
333
NullLiteralExpr -> NullLiteral;
276
334
IfStmt(#1 , #2 ) -> IfElse(#1 , #2 );
277
335
278
- // Arrays
279
-
280
336
ArrayType(#1 ) -> ArrayType(#1 , DimensionList(Dimension));
281
337
ArrayInitializerExpr(#1 ...) -> ArrayInitializer(#1 );
282
338
283
- //
284
-
285
339
CompilationUnit(#1 ) -> Program(#1 );
286
340
287
- // Class declaration ClassDeclaration <- [ModifierBlock], Identifier, [ExtendsBlock], [ImplementsBlock], ClassBody;
288
-
289
341
ClassOrInterfaceDeclaration(Modifier#1 , #2 , InterfaceType(#3 )) ->
290
342
ClassDeclaration(ModifierBlock(#1 ), #2 , ImplementsBlock(ClassType(Name (#3 ))), ClassBody);
291
343
@@ -297,8 +349,6 @@ ClassOrInterfaceDeclaration(Modifier#1, #2, #3...) ->
297
349
298
350
ClassOrInterfaceDeclaration(#1 , #2 ...) -> ClassDeclaration(#1 , ClassBody(#2 ));
299
351
300
- // Field and variable declaration
301
-
302
352
ObjectCreationExpr(#1 ) -> ObjectCreationExpression(#1 );
303
353
ObjectCreationExpr(#1 , #2 ...) -> ObjectCreationExpression(#1 , ExpressionList(#2 ));
304
354
@@ -314,8 +364,6 @@ VariableDeclarationExpr(VariableDeclarator(#1, #2, #3)) -> VariableDeclaration(#
314
364
VariableDeclarationExpr(Modifier#3 , VariableDeclarator(#1 , #2 )) -> VariableDeclaration(ModifierBlock(#3 ), #1 , DeclaratorList(Declarator(#2 )));
315
365
VariableDeclarationExpr(Modifier#4 , VariableDeclarator(#1 , #2 , #3 )) -> VariableDeclaration(ModifierBlock(#4 ), #1 , DeclaratorList(Declarator(#2 , #3 )));
316
366
317
- // Function declaration FunctionDeclaration <- [modifiers@ModifierBlock], [typename@TypeName], name@Identifier, parameters@ParameterBlock, body@StatementBlock;
318
-
319
367
MethodDeclaration(Modifier#1 , #2 , Parameter#3 , #4 , #5 ) ->
320
368
FunctionDeclaration(ModifierBlock(#1 ), #4 , #2 , ParameterBlock(#3 ), #5 );
321
369
MethodDeclaration(Modifier#1 , #2 , Parameter#3 , Exception#6 , #4 , #5 ) ->
@@ -324,18 +372,24 @@ MethodDeclaration(Modifier#1, #2, Parameter#3, Exception#6, #4, #5) ->
324
372
ConstructorDeclaration(Modifier#1 , #2 , Parameter#3 , #4 ) ->
325
373
FunctionDeclaration(ModifierBlock(#1 ), #2 , ParameterBlock(#3 ), #4 );
326
374
375
+ /*
376
+ III. JavaScript
377
+
378
+ 1. Red nodes
379
+ */
380
+
327
381
js:
328
382
329
383
ClassItem <- & | Property;
330
384
Expression <- & | ObjectLiteral;
331
385
ObjectLiteral <- {Property};
332
- // PropertyList <- {Property};
333
- //ObjectLiteral <- 0;
334
386
Property <- name@Identifier, value@Expression;
335
387
Yield <- Expression;
336
388
337
- // ObjectCreationExpr(#1, #2...) -> ObjectCreationExpression(#1, ExpressionList(#2));
338
- // ObjectCreationExpression <- datatype@TypeName, [arguments@ExpressionList];
389
+ /*
390
+ 2. Transformation rules
391
+ */
392
+
339
393
propertyAssignment(propertyName(identifierName(#1 )), #2 ) -> Property(#1 , #2 );
340
394
objectLiteral(#1 ...) -> ObjectLiteral(#1 );
341
395
singleExpression(literal<"new" >, Variable(#1 ), arguments(#2 ...)) ->
@@ -470,7 +524,6 @@ singleExpression(#1, literal<"--">) -> PostDecrement(#1);
470
524
singleExpression(literal<"-" >, #1 ) -> Negative(#1 );
471
525
singleExpression(literal<"+" >, #1 ) -> Positive(#1 );
472
526
473
-
474
527
identifier(literal<#1 >) -> Identifier<#1 >;
475
528
476
529
singleExpression(#1 , literal<"=" >, #2 ) -> SimpleAssignment(#1 , #2 );
@@ -517,8 +570,6 @@ program(sourceElements(#1...)) -> Program(#1);
517
570
518
571
returnStatement(literal<"return" >, expressionSequence(#1 )) -> Return (#1 );
519
572
520
- // Class declaration ClassDeclaration <- [ModifierBlock], Identifier, [ExtendsBlock], [ImplementsBlock], ClassBody;
521
-
522
573
classDeclaration(literal<"class" >, #1 , classTail(literal<"extends" >, Variable(#2 )))
523
574
-> ClassDeclaration(#1 , ExtendsBlock(ClassType(#2 )), ClassBody);
524
575
classDeclaration(literal<"class" >, #1 , classTail(#2 , classElement(emptyStatement_)))
@@ -528,8 +579,6 @@ classDeclaration(literal<"class">, #1, classTail(#2...)) -> ClassDeclaration(#1,
528
579
classElement(propertyName(identifierName(#1 )), literal<"=" >, #2 ) -> Property(#1 , #2 );
529
580
classElement(#1 ) -> #1 ;
530
581
531
- // Function declaration FunctionDeclaration <- [modifiers@ModifierBlock], [typename@TypeName], name@Identifier, parameters@ParameterBlock, body@StatementBlock;
532
-
533
582
functionBody(sourceElements(sourceElement(statement(expressionStatement(expressionSequence(#1 )))))) ->
534
583
StatementBlock(#1 );
535
584
functionBody(sourceElements(sourceElement(statement(#1 )))) ->
@@ -547,6 +596,9 @@ methodDefinition(propertyName(identifierName(#1)), #2, #3) -> FunctionDeclarati
547
596
formalParameterArg(assignable(#1 )) -> Parameter(#1 );
548
597
formalParameterList(#1 ...) -> ParameterBlock(#1 );
549
598
599
+ /*
600
+ IV. Python
601
+ */
550
602
551
603
python:
552
604
@@ -656,8 +708,6 @@ file_input(#1, small_stmt(#2)) -> Program(#1, #2);
656
708
657
709
file_input(#1 ...) -> Program(#1 );
658
710
659
- // Class declaration ClassDeclaration <- [ModifierBlock], Identifier, [ExtendsBlock], [ImplementsBlock], ClassBody;
660
-
661
711
classdef(literal<"class" >, name(literal<#1 >), suite(small_stmt(literal<"pass" >)))
662
712
-> ClassDeclaration(Identifier<#1 >, ClassBody);
663
713
0 commit comments