[pypy-svn] r12815 - pypy/branch/pycompiler/module/recparser/test
adim at codespeak.net
adim at codespeak.net
Thu May 26 19:18:51 CEST 2005
Author: adim
Date: Thu May 26 19:18:51 2005
New Revision: 12815
Added:
pypy/branch/pycompiler/module/recparser/test/test_lookahead.py
Log:
- forgot to add this file on last checkin
Added: pypy/branch/pycompiler/module/recparser/test/test_lookahead.py
==============================================================================
--- (empty file)
+++ pypy/branch/pycompiler/module/recparser/test/test_lookahead.py Thu May 26 19:18:51 2005
@@ -0,0 +1,70 @@
+from pypy.module.recparser.grammar import Alternative, Sequence, KleenStar, \
+ Token, EmptyToken, build_first_sets
+
+class TestLookAheadBasics:
+
+ def setup_method(self, method):
+ self.tok1 = Token('t1', 'foo')
+ self.tok2 = Token('t2', 'bar')
+ self.tok3 = Token('t3', 'foobar')
+ self.tokens = [self.tok1, self.tok2, self.tok3]
+ build_first_sets(self.tokens)
+
+ def test_basic_token(self):
+ assert self.tok1.first_set == [self.tok1]
+
+
+ def test_basic_alternative(self):
+ alt = Alternative('alt', *self.tokens)
+ build_first_sets([alt])
+ assert alt.first_set == self.tokens
+
+
+ def test_basic_sequence(self):
+ seq = Sequence('seq', *self.tokens)
+ build_first_sets([seq])
+ assert seq.first_set == [self.tokens[0]]
+
+ def test_basic_kleenstar(self):
+ tok1, tok2, tok3 = self.tokens
+ kstar = KleenStar('kstar', 1, 3, tok1)
+ build_first_sets([kstar])
+ assert kstar.first_set == [tok1]
+ kstar = KleenStar('kstar', 0, 3, tok1)
+ build_first_sets([kstar])
+ assert kstar.first_set == [tok1, EmptyToken]
+
+
+def test_token_comparison():
+ assert Token('t1', 'foo') == Token('t1', 'foo')
+ assert ('t1', 'foo') == Token('t1', 'foo')
+
+ # assert Token('t1', 'foo') == Token('t1', None)
+ assert ('t1', 'foo') == Token('t1', None)
+
+ assert Token('t1', 'foo') != Token('t2', 'foo')
+ assert ('t1', 'foo') != Token('t2', 'foo')
+
+ assert Token('t2', 'foo') != Token('t1', None)
+ assert ('t2', 'foo') != Token('t1', None)
+
+
+class TestLookAhead:
+
+ def setup_method(self, method):
+ self.LOW = Token('LOW', 'low')
+ self.CAP = Token('CAP' ,'cap')
+ self.A = Alternative('A')
+ k1 = KleenStar('k1', 0, rule=self.LOW)
+ k2 = KleenStar('k2', 0, rule=self.CAP)
+ self.B = Sequence('B', k1, self.A)
+ self.C = Sequence('C', k2, self.A)
+ self.A.args = [self.B, self.C]
+ build_first_sets([self.A, self.B, self.C, self.LOW, self.CAP, k1, k2])
+
+ def test_S_first_set(self):
+ for s in [('LOW', 'low'), EmptyToken, ('CAP', 'cap')]:
+ assert s in self.A.first_set
+ assert s in self.B.first_set
+ assert s in self.C.first_set
+
More information about the Pypy-commit
mailing list