Beispiel #1
0
 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"   ^"))
Beispiel #2
0
 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"^"))
Beispiel #3
0
 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"^--^"))
Beispiel #4
0
 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"^-^"))
Beispiel #5
0
 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"^"
         )
     )
Beispiel #6
0
 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"^"
     ))
Beispiel #7
0
 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"^-^"
         )
     )
Beispiel #8
0
 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"^--^"
         )
     )
Beispiel #9
0
 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"   ^"
         )
     )
Beispiel #10
0
 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" ^"
         )
     )
Beispiel #11
0
 def test_range(self):
     self.assertEqual(
         parse(u"[a-c]"),
         Either(
             frozenset([
                 Range(Character(u"a"), Character(u"c"), DEFAULT_ALPHABET)
             ])))
Beispiel #12
0
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
Beispiel #13
0
 def test_range(self):
     self.assertEqual(
         parse(u"[a-c]"),
         Either(frozenset([Range(
             Character(u"a"),
             Character(u"c"),
             DEFAULT_ALPHABET
         )]))
     )
Beispiel #14
0
 def test_epsilon(self):
     self.assertEqual(parse(u""), Epsilon())
Beispiel #15
0
 def __init__(self, regex):
     self.regex = regex
     self.ast = parse(regex)
Beispiel #16
0
 def __init__(self, regex):
     self.regex = regex
     self.ast = parse(regex)
Beispiel #17
0
 def test_epsilon(self):
     self.assertEqual(parse(u""), Epsilon())
Beispiel #18
0
 def test_one_or_more(self):
     self.assertEqual(
         parse(u"a+"),
         Concatenation(Character(u"a"), Repetition(Character(u"a"))))
Beispiel #19
0
 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"^"))
Beispiel #20
0
 def test_union(self):
     self.assertEqual(
         parse(u"a|b"),
         Union(Character(u"a"), Character(u"b"))
     )
Beispiel #21
0
 def test_either(self):
     self.assertEqual(
         parse(u"[ab]"),
         Either(frozenset(map(Character, u"ab")))
     )
Beispiel #22
0
 def test_concatenation(self):
     self.assertEqual(parse(u"ab"),
                      Concatenation(Character(u"a"), Character(u"b")))
Beispiel #23
0
 def test_union(self):
     self.assertEqual(parse(u"a|b"), Union(Character(u"a"),
                                           Character(u"b")))
Beispiel #24
0
 def test_group(self):
     self.assertEqual(
         parse(u"(a)"),
         Group(Character(u"a"))
     )
Beispiel #25
0
 def test_zero_or_more(self):
     self.assertEqual(parse(u"a*"), Repetition(Character(u"a")))
Beispiel #26
0
 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"  ^"))
Beispiel #27
0
 def test_either(self):
     self.assertEqual(parse(u"[ab]"),
                      Either(frozenset(map(Character, u"ab"))))
Beispiel #28
0
 def test_character(self):
     self.assertEqual(parse(u"a"), Character(u"a"))
Beispiel #29
0
 def test_concatenation(self):
     self.assertEqual(
         parse(u"ab"),
         Concatenation(Character(u"a"), Character(u"b"))
     )
Beispiel #30
0
 def test_character(self):
     self.assertEqual(parse(u"a"), Character(u"a"))
Beispiel #31
0
 def test_zero_or_more(self):
     self.assertEqual(
         parse(u"a*"),
         Repetition(Character(u"a"))
     )
Beispiel #32
0
 def test_neither(self):
     self.assertEqual(
         parse(u"[^ab]"),
         Neither(frozenset(map(Character, u"ab")), DEFAULT_ALPHABET)
     )
Beispiel #33
0
 def test_one_or_more(self):
     self.assertEqual(
         parse(u"a+"),
         Concatenation(Character(u"a"), Repetition(Character(u"a")))
     )
Beispiel #34
0
 def test_group(self):
     self.assertEqual(parse(u"(a)"), Group(Character(u"a")))
Beispiel #35
0
 def __init__(self, definitions):
     self.definitions = []
     for regex, token_cls in definitions:
         self.definitions.append((parse(regex).compile(), token_cls))
Beispiel #36
0
 def test_neither(self):
     self.assertEqual(
         parse(u"[^ab]"),
         Neither(frozenset(map(Character, u"ab")), DEFAULT_ALPHABET))