blob: a53f92a3d3b4e87790c1752e67ce2827897855ae [file] [log] [blame]
Ben Gruver324c4642011-11-15 16:02:09 -08001import 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()