Search in sources :

Example 1 with HqlParser

use of org.hibernate.hql.internal.ast.HqlParser in project hibernate-orm by hibernate.

the class ASTIteratorTest method testSimpleTree.

@Test
public void testSimpleTree() throws Exception {
    String input = "select foo from foo in class org.hibernate.test.Foo, fee in class org.hibernate.test.Fee where foo.dependent = fee order by foo.string desc, foo.component.count asc, fee.id";
    HqlParser parser = HqlParser.getInstance(input);
    parser.statement();
    AST ast = parser.getAST();
    ASTPrinter printer = new ASTPrinter(HqlTokenTypes.class);
    printer.showAst(ast, new PrintWriter(System.out));
    ASTIterator iterator = new ASTIterator(ast);
    int count = 0;
    while (iterator.hasNext()) {
        assertTrue(iterator.next() instanceof AST);
        count++;
    }
    assertEquals(43, count);
    UnsupportedOperationException uoe = null;
    try {
        iterator.remove();
    } catch (UnsupportedOperationException e) {
        uoe = e;
    }
    assertNotNull(uoe);
}
Also used : AST(antlr.collections.AST) ASTPrinter(org.hibernate.hql.internal.ast.util.ASTPrinter) HqlParser(org.hibernate.hql.internal.ast.HqlParser) ASTIterator(org.hibernate.hql.internal.ast.util.ASTIterator) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 2 with HqlParser

use of org.hibernate.hql.internal.ast.HqlParser in project hibernate-orm by hibernate.

the class EJBQLTest method parse.

private AST parse(String input, boolean logging) throws RecognitionException, TokenStreamException {
    if (logging) {
        System.out.println("input: ->" + input + "<-");
    }
    HqlParser parser = HqlParser.getInstance(input);
    parser.setFilter(false);
    parser.statement();
    AST ast = parser.getAST();
    if (logging) {
        System.out.println("AST  :  " + ast.toStringTree() + "");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        parser.showAst(ast, new PrintStream(baos));
        System.out.println(baos.toString());
    }
    assertEquals("At least one error occurred during parsing!", 0, parser.getParseErrorHandler().getErrorCount());
    return ast;
}
Also used : PrintStream(java.io.PrintStream) AST(antlr.collections.AST) HqlParser(org.hibernate.hql.internal.ast.HqlParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with HqlParser

use of org.hibernate.hql.internal.ast.HqlParser in project hibernate-orm by hibernate.

the class HqlParserTest method testNoFrom.

@Test
public void testNoFrom() throws Exception {
    System.out.println("***** This test ensures that an error is detected ERROR MESSAGES ARE OKAY!  *****");
    HqlParser parser = HqlParser.getInstance("");
    parser.setFilter(false);
    parser.statement();
    assertEquals("Parser allowed no FROM clause!", 1, parser.getParseErrorHandler().getErrorCount());
    System.out.println("***** END OF ERROR TEST  *****");
}
Also used : HqlParser(org.hibernate.hql.internal.ast.HqlParser) Test(org.junit.Test)

Example 4 with HqlParser

use of org.hibernate.hql.internal.ast.HqlParser in project hibernate-orm by hibernate.

the class HqlParserTest method doParse.

private AST doParse(String input, boolean filter) throws RecognitionException, TokenStreamException {
    System.out.println("input: ->" + ASTPrinter.escapeMultibyteChars(input) + "<-");
    HqlParser parser = HqlParser.getInstance(input);
    parser.setFilter(filter);
    parser.statement();
    AST ast = parser.getAST();
    System.out.println("AST  :  " + ast.toStringTree() + "");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    parser.showAst(ast, new PrintStream(baos));
    System.out.println(baos.toString());
    assertEquals("At least one error occurred during parsing!", 0, parser.getParseErrorHandler().getErrorCount());
    return ast;
}
Also used : PrintStream(java.io.PrintStream) AST(antlr.collections.AST) HqlParser(org.hibernate.hql.internal.ast.HqlParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with HqlParser

use of org.hibernate.hql.internal.ast.HqlParser in project querydsl by querydsl.

the class QueryHelper method parse.

public void parse() throws RecognitionException, TokenStreamException {
    String input = toString();
    logger.fine("input: " + input.replace('\n', ' '));
    HqlParser parser = HqlParser.getInstance(input);
    parser.setFilter(false);
    parser.statement();
    AST ast = parser.getAST();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    parser.showAst(ast, new PrintStream(baos));
    assertEquals("At least one error occurred during parsing " + input, 0, parser.getParseErrorHandler().getErrorCount());
}
Also used : PrintStream(java.io.PrintStream) AST(antlr.collections.AST) HqlParser(org.hibernate.hql.internal.ast.HqlParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

HqlParser (org.hibernate.hql.internal.ast.HqlParser)5 AST (antlr.collections.AST)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PrintStream (java.io.PrintStream)3 Test (org.junit.Test)2 PrintWriter (java.io.PrintWriter)1 ASTIterator (org.hibernate.hql.internal.ast.util.ASTIterator)1 ASTPrinter (org.hibernate.hql.internal.ast.util.ASTPrinter)1