Ben Gruver | 324c464 | 2011-11-15 16:02:09 -0800 | [diff] [blame] | 1 | import unittest |
| 2 | import textwrap |
| 3 | import antlr3 |
| 4 | import antlr3.tree |
| 5 | import stringtemplate3 |
| 6 | import testbase |
| 7 | import sys |
| 8 | import os |
| 9 | from StringIO import StringIO |
| 10 | |
| 11 | # FIXME: port other tests from TestLexer.java |
| 12 | |
| 13 | class 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 | |
| 48 | if __name__ == '__main__': |
| 49 | unittest.main() |