Search in sources :

Example 1 with AstBuilder

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

the class AstBuilderFactory method createAstBuilder.

public static AstBuilder createAstBuilder(CompilationUnit source, TokenStream tokens) {
    KalangParser p = new KalangParser(tokens);
    AstBuilder sp = new AstBuilder(source, p);
    p.setErrorHandler(new DefaultErrorStrategy() {

        @Override
        public void reportError(Parser recognizer, RecognitionException e) {
            String msg = AntlrErrorString.exceptionString(recognizer, e);
            Token end = e.getOffendingToken();
            Token start;
            RuleContext ctx = e.getCtx();
            if (ctx instanceof ParserRuleContext) {
                start = ((ParserRuleContext) ctx).getStart();
            } else {
                start = end;
            }
            sp.getDiagnosisReporter().report(Diagnosis.Kind.ERROR, msg, start, end);
        }
    });
    return sp;
}
Also used : AstBuilder(kalang.compiler.AstBuilder) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) KalangParser(kalang.antlr.KalangParser) RuleContext(org.antlr.v4.runtime.RuleContext) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) Token(org.antlr.v4.runtime.Token) RecognitionException(org.antlr.v4.runtime.RecognitionException) KalangParser(kalang.antlr.KalangParser) Parser(org.antlr.v4.runtime.Parser)

Example 2 with AstBuilder

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

AstBuilder (kalang.compiler.AstBuilder)2 KalangParser (kalang.antlr.KalangParser)1 CompilationUnit (kalang.compiler.CompilationUnit)1 DefaultCompileContext (kalang.compiler.DefaultCompileContext)1 KalangSource (kalang.compiler.KalangSource)1 ParseTreeNavigator (kalang.util.ParseTreeNavigator)1 DefaultErrorStrategy (org.antlr.v4.runtime.DefaultErrorStrategy)1 Parser (org.antlr.v4.runtime.Parser)1 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 RuleContext (org.antlr.v4.runtime.RuleContext)1 Token (org.antlr.v4.runtime.Token)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 Test (org.junit.Test)1