@Override public void action(RuleContext _localctx, int ruleIndex, int actionIndex) { switch (ruleIndex) { case 6: MINUS_action((RuleContext) _localctx, actionIndex); break; case 7: SLASH_action((RuleContext) _localctx, actionIndex); break; case 8: LPAR_action((RuleContext) _localctx, actionIndex); break; case 9: RPAR_action((RuleContext) _localctx, actionIndex); break; case 19: WS_action((RuleContext) _localctx, actionIndex); break; } }
/** * En partant d'une noeud permet de déterminer si il est dans l'expression d'une abstraction ou non. * * @param ctx Le noeud courant * @return Retourne vrai si le noeud est sous une abstraction et faux dans l'autre cas. */ private boolean underAbs(RuleContext ctx) { RuleContext parent = ctx.parent; if (parent == null) { return false; } while (parent.getParent() != null) { if (parent instanceof LambdaParser.AbstractionContext) { return true; } parent = parent.parent; } return false; }
public static AstBuilder createAstBuilder(CompilationUnit source,TokenStream tokens){ KalangParser p = new KalangParser(tokens); AstBuilder sp = new AstBuilder(source, p); p.setErrorHandler(new DefaultErrorStrategy() { @Override public void reportError(Parser recognizer, RecognitionException e) { String msg = AntlrErrorString.exceptionString(recognizer, e); Token end = e.getOffendingToken(); Token start; RuleContext ctx = e.getCtx(); if(ctx instanceof ParserRuleContext){ start = ((ParserRuleContext) ctx).getStart(); }else{ start = end; } sp.getDiagnosisReporter().report(Diagnosis.Kind.ERROR, msg,start,end); } }); return sp; }
@Override public String formatMessage(SyntaxError error) { RecognitionException exception = error.getException(); if (exception instanceof InputMismatchException) { InputMismatchException mismatchException = (InputMismatchException) exception; RuleContext ctx = mismatchException.getCtx(); int ruleIndex = ctx.getRuleIndex(); switch (ruleIndex) { case ProtoParser.RULE_ident: return ProtostuffBundle.message("error.expected.identifier"); default: break; } } return super.formatMessage(error); }
private static ExprContext rewriteHaving(GeneratorContext ctx, Planner planner, ExprContext expr) { ExprContext rewritten = new ExprContext(expr.getParent(), expr.invokingState); for (ParseTree i:expr.children) { if (i instanceof Expr_functionContext) { rewritten.addChild(rewriteHaving(ctx, planner, (Expr_functionContext)i)); } else if (i instanceof ExprContext) { rewritten.addChild(rewriteHaving(ctx, planner, (ExprContext)i)); } else if (i instanceof RuleContext) { rewritten.addChild((RuleContext)i); } else if (i instanceof TerminalNode) { rewritten.addChild((TerminalNode)i); } else { throw new CodingError(); } } return rewritten; }
private static RuleContext rewriteHaving(GeneratorContext ctx, Planner planner, Expr_functionContext rule) { String funcname = rule.function_name().getText().toLowerCase(); if (_aggregates.contains(funcname)) { OutputField field = findExisting(ctx, planner, rule); if (field != null) { return createColumnName_(rule, field); } else { Operator expr = ExprGenerator.gen(ctx, planner, (ExprContext)rule.getParent()); field = planner.addOutputField("*" + planner.getOutputFields().size(), expr); return createColumnName_(rule, field); } } else { return rule; } }
/** * Searches the children of the given context for a context of the given clazz * type and returns its Text Value. If there are multiple relevant child nodes, * we will return the text value for one of them at random. */ public String getChildContextText(RuleContext ctx) { if (ctx == null) { return ""; } if (ctx instanceof TypeNameContext) { return ctx.getText(); } String s = ""; for (int i = 0; i < ctx.getChildCount(); i++) { if (!(ctx.getChild(i) instanceof TerminalNodeImpl)) { try { String t = getChildContextText((RuleContext) ctx.getChild(i)); if (!t.isEmpty()) { s += t + ","; } } catch (Exception e) { // do nothing } } } return s.replaceAll(",$", ""); }
@Override public Object visitPairedTag(StpParser.PairedTagContext ctx) { RuleContext tag = (RuleContext) ctx.getChild(0); Token firstOTag = searchTokenFromHead(tag, SilverstripeTokenId.OTAG); Token firstCTag = searchTokenFromHead(tag, SilverstripeTokenId.CTAG); Token secondOTag = searchTokenFromTail(tag, SilverstripeTokenId.OTAG); Token secondCTag = searchTokenFromTail(tag, SilverstripeTokenId.CTAG); boolean offsetMatched = (position >= firstOTag.getStartIndex() && position <= firstCTag.getStopIndex()) || (position >= secondOTag.getStartIndex() && position <= secondCTag.getStopIndex()); if(offsetMatched) { matchedRegions.add(firstOTag.getStartIndex()); matchedRegions.add(firstCTag.getStopIndex()+1); matchedRegions.add(secondOTag.getStartIndex()); matchedRegions.add(secondCTag.getStopIndex()+1); } return super.visitPairedTag(ctx); }
private String translateToAssertionsPushCalls(SingleExpressionContext assertionStatementLeftHandSide, RuleContext ctx) { String translatedText = ctx.getText(); String xpath = "//singleExpression"; ParseTree parseTree = (ParseTree) ctx; translatedText = translateAssertionFunctionShortcuts(ctx, false); ParseTreePattern assertionExpressionPattern = this.parser.compileParseTreePattern( "<assertionOperator> <singleExpression>", HL7MappingValidatorParser.RULE_singleExpression); List<ParseTreeMatch> assertionExpressionMatches = assertionExpressionPattern.findAll(parseTree, xpath); for (ParseTreeMatch match : assertionExpressionMatches) { AssertionOperatorContext operator = (AssertionOperatorContext) match.get("assertionOperator"); SingleExpressionContext assertionStatementRightHandSide = (SingleExpressionContext) match.get("singleExpression"); String translatedFunctionCall = translateToAssertionsPushCall(assertionStatementLeftHandSide, operator, assertionStatementRightHandSide); String matchTextTranslated = translateAssertionFunctionShortcuts((RuleContext) match.getTree(), false); translatedText = translatedText.replace(matchTextTranslated, translatedFunctionCall); } return translatedText; }
private <ExprType> Declaration findProperMethodScopeAround(final ExprType ctxExpr, final RuleContext ctxParent, final Token ctxGetStart) { Declaration retval = null; RuleContext executionContext = ctxParent; while ((executionContext instanceof ProgramContext) == false) { if (executionContext instanceof Method_declContext) { final Method_declContext mdeclContext = (Method_declContext)executionContext; final Method_decl_hookContext hookContext = mdeclContext.method_decl_hook(); final Method_signatureContext signatureCtx = hookContext.method_signature(); final String methodName = signatureCtx.method_name().getText(); final MethodDeclaration methodDecl = currentScope_.lookupMethodDeclarationRecursive(methodName, null, true); retval = methodDecl; break; } else if (executionContext instanceof Role_bodyContext) { break; } else if (executionContext instanceof Role_declContext) { break; } else if (executionContext instanceof Class_bodyContext) { break; } else if (executionContext instanceof Type_declarationContext) { break; } executionContext = executionContext.parent; } return retval; }
@Override @RuleDependencies({ @RuleDependency(recognizer=GrammarParser.class, rule=GrammarParser.RULE_modeSpec, version=3, dependents=Dependents.PARENTS), @RuleDependency(recognizer=GrammarParser.class, rule=GrammarParser.RULE_ruleSpec, version=3, dependents=Dependents.PARENTS), }) public Tuple2<? extends ParseTree, Integer> visitModeSpec(ModeSpecContext ctx) { // use the preceeding rule (if any), otherwise relative to mode for (int i = priorSiblings.size() - 2; i >= 0; i--) { ParseTree sibling = priorSiblings.get(i); if (!(sibling instanceof RuleNode)) { continue; } RuleContext context = ((RuleNode)sibling).getRuleContext(); if (context.getRuleIndex() == GrammarParser.RULE_ruleSpec) { return Tuple.create(context, 0); } } return Tuple.create(ctx, getCodeStyle().getIndentSize()); }
public static RuleContext getTopContext(Parser parser, RuleContext context, IntervalSet values, boolean checkTop) { if (checkTop && context instanceof ParserRuleContext) { if (values.contains(context.getRuleIndex())) { return context; } } if (context.isEmpty()) { return null; } if (values.contains(parser.getATN().states.get(context.invokingState).ruleIndex)) { return context.parent; } return getTopContext(parser, context.parent, values, false); }
@Override public Map<RuleContext, CaretReachedException> getParseTrees(TParser parser) { List<MultipleDecisionData> potentialAlternatives = new ArrayList<>(); IntegerList currentPath = new IntegerList(); Map<RuleContext, CaretReachedException> results = new IdentityHashMap<>(); // make sure the token stream is initialized before getting the index parser.getInputStream().LA(1); int initialToken = parser.getInputStream().index(); while (true) { parser.getInputStream().seek(initialToken); tryParse(parser, potentialAlternatives, currentPath, results); if (!incrementCurrentPath(potentialAlternatives, currentPath)) { break; } } LOGGER.log(Level.FINE, "Forest parser constructed {0} parse trees.", results.size()); if (LOGGER.isLoggable(Level.FINEST)) { for (Map.Entry<RuleContext, CaretReachedException> entry : results.entrySet()) { LOGGER.log(Level.FINEST, entry.getKey().toStringTree(parser instanceof Parser ? (Parser)parser : null)); } } return results; }
public static Interval getSourceInterval(@NonNull ParseTree context) { Parameters.notNull("context", context); if (context instanceof TerminalNode) { TerminalNode terminalNode = (TerminalNode)context; Token token = terminalNode.getSymbol(); return new Interval(token.getStartIndex(), token.getStopIndex()); } else if (context instanceof RuleNode) { RuleNode ruleNode = (RuleNode)context; RuleContext ruleContext = ruleNode.getRuleContext(); if (ruleContext instanceof ParserRuleContext) { return getSourceInterval((ParserRuleContext)ruleContext); } else { Token startSymbol = getStartSymbol(context); Token stopSymbol = getStopSymbol(context); if (startSymbol == null || stopSymbol == null) { return Interval.INVALID; } return new Interval(startSymbol.getStartIndex(), stopSymbol.getStopIndex()); } } else { return Interval.INVALID; } }
public static Token getStartSymbol(ParseTree context) { TerminalNode node = getStartNode(context); if (node != null) { return node.getSymbol(); } if (!(context instanceof RuleNode)) { return null; } RuleContext ruleContext = ((RuleNode)context).getRuleContext(); if (ruleContext instanceof ParserRuleContext) { return ((ParserRuleContext)ruleContext).getStart(); } return null; }
@CheckForNull public static <ContextClass> ContextClass findAncestor(@NonNull ParseTree tree, @NonNull Class<ContextClass> nodeType) { for (ParseTree current = tree; current != null; current = current.getParent()) { if (!(current instanceof RuleNode)) { continue; } RuleNode ruleNode = (RuleNode)current; RuleContext ruleContext = ruleNode.getRuleContext(); if (nodeType.isInstance(ruleContext)) { return nodeType.cast(ruleContext); } } return null; }
@Override public void enterBeq_beq(tnsnamesParser.Beq_beqContext ctx) { RuleContext parentCtx = ctx.getParent(); String parentRule = this.ruleNames[parentCtx.getRuleIndex()]; // Check for redefinitions. BEQ. if (parentRule == "beq_parameter") { checkForRedefinition("PROTOCOL", firstBEQ_ProtocolLine, this.lineNumber); } // Check for redefinitions. ARGS. if (parentRule == "bad_address") { checkForRedefinition("PROTOCOL", firstBAD_ProtocolLine, this.lineNumber); } }
@Override public void enterD_recv_buf(tnsnamesParser.D_recv_bufContext ctx) { // Who is my parent rule? RuleContext parentCtx = ctx.getParent(); String parentRule = this.ruleNames[parentCtx.getRuleIndex()]; // Check for redefinitions, DESCRIPTION. if (parentRule == "d_parameter") { checkForRedefinition("RECV_BUF_SIZE", firstRecvBufLine, this.lineNumber); } // Check for redefinitions, DESCRIPTION. if (parentRule == "a_parameter") { checkForRedefinition("RECV_BUF_SIZE", firstA_RecvBufLine, this.lineNumber); } }
@Override public void enterD_send_buf(tnsnamesParser.D_send_bufContext ctx) { // Who is my parent rule? RuleContext parentCtx = ctx.getParent(); String parentRule = this.ruleNames[parentCtx.getRuleIndex()]; // Check for redefinitions, DESCRIPTION. if (parentRule == "d_parameter") { checkForRedefinition("SEND_BUF_SIZE", firstSendBufLine, this.lineNumber); } // Check for redefinitions, DESCRIPTION. if (parentRule == "a_parameter") { checkForRedefinition("SEND_BUF_SIZE", firstA_SendBufLine, this.lineNumber); } }
@NotNull protected ATNConfigSet computeStartState(@NotNull ATNState p, @Nullable RuleContext ctx, boolean fullCtx) { // always at least the implicit call to start rule PredictionContext initialContext = PredictionContext.fromRuleContext(atn, ctx); ATNConfigSet configs = new ATNConfigSet(fullCtx); for (int i=0; i<p.getNumberOfTransitions(); i++) { ATNState target = p.transition(i).target; ATNConfig c = new ATNConfig(target, i+1, initialContext); Set<ATNConfig> closureBusy = new HashSet<ATNConfig>(); closure(c, configs, closureBusy, true, fullCtx, false); } return configs; }
private void RPAR_action(RuleContext _localctx, int actionIndex) { switch (actionIndex) { case 3: _channel = HIDDEN; break; } }
private void WS_action(RuleContext _localctx, int actionIndex) { switch (actionIndex) { case 4: _channel = HIDDEN; break; } }
private void SLASH_action(RuleContext _localctx, int actionIndex) { switch (actionIndex) { case 1: _channel = HIDDEN; break; } }
private void LPAR_action(RuleContext _localctx, int actionIndex) { switch (actionIndex) { case 2: _channel = HIDDEN; break; } }
private void MINUS_action(RuleContext _localctx, int actionIndex) { switch (actionIndex) { case 0: _channel = HIDDEN; break; } }
public ParseTree producesSyntaxTree(String expected) { try { RuleContext ruleContext = (RuleContext) method.invoke(parser); String actual = ruleContext.toStringTree(parser); assertThat(actual) .named(code) .isEqualTo(expected); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } return this; }
private RuleContext findParentNode(RuleContext ctx, Class<?> classtype) { RuleContext p = ctx.getParent(); while(p!=null) { if(p.getClass().equals(classtype)) { return p; } p = p.getParent(); } return null; }
private ParseTree findLeftSibling(RuleContext ctx) { RuleContext p = ctx.getParent(); if(p!=null) { for(int i=0; i<p.getChildCount(); i++) { if(p.getChild(i).equals(ctx)) { if(i>0) return p.getChild(i-1); break; } } } return null; }