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

The following examples show how to use org.antlr.v4.runtime.tree.ParseTree#toStringTree() . 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: AbstractDataSetResource.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
public String validateFormula(String formula, List<SimpleSelectionField> columns) throws ValidationException, JSONException {

		validateBrackets(formula);
		validateFields(formula, columns);

		formula = "select ".concat(formula);
		CharStream inputStream = CharStreams.fromString(formula);
		SQLiteLexer tokenSource = new SQLiteLexer(new CaseChangingCharStream(inputStream, true));
		TokenStream tokenStream = new CommonTokenStream(tokenSource);
		SQLiteParser sQLiteParser = new SQLiteParser(tokenStream);

		sQLiteParser.addErrorListener(ThrowingErrorListener.INSTANCE);
		try {
			ParseTree root = sQLiteParser.select_stmt();
			root.toStringTree();
		} catch (Exception e) {
			throw new ValidationException(e);
		}
		if (sQLiteParser.getNumberOfSyntaxErrors() > 0) {
			throw new ValidationException();

		}

		return VALIDATION_OK;
	}
 
Example 2
Source File: ProgramParser.java    From yql-plus with Apache License 2.0 6 votes vote down vote up
private OperatorNode<SequenceOperator> convertQuery(ParseTree node, Scope scope) {
    if (node instanceof Select_statementContext
            || node instanceof Insert_statementContext
            || node instanceof Update_statementContext
            || node instanceof Delete_statementContext) {
        return convertSelectOrInsertOrUpdateOrDelete(node, scope.getRoot());
    } else if (node instanceof Source_statementContext) { //for pipe
        Source_statementContext sourceStatementContext = (Source_statementContext) node;
        return convertPipe(sourceStatementContext.query_statement(), sourceStatementContext.pipeline_step(), scope);
    } else if (node instanceof Merge_statementContext) {
        return convertMerge(((Merge_statementContext) node).merge_component(), scope);
    } else if (node instanceof Execute_statementContext) {
        return convertExecute((Execute_statementContext) node, scope);
    } else {
        throw new IllegalArgumentException("Unexpected argument type to convertQueryStatement: " + node.toStringTree());
    }

}
 
Example 3
Source File: ProgramParser.java    From vespa with Apache License 2.0 6 votes vote down vote up
private OperatorNode<SequenceOperator> convertQuery(ParseTree node, Scope scope) {
    if (node instanceof Select_statementContext
       || node instanceof Insert_statementContext
       || node instanceof Update_statementContext
       || node instanceof Delete_statementContext) {
        return convertSelectOrInsertOrUpdateOrDelete(node, scope.getRoot());
    } else if (node instanceof Source_statementContext) { //for pipe
        Source_statementContext sourceStatementContext = (Source_statementContext)node;
        return convertPipe(sourceStatementContext.query_statement(), sourceStatementContext.pipeline_step(), scope);
    } else if (node instanceof Merge_statementContext) {
        return convertMerge(((Merge_statementContext)node).merge_component(), scope);
    } else {
        throw new IllegalArgumentException("Unexpected argument type to convertQueryStatement: " + node.toStringTree());
    }

}
 
Example 4
Source File: ForLoopTests.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testEmptyFor()
{
	String input = "for(; ;){}";
	AntlrParserDriver functionParser = createFunctionDriver();
	ParseTree tree = functionParser.parseString(input);
	String output = tree.toStringTree(functionParser.getAntlrParser());
	assertTrue(output.contains("selection_or_iteration"));
}
 
Example 5
Source File: ForLoopTests.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testDeclInFor()
{
	String input = "for(int k = 0; k < 10; k++ ){}";
	AntlrParserDriver functionParser = createFunctionDriver();
	ParseTree tree = functionParser.parseString(input);
	String output = tree.toStringTree(functionParser.getAntlrParser());
	assertTrue(output.contains(
			"for ( (for_init_statement (simple_decl (var_decl (type_name (base_type int))"));
}
 
Example 6
Source File: ForLoopTests.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testComplexFor()
{
	String input = "for(int k = 0; k < 10; ( k += ((c = text[k]) >= sBMHCharSetSize) ? patlen : skip[c]) ){}";
	AntlrParserDriver functionParser = createFunctionDriver();
	ParseTree tree = functionParser.parseString(input);
	String output = tree.toStringTree(functionParser.getAntlrParser());
	assertTrue(output.contains("assign_expr"));
}
 
Example 7
Source File: FunctionCallTests.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testFunctionCall()
{
	String input = "foo(x);";
	AntlrParserDriver functionParser = createFunctionDriver();
	ParseTree tree = functionParser.parseString(input);
	String output = tree.toStringTree(functionParser.getAntlrParser());
	assertTrue(output.contains("function_argument_list"));
}
 
Example 8
Source File: FunctionCallTests.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testTwoParameters()
{
	String input = "foo(x,y);";
	AntlrParserDriver functionParser = createFunctionDriver();
	ParseTree tree = functionParser.parseString(input);
	String output = tree.toStringTree(functionParser.getAntlrParser());
	assertTrue(output.contains(", (function_argument"));
}
 
Example 9
Source File: FunctionCallTests.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testCallViaPtr()
{
	String input = "ptr->foo(x);";
	AntlrParserDriver functionParser = createFunctionDriver();
	ParseTree tree = functionParser.parseString(input);
	String output = tree.toStringTree(functionParser.getAntlrParser());
	assertTrue(output.contains("function_argument_list"));
}
 
Example 10
Source File: FunctionCallTests.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testCallWithExprInArg()
{
	String input = "foo(x == 1, x++);";
	AntlrParserDriver functionParser = createFunctionDriver();
	ParseTree tree = functionParser.parseString(input);
	String output = tree.toStringTree(functionParser.getAntlrParser());
	assertTrue(output.contains("function_argument_list"));
}
 
Example 11
Source File: FunctionCommentTests.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void compareParses(String actual, String expected) {
  AntlrParserDriver functionParser = createFunctionDriver();
  ParseTree actualTree = functionParser.parseString(actual);
  ParseTree expectedTree = functionParser.parseString(expected);
  String actualOutput = actualTree.toStringTree(functionParser.getAntlrParser());
  String expectedOutput = expectedTree.toStringTree(functionParser.getAntlrParser());
  assertEquals(actualOutput, expectedOutput);
}
 
Example 12
Source File: AssignmentTests.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testAssignmentExpr() {
	String input = "x = y + 1;";
	AntlrParserDriver functionParser = createFunctionDriver();
	ParseTree tree = functionParser.parseString(input);
	String output = tree.toStringTree(functionParser.getAntlrParser());
	assertTrue(output.contains("assign_expr"));
}
 
Example 13
Source File: AssignmentTests.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testComplexAssignment() {
	String input = "k += ((c = text[k]) >= sBMHCharSetSize) ? patlen : skip[c];";
	AntlrParserDriver functionParser = createFunctionDriver();
	ParseTree tree = functionParser.parseString(input);
	String output = tree.toStringTree(functionParser.getAntlrParser());
	assertTrue(output.contains("assign_expr"));
}
 
Example 14
Source File: AssignmentTests.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testPrivateInName() {
	String input = "struct acpi_battery *battery = m->private;";
	AntlrParserDriver functionParser = createFunctionDriver();
	ParseTree tree = functionParser.parseString(input);
	String output = tree.toStringTree(functionParser.getAntlrParser());
	assertTrue(output.contains("simple_decl"));
}
 
Example 15
Source File: FunctionParserTest.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 4 votes vote down vote up
private String generateParserOutput(String input) {
	AntlrParserDriver functionParser = createFunctionDriver();
	ParseTree tree = functionParser.parseString(input);
	return tree.toStringTree(functionParser.getAntlrParser());
}