t056lexer.py revision 324c4644fee44b9898524c09511bd33c3f12e2df
1import unittest
2import textwrap
3import antlr3
4import antlr3.tree
5import stringtemplate3
6import testbase
7import sys
8import os
9from StringIO import StringIO
10
11# FIXME: port other tests from TestLexer.java
12
13class T(testbase.ANTLRTest):
14    def execParser(self, grammar, grammarEntry, input):
15        lexerCls, parserCls = self.compileInlineGrammar(grammar)
16
17        cStream = antlr3.StringStream(input)
18        lexer = lexerCls(cStream)
19        tStream = antlr3.CommonTokenStream(lexer)
20        parser = parserCls(tStream)
21        result = getattr(parser, grammarEntry)()
22        return result
23
24
25    def testRefToRuleDoesNotSetChannel(self):
26        # this must set channel of A to HIDDEN.  $channel is local to rule
27        # like $type.
28        grammar = textwrap.dedent(
29            r'''
30            grammar P;
31            options {
32              language=Python;
33            }
34            a returns [foo]: A EOF { $foo = '\%s, channel=\%d' \% ($A.text, $A.channel); } ;
35            A : '-' WS I ;
36            I : '0'..'9'+ ;
37            WS : (' '|'\n') {$channel=HIDDEN;} ;
38            ''')
39
40        found = self.execParser(
41            grammar, 'a',
42            "- 34"
43            )
44
45        self.failUnlessEqual("- 34, channel=0", found)
46
47
48if __name__ == '__main__':
49    unittest.main()
50