Search in sources :

Example 1 with KalangSource

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

the class KalangClassLoader method findClass.

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
    Class clazz = loadedClasses.get(name);
    if (clazz != null)
        return clazz;
    KalangSource src = sourceLoader.loadSource(name);
    if (src != null) {
        compiler.addSource(src);
        compiler.compile();
    }
    clazz = loadedClasses.get(name);
    if (clazz != null)
        return clazz;
    return super.findClass(name);
}
Also used : Ast2Class(kalang.compiler.codegen.Ast2Class) KalangSource(kalang.compiler.KalangSource)

Example 2 with KalangSource

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

the class KalangSourceUtil method create.

public static KalangSource create(File dir, File sourceFile) throws IOException {
    String clsName = ClassNameUtil.getClassName(dir, sourceFile);
    KalangSource ks = new KalangSource(clsName, FileUtils.readFileToString(sourceFile), sourceFile.getName());
    return ks;
}
Also used : KalangSource(kalang.compiler.KalangSource)

Example 3 with KalangSource

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

the class KalangClassLoader method handleDiagnosis.

@Override
public void handleDiagnosis(Diagnosis diagnosis) {
    if (diagnosis.getKind().isError()) {
        KalangSource source = diagnosis.getSource();
        OffsetRange offset = diagnosis.getOffset();
        throw new CompileException(String.format("%s:%s\n%s", source.getFileName(), offset.startLine, diagnosis.getDescription()));
    }
}
Also used : OffsetRange(kalang.compiler.OffsetRange) KalangSource(kalang.compiler.KalangSource)

Example 4 with KalangSource

use of kalang.compiler.KalangSource 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

KalangSource (kalang.compiler.KalangSource)4 AstBuilder (kalang.compiler.AstBuilder)1 CompilationUnit (kalang.compiler.CompilationUnit)1 DefaultCompileContext (kalang.compiler.DefaultCompileContext)1 OffsetRange (kalang.compiler.OffsetRange)1 Ast2Class (kalang.compiler.codegen.Ast2Class)1 ParseTreeNavigator (kalang.util.ParseTreeNavigator)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 Test (org.junit.Test)1