Java Code Examples for org.antlr.v4.runtime.tree.ParseTree#getChildCount()

The following examples show how to use org.antlr.v4.runtime.tree.ParseTree#getChildCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AntlrUtils.java    From sonar-tsql-plugin with GNU General Public License v3.0 7 votes vote down vote up
public static void print(final ParseTree node, final int level, CommonTokenStream stream) {
	final Interval sourceInterval = node.getSourceInterval();

	final Token firstToken = stream.get(sourceInterval.a);

	int line = firstToken.getLine();
	int charStart = firstToken.getCharPositionInLine();

	int endLine = line;
	int endChar = charStart + firstToken.getText().length();

	String data = "@(" + line + ":" + charStart + "," + endLine + ":" + endChar + ") with text: "
			+ firstToken.getText();
	final int tmp = level + 1;
	final StringBuilder sb = new StringBuilder();
	sb.append(StringUtils.repeat("\t", level));
	sb.append(node.getClass().getSimpleName() + ": " + data + " :" + node.getText());
	System.out.println(sb.toString());
	final int n = node.getChildCount();
	for (int i = 0; i < n; i++) {

		final ParseTree c = node.getChild(i);
		print(c, tmp, stream);

	}
}
 
Example 2
Source File: ProgramParser.java    From vespa with Apache License 2.0 6 votes vote down vote up
private OperatorNode<ExpressionOperator> readValues(List<Field_defContext> fieldDefs, Scope scope) {
    List<String> fieldNames;
    List<OperatorNode<ExpressionOperator>> fieldValues;
    int numPairs = fieldDefs.size();
    fieldNames = Lists.newArrayListWithExpectedSize(numPairs);
    fieldValues = Lists.newArrayListWithExpectedSize(numPairs);
    for (int j = 0; j < numPairs; j++) {
        ParseTree startNode = fieldDefs.get(j);
        while(startNode.getChildCount() < 3) {
            startNode = startNode.getChild(0);
        }
        fieldNames.add((String) convertExpr(startNode.getChild(0), scope).getArgument(1));
        fieldValues.add(convertExpr(startNode.getChild(2), scope));
    }
    return OperatorNode.create(ExpressionOperator.MAP, fieldNames, fieldValues);
}
 
Example 3
Source File: GrammarUtils.java    From metron with Apache License 2.0 6 votes vote down vote up
private static void walk(ParseTree tree, AST ast) {
  if (tree.getChildCount() == 0) {
    new AST(ast, tree);
  }
  else if (tree.getChildCount() == 1) {
    walk(tree.getChild(0), ast);
  }
  else if (tree.getChildCount() > 1) {
    for (int i = 0; i < tree.getChildCount(); i++) {
      AST temp = new AST(ast, tree.getChild(i));
      if (!(temp.payload instanceof Token)) {
        walk(tree.getChild(i), temp);
      }
    }
  }
}
 
Example 4
Source File: DistkvNewSqlListener.java    From distkv with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void enterDictPut(DistkvNewSQLParser.DictPutContext ctx) {
  Preconditions.checkState(parsedResult == null);
  Preconditions.checkState(ctx.children.size() == 3);
  DictProtocol.DictPutRequest.Builder builder = DictProtocol.DictPutRequest.newBuilder();
  final ParseTree keyValuePairsParseTree = ctx.children.get(2);
  final int numKeyValuePairs = keyValuePairsParseTree.getChildCount();
  DictProtocol.DistKVDict.Builder distKVDictBuilder = DictProtocol.DistKVDict.newBuilder();
  for (int i = 0; i < numKeyValuePairs; ++i) {
    final ParseTree keyValuePairParseTree = keyValuePairsParseTree.getChild(i);
    Preconditions.checkState(keyValuePairParseTree.getChildCount() == 2);
    distKVDictBuilder.addKeys(keyValuePairParseTree.getChild(0).getText());
    distKVDictBuilder.addValues(keyValuePairParseTree.getChild(1).getText());
  }
  builder.setDict(distKVDictBuilder.build());
  DistkvRequest request = DistkvRequest.newBuilder()
      .setKey(ctx.children.get(1).getText())
      .setRequestType(DICT_PUT)
      .setRequest(Any.pack(builder.build()))
      .build();
  parsedResult = new DistkvParsedResult(DICT_PUT, request);
}
 
Example 5
Source File: StepPrev.java    From yauaa with Apache License 2.0 6 votes vote down vote up
private ParseTree prev(ParseTree tree) {
    ParseTree parent = up(tree);

    if (parent == null) {
        return null;
    }

    ParseTree prevChild = null;
    ParseTree child = null;
    int i;
    for (i = 0; i < parent.getChildCount(); i++) {
        if (!treeIsSeparator(child)) {
            prevChild = child;
        }
        child = parent.getChild(i);
        if (child == tree) {
            break; // Found it
        }
    }
    return prevChild;
}
 
Example 6
Source File: StepNext.java    From yauaa with Apache License 2.0 6 votes vote down vote up
private ParseTree next(ParseTree tree) {
    ParseTree parent = up(tree);

    if (parent == null) {
        return null;
    }

    ParseTree child;
    boolean foundCurrent = false;
    for (int i = 0; i < parent.getChildCount(); i++) {
        child = parent.getChild(i);
        if (foundCurrent) {
            if (treeIsSeparator(child)) {
                continue;
            }
            return child;
        }

        if (child == tree) {
            foundCurrent = true;
        }
    }
    return null; // There is no next
}
 
Example 7
Source File: ExpressionDepOperator.java    From BigDataScript with Apache License 2.0 6 votes vote down vote up
@Override
protected void parse(ParseTree tree) {
	// Find 'dependency' operator (i.e. '<-')
	int depIdx = indexOf(tree, "<-");

	// Create lists of expressions
	ArrayList<Expression> listl = new ArrayList<>(); // Operator's left side
	ArrayList<Expression> listr = new ArrayList<>(); // Operator's right side
	for (int i = 0; i < tree.getChildCount(); i++) {
		if (isTerminal(tree, i, DEP_OPERATOR) || isTerminal(tree, i, DEP_SEPARATOR)) {
			// Do not add this node
		} else if (i < depIdx) listl.add((Expression) factory(tree, i)); // Add to left
		else if (i > depIdx) listr.add((Expression) factory(tree, i)); // Add to right
	}

	// Create arrays
	left = listl.toArray(new Expression[0]);
	right = listr.toArray(new Expression[0]);
}
 
Example 8
Source File: Python3MethodParser.java    From Siamese with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Iterative depth first traversal of the root RuleContext to find all TerminalNodeImpl
 * (in the order of ascending file and token position as in the original source code)
 * which are then used to create a file-level method.
 * @param ruleContext Root RuleContext of the parsed file
 * @return {@link crest.siamese.document.Method}
 */
private Method createFileMethod(RuleContext ruleContext) {
    List<TerminalNodeImpl> terminalNodes = new ArrayList<>();

    Stack<ParseTree> stack = new Stack<>();
    stack.push(ruleContext);

    while (!stack.isEmpty()) {
        ParseTree curr = stack.pop();
        if (curr instanceof RuleContext) {
            // First child would be at top of stack
            for (int i = curr.getChildCount() - 1; i >= 0; i--) {
                stack.push(curr.getChild(i));
            }
        } else if (curr instanceof TerminalNodeImpl) {
            terminalNodes.add((TerminalNodeImpl) curr);
        }
    }

    int startLine = terminalNodes.get(0).getSymbol().getLine();
    int endLine = terminalNodes.get(terminalNodes.size() - 1).getSymbol().getLine();
    List<Parameter> params = new ArrayList<>();
    String src = reformat(terminalNodes);

    return new Method(FILE_PATH, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY,
            StringUtils.EMPTY, src, startLine, endLine, params, StringUtils.EMPTY);
}
 
Example 9
Source File: BNFListener.java    From openCypher with Apache License 2.0 5 votes vote down vote up
private void pullUpItem(ParseTree ctx) {

        if (ctx.getChildCount() != 1) {
            throw new IllegalStateException("There should be only one child at " + ctx.getText() + " but there are " +
                        ctx.getChildCount());
        }
        GrammarItem movedItem = getItem(ctx.getChild(0));
        if (movedItem != null) {
            items.put(ctx,  movedItem);
        } else {
            throw new IllegalStateException("No item to be moved from " + ctx.getChild(0).getClass().getSimpleName()
                    + " up to " + ctx.getClass().getSimpleName());
        }
    }
 
Example 10
Source File: RefactorUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static int childIndexOf(ParseTree t, ParseTree child) {
	if ( t==null || child==null ) return -1;
	for (int i = 0; i < t.getChildCount(); i++) {
		if ( child==t.getChild(i) ) return i;
	}
	return -1;
}
 
Example 11
Source File: ParseTreeUtils.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static String childTokenString(ParseTree ctx) {

    // The reason we don't just call ctx.getText()
    // here is because it removes whitespace, making
    // 'inti' from 'int i'.

    if (ctx == null) {
      return "";
    }

    int numChildren = ctx.getChildCount();

    if (numChildren == 0) {
      return ctx.getText();
    }

    StringBuilder retval = new StringBuilder();

    for (int i = 0; i < numChildren; i++) {
      ParseTree child = ctx.getChild(i);
      String childText = childTokenString(child);
      if (!childText.equals("")) {
        retval.append(childText).append(" ");
      }
    }

    if (retval.length() > 0) {
      return retval.substring(0, retval.length() - 1);
    }
    return retval.toString();
  }
 
Example 12
Source File: ForInit.java    From BigDataScript with Apache License 2.0 5 votes vote down vote up
@Override
protected void parse(ParseTree tree) {
	String nodeTxt = tree.getChild(0).getClass().getSimpleName();

	if (nodeTxt.startsWith("VarDeclaration")) varDeclaration = (VarDeclaration) factory(tree, 0);
	else {
		// Expressions
		int num = tree.getChildCount();
		expressions = new Expression[num];
		for (int i = 0; i < num; i++)
			expressions[i] = (Expression) factory(tree, i);
	}
}
 
Example 13
Source File: ParserBasedImpl.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
/**
 * Navigate from root to predicateListContext of whereClauseContext, where all the filtering happens
 * @return a list of sorted tuples List<Pair<List<colName>, Score>>
 */
List<Pair<List<String>, BigFraction>> parseQuery() {
  LOGGER.debug("Parsing query: {}", _queryString);
  PQL2Parser.OptionalClauseContext optionalClauseContext;
  PQL2Parser.WhereClauseContext whereClauseContext = null;

  if (_queryString == null) {
    return Collections.EMPTY_LIST;
  }

  try {
    PQL2Lexer lexer = new PQL2Lexer(new ANTLRInputStream(_queryString));
    PQL2Parser parser = new PQL2Parser(new CommonTokenStream(lexer));
    ParseTree selectStatement = parser.root().statement().selectStatement();
    LOGGER.debug("selectStatement: {}", selectStatement.getText());

    for (int i = 0; i < selectStatement.getChildCount(); i++) {
      if (selectStatement.getChild(i) instanceof PQL2Parser.OptionalClauseContext) {
        optionalClauseContext = (PQL2Parser.OptionalClauseContext) selectStatement.getChild(i);
        LOGGER.debug("optionalClauseContext: {}", optionalClauseContext.getText());
        if (optionalClauseContext.getChild(0) instanceof PQL2Parser.WhereClauseContext) {
          whereClauseContext = (PQL2Parser.WhereClauseContext) optionalClauseContext.getChild(0);
          break;
        }
      }
    }
  } catch (Exception e) {
    return Collections.EMPTY_LIST;
  }
  if (whereClauseContext == null) {
    return Collections.EMPTY_LIST;
  }

  LOGGER.debug("whereClauseContext: {}", whereClauseContext.getText());

  List<Pair<List<String>, BigFraction>> results = parsePredicateList(whereClauseContext.predicateList());
  return results.stream().limit(_algorithmOrder).collect(Collectors.toList());
}
 
Example 14
Source File: ANTLRUtils.java    From proleap-vb6-parser with MIT License 5 votes vote down vote up
public static List<ParseTree> findChildren(final ParseTree ctx) {
	final List<ParseTree> result = new ArrayList<ParseTree>();
	final int n = ctx.getChildCount();

	for (int i = 0; i < n; i++) {
		final ParseTree currentChild = ctx.getChild(i);
		result.add(currentChild);
	}

	return result;
}
 
Example 15
Source File: VarDeclaration.java    From BigDataScript with Apache License 2.0 5 votes vote down vote up
@Override
protected void parse(ParseTree tree) {
	int idx = 0;

	String classname = tree.getChild(0).getClass().getSimpleName();
	if (classname.equals("VariableInitImplicitContext")) {
		// Variable 'short' declaration
		// Format : varMame := initValue
		// E.g.   : i := 2
		implicit = true;
		varInit = new VariableInit[1];
		varInit[0] = (VariableInit) factory(tree, idx);
	} else {
		// Variable 'classic' declaration
		// Format : type varMame = initValue
		// E.g.   : int i = 2
		implicit = false;
		type = (Type) factory(tree, idx++);

		// Create VarInit nodes
		int num = tree.getChildCount() / 2;
		varInit = new VariableInit[num];

		// Parse all VarInit nodes
		for (int i = idx, j = 0; i < tree.getChildCount(); i += 2, j++) { // Note: i steps over the comma delimiter, that's why "+= 2"
			varInit[j] = (VariableInit) factory(tree, i);
			varInit[j].setVarDeclaration(this);
		}

		for (VariableInit vi : varInit)
			vi.setType(type);
	}
}
 
Example 16
Source File: BdsCompiler.java    From BigDataScript with Apache License 2.0 4 votes vote down vote up
/**
 * Create an AST from a program (using ANTLR lexer & parser)
 * Returns null if error
 * Use 'alreadyIncluded' to keep track of from 'include' statements
 */
ParseTree createAst(CharStream input, boolean debug, Set<String> alreadyIncluded) {
	BigDataScriptLexer lexer = null;
	BigDataScriptParser parser = null;

	try {
		//---
		// Lexer: Create a lexer that feeds off of input CharStream
		//---
		lexer = new BigDataScriptLexer(input) {
			@Override
			public void recover(LexerNoViableAltException e) {
				throw new RuntimeException(e); // Bail out
			}
		};

		//---
		// Parser
		//---
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		parser = new BigDataScriptParser(tokens);

		// Parser error handling
		parser.setErrorHandler(new CompileErrorStrategy()); // Bail out with pendingException if errors in parser
		parser.addErrorListener(new CompilerErrorListener()); // Catch some other error messages that 'CompileErrorStrategy' fails to catch

		// Begin parsing at main rule
		ParseTree tree = parserNode(parser);

		// Error loading file?
		if (tree == null) {
			System.err.println("Can't parse file '" + programFileName + "'");
			return null;
		}

		// Show main nodes
		if (debug) {
			Timer.showStdErr("AST:");
			for (int childNum = 0; childNum < tree.getChildCount(); childNum++) {
				Tree child = tree.getChild(childNum);
				System.err.println("\t\tChild " + childNum + ":\t" + child + "\tTree:'" + child.toStringTree() + "'");
			}
		}

		// Included files
		boolean resolveIncludePending = true;
		while (resolveIncludePending)
			resolveIncludePending = resolveIncludes(tree, debug, alreadyIncluded);

		return tree;
	} catch (Exception e) {
		String msg = e.getMessage();
		CompilerMessages.get().addError("Could not compile " + programFileName //
				+ (msg != null ? " :" + e.getMessage() : "") //
		);
		return null;
	}
}
 
Example 17
Source File: ParseTreeUtils.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
static void verifyChildCount(final ParseTree tree, final int expected) {
    if (tree.getChildCount() != expected) {
        throw illegalShape(tree);
    }
}
 
Example 18
Source File: ResourceDao.java    From osiam with MIT License 4 votes vote down vote up
public <T extends ResourceEntity> SearchResult<T> search(Class<T> clazz, ParseTree filterTree, int count,
        int startIndex,
        String sortBy, String sortOrder, FilterParser<T> filterParser) {

    CriteriaBuilder cb = em.getCriteriaBuilder();

    CriteriaQuery<T> resourceQuery = cb.createQuery(clazz);
    Root<T> resourceRoot = resourceQuery.from(clazz);

    Subquery<Long> internalIdQuery = resourceQuery.subquery(Long.class);
    Root<T> internalIdRoot = internalIdQuery.from(clazz);
    internalIdQuery.select(internalIdRoot.get(ResourceEntity_.internalId));

    if (filterTree != null && filterTree.getChildCount() > 0) {
        Predicate predicate = filterParser.createPredicateAndJoin(filterTree, internalIdRoot);
        internalIdQuery.where(predicate);
    }

    resourceQuery.select(resourceRoot).where(
            cb.in(resourceRoot.get(ResourceEntity_.internalId)).value(internalIdQuery));

    // TODO: evaluate if a User-/GroupDao supplied default sortBy field is possible
    Expression<?> sortByField = resourceRoot.get(ResourceEntity_.id);

    if (sortBy != null && !sortBy.isEmpty()) {
        sortByField = filterParser.createSortByField(sortBy, resourceRoot);
    }

    // default order is ascending
    Order order = cb.asc(sortByField);

    if (sortOrder.equalsIgnoreCase("descending")) {
        order = cb.desc(sortByField);
    }

    resourceQuery.orderBy(order);

    TypedQuery<T> query = em.createQuery(resourceQuery);
    query.setFirstResult(startIndex);
    query.setMaxResults(count);

    List<T> results = query.getResultList();

    long totalResult = getTotalResults(clazz, internalIdQuery);

    return new SearchResult<>(results, totalResult);
}
 
Example 19
Source File: Case.java    From BigDataScript with Apache License 2.0 4 votes vote down vote up
protected boolean isEndOfStatements(ParseTree tree, int idx) {
	if (idx >= tree.getChildCount()) return true;
	return isTerminal(tree, idx, "case") || isTerminal(tree, idx, "default");
}
 
Example 20
Source File: APathToXPathConverter.java    From archie with Apache License 2.0 4 votes vote down vote up
/**
 * Converts and ANTLR ParseTree of the XPath grammar to APath String output
 * Adds the output to the given StringBuilder.
 *
 * Simple approach that writes every element, adding some whitespace where needed, and converting just a few elements
 * @param output the output to write to
 * @param tree the parse tree
 * @param inPredicate whether we are inside or outside a predicate
 */
private static void writeTree(StringBuilder output, ParseTree tree, boolean inPredicate) {


    for(int i = 0; i < tree.getChildCount(); i++) {
        ParseTree child = tree.getChild(i);
        if(child instanceof TerminalNode) {
            boolean shouldAppendSpace = literalsThatShouldHaveSpacing.contains(child.getText().toLowerCase());
            if(shouldAppendSpace) {
                output.append(" ");
            }
            output.append(child.getText());
            if(shouldAppendSpace) {
                output.append(" ");
            }
        } else if (child instanceof AndExprContext) {
            for(int j = 0; j < child.getChildCount(); j++) {
                ParseTree andChild = child.getChild(j);
                if(andChild instanceof TerminalNode) {
                    output.append(" and "); //this rewrites the "," syntax that is equivalent to 'and'
                } else {
                    writeTree(output, andChild, inPredicate);
                }
            }

        } else if (child instanceof PredicateContext) {
            writeTree(output, child, true);
        } else if(inPredicate && child instanceof RelativeLocationPathContext) {
            Matcher idCodeMatcher = idCodePattern.matcher(child.getText());

            if (idCodeMatcher.matches()) {
                output.append("@archetype_node_id = '");
                output.append(child.getText());
                output.append("'");
            } else {
                writeTree(output, child, inPredicate);
            }
        } else if(inPredicate && child instanceof FilterExprContext) {
            FilterExprContext filterExprContext = (FilterExprContext) child;
            //not sure if we should support [id5, 1]. This is not standard xpath!
            Matcher numberMatcher = numberPattern.matcher(child.getText());
            if(numberMatcher.matches()) {
                output.append("position() = ");
                output.append(child.getText());
            } else if(filterExprContext.primaryExpr().Literal() != null) {
                output.append("name/value = ");
                output.append(child.getText());
            } else {
                writeTree(output, child, inPredicate);
            }
        } else {
            writeTree(output, child, inPredicate);
        } //TODO: rewrite id nodes and some other nodes when we find them in this very simple bit of code :)
    }
}