List of usage examples for org.antlr.v4.runtime ParserRuleContext getRuleIndex
public int getRuleIndex()
From source file:dijkstra.utility.DijkstraTraceListener.java
License:Open Source License
private String ruleName(ParserRuleContext ctx) { return "[" + parser.getRuleNames()[ctx.getRuleIndex()] + "]"; }
From source file:org.hibernate.sqm.parser.hql.internal.HqlParseTreePrinter.java
License:Apache License
@Override public void enterEveryRule(@NotNull ParserRuleContext ctx) { final String ruleName = parser.getRuleNames()[ctx.getRuleIndex()]; if (!ruleName.endsWith("Keyword")) { HQL_LOGGER.debugf("%s %s (%s) [`%s`]", enterRulePadding(), ctx.getClass().getSimpleName(), ruleName, ctx.getText());/*from w w w .java 2s . co m*/ } super.enterEveryRule(ctx); }
From source file:org.hibernate.sqm.parser.hql.internal.HqlParseTreePrinter.java
License:Apache License
@Override public void exitEveryRule(@NotNull ParserRuleContext ctx) { super.exitEveryRule(ctx); final String ruleName = parser.getRuleNames()[ctx.getRuleIndex()]; if (!ruleName.endsWith("Keyword")) { HQL_LOGGER.debugf("%s %s (%s) [`%s`]", exitRulePadding(), ctx.getClass().getSimpleName(), parser.getRuleNames()[ctx.getRuleIndex()], ctx.getText()); }//from w w w . ja va2 s . com }
From source file:org.mar9000.salt.visitor.SaltTextVisitor.java
License:Open Source License
private String genericRule(ParserRuleContext ctx) { StringBuffer buf = new StringBuffer(OPEN_RULE); int i = ctx.getRuleIndex(); String ruleName = ruleNames[i]; buf.append(ruleName);/*from w ww . j a va 2 s . c o m*/ afterRule(buf); String childrenResult = visitChildren(ctx); buf.append(childrenResult).append(CLOSE_RULE); return buf.toString(); }
From source file:org.rnott.mock.ExpressionLanguageEvaluator.java
License:Apache License
/** * Begin processing an EL expression./* w w w. j a v a 2 s . c o m*/ * <p> * @param ctx the EL expression context. */ @Override public void enterExpression(ExpressionContext ctx) { // expression is an EL method or property super.enterExpression(ctx); ParserRuleContext p = (ParserRuleContext) ctx.getChild(0); if (p == null) { //throw new IllegalStateException( ctx. ); } switch (p.getRuleIndex()) { case ExpressionLanguageParser.RULE_method: // append invocation of an EL method content.append(evaluate((MethodContext) p)); break; case ExpressionLanguageParser.RULE_property: // append resolution of an EL property content.append(evaluate((PropertyContext) p)); break; } }
From source file:org.rnott.mock.ExpressionLanguageEvaluator.java
License:Apache License
/** * Evaluate an EL method./*from w ww.j ava 2s . c om*/ * <p> * @param ctx the EL method context. * @return the resolved value. * @throws IllegalStateException if the EL method specifies an unknown evaluator. */ private Object evaluate(MethodContext ctx) { String s = ctx.getChild(1).getText(); int pos = s.indexOf('.'); String type = s.substring(0, pos); String method = s.substring(pos + 1); ParametersContext pc = (ParametersContext) ctx.getChild(2); List<Object> params = new ArrayList<Object>(); for (int i = 1, count = pc.getChildCount() - 1; i < count; i++) { if (pc.getChild(i) instanceof ParameterContext) { ParserRuleContext p = (ParserRuleContext) pc.getChild(i).getChild(0); switch (p.getRuleIndex()) { case ExpressionLanguageParser.RULE_literal: params.add(evaluate((LiteralContext) p)); break; case ExpressionLanguageParser.RULE_method: params.add(evaluate((MethodContext) p)); break; case ExpressionLanguageParser.RULE_property: params.add(evaluate((PropertyContext) p)); break; } } } return MockContext.get().getEvaluator(type).evaluate(method, params.toArray()); }
From source file:org.tvl.goworks.editor.go.formatting.GoIndentTask.java
License:Open Source License
@RuleDependencies({ @RuleDependency(recognizer = GoParser.class, rule = GoParser.RULE_constDecl, version = 0), @RuleDependency(recognizer = GoParser.class, rule = GoParser.RULE_typeDecl, version = 0), @RuleDependency(recognizer = GoParser.class, rule = GoParser.RULE_varDecl, version = 0), @RuleDependency(recognizer = GoParser.class, rule = GoParser.RULE_importDecl, version = 0), @RuleDependency(recognizer = GoParser.class, rule = GoParser.RULE_block, version = 0), @RuleDependency(recognizer = GoParser.class, rule = GoParser.RULE_literalValue, version = 0), @RuleDependency(recognizer = GoParser.class, rule = GoParser.RULE_structType, version = 0), @RuleDependency(recognizer = GoParser.class, rule = GoParser.RULE_interfaceType, version = 0), @RuleDependency(recognizer = GoParser.class, rule = GoParser.RULE_exprSwitchStmt, version = 0), @RuleDependency(recognizer = GoParser.class, rule = GoParser.RULE_typeSwitchStmt, version = 0), @RuleDependency(recognizer = GoParser.class, rule = GoParser.RULE_selectStmt, version = 0), }) private int getIndent(ParseTree node) throws BadLocationException { // System.out.println(node.toStringTree(parser)); int nodeLineStart = -1; for (ParseTree current = node; current != null; current = current.getParent()) { if (!(current instanceof RuleNode)) { continue; }// w w w.j a va 2s . c o m ParserRuleContext ruleContext = (ParserRuleContext) ((RuleNode) current).getRuleContext(); if (nodeLineStart == -1) { nodeLineStart = context.lineStartOffset(ruleContext.start.getStartIndex()); } switch (ruleContext.getRuleIndex()) { case GoParser.RULE_constDecl: case GoParser.RULE_typeDecl: case GoParser.RULE_varDecl: case GoParser.RULE_importDecl: { TerminalNode leftParen = ruleContext.getToken(GoParser.LeftParen, 0); if (leftParen == null) { continue; } // get the indent of the line where the block starts int blockLineOffset = context.lineStartOffset(leftParen.getSymbol().getStartIndex()); int blockIndent = context.lineIndent(blockLineOffset); if (nodeLineStart == blockLineOffset) { return blockIndent; } if (node instanceof TerminalNode) { // no extra indent if the first node on the line is the closing brace of the block if (node == ruleContext.getToken(GoParser.RightParen, 0)) { return blockIndent; } } return blockIndent + getCodeStyle().getIndentSize(); } case GoParser.RULE_block: case GoParser.RULE_literalValue: case GoParser.RULE_structType: case GoParser.RULE_interfaceType: case GoParser.RULE_exprSwitchStmt: case GoParser.RULE_typeSwitchStmt: case GoParser.RULE_selectStmt: { TerminalNode leftBrace = ruleContext.getToken(GoParser.LeftBrace, 0); if (leftBrace == null) { continue; } // get the indent of the line where the block starts int blockLineOffset = context.lineStartOffset(leftBrace.getSymbol().getStartIndex()); int blockIndent = context.lineIndent(blockLineOffset); if (nodeLineStart == blockLineOffset) { return blockIndent; } if (node instanceof TerminalNode) { // no extra indent if the first node on the line is the closing brace of the block if (node == ruleContext.getToken(GoParser.RightBrace, 0)) { return blockIndent; } else { Token symbol = ((TerminalNode) node).getSymbol(); switch (symbol.getType()) { case GoParser.Case: case GoParser.Default: return blockIndent; default: break; } } } return blockIndent + getCodeStyle().getIndentSize(); } default: if (current.getParent() == null) { int outerLineOffset = context.lineStartOffset(ruleContext.start.getStartIndex()); int outerIndent = context.lineIndent(outerLineOffset); return outerIndent; } continue; } } return 0; }
From source file:org.tvl.goworks.editor.go.parser.CodeModelBuilderListener.java
License:Open Source License
@NonNull private static String createAnonymousTypeName(@NonNull ParserRuleContext context) { return String.format("$%s_%d", GoParser.ruleNames[context.getRuleIndex()], context.getStart().getStartIndex()); }
From source file:tilda.grammar.TildaSQLTreePrinter.java
License:Apache License
@Override public void enterEveryRule(ParserRuleContext ctx) { LOG.debug(PaddingUtil.getPad(ctx.depth() * 3) + _Parser.getRuleNames()[ctx.getRuleIndex()]); if (isLeafNode(ctx) == true) LOG.debug(PaddingUtil.getPad(ctx.depth() * 3) + " -] " + ctx.getText()); if (_Path.containsKey(ctx.parent) == false) _Path.put(ctx.parent, new ArrayList<String>()); if (_Path.containsKey(ctx) == false) _Path.put(ctx, new ArrayList<String>()); _Path.get(ctx).add(_Parser.getRuleNames()[ctx.getRuleIndex()]); }