def test_range_missing_end(self): with self.assertRaises(ParserError) as context: parse(u"[a-]") exception = context.exception self.assertEqual(exception.reason, u"expected character, found instruction: ]") self.assertEqual(exception.annotation, (u"[a-]\n" u" ^"))
def test_zero_or_more_missing_repeatable(self): with self.assertRaises(ParserError) as context: parse(u"*") exception = context.exception self.assertEqual(exception.reason, u"* is not preceded by a repeatable expression") self.assertEqual(exception.annotation, (u"*\n" u"^"))
def test_either_missing_end(self): with self.assertRaises(ParserError) as context: parse(u"[ab") exception = context.exception self.assertEqual( exception.reason, u"unexpected end of string, expected ] corresponding to [") self.assertEqual(exception.annotation, (u"[ab\n" u"^--^"))
def test_group_missing_end(self): with self.assertRaises(ParserError) as context: parse(u"(a") exception = context.exception self.assertEqual( exception.reason, u"unexpected end of string, expected ) corresponding to (") self.assertEqual(exception.annotation, (u"(a\n" u"^-^"))
def test_range_missing_start(self): with self.assertRaises(ParserError) as context: parse(u"[-c]") exception = context.exception self.assertEqual(exception.reason, u"range is missing start") self.assertEqual( exception.annotation, ( u"[-c]\n" u"^" ) )
def test_zero_or_more_missing_repeatable(self): with self.assertRaises(ParserError) as context: parse(u"*") exception = context.exception self.assertEqual( exception.reason, u"* is not preceded by a repeatable expression" ) self.assertEqual(exception.annotation, ( u"*\n" u"^" ))
def test_group_missing_end(self): with self.assertRaises(ParserError) as context: parse(u"(a") exception = context.exception self.assertEqual( exception.reason, u"unexpected end of string, expected ) corresponding to (" ) self.assertEqual( exception.annotation, ( u"(a\n" u"^-^" ) )
def test_either_missing_end(self): with self.assertRaises(ParserError) as context: parse(u"[ab") exception = context.exception self.assertEqual( exception.reason, u"unexpected end of string, expected ] corresponding to [" ) self.assertEqual( exception.annotation, ( u"[ab\n" u"^--^" ) )
def test_range_missing_end(self): with self.assertRaises(ParserError) as context: parse(u"[a-]") exception = context.exception self.assertEqual( exception.reason, u"expected character, found instruction: ]" ) self.assertEqual( exception.annotation, ( u"[a-]\n" u" ^" ) )
def test_group_missing_begin(self): with self.assertRaises(ParserError) as context: parse(u"a)") exception = context.exception self.assertEqual( exception.reason, u"found unmatched )" ) self.assertEqual( exception.annotation, ( u"a)\n" u" ^" ) )
def test_range(self): self.assertEqual( parse(u"[a-c]"), Either( frozenset([ Range(Character(u"a"), Character(u"c"), DEFAULT_ALPHABET) ])))
def Regex(regex, optimize_flag=False): ast = parse(tokens(regex)) if len(ast) == 0: raise ValueError("Nothing to parse") automata = generate(ast) if optimize_flag: return optimize(automata) else: return automata
def test_range(self): self.assertEqual( parse(u"[a-c]"), Either(frozenset([Range( Character(u"a"), Character(u"c"), DEFAULT_ALPHABET )])) )
def test_epsilon(self): self.assertEqual(parse(u""), Epsilon())
def __init__(self, regex): self.regex = regex self.ast = parse(regex)
def test_one_or_more(self): self.assertEqual( parse(u"a+"), Concatenation(Character(u"a"), Repetition(Character(u"a"))))
def test_range_missing_start(self): with self.assertRaises(ParserError) as context: parse(u"[-c]") exception = context.exception self.assertEqual(exception.reason, u"range is missing start") self.assertEqual(exception.annotation, (u"[-c]\n" u"^"))
def test_union(self): self.assertEqual( parse(u"a|b"), Union(Character(u"a"), Character(u"b")) )
def test_either(self): self.assertEqual( parse(u"[ab]"), Either(frozenset(map(Character, u"ab"))) )
def test_concatenation(self): self.assertEqual(parse(u"ab"), Concatenation(Character(u"a"), Character(u"b")))
def test_union(self): self.assertEqual(parse(u"a|b"), Union(Character(u"a"), Character(u"b")))
def test_group(self): self.assertEqual( parse(u"(a)"), Group(Character(u"a")) )
def test_zero_or_more(self): self.assertEqual(parse(u"a*"), Repetition(Character(u"a")))
def test_either_missing_begin(self): with self.assertRaises(ParserError) as context: parse(u"ab]") exception = context.exception self.assertEqual(exception.reason, u"found unmatched ]") self.assertEqual(exception.annotation, (u"ab]\n" u" ^"))
def test_either(self): self.assertEqual(parse(u"[ab]"), Either(frozenset(map(Character, u"ab"))))
def test_character(self): self.assertEqual(parse(u"a"), Character(u"a"))
def test_concatenation(self): self.assertEqual( parse(u"ab"), Concatenation(Character(u"a"), Character(u"b")) )
def test_zero_or_more(self): self.assertEqual( parse(u"a*"), Repetition(Character(u"a")) )
def test_neither(self): self.assertEqual( parse(u"[^ab]"), Neither(frozenset(map(Character, u"ab")), DEFAULT_ALPHABET) )
def test_one_or_more(self): self.assertEqual( parse(u"a+"), Concatenation(Character(u"a"), Repetition(Character(u"a"))) )
def test_group(self): self.assertEqual(parse(u"(a)"), Group(Character(u"a")))
def __init__(self, definitions): self.definitions = [] for regex, token_cls in definitions: self.definitions.append((parse(regex).compile(), token_cls))
def test_neither(self): self.assertEqual( parse(u"[^ab]"), Neither(frozenset(map(Character, u"ab")), DEFAULT_ALPHABET))