Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hashed labels to expression rule in JavaParser #4310

Merged
merged 4 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

@kaby76 kaby76 Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Malformed start rules, where EOF is not on all alts, and more generally, an ATN that does not exit with EOF, will need to be detected. kaby76/g4-scripts#5

| 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
Loading