List of usage examples for org.antlr.v4.runtime.tree RuleNode getChild
@Override
ParseTree getChild(int i);
From source file:codemate.Fortran.FortranTemplater.java
License:Open Source License
/** * visitChildren/*from w w w . j ava 2 s . c om*/ * * This method overrides the original one which will be interfered when I * make changes to the parse tree (e.g. inserting multiple nodes) by * changing the loop style. */ public Void visitChildren(RuleNode node) { Void result = defaultResult(); for (int i = 0; i < node.getChildCount(); i++) { if (!shouldVisitNextChild(node, result)) break; ParseTree c = node.getChild(i); Void childResult = c.accept(this); result = aggregateResult(result, childResult); } return result; }
From source file:com.blazebit.persistence.impl.expression.JPQLSelectExpressionVisitorImpl.java
License:Apache License
@Override public Expression visitChildren(RuleNode node) { CompositeExpression result = null;// w w w .jav a 2 s.co m int n = node.getChildCount(); if (shouldVisitNextChild(node, result)) { if (n > 0 && shouldVisitNextChild(node, result)) { if (n == 1) { return node.getChild(0).accept(this); } else { result = accept(node.getChild(0)); for (int i = 1; i < n; i++) { if (!shouldVisitNextChild(node, result)) { break; } ParseTree c = node.getChild(i); acceptAndCompose(result, c); } } } } return result; }
From source file:com.espertech.esper.epl.parse.ASTConstantHelper.java
License:Open Source License
/** * Parse the AST constant node and return Object value. * @param node - parse node for which to parse the string value * @return value matching AST node type//from ww w . ja v a 2 s. c o m */ public static Object parse(ParseTree node) { if (node instanceof TerminalNode) { TerminalNode terminal = (TerminalNode) node; switch (terminal.getSymbol().getType()) { case EsperEPL2GrammarParser.BOOLEAN_TRUE: return BoolValue.parseString(terminal.getText()); case EsperEPL2GrammarParser.BOOLEAN_FALSE: return BoolValue.parseString(terminal.getText()); case EsperEPL2GrammarParser.VALUE_NULL: return null; default: throw ASTWalkException.from( "Encountered unexpected constant type " + terminal.getSymbol().getType(), terminal.getSymbol()); } } else { RuleNode ruleNode = (RuleNode) node; int ruleIndex = ruleNode.getRuleContext().getRuleIndex(); if (ruleIndex == EsperEPL2GrammarParser.RULE_number) { return parseNumber(ruleNode, 1); } else if (ruleIndex == EsperEPL2GrammarParser.RULE_numberconstant) { RuleNode number = findChildRuleByType(ruleNode, EsperEPL2GrammarParser.RULE_number); if (ruleNode.getChildCount() > 1) { if (ASTUtil.isTerminatedOfType(ruleNode.getChild(0), EsperEPL2GrammarLexer.MINUS)) { return parseNumber(number, -1); } return parseNumber(number, 1); } else { return parseNumber(number, 1); } } else if (ruleIndex == EsperEPL2GrammarParser.RULE_stringconstant) { return StringValue.parseString(node.getText()); } else if (ruleIndex == EsperEPL2GrammarParser.RULE_constant) { return parse(ruleNode.getChild(0)); } throw ASTWalkException.from("Encountered unrecognized constant", node.getText()); } }
From source file:com.espertech.esper.epl.parse.ASTConstantHelper.java
License:Open Source License
private static int getSingleChildTokenType(RuleNode node) { return ((TerminalNode) node.getChild(0)).getSymbol().getType(); }
From source file:net.klazz.symboliclua.conv.Converter.java
License:Open Source License
@Override public String visitChildren(RuleNode node) { StringBuilder builder = new StringBuilder(); List<String> spaces = whitespaces(node); for (int i = 0; i < node.getChildCount(); i++) { builder.append(spaces.get(i));//from ww w . j a v a2 s .c om builder.append(super.visit(node.getChild(i))); } builder.append(spaces.get(node.getChildCount())); return builder.toString(); }
From source file:net.klazz.symboliclua.conv.Converter.java
License:Open Source License
private List<String> whitespaces(RuleNode node) { List<String> ret = new ArrayList<>(); Interval interval = node.getSourceInterval(); int a = node.getParent() == null ? 0 : interval.a; int b = node.getParent() == null ? mStream.size() - 1 : interval.b; for (int i = 0; i < node.getChildCount(); i++) { ParseTree c = node.getChild(i); Interval v = c.getSourceInterval(); ret.add(a < v.a ? mStream.getText(new Interval(a, v.a - 1)) : ""); a = v.b + 1;/* w w w . j a va 2 s. co m*/ } ret.add(a <= b ? mStream.getText(new Interval(a, b)) : ""); return ret; }
From source file:net.lldp.checksims.algorithm.syntaxtree.java.SuperQuickTreeWalker.java
License:Open Source License
@Override public AST visitChildren(RuleNode rn) { String name = rn.getClass().getSimpleName(); int children = rn.getChildCount(); List<AST> asts = new LinkedList<AST>(); for (int i = 0; i < children; i++) { ParseTree pt = rn.getChild(i); AST t = pt.accept(this); if (t == null) { t = new AST(name); }//from w w w . j a v a 2s . com asts.add(t); } return new AST(name, asts.stream()); }
From source file:nl.han.ica.ap.purify.module.java.duplicatecode.HashVisitor.java
License:Open Source License
@Override public Integer visitChildren(RuleNode arg0) { int iHash = 1; boolean bVisit = false; for (int i = arg0.getChildCount() - 1; i >= 0; i--) { Integer iVisit = visit(arg0.getChild(i)); // If the visit resulted no hash ignore the hash. if (iVisit != null) { iHash = iHash * PRIME + iVisit; bVisit = true;/* w w w . j a v a 2 s . c om*/ } } if (bVisit) { return iHash; } else { return defaultResult(); // No result. } }
From source file:no.sintef.bvr.constraints.bcl.BCLBuilder.java
License:LGPL
public BCLExpression recurse(RuleNode root, int depth, BVRModel cu, boolean verbose) { String name;/*from ww w . j a v a 2 s . c om*/ // Collapse place holder nodes while (true) { while (root.getChildCount() == 1 && (root.getChild(0) instanceof RuleNode)) root = (RuleNode) root.getChild(0); // Get name name = root.getClass().getSimpleName(); name = name.substring(0, name.length() - 7); if (name.equals("Expterm") || name.equals("ExpLogUn")) { if (root.getChildCount() == 3 && root.getChild(0).toString().equals("(") && root.getChild(2).toString().equals(")")) root = (RuleNode) root.getChild(1); else if (root.getChildCount() == 4 && root.getChild(1).toString().equals("(") && root.getChild(3).toString().equals(")")) { break; } else break; } else { break; } } // Get name name = root.getClass().getSimpleName(); name = name.substring(0, name.length() - 7); // print type if (verbose) { for (int i = 0; i < depth; i++) System.out.print(" "); System.out.print(name); } // Construct Node BCLExpression e; if (root instanceof VspecContext) { // Get fully qualified name VspecContext rt = (VspecContext) root; List<String> fcname = new ArrayList<String>(); fcname.add(root.getChild(0).toString()); while (rt.getChildCount() == 3) { rt = (VspecContext) rt.getChild(2); fcname.add(rt.getChild(0).toString()); } TargetRef r = BvrFactory.eINSTANCE.createTargetRef(); VSpec prev = null, cur = null; for (String s : fcname) { if (prev == null) { prev = findVspec(s, cu); cur = prev; } else { cur = findVspec(s, prev); } } if (verbose) System.out.println(" " + fcname); if (cur == null) throw new UnsupportedOperationException("can not find vspec with the given name -> " + fcname); Target vspecTarget = strategy.getVSpecTarget(cur); r.setTarget(vspecTarget); e = r; } else if (root instanceof LiteralexpContext) { String s = root.getChild(0).toString(); if (s.startsWith("\"") && s.endsWith("\"")) { StringLiteralExp r = BvrFactory.eINSTANCE.createStringLiteralExp(); s = s.substring(1, s.length() - 1); r.setString(s); e = r; } else if (s.contains(".")) { RealLiteralExp r = BvrFactory.eINSTANCE.createRealLiteralExp(); r.setReal(s); e = r; } else if (s.equals("true") || s.equals("false")) { BooleanLiteralExp r = BvrFactory.eINSTANCE.createBooleanLiteralExp(); r.setBool(Boolean.parseBoolean(s)); e = r; } else { IntegerLiteralExp r = BvrFactory.eINSTANCE.createIntegerLiteralExp(); r.setInteger(new Integer(s)); e = r; } if (verbose) System.out.println(" " + s); } else { OperationCallExp o = BvrFactory.eINSTANCE.createOperationCallExp(); if (verbose) System.out.println(); if (root.getChildCount() == 3) { // Find operation String opStr = root.getChild(1).toString(); // Map if (opStr.equals("implies")) o.setOperation(bvr.Operation.getByName("logImplies")); else if (opStr.equals("iff")) o.setOperation(bvr.Operation.getByName("logIff")); else if (opStr.equals("and")) o.setOperation(bvr.Operation.getByName("logAnd")); else if (opStr.equals("or")) o.setOperation(bvr.Operation.getByName("logOr")); else if (opStr.equals("xor")) o.setOperation(bvr.Operation.getByName("logXor")); else if (opStr.equals("=")) o.setOperation(bvr.Operation.getByName("eq")); else if (opStr.equals("<=")) o.setOperation(bvr.Operation.getByName("lte")); else if (opStr.equals(">=")) o.setOperation(bvr.Operation.getByName("gte")); else if (opStr.equals("<")) o.setOperation(bvr.Operation.getByName("lt")); else if (opStr.equals(">")) o.setOperation(bvr.Operation.getByName("gt")); else if (opStr.equals("*")) o.setOperation(bvr.Operation.getByName("arithMult")); else if (opStr.equals("/")) o.setOperation(bvr.Operation.getByName("arithDev")); else if (opStr.equals("+")) o.setOperation(bvr.Operation.getByName("arithPlus")); else if (opStr.equals("-")) o.setOperation(bvr.Operation.getByName("arithMinus")); else { for (int i = 0; i < root.getChildCount(); i++) System.out.println(root.getChild(i)); throw new UnsupportedOperationException(); } // Recurse // System.out.println("\"" + root.getChild(0).toString()+"\""); BCLExpression a1 = recurse((RuleNode) root.getChild(0), depth + 1, cu, verbose); o.getArgument().add(a1); BCLExpression a2 = recurse((RuleNode) root.getChild(2), depth + 1, cu, verbose); o.getArgument().add(a2); } else if (root.getChildCount() == 2) { // Find operation String opStr = root.getChild(0).toString(); // Map if (opStr.equals("not")) o.setOperation(bvr.Operation.getByName("logNot")); else if (opStr.equals("isDefined")) o.setOperation(bvr.Operation.getByName("isDefined")); else if (opStr.equals("isUndefined")) o.setOperation(bvr.Operation.getByName("isUndefined")); else if (opStr.equals("-")) o.setOperation(bvr.Operation.getByName("arithNeg")); else throw new UnsupportedOperationException(); // Recurse // System.out.println("\"" + root.getChild(0).toString()+"\""); BCLExpression a1 = recurse((RuleNode) root.getChild(1), depth + 1, cu, verbose); o.getArgument().add(a1); } else if (root.getChildCount() == 4) { // Find operation String opStr = root.getChild(0).toString(); // Map if (opStr.equals("not")) o.setOperation(bvr.Operation.getByName("logNot")); else if (opStr.equals("isDefined")) o.setOperation(bvr.Operation.getByName("isDefined")); else if (opStr.equals("isUndefined")) o.setOperation(bvr.Operation.getByName("isUndefined")); else throw new UnsupportedOperationException(); // Recurse // System.out.println("\"" + root.getChild(0).toString()+"\""); BCLExpression a1 = recurse((RuleNode) root.getChild(2), depth + 1, cu, verbose); o.getArgument().add(a1); } else { for (int i = 0; i < root.getChildCount(); i++) System.out.println(root.getChild(i)); throw new UnsupportedOperationException(); } e = o; } return e; }
From source file:no.sintef.bvr.constraints.bcl.Loader.java
License:LGPL
public static void main(String[] args) throws FileNotFoundException, IOException { CharStream input = new ANTLRInputStream(new FileInputStream("TestData/test2.bcl")); BCLLexer lexer = new BCLLexer(input); TokenStream tokens = new CommonTokenStream(lexer); BCLParser parser = new BCLParser(tokens); // Build BVR// w w w . ja v a 2 s . co m SPLCABVRModel cm = new SPLCABVRModel(); // ConfigurableUnit cu = buildExampleBVR(cm); BVRModel model = buildExampleBVR(cm); Variable minSpeed = BvrFactory.eINSTANCE.createVariable(); // Variabletype minSpeedType = // bvrFactory.eINSTANCE.createVariabletype(); minSpeed.setName("minSpeed"); // minSpeed.setType(minSpeedType); // cu.getOwnedVSpec().add(minSpeed); model.getVariabilityModel().getVariable().add(minSpeed); // Build model RuleNode root = parser.constraint().getRuleContext(); BCLConstraint c = BvrFactory.eINSTANCE.createBCLConstraint(); BCLExpression e = new BCLBuilder().recurse((RuleNode) root.getChild(0), 0, model, true); c.getExpression().add(e); // Pretty Print String s = new BCLPrettyPrinter().prettyPrint(e); System.out.println(s); }