Search in sources :

Example 1 with CompilationUnit

use of kalang.compiler.CompilationUnit in project kalang by kasonyang.

the class KalangCompilerTest method test.

@Test
public void test() {
    KalangCompiler kc = new KalangCompiler();
    kc.addSource("Test", "class{  }", "Test.kl");
    kc.compile();
    CompilationUnit unit = kc.getCompilationUnit("Test");
    assert unit != null;
    CommonTokenStream ts = unit.getTokenStream();
    // the tokens contains tokens in all channels
    List<Token> tokens = ts.getTokens();
    assertEquals(5, ts.size());
    testTokenNavigator(tokens.toArray(new Token[0]), unit.getAstBuilder().getParseTree());
}
Also used : CompilationUnit(kalang.compiler.CompilationUnit) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Token(org.antlr.v4.runtime.Token) KalangCompiler(kalang.compiler.KalangCompiler) Test(org.junit.Test)

Example 2 with CompilationUnit

use of kalang.compiler.CompilationUnit in project kalang by kasonyang.

the class FileSystemCompiler method compile.

public void compile() throws IOException {
    if (outputDir == null) {
        throw new IllegalStateException("output diretory is null");
    }
    URLClassLoader pathClassLoader = new URLClassLoader(classPaths.toArray(new URL[classPaths.size()]), this.classLoader);
    AstLoader astLoader = new JavaAstLoader(this.parentAstLoader, pathClassLoader);
    KalangCompiler compiler = new KalangCompiler(astLoader) {

        @Override
        public CodeGenerator createCodeGenerator(CompilationUnit compilationUnit) {
            FileSystemOutputManager om = new FileSystemOutputManager(outputDir, extension);
            return new ClassWriter(om);
        }
    };
    compiler.setConfiguration(configuration);
    for (Map.Entry<String, File> e : sourceFiles.entrySet()) {
        String className = e.getKey();
        File file = e.getValue();
        compiler.addSource(className, FileUtils.readFileToString(file), file.getName());
    }
    FileSystemSourceLoader sourceLoader = new FileSystemSourceLoader(sourcePaths.toArray(new File[sourcePaths.size()]), new String[] { "kl", "kalang" });
    compiler.setSourceLoader(sourceLoader);
    if (diagnosisHandler != null) {
        compiler.setDiagnosisHandler(diagnosisHandler);
    }
    compiler.compile();
}
Also used : CompilationUnit(kalang.compiler.CompilationUnit) AstLoader(kalang.compiler.AstLoader) JavaAstLoader(kalang.compiler.JavaAstLoader) KalangCompiler(kalang.compiler.KalangCompiler) JavaAstLoader(kalang.compiler.JavaAstLoader)

Example 3 with CompilationUnit

use of kalang.compiler.CompilationUnit in project kalang by kasonyang.

the class CompilantUnitTest method test.

@Test
public void test() {
    KalangSource source = new KalangSource("Test", "class{" + "void main(){" + "}" + "}", "Test.kl");
    CompilationUnit cu = new CompilationUnit(source, new DefaultCompileContext());
    cu.compile(CompilePhase.PHASE_PARSING);
    AstBuilder astBuilder = cu.getAstBuilder();
    ParseTreeNavigator treeNav = new ParseTreeNavigator(astBuilder.getParseTree());
    ParseTree tree = treeNav.getParseTree(0);
    assertNotNull(tree);
    ParseTree treeMd = treeNav.getParseTree(2);
    ParseTree treeMdEnd = treeNav.getParseTree(7);
    // System.out.println(treeMd);
    // System.out.println(treeMdEnd);
    assertNotNull(treeMd);
    assertNotNull(treeMdEnd);
// assertEquals(treeMd, treeMdEnd.getParent().getParent());
// AstNode ast = sp.getAstNode(tree);
// assertNotNull(ast);
// System.out.println("ast:"+ast);
// RuleContext treeOfAst = sp.getParseTree(ast);
// assertNotNull(treeOfAst);
}
Also used : CompilationUnit(kalang.compiler.CompilationUnit) AstBuilder(kalang.compiler.AstBuilder) DefaultCompileContext(kalang.compiler.DefaultCompileContext) KalangSource(kalang.compiler.KalangSource) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeNavigator(kalang.util.ParseTreeNavigator) Test(org.junit.Test)

Aggregations

CompilationUnit (kalang.compiler.CompilationUnit)3 KalangCompiler (kalang.compiler.KalangCompiler)2 Test (org.junit.Test)2 AstBuilder (kalang.compiler.AstBuilder)1 AstLoader (kalang.compiler.AstLoader)1 DefaultCompileContext (kalang.compiler.DefaultCompileContext)1 JavaAstLoader (kalang.compiler.JavaAstLoader)1 KalangSource (kalang.compiler.KalangSource)1 ParseTreeNavigator (kalang.util.ParseTreeNavigator)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 Token (org.antlr.v4.runtime.Token)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1