Search in sources :

Example 21 with CompilationUnit

use of japa.parser.ast.CompilationUnit in project japid42 by branaway.

the class JavaSyntaxTool method parse.

public static CompilationUnit parse(String src) throws ParseException {
    ByteArrayInputStream in;
    try {
        in = new ByteArrayInputStream(src.getBytes(UTF_8));
        CompilationUnit cu;
        cu = JavaParser.parse(in, UTF_8);
        return cu;
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TokenMgrError e) {
        throw new ParseException(e.getMessage());
    }
    return null;
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TokenMgrError(japa.parser.TokenMgrError) ParseException(japa.parser.ParseException)

Example 22 with CompilationUnit

use of japa.parser.ast.CompilationUnit in project japid42 by branaway.

the class JavaSyntaxTool method parseArgs.

// XXX this method does not properly parse thingsl like A<t> a
// it does not detect the error
@SuppressWarnings("unchecked")
public static List<String> parseArgs(String line) {
    final List<String> ret = new ArrayList<String>();
    if (line == null || line.trim().length() == 0)
        return ret;
    @SuppressWarnings("rawtypes") VoidVisitorAdapter visitor = new VoidVisitorAdapter() {

        @Override
        public void visit(MethodCallExpr n, Object arg) {
            List<Expression> args = n.getArgs();
            // api issue: args can be null in case of empty arg list
            if (args != null)
                for (Expression e : args) {
                    ret.add(e.toString());
                }
        }
    };
    line = line.trim();
    if (line.startsWith("(")) {
        // perhaps it's in the form of (...)
        String cl = String.format(classTempForArgsNoParenthesis, line);
        try {
            CompilationUnit cu = parse(cl);
            cu.accept(visitor, null);
            return ret;
        } catch (ParseException e) {
        // perhaps not really (...). fall through
        }
    }
    String cl = String.format(classTempForArgs, line);
    try {
        CompilationUnit cu = parse(cl);
        cu.accept(visitor, null);
    } catch (ParseException e) {
        throw new RuntimeException("the line does not seem to be a valid arg list: " + line);
    }
    return ret;
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) Expression(japa.parser.ast.expr.Expression) ArrayList(java.util.ArrayList) ParseException(japa.parser.ParseException) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter) MethodCallExpr(japa.parser.ast.expr.MethodCallExpr)

Example 23 with CompilationUnit

use of japa.parser.ast.CompilationUnit in project enumerable by hraberg.

the class InMemoryCompilerTest method inMemoryCompilationFromJavaParserAST.

@Test
public void inMemoryCompilationFromJavaParserAST() throws Exception {
    CompilationUnit cu = createCU();
    assertEquals("Hello World from AST", invokeMain(compiler.compile(getFullyQualifiedName(cu), cu.toString())).trim());
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) Test(org.junit.Test)

Example 24 with CompilationUnit

use of japa.parser.ast.CompilationUnit in project Japid by branaway.

the class CompilerTests method testTagline.

@Test
public void testTagline() throws IOException, ParseException {
    String srcFile = "tests/tagline.html";
    String src = readFile(srcFile);
    JapidTemplate bt = new JapidTemplate("tagline.html", src);
    JapidAbstractCompiler cp = new JapidTemplateCompiler();
    cp.compile(bt);
    String code = bt.javaSource;
    System.out.println(code);
    CompilationUnit cu = JavaSyntaxTool.parse(code);
//		System.out.println(cu);
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) JapidAbstractCompiler(cn.bran.japid.compiler.JapidAbstractCompiler) JapidTemplate(cn.bran.japid.template.JapidTemplate) JapidTemplateCompiler(cn.bran.japid.compiler.JapidTemplateCompiler) Test(org.junit.Test)

Example 25 with CompilationUnit

use of japa.parser.ast.CompilationUnit in project Japid by branaway.

the class CompilerTests method testTemplateWithCallbackTagCalls.

@Test
public void testTemplateWithCallbackTagCalls() throws IOException, ParseException {
    String src = readFile("JapidSample/app/japidviews/templates/AllPost.html");
    JapidTemplate bt = new JapidTemplate("japidviews/templates/AllPost.html", src);
    JapidAbstractCompiler cp = new JapidTemplateCompiler();
    cp.compile(bt);
    System.out.println(bt.javaSource);
    CompilationUnit cu = JavaSyntaxTool.parse(bt.javaSource);
    System.out.println(cu);
//		assertTrue("invalid java code", JavaSyntaxValidator.isValid(bt.javaSource));
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) JapidAbstractCompiler(cn.bran.japid.compiler.JapidAbstractCompiler) JapidTemplate(cn.bran.japid.template.JapidTemplate) JapidTemplateCompiler(cn.bran.japid.compiler.JapidTemplateCompiler) Test(org.junit.Test)

Aggregations

CompilationUnit (japa.parser.ast.CompilationUnit)34 Test (org.junit.Test)17 ParseException (japa.parser.ParseException)15 JapidAbstractCompiler (cn.bran.japid.compiler.JapidAbstractCompiler)12 JapidTemplate (cn.bran.japid.template.JapidTemplate)12 VoidVisitorAdapter (japa.parser.ast.visitor.VoidVisitorAdapter)10 JapidTemplateCompiler (cn.bran.japid.compiler.JapidTemplateCompiler)9 MethodCallExpr (japa.parser.ast.expr.MethodCallExpr)7 ArrayList (java.util.ArrayList)6 Parameter (japa.parser.ast.body.Parameter)5 TypeParameter (japa.parser.ast.TypeParameter)4 AssignExpr (japa.parser.ast.expr.AssignExpr)4 Expression (japa.parser.ast.expr.Expression)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 JapidLayoutCompiler (cn.bran.japid.compiler.JapidLayoutCompiler)3 PackageDeclaration (japa.parser.ast.PackageDeclaration)3 ClassOrInterfaceDeclaration (japa.parser.ast.body.ClassOrInterfaceDeclaration)3 MethodDeclaration (japa.parser.ast.body.MethodDeclaration)3 TokenMgrError (japa.parser.TokenMgrError)2 BlockComment (japa.parser.ast.BlockComment)2