Search in sources :

Example 1 with ParseTreeNavigator

use of kalang.util.ParseTreeNavigator in project kalang by kasonyang.

the class KalangCompilerTest method testTokenNavigator.

private void testTokenNavigator(Token[] tokens, ParseTree tree) {
    TokenNavigator tokenNav = new TokenNavigator(tokens);
    ParseTreeNavigator treeNav = new ParseTreeNavigator(tree);
    assertEquals(0, tokenNav.getCurrentTokenIndex());
    assertEquals("class", treeNav.getParseTree(tokenNav.getCurrentToken()).getText());
    tokenNav.move(5);
    assertEquals("{", tokenNav.getCurrentToken().getText());
    assertEquals(1, tokenNav.getCurrentTokenIndex());
    tokenNav.next(KalangLexer.DEFAULT_TOKEN_CHANNEL);
    assertEquals("}", tokenNav.getCurrentToken().getText());
    tokenNav.move(8);
    assertEquals("}", tokenNav.getCurrentToken().getText());
    assertEquals("}", treeNav.getParseTree(tokenNav.getCurrentToken()).getText());
    assertEquals(3, tokenNav.getCurrentTokenIndex());
}
Also used : TokenNavigator(kalang.util.TokenNavigator) ParseTreeNavigator(kalang.util.ParseTreeNavigator)

Example 2 with ParseTreeNavigator

use of kalang.util.ParseTreeNavigator 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

ParseTreeNavigator (kalang.util.ParseTreeNavigator)2 AstBuilder (kalang.compiler.AstBuilder)1 CompilationUnit (kalang.compiler.CompilationUnit)1 DefaultCompileContext (kalang.compiler.DefaultCompileContext)1 KalangSource (kalang.compiler.KalangSource)1 TokenNavigator (kalang.util.TokenNavigator)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 Test (org.junit.Test)1