Skip to content

Commit

Permalink
add hashed labels to expression rule in JavaParser (antlr#4310)
Browse files Browse the repository at this point in the history
* add hashed labels to expression rule in JavaParser

* fix hashed labels names

* add EOF to compilationUnit rule

* fix trees in tests

---------

Co-authored-by: asamatova <asamatova@ptsecurity.com>
  • Loading branch information
samatanna and asamatova authored Nov 17, 2024
1 parent 35a6f12 commit 97bd288
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 34 deletions.
69 changes: 41 additions & 28 deletions java/java/JavaParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ options {
}

compilationUnit
: packageDeclaration? (importDeclaration | ';')* (typeDeclaration | ';')*
: packageDeclaration? (importDeclaration | ';')* (typeDeclaration | ';')* EOF
| moduleDeclaration EOF
;

Expand Down Expand Up @@ -599,46 +599,59 @@ methodCall
expression
// Expression order in accordance with https://introcs.cs.princeton.edu/java/11precedence/
// Level 16, Primary, array and member access
: primary
| expression '[' expression ']'
: primary #PrimaryExpression
| expression '[' expression ']' #SquareBracketExpression
| expression bop = '.' (
identifier
| methodCall
| THIS
| NEW nonWildcardTypeArguments? innerCreator
| SUPER superSuffix
| explicitGenericInvocation
)
) #MemberReferenceExpression
// Method calls and method references are part of primary, and hence level 16 precedence
| methodCall
| expression '::' typeArguments? identifier
| typeType '::' (typeArguments? identifier | NEW)
| classType '::' typeArguments? NEW
| switchExpression // Java17
| methodCall #MethodCallExpression
| expression '::' typeArguments? identifier #MethodReferenceExpression
| typeType '::' (typeArguments? identifier | NEW) #MethodReferenceExpression
| classType '::' typeArguments? NEW #MethodReferenceExpression

// Java17
| switchExpression #ExpressionSwitch

// Level 15 Post-increment/decrement operators
| expression postfix = ('++' | '--')
| expression postfix = ('++' | '--') #PostIncrementDecrementOperatorExpression

// Level 14, Unary operators
| prefix = ('+' | '-' | '++' | '--' | '~' | '!') expression
| prefix = ('+' | '-' | '++' | '--' | '~' | '!') expression #UnaryOperatorExpression

// Level 13 Cast and object creation
| '(' annotation* typeType ('&' typeType)* ')' expression
| NEW creator
| '(' annotation* typeType ('&' typeType)* ')' expression #CastExpression
| NEW creator #ObjectCreationExpression

// Level 12 to 1, Remaining operators
| expression bop = ('*' | '/' | '%') expression // Level 12, Multiplicative operators
| expression bop = ('+' | '-') expression // Level 11, Additive operators
| expression ('<' '<' | '>' '>' '>' | '>' '>') expression // Level 10, Shift operators
| expression bop = ('<=' | '>=' | '>' | '<') expression // Level 9, Relational operators
| expression bop = INSTANCEOF (typeType | pattern)
| expression bop = ('==' | '!=') expression // Level 8, Equality Operators
| expression bop = '&' expression // Level 7, Bitwise AND
| expression bop = '^' expression // Level 6, Bitwise XOR
| expression bop = '|' expression // Level 5, Bitwise OR
| expression bop = '&&' expression // Level 4, Logic AND
| expression bop = '||' expression // Level 3, Logic OR
| <assoc = right> expression bop = '?' expression ':' expression // Level 2, Ternary
// Level 12, Multiplicative operators
| expression bop = ('*' | '/' | '%') expression #BinaryOperatorExpression
// Level 11, Additive operators
| expression bop = ('+' | '-') expression #BinaryOperatorExpression
// Level 10, Shift operators
| expression ('<' '<' | '>' '>' '>' | '>' '>') expression #BinaryOperatorExpression
// Level 9, Relational operators
| expression bop = ('<=' | '>=' | '>' | '<') expression #BinaryOperatorExpression
| expression bop = INSTANCEOF (typeType | pattern) #InstanceOfOperatorExpression
// Level 8, Equality Operators
| expression bop = ('==' | '!=') expression #BinaryOperatorExpression
// Level 7, Bitwise AND
| expression bop = '&' expression #BinaryOperatorExpression
// Level 6, Bitwise XOR
| expression bop = '^' expression #BinaryOperatorExpression
// Level 5, Bitwise OR
| expression bop = '|' expression #BinaryOperatorExpression
// Level 4, Logic AND
| expression bop = '&&' expression #BinaryOperatorExpression
// Level 3, Logic OR
| expression bop = '||' expression #BinaryOperatorExpression
// Level 2, Ternary
| <assoc = right> expression bop = '?' expression ':' expression #TernaryExpression
// Level 1, Assignment
| <assoc = right> expression bop = (
'='
Expand All @@ -653,10 +666,10 @@ expression
| '>>>='
| '<<='
| '%='
) expression
) expression #BinaryOperatorExpression

// Level 0, Lambda Expression
| lambdaExpression // Java8
// Level 0, Lambda Expression // Java8
| lambdaExpression #ExpressionLambda
;

// Java17
Expand Down
2 changes: 1 addition & 1 deletion java/java/examples/ExpressionOrder.java.tree
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(compilationUnit (importDeclaration import (qualifiedName (identifier java) . (identifier util) . (identifier regex) . (identifier Pattern) . (identifier CharPredicate)) ;) (typeDeclaration (classOrInterfaceModifier public) (classDeclaration class (identifier ExpressionOrder) (classBody { (classBodyDeclaration (modifier (classOrInterfaceModifier public)) (modifier (classOrInterfaceModifier static)) (memberDeclaration (methodDeclaration (typeTypeOrVoid void) (identifier main) (formalParameters ( (formalParameterList (formalParameter (typeType (classOrInterfaceType (typeIdentifier String)) [ ]) (variableDeclaratorId (identifier args)))) )) (methodBody (block { (blockStatement (localVariableDeclaration (typeType (classOrInterfaceType (typeIdentifier Charpredicate))) (variableDeclarators (variableDeclarator (variableDeclaratorId (identifier a)) = (variableInitializer (expression (methodCall (identifier func) (arguments ( (expressionList (expression (primary (literal "a"))) , (expression (primary (literal true)))) )))))))) ;) }))))) (classBodyDeclaration (modifier (classOrInterfaceModifier public)) (modifier (classOrInterfaceModifier static)) (memberDeclaration (methodDeclaration (typeTypeOrVoid (typeType (classOrInterfaceType (typeIdentifier CharPredicate)))) (identifier func) (formalParameters ( (formalParameterList (formalParameter (typeType (classOrInterfaceType (typeIdentifier String))) (variableDeclaratorId (identifier name))) , (formalParameter (typeType (primitiveType boolean)) (variableDeclaratorId (identifier caseIns)))) )) (methodBody (block { (blockStatement (statement return (expression (expression (primary (identifier caseIns))) ? (expression (lambdaExpression (lambdaParameters (identifier c)) -> (lambdaBody (expression (expression (expression (expression (primary (identifier Character))) . (methodCall (identifier isUpperCase) (arguments ( (expressionList (expression (primary (identifier c)))) )))) || (expression (expression (primary (identifier Character))) . (methodCall (identifier isLowerCase) (arguments ( (expressionList (expression (primary (identifier c)))) ))))) || (expression (expression (primary (identifier Character))) . (methodCall (identifier isTitleCase) (arguments ( (expressionList (expression (primary (identifier c)))) )))))))) : (expression (expression (primary (identifier Character))) :: (identifier isUpperCase))) ;)) (blockStatement (statement ;)) }))))) (classBodyDeclaration (memberDeclaration (methodDeclaration (typeTypeOrVoid void) (identifier forEachRemaining) (formalParameters ( (formalParameterList (formalParameter (typeType (classOrInterfaceType (typeIdentifier Consumer) (typeArguments < (typeArgument ? super (typeType (classOrInterfaceType (typeIdentifier Double)))) >))) (variableDeclaratorId (identifier action)))) )) (methodBody (block { (blockStatement (statement (expression (methodCall (identifier forEachRemaining) (arguments ( (expressionList (expression ( (typeType (classOrInterfaceType (typeIdentifier DoubleConsumer))) ) (expression (expression (primary (identifier action))) :: (identifier accept)))) )))) ;)) }))))) }))))
(compilationUnit (importDeclaration import (qualifiedName (identifier java) . (identifier util) . (identifier regex) . (identifier Pattern) . (identifier CharPredicate)) ;) (typeDeclaration (classOrInterfaceModifier public) (classDeclaration class (identifier ExpressionOrder) (classBody { (classBodyDeclaration (modifier (classOrInterfaceModifier public)) (modifier (classOrInterfaceModifier static)) (memberDeclaration (methodDeclaration (typeTypeOrVoid void) (identifier main) (formalParameters ( (formalParameterList (formalParameter (typeType (classOrInterfaceType (typeIdentifier String)) [ ]) (variableDeclaratorId (identifier args)))) )) (methodBody (block { (blockStatement (localVariableDeclaration (typeType (classOrInterfaceType (typeIdentifier Charpredicate))) (variableDeclarators (variableDeclarator (variableDeclaratorId (identifier a)) = (variableInitializer (expression (methodCall (identifier func) (arguments ( (expressionList (expression (primary (literal "a"))) , (expression (primary (literal true)))) )))))))) ;) }))))) (classBodyDeclaration (modifier (classOrInterfaceModifier public)) (modifier (classOrInterfaceModifier static)) (memberDeclaration (methodDeclaration (typeTypeOrVoid (typeType (classOrInterfaceType (typeIdentifier CharPredicate)))) (identifier func) (formalParameters ( (formalParameterList (formalParameter (typeType (classOrInterfaceType (typeIdentifier String))) (variableDeclaratorId (identifier name))) , (formalParameter (typeType (primitiveType boolean)) (variableDeclaratorId (identifier caseIns)))) )) (methodBody (block { (blockStatement (statement return (expression (expression (primary (identifier caseIns))) ? (expression (lambdaExpression (lambdaParameters (identifier c)) -> (lambdaBody (expression (expression (expression (expression (primary (identifier Character))) . (methodCall (identifier isUpperCase) (arguments ( (expressionList (expression (primary (identifier c)))) )))) || (expression (expression (primary (identifier Character))) . (methodCall (identifier isLowerCase) (arguments ( (expressionList (expression (primary (identifier c)))) ))))) || (expression (expression (primary (identifier Character))) . (methodCall (identifier isTitleCase) (arguments ( (expressionList (expression (primary (identifier c)))) )))))))) : (expression (expression (primary (identifier Character))) :: (identifier isUpperCase))) ;)) (blockStatement (statement ;)) }))))) (classBodyDeclaration (memberDeclaration (methodDeclaration (typeTypeOrVoid void) (identifier forEachRemaining) (formalParameters ( (formalParameterList (formalParameter (typeType (classOrInterfaceType (typeIdentifier Consumer) (typeArguments < (typeArgument ? super (typeType (classOrInterfaceType (typeIdentifier Double)))) >))) (variableDeclaratorId (identifier action)))) )) (methodBody (block { (blockStatement (statement (expression (methodCall (identifier forEachRemaining) (arguments ( (expressionList (expression ( (typeType (classOrInterfaceType (typeIdentifier DoubleConsumer))) ) (expression (expression (primary (identifier action))) :: (identifier accept)))) )))) ;)) }))))) }))) <EOF>)
2 changes: 1 addition & 1 deletion java/java/examples/GenericConstructor.java.tree
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(compilationUnit (importDeclaration import (qualifiedName (identifier java) . (identifier util) . (identifier ArrayList)) ;) (typeDeclaration (classOrInterfaceModifier public) (classDeclaration class (identifier GenericConstructor) (classBody { (classBodyDeclaration (modifier (classOrInterfaceModifier public)) (modifier (classOrInterfaceModifier static)) (memberDeclaration (methodDeclaration (typeTypeOrVoid void) (identifier main) (formalParameters ( (formalParameterList (formalParameter (typeType (classOrInterfaceType (typeIdentifier String)) [ ]) (variableDeclaratorId (identifier args)))) )) (methodBody (block { (blockStatement (localVariableDeclaration (typeType (classOrInterfaceType (typeIdentifier ArrayList) (typeArguments < (typeArgument (typeType (classOrInterfaceType (typeIdentifier Integer)))) >))) (variableDeclarators (variableDeclarator (variableDeclaratorId (identifier myList)) = (variableInitializer (expression new (creator (createdName (identifier ArrayList) (typeArgumentsOrDiamond < >)) (classCreatorRest (arguments ( ))))))))) ;) }))))) }))))
(compilationUnit (importDeclaration import (qualifiedName (identifier java) . (identifier util) . (identifier ArrayList)) ;) (typeDeclaration (classOrInterfaceModifier public) (classDeclaration class (identifier GenericConstructor) (classBody { (classBodyDeclaration (modifier (classOrInterfaceModifier public)) (modifier (classOrInterfaceModifier static)) (memberDeclaration (methodDeclaration (typeTypeOrVoid void) (identifier main) (formalParameters ( (formalParameterList (formalParameter (typeType (classOrInterfaceType (typeIdentifier String)) [ ]) (variableDeclaratorId (identifier args)))) )) (methodBody (block { (blockStatement (localVariableDeclaration (typeType (classOrInterfaceType (typeIdentifier ArrayList) (typeArguments < (typeArgument (typeType (classOrInterfaceType (typeIdentifier Integer)))) >))) (variableDeclarators (variableDeclarator (variableDeclaratorId (identifier myList)) = (variableInitializer (expression new (creator (createdName (identifier ArrayList) (typeArgumentsOrDiamond < >)) (classCreatorRest (arguments ( ))))))))) ;) }))))) }))) <EOF>)
2 changes: 1 addition & 1 deletion java/java/examples/LocalVariableDeclaration.java.tree
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(compilationUnit (importDeclaration import (qualifiedName (identifier java) . (identifier util) . (identifier ArrayList)) ;) (typeDeclaration (classOrInterfaceModifier public) (classDeclaration class (identifier LocalVariableDeclaration) (classBody { (classBodyDeclaration (modifier (classOrInterfaceModifier public)) (modifier (classOrInterfaceModifier static)) (memberDeclaration (methodDeclaration (typeTypeOrVoid void) (identifier main) (formalParameters ( (formalParameterList (formalParameter (typeType (classOrInterfaceType (typeIdentifier String)) [ ]) (variableDeclaratorId (identifier args)))) )) (methodBody (block { (blockStatement (localVariableDeclaration (typeType (classOrInterfaceType (typeIdentifier ArrayList) (typeArguments < (typeArgument (typeType (classOrInterfaceType (typeIdentifier Integer)))) >))) (variableDeclarators (variableDeclarator (variableDeclaratorId (identifier myList)) = (variableInitializer (expression new (creator (createdName (identifier ArrayList) (typeArgumentsOrDiamond (typeArguments < (typeArgument (typeType (classOrInterfaceType (typeIdentifier Integer)))) >))) (classCreatorRest (arguments ( ))))))))) ;) (blockStatement (statement (expression (expression (primary (identifier myList))) . (methodCall (identifier add) (arguments ( (expressionList (expression (primary (literal (integerLiteral 2))))) )))) ;)) (blockStatement (localVariableDeclaration var (identifier myVal) = (expression (expression (primary (identifier myList))) . (methodCall (identifier get) (arguments ( (expressionList (expression (primary (literal (integerLiteral 0))))) ))))) ;) }))))) }))))
(compilationUnit (importDeclaration import (qualifiedName (identifier java) . (identifier util) . (identifier ArrayList)) ;) (typeDeclaration (classOrInterfaceModifier public) (classDeclaration class (identifier LocalVariableDeclaration) (classBody { (classBodyDeclaration (modifier (classOrInterfaceModifier public)) (modifier (classOrInterfaceModifier static)) (memberDeclaration (methodDeclaration (typeTypeOrVoid void) (identifier main) (formalParameters ( (formalParameterList (formalParameter (typeType (classOrInterfaceType (typeIdentifier String)) [ ]) (variableDeclaratorId (identifier args)))) )) (methodBody (block { (blockStatement (localVariableDeclaration (typeType (classOrInterfaceType (typeIdentifier ArrayList) (typeArguments < (typeArgument (typeType (classOrInterfaceType (typeIdentifier Integer)))) >))) (variableDeclarators (variableDeclarator (variableDeclaratorId (identifier myList)) = (variableInitializer (expression new (creator (createdName (identifier ArrayList) (typeArgumentsOrDiamond (typeArguments < (typeArgument (typeType (classOrInterfaceType (typeIdentifier Integer)))) >))) (classCreatorRest (arguments ( ))))))))) ;) (blockStatement (statement (expression (expression (primary (identifier myList))) . (methodCall (identifier add) (arguments ( (expressionList (expression (primary (literal (integerLiteral 2))))) )))) ;)) (blockStatement (localVariableDeclaration var (identifier myVal) = (expression (expression (primary (identifier myList))) . (methodCall (identifier get) (arguments ( (expressionList (expression (primary (literal (integerLiteral 0))))) ))))) ;) }))))) }))) <EOF>)
Loading

0 comments on commit 97bd288

Please sign in to comment.