def test_variable(self): parsed = Regex.Parser(ur"[[:alpha:]]{1,32}{Hello}{2,34}").parse() expected = Regex.Concatenation([ Regex.Repetition( Regex.Literal([(ord(u"a"), ord(u"z")), (ord(u"A"), ord(u"Z"))]), 1, 32), Regex.Repetition(Regex.Variable("Hello"), 2, 34) ]) self.assertEqual(repr(parsed), repr(expected))
def test_float_pattern(self): expected = Regex.Alternation([ Regex.Concatenation([ Regex.Repetition(Regex.Literal([(ord(u"0"), ord(u"9"))]), 1, -1), Regex.Literal([(ord(u"."), ord(u"."))]), Regex.Repetition(Regex.Literal([(ord(u"0"), ord(u"9"))]), 0, -1) ]), Regex.Concatenation([ Regex.Repetition(Regex.Literal([(ord(u"0"), ord(u"9"))]), 0, -1), Regex.Literal([(ord(u"."), ord(u"."))]), Regex.Repetition(Regex.Literal([(ord(u"0"), ord(u"9"))]), 1, -1) ]) ]) parsed = Regex.Parser(ur"[[:digit:]]+\.[0-9]*|[0-9]*\.[0-9]+").parse() self.assertEqual(repr(parsed), repr(expected))
def test_simple_case(self): defines = { 'a': Regex.Parser("[abc]+").parse(), 'b': Regex.Parser("[xyz]*{a}d").parse() } regex = Regex.Parser("Hello{b}Ok").parse() resolver = Regex.VariableResolver(defines) regex.accept(resolver) resolved = resolver.get() expected = Regex.Concatenation([ Regex.Literal([(ord('H'), ord('H'))]), Regex.Literal([(ord('e'), ord('e'))]), Regex.Literal([(ord('l'), ord('l'))]), Regex.Literal([(ord('l'), ord('l'))]), Regex.Literal([(ord('o'), ord('o'))]), Regex.Concatenation([ Regex.Repetition( Regex.Literal([((ord('x'), ord('z')))]), 0, Regex.Repetition.Infinity), Regex.Repetition( Regex.Literal([((ord('a'), ord('c')))]), 1, Regex.Repetition.Infinity), Regex.Literal([(ord('d'), ord('d'))])]), Regex.Literal([(ord('O'), ord('O'))]), Regex.Literal([(ord('k'), ord('k'))])]) self.assertEqual(repr(resolved), repr(expected))