1 import unittest 2 import textwrap 3 import antlr3 4 import antlr3.tree 5 import testbase 6 7 class T(testbase.ANTLRTest): 8 def walkerClass(self, base): 9 class TWalker(base): 10 def __init__(self, *args, **kwargs): 11 base.__init__(self, *args, **kwargs) 12 13 self.traces = [] 14 15 16 def traceIn(self, ruleName, ruleIndex): 17 self.traces.append('>'+ruleName) 18 19 20 def traceOut(self, ruleName, ruleIndex): 21 self.traces.append('<'+ruleName) 22 23 24 def recover(self, input, re): 25 # no error recovery yet, just crash! 26 raise 27 28 return TWalker 29 30 31 def setUp(self): 32 self.compileGrammar() 33 self.compileGrammar('t047treeparserWalker.g', options='-trace') 34 35 36 def testWalker(self): 37 input = textwrap.dedent( 38 '''\ 39 char c; 40 int x; 41 42 void bar(int x); 43 44 int foo(int y, char d) { 45 int i; 46 for (i=0; i<3; i=i+1) { 47 x=3; 48 y=5; 49 } 50 } 51 ''') 52 53 cStream = antlr3.StringStream(input) 54 lexer = self.getLexer(cStream) 55 tStream = antlr3.CommonTokenStream(lexer) 56 parser = self.getParser(tStream) 57 r = parser.program() 58 59 self.failUnlessEqual( 60 r.tree.toStringTree(), 61 "(VAR_DEF char c) (VAR_DEF int x) (FUNC_DECL (FUNC_HDR void bar (ARG_DEF int x))) (FUNC_DEF (FUNC_HDR int foo (ARG_DEF int y) (ARG_DEF char d)) (BLOCK (VAR_DEF int i) (for (= i 0) (< i 3) (= i (+ i 1)) (BLOCK (= x 3) (= y 5)))))" 62 ) 63 64 nodes = antlr3.tree.CommonTreeNodeStream(r.tree) 65 nodes.setTokenStream(tStream) 66 walker = self.getWalker(nodes) 67 walker.program() 68 69 # FIXME: need to crosscheck with Java target (compile walker with 70 # -trace option), if this is the real list. For now I'm happy that 71 # it does not crash ;) 72 self.failUnlessEqual( 73 walker.traces, 74 [ '>program', '>declaration', '>variable', '>type', '<type', 75 '>declarator', '<declarator', '<variable', '<declaration', 76 '>declaration', '>variable', '>type', '<type', '>declarator', 77 '<declarator', '<variable', '<declaration', '>declaration', 78 '>functionHeader', '>type', '<type', '>formalParameter', 79 '>type', '<type', '>declarator', '<declarator', 80 '<formalParameter', '<functionHeader', '<declaration', 81 '>declaration', '>functionHeader', '>type', '<type', 82 '>formalParameter', '>type', '<type', '>declarator', 83 '<declarator', '<formalParameter', '>formalParameter', '>type', 84 '<type', '>declarator', '<declarator', '<formalParameter', 85 '<functionHeader', '>block', '>variable', '>type', '<type', 86 '>declarator', '<declarator', '<variable', '>stat', '>forStat', 87 '>expr', '>expr', '>atom', '<atom', '<expr', '<expr', '>expr', 88 '>expr', '>atom', '<atom', '<expr', '>expr', '>atom', '<atom', 89 '<expr', '<expr', '>expr', '>expr', '>expr', '>atom', '<atom', 90 '<expr', '>expr', '>atom', '<atom', '<expr', '<expr', '<expr', 91 '>block', '>stat', '>expr', '>expr', '>atom', '<atom', '<expr', 92 '<expr', '<stat', '>stat', '>expr', '>expr', '>atom', '<atom', 93 '<expr', '<expr', '<stat', '<block', '<forStat', '<stat', 94 '<block', '<declaration', '<program' 95 ] 96 ) 97 98 def testRuleLabelPropertyRefText(self): 99 self.compileGrammar() 100 self.compileGrammar('t047treeparserWalker.g', options='-trace') 101 102 input = textwrap.dedent( 103 '''\ 104 char c; 105 ''') 106 107 cStream = antlr3.StringStream(input) 108 lexer = self.getLexer(cStream) 109 tStream = antlr3.CommonTokenStream(lexer) 110 parser = self.getParser(tStream) 111 r = parser.variable() 112 113 nodes = antlr3.tree.CommonTreeNodeStream(r.tree) 114 nodes.setTokenStream(tStream) 115 walker = self.getWalker(nodes) 116 r = walker.variable() 117 118 self.failUnlessEqual(r, 'c') 119 120 121 if __name__ == '__main__': 122 unittest.main() 123