List of usage examples for org.antlr.v4.runtime ParserRuleContext getParent
@Override
public ParserRuleContext getParent()
From source file:processing.mode.java.preproc.PdeParseTreeListener.java
License:Open Source License
/** * Endpoint for ANTLR to call after parsing a method declaration. * * <p>/*from w w w .j a v a 2 s .c o m*/ * Endpoint for ANTLR to call after parsing a method declaration, making any method "public" * that has: * * <ul> * <li>no other access modifier</li> * <li>return type "void"</li> * <li>is either in the context of the sketch class</li> * <li>is in the context of a class definition that extends PApplet</li> * </ul> * </p> * * @param ctx ANTLR context for the method declaration */ public void exitMethodDeclaration(ProcessingParser.MethodDeclarationContext ctx) { ParserRuleContext memCtx = ctx.getParent(); ParserRuleContext clsBdyDclCtx = memCtx.getParent(); ParserRuleContext clsBdyCtx = clsBdyDclCtx.getParent(); ParserRuleContext clsDclCtx = clsBdyCtx.getParent(); boolean inSketchContext = clsBdyCtx instanceof ProcessingParser.StaticProcessingSketchContext || clsBdyCtx instanceof ProcessingParser.ActiveProcessingSketchContext; boolean inPAppletContext = inSketchContext || (clsDclCtx instanceof ProcessingParser.ClassDeclarationContext && clsDclCtx.getChildCount() >= 4 && clsDclCtx.getChild(2).getText().equals("extends") && clsDclCtx.getChild(3).getText().endsWith("PApplet")); // Find modifiers ParserRuleContext possibleModifiers = ctx; while (!(possibleModifiers instanceof ProcessingParser.ClassBodyDeclarationContext)) { possibleModifiers = possibleModifiers.getParent(); } // Look for visibility modifiers and annotations boolean hasVisibilityModifier = false; int numChildren = possibleModifiers.getChildCount(); ParserRuleContext annoationPoint = null; for (int i = 0; i < numChildren; i++) { boolean childIsVisibility; ParseTree child = possibleModifiers.getChild(i); String childText = child.getText(); childIsVisibility = childText.equals("public"); childIsVisibility = childIsVisibility || childText.equals("private"); childIsVisibility = childIsVisibility || childText.equals("protected"); hasVisibilityModifier = hasVisibilityModifier || childIsVisibility; boolean isModifier = child instanceof ProcessingParser.ModifierContext; if (isModifier && isAnnoation((ProcessingParser.ModifierContext) child)) { annoationPoint = (ParserRuleContext) child; } } // Insert at start of method or after annoation if (!hasVisibilityModifier) { if (annoationPoint == null) { createInsertBefore(possibleModifiers.getStart(), " public "); } else { createInsertAfter(annoationPoint.getStop(), " public "); } } // Check if this was main if ((inSketchContext || inPAppletContext) && hasVisibilityModifier && ctx.getChild(1).getText().equals("main")) { foundMain = true; } }
From source file:uk.ac.open.crc.jim.parser.java17.Java17VisitorImplementation.java
License:Apache License
private Species getSpeciesFor(JavaParser.ClassDeclarationContext context) { Species species;//from ww w .j av a 2 s .c o m ParserRuleContext parent = context.getParent(); ParserRuleContext grandParent = parent.getParent(); // at worst this will be compilationUnit if (parent instanceof JavaParser.TypeDeclarationContext) { if (grandParent instanceof JavaParser.CompilationUnitContext) { // top level class is a child of typeDeclaration in compilationUnit species = Species.CLASS; } else if (grandParent instanceof JavaParser.BlockStatementContext) { // only other grandParent is blockStatement species = Species.LOCAL_CLASS; } else { // sanity check -- we should never be here // intended for development LOGGER.error("Class declaration found in unexpected context in {} at line: {}", this.javaFileName, context.start.getLine()); throw new IllegalStateException("Class found in unexpected context"); } } else if (parent instanceof JavaParser.MemberDeclarationContext || parent instanceof JavaParser.InterfaceMemberDeclarationContext) { // member class is a child of memberDeclaration in class or interface species = Species.MEMBER_CLASS; } else if (parent instanceof JavaParser.AnnotationTypeElementRestContext) { // this definition has worked in the JavaCC visitor. // There may, however, be a need to change it. species = Species.MEMBER_CLASS; } else { // second sanity check -- we should never be here // intended for development LOGGER.error("Class declaration found in unexpected context in {} at line: {}", this.javaFileName, context.start.getLine()); throw new IllegalStateException("Class found in unexpected context"); } return species; }