Example usage for org.antlr.v4.runtime ParserRuleContext getParent

List of usage examples for org.antlr.v4.runtime ParserRuleContext getParent

Introduction

In this page you can find the example usage for org.antlr.v4.runtime ParserRuleContext getParent.

Prototype

@Override
    
    public ParserRuleContext getParent() 

Source Link

Usage

From source file:org.ballerinalang.langserver.completions.CompletionSubRuleParser.java

License:Open Source License

/**
 * When the Antlr grasps a parser rule context we need to moderate the context.
 * This is because contexts below expression or statement is too fine grain for completion.
 * Therefore we grab the most recent/suitable expression/ statement.
 * // w  ww.j a v a 2  s  . c om
 * Note: If within a statement or expression resolver we need more fine grain parsing based on the parser rule
 * context, we can use the children rules for computations.
 * 
 * @param context   Language Server Completion Context
 */
private static void moderateParserRuleContext(LSContext context) {
    ParserRuleContext parserRuleContext = context.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY);
    if (parserRuleContext == null) {
        return;
    }

    while (true) {
        if (parserRuleContext == null
                || parserRuleContext.getClass().equals(BallerinaParser.ExpressionContext.class)
                || moderateContextTypes.contains(parserRuleContext.getClass())
                || parserRuleContext instanceof BallerinaParser.StatementContext
                || parserRuleContext instanceof BallerinaParser.DefinitionContext
                || parserRuleContext instanceof BallerinaParser.ObjectFieldDefinitionContext) {
            context.put(CompletionKeys.PARSER_RULE_CONTEXT_KEY, parserRuleContext);
            break;
        }
        parserRuleContext = parserRuleContext.getParent();
    }
}

From source file:org.ballerinalang.langserver.completions.resolvers.CompletionItemsContext.java

License:Open Source License

/**
 * Resolves into the finest completion context.
 *
 * @param ctx {@link LSContext}/*from  w  w  w .j a v  a 2s. c o m*/
 * @return {@link CompletionItemsContext}
 */
public CompletionItemsContext resolve(LSContext ctx) {
    ParserRuleContext prCtx = ctx.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY);
    ParserRuleContext contextParent = (prCtx != null) ? prCtx.getParent() : null;
    List<String> poppedTokens = CommonUtil.getPoppedTokenStrings(ctx);
    switch (this) {
    case TOP_LEVEL:
        CompletionItemsContext resolver = prCtx == null ? this
                : CompletionItemResolver.get(prCtx.getClass(), ctx);
        if (isAnnotationStart(ctx)) {
            return CompletionItemResolver.get(BallerinaParser.AnnotationAttachmentContext.class, ctx);
        }
        return resolver;
    case SERVICE:
        if (isAnnotationStart(ctx)) {
            return CompletionItemResolver.get(BallerinaParser.AnnotationAttachmentContext.class, ctx);
        } else if ((prCtx == null || prCtx instanceof BallerinaParser.ObjectFieldDefinitionContext)
                && poppedTokens.contains(UtilSymbolKeys.EQUAL_SYMBOL_KEY)) {
            return CompletionItemResolver.get(BallerinaParser.VariableDefinitionStatementContext.class, ctx);
        }
        return this;
    case RESOURCE:
        if (prCtx != null) {
            return CompletionItemResolver.get(prCtx.getClass(), ctx);
        }
        return this;
    case OBJECT_TYPE:
        if (poppedTokens.contains(UtilSymbolKeys.EQUAL_SYMBOL_KEY)) {
            // If the popped tokens contains the equal symbol, then the variable definition is being writing
            ctx.put(CompletionKeys.PARSER_RULE_CONTEXT_KEY,
                    new BallerinaParser.VariableDefinitionStatementContext(null, -1));
            return CompletionItemResolver.get(BallerinaParser.VariableDefinitionStatementContext.class, ctx);

        }
        return this;
    case FUNCTION:
        if (prCtx == null) {
            return this;
        }
        CompletionItemsContext contextResolver = CompletionItemResolver.get(prCtx.getClass(), ctx);
        return (contextResolver == null) ? this : contextResolver;
    case BLOCK_STATEMENT:
        ParserRuleContext parserRuleContext = ctx.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY);
        if (parserRuleContext != null) {
            return CompletionItemResolver.get(parserRuleContext.getClass(), ctx);
        } else {
            return CompletionItemResolver.get(BLangStatement.class, ctx);
        }
    case RECORD:
        if (poppedTokens.contains(UtilSymbolKeys.EQUAL_SYMBOL_KEY)) {
            // If the popped tokens contains the equal symbol, then the variable definition is being writing
            // This parser rule context is used to select the proper sorter.
            ctx.put(CompletionKeys.PARSER_RULE_CONTEXT_KEY,
                    new BallerinaParser.VariableDefinitionStatementContext(null, -1));
            return CompletionItemResolver.get(BallerinaParser.VariableDefinitionStatementContext.class, ctx);
        }
        return this;
    case PR_WORKER_INTERACTION:
        if (contextParent instanceof BallerinaParser.BinaryEqualExpressionContext) {
            contextParent = contextParent.getParent();
        }
        if (contextParent != null) {
            return CompletionItemResolver.get(contextParent.getClass(), ctx);
        }
        return this;
    case PR_WORKER_STMT:
        if (isInvocationOrInteractionOrFieldAccess(ctx)) {
            return this;
        }
        return CompletionItemResolver.get(BLangStatement.class, ctx);
    case PR_RETURN_STMT_DEFINITION:
        return CompletionItemResolver.get(BallerinaParser.VariableDefinitionStatementContext.class, ctx);
    case PR_GLOBAL_VARIABLE_DEFINITION:
        List<String> consumedTokens = ctx.get(CompletionKeys.FORCE_CONSUMED_TOKENS_KEY).stream()
                .map(Token::getText).collect(Collectors.toList());
        if (consumedTokens.get(0).equals(UtilSymbolKeys.FUNCTION_KEYWORD_KEY)) {
            return CompletionItemResolver.get(BallerinaParser.DefinitionContext.class, ctx);
        }
        return this;
    case PR_EXPRESSION:
        if (contextParent instanceof BallerinaParser.BinaryEqualExpressionContext
                || contextParent instanceof BallerinaParser.ExpressionListContext) {
            contextParent = contextParent.getParent();
        }
        if (contextParent != null) {
            return CompletionItemResolver.get(contextParent.getClass(), ctx);
        }
        return this;
    case PR_ASSIGNMENT_STATEMENT:
        return CompletionItemResolver.get(BallerinaParser.VariableDefinitionStatementContext.class, ctx);
    case PR_STATEMENT:
        if (isInvocationOrInteractionOrFieldAccess(ctx)) {
            return this;
        }
        return CompletionItemResolver.get(BLangStatement.class, ctx);
    case STATEMENT:
    case PARAMETER:
    case PACKAGE_NAME:
    case RECORD_LITERAL:
    case MATCH_EXPRESSION:
    case MATCH:
    case ANNOTATION_ATTACHMENT:
    case PR_WORKER_REPLY:
    case PR_WORKER_TRIGGER_WORKER_STMT:
    case PR_SERVICE_ENDPOINT_ATTACHMENT:
    case PR_SERVICE_DEFINITION:
    case PR_PANIC_STATEMENT:
    case PR_MATCH_STATEMENT:
    case PR_FUNCTION_DEFINITION:
    case PR_DEFINITION:
    case PR_CONDITIONAL_CLAUSE:
    case PR_ATTACHMENT_POINT:
    case PR_ANNOTATION_ATTACHMENT:
    case PR_ENDPOINT_DECLARATION:
    case PR_VARIABLE_DEFINITION:
        return this;
    }
    return null;
}

From source file:org.ballerinalang.langserver.completions.resolvers.CompletionItemScope.java

License:Open Source License

/**
 * Resolves into the finest completion context.
 *
 * @param ctx {@link LSContext}// ww w  .  j av a2 s .  c  o  m
 * @return {@link CompletionItemScope}
 */
public CompletionItemScope resolve(LSContext ctx) {
    ParserRuleContext prCtx = ctx.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY);
    ParserRuleContext contextParent = (prCtx != null) ? prCtx.getParent() : null;
    List<String> poppedTokens = CommonUtil.getPoppedTokenStrings(ctx);
    switch (this) {
    case TOP_LEVEL:
        CompletionItemScope resolver = prCtx == null ? this : CompletionItemResolver.get(prCtx.getClass(), ctx);
        if (isAnnotationStart(ctx)) {
            return CompletionItemResolver.get(BallerinaParser.AnnotationAttachmentContext.class, ctx);
        }
        return resolver;
    case SERVICE:
        if (isAnnotationStart(ctx)) {
            return CompletionItemResolver.get(BallerinaParser.AnnotationAttachmentContext.class, ctx);
        } else if ((prCtx == null || prCtx instanceof BallerinaParser.ObjectFieldDefinitionContext)
                && poppedTokens.contains(UtilSymbolKeys.EQUAL_SYMBOL_KEY)) {
            return CompletionItemResolver.get(BallerinaParser.VariableDefinitionStatementContext.class, ctx);
        }
        return this;
    case RESOURCE:
        if (prCtx != null) {
            return CompletionItemResolver.get(prCtx.getClass(), ctx);
        }
        return this;
    case OBJECT_TYPE:
        if (poppedTokens.contains(UtilSymbolKeys.EQUAL_SYMBOL_KEY)) {
            // If the popped tokens contains the equal symbol, then the variable definition is being writing
            ctx.put(CompletionKeys.PARSER_RULE_CONTEXT_KEY,
                    new BallerinaParser.VariableDefinitionStatementContext(null, -1));
            return CompletionItemResolver.get(BallerinaParser.VariableDefinitionStatementContext.class, ctx);

        }
        return this;
    case FUNCTION:
        if (prCtx == null) {
            return this;
        }
        CompletionItemScope contextResolver = CompletionItemResolver.get(prCtx.getClass(), ctx);
        return (contextResolver == null) ? this : contextResolver;
    case BLOCK_STATEMENT:
        ParserRuleContext parserRuleContext = ctx.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY);
        if (parserRuleContext != null) {
            return CompletionItemResolver.get(parserRuleContext.getClass(), ctx);
        } else {
            return CompletionItemResolver.get(BLangStatement.class, ctx);
        }
    case RECORD:
        if (poppedTokens.contains(UtilSymbolKeys.EQUAL_SYMBOL_KEY)) {
            // If the popped tokens contains the equal symbol, then the variable definition is being writing
            // This parser rule context is used to select the proper sorter.
            ctx.put(CompletionKeys.PARSER_RULE_CONTEXT_KEY,
                    new BallerinaParser.VariableDefinitionStatementContext(null, -1));
            return CompletionItemResolver.get(BallerinaParser.VariableDefinitionStatementContext.class, ctx);
        }
        return this;
    case PR_WORKER_INTERACTION:
        if (contextParent instanceof BallerinaParser.BinaryEqualExpressionContext) {
            contextParent = contextParent.getParent();
        }
        if (contextParent != null) {
            return CompletionItemResolver.get(contextParent.getClass(), ctx);
        }
        return this;
    case PR_WORKER_STMT:
        if (isInvocationOrInteractionOrFieldAccess(ctx)) {
            return this;
        }
        return CompletionItemResolver.get(BLangStatement.class, ctx);
    case PR_RETURN_STMT_DEFINITION:
        return CompletionItemResolver.get(BallerinaParser.VariableDefinitionStatementContext.class, ctx);
    case PR_GLOBAL_VARIABLE_DEFINITION:
        List<String> consumedTokens = ctx.get(CompletionKeys.FORCE_CONSUMED_TOKENS_KEY).stream()
                .map(Token::getText).collect(Collectors.toList());
        if (consumedTokens.get(0).equals(UtilSymbolKeys.FUNCTION_KEYWORD_KEY)) {
            return CompletionItemResolver.get(BallerinaParser.DefinitionContext.class, ctx);
        }
        return this;
    case PR_EXPRESSION:
        if (contextParent instanceof BallerinaParser.BinaryEqualExpressionContext
                || contextParent instanceof BallerinaParser.ExpressionListContext) {
            contextParent = contextParent.getParent();
        }
        if (contextParent != null) {
            return CompletionItemResolver.get(contextParent.getClass(), ctx);
        }
        return this;
    case PR_ASSIGNMENT_STATEMENT:
        return CompletionItemResolver.get(BallerinaParser.VariableDefinitionStatementContext.class, ctx);
    case PR_STATEMENT:
        if (isInvocationOrInteractionOrFieldAccess(ctx)) {
            return this;
        }
        return CompletionItemResolver.get(BLangStatement.class, ctx);
    case STATEMENT:
    case PARAMETER:
    case PACKAGE_NAME:
    case RECORD_LITERAL:
    case MATCH_EXPRESSION:
    case MATCH:
    case ANNOTATION_ATTACHMENT:
    case PR_WORKER_REPLY:
    case PR_WORKER_TRIGGER_WORKER_STMT:
    case PR_SERVICE_ENDPOINT_ATTACHMENT:
    case PR_SERVICE_DEFINITION:
    case PR_PANIC_STATEMENT:
    case PR_MATCH_STATEMENT:
    case PR_FUNCTION_DEFINITION:
    case PR_DEFINITION:
    case PR_CONDITIONAL_CLAUSE:
    case PR_ATTACHMENT_POINT:
    case PR_ANNOTATION_ATTACHMENT:
    case PR_ENDPOINT_DECLARATION:
    case PR_VARIABLE_DEFINITION:
        return this;
    }
    return null;
}

From source file:org.ballerinalang.langserver.completions.resolvers.parsercontext.ParserRuleExpressionContextContext.java

License:Open Source License

@Override
public CompletionItemsContext resolve(LSContext context) {
    ParserRuleContext contextParent = context.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY).getParent();
    if (contextParent instanceof BallerinaParser.BinaryEqualExpressionContext
            || contextParent instanceof BallerinaParser.ExpressionListContext) {
        contextParent = contextParent.getParent();
    }//from w  ww .  j  av  a  2 s  .c om
    if (contextParent != null) {
        return CompletionItemResolver.get(contextParent.getClass(), context);
    }

    return this;
}

From source file:org.ballerinalang.langserver.completions.resolvers.parsercontext.ParserRuleExpressionContextResolver.java

License:Open Source License

@Override
public List<CompletionItem> resolveItems(LSServiceOperationContext context) {
    ParserRuleContext contextParent = context.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY).getParent();
    if (contextParent instanceof BallerinaParser.BinaryEqualExpressionContext
            || contextParent instanceof BallerinaParser.ExpressionListContext) {
        contextParent = contextParent.getParent();
    }//from   w  w w.  ja v  a 2s. c  o m
    if (contextParent != null) {
        return CompletionItemResolver.get(contextParent.getClass()).resolveItems(context);
    }

    return new ArrayList<>();
}

From source file:org.ballerinalang.langserver.completions.resolvers.parsercontext.ParserRuleTypeNameContextResolver.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {

    ArrayList<CompletionItem> completionItems = new ArrayList<>();
    TokenStream tokenStream = completionContext.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
    ParserRuleContext parserRuleContext = completionContext.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY);
    CompletionItemSorter itemSorter = ItemSorters.getSorterByClass(DefaultItemSorter.class);

    if (parserRuleContext.getParent() instanceof BallerinaParser.CatchClauseContext
            && CommonUtil.isWithinBrackets(completionContext, Collections.singletonList(CATCH_KEY_WORD))) {
        this.populateCompletionItemList(filterCatchConditionSymbolInfo(completionContext), completionItems);
    } else if (tokenStream.get(completionContext.get(DocumentServiceKeys.TOKEN_INDEX_KEY)).getText()
            .equals(":")) {
        /*//from   w ww . java  2s  .  co  m
        TODO: ATM, this particular condition becomes true only when try to access packages' items in the 
        endpoint definition context
         */
        this.populateCompletionItemList(filterEndpointContextSymbolInfo(completionContext), completionItems);
    } else {
        StatementTemplateFilter statementTemplateFilter = new StatementTemplateFilter();
        // Add the statement templates
        completionItems.addAll(statementTemplateFilter.filterItems(completionContext));
        this.populateBasicTypes(completionItems, completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY));
        itemSorter = ItemSorters
                .getSorterByClass(completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY).getClass());
    }
    itemSorter.sortItems(completionContext, completionItems);

    return completionItems;
}

From source file:org.ballerinalang.langserver.completions.resolvers.parsercontext.ParserRuleWorkerInteractionContextContext.java

License:Open Source License

@Override
public CompletionItemsContext resolve(LSContext context) {
    ParserRuleContext contextParent = context.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY).getParent();
    if (contextParent instanceof BallerinaParser.BinaryEqualExpressionContext) {
        contextParent = contextParent.getParent();
    }//  ww  w .j a v  a2 s . c o  m
    if (contextParent != null) {
        return CompletionItemResolver.get(contextParent.getClass(), context);
    }

    return this;
}

From source file:org.ballerinalang.langserver.completions.resolvers.parsercontext.ParserRuleWorkerInteractionContextResolver.java

License:Open Source License

@Override
public List<CompletionItem> resolveItems(LSServiceOperationContext context) {
    ParserRuleContext contextParent = context.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY).getParent();
    if (contextParent instanceof BallerinaParser.BinaryEqualExpressionContext) {
        contextParent = contextParent.getParent();
    }//from  www  . j  a  v  a 2  s . com
    if (contextParent != null) {
        return CompletionItemResolver.get(contextParent.getClass()).resolveItems(context);
    }

    return new ArrayList<>();
}

From source file:org.tinygroup.template.interpret.TemplateFromContext.java

License:GNU General Public License

/**
 * ??TextContext?...?BlockContext//from   w  w w. j a v  a  2  s .com
 * @param parseTree
 * @return
 */
private ParserRuleContext getParseTrreeParentButBlock(ParserRuleContext parseTree) {
    ParserRuleContext parentParserRuleContext = parseTree.getParent();
    if (parentParserRuleContext instanceof TinyTemplateParser.TemplateContext)
        return parentParserRuleContext;
    if (parentParserRuleContext instanceof TinyTemplateParser.BlockContext) {
        if (parentParserRuleContext.getParent() instanceof TinyTemplateParser.TemplateContext)
            return parentParserRuleContext;
        if (parentParserRuleContext.getParent() instanceof TinyTemplateParser.BlockContext) {
            return getParseTrreeParentButBlock(parentParserRuleContext.getParent());
        } else
            return parentParserRuleContext.getParent();
    }
    return parentParserRuleContext;
}

From source file:processing.mode.java.preproc.PdeParseTreeListener.java

License:Open Source License

/**
 * Manage parsing out a size or fullscreen call.
 *
 * @param ctx The context of the call./*from  w w w  . j  av  a  2 s  .  c o m*/
 */
private void handleSizeCall(ParserRuleContext ctx) {
    ParserRuleContext testCtx = ctx.getParent().getParent().getParent().getParent();

    boolean isInGlobal = testCtx instanceof ProcessingParser.StaticProcessingSketchContext;

    boolean isInSetup;
    if (!isInGlobal) {
        ParserRuleContext methodDeclaration = testCtx.getParent().getParent();

        isInSetup = isMethodSetup(methodDeclaration);
    } else {
        isInSetup = false;
    }

    ParseTree argsContext = ctx.getChild(2);

    boolean thisRequiresRewrite = false;

    boolean isSize = ctx.getChild(0).getText().equals(SIZE_METHOD_NAME);
    boolean isFullscreen = ctx.getChild(0).getText().equals(FULLSCREEN_METHOD_NAME);

    if (isInGlobal || isInSetup) {
        thisRequiresRewrite = true;

        if (isSize && argsContext.getChildCount() > 2) {
            sketchWidth = argsContext.getChild(0).getText();
            if (PApplet.parseInt(sketchWidth, -1) == -1 && !sketchWidth.equals("displayWidth")) {
                thisRequiresRewrite = false;
            }

            sketchHeight = argsContext.getChild(2).getText();
            if (PApplet.parseInt(sketchHeight, -1) == -1 && !sketchHeight.equals("displayHeight")) {
                thisRequiresRewrite = false;
            }

            if (argsContext.getChildCount() > 3) {
                sketchRenderer = argsContext.getChild(4).getText();
                if (!(sketchRenderer.equals("P2D") || sketchRenderer.equals("P3D")
                        || sketchRenderer.equals("OPENGL") || sketchRenderer.equals("JAVA2D")
                        || sketchRenderer.equals("FX2D"))) {
                    thisRequiresRewrite = false;
                }
            }
        }

        if (isFullscreen) {
            sketchWidth = "displayWidth";
            sketchWidth = "displayHeight";

            thisRequiresRewrite = true;
            sizeIsFullscreen = true;

            if (argsContext.getChildCount() > 0) {
                sketchRenderer = argsContext.getChild(0).getText();
                if (!(sketchRenderer.equals("P2D") || sketchRenderer.equals("P3D")
                        || sketchRenderer.equals("OPENGL") || sketchRenderer.equals("JAVA2D")
                        || sketchRenderer.equals("FX2D"))) {
                    thisRequiresRewrite = false;
                }
            }
        }
    }

    if (thisRequiresRewrite) {
        createDelete(ctx.start, ctx.stop);
        createInsertAfter(ctx.stop, "/* size commented out by preprocessor */");
        sizeRequiresRewrite = true;
    }
}