Ejemplo n.º 1
0
 def test_article_specially_crafted_regex(self, n=100):
     self.assertEqual((0, n), pyrex.rex('a?' * n + 'a' * n).match('a' * n))
Ejemplo n.º 2
0
 def test_would_never_end(self):
     self.assertEqual(None, pyrex.rex('(.+.+)+y').match('a' * 29))
Ejemplo n.º 3
0
 def test_will_match_greedly(self):
     self.assertEqual((0, 30), pyrex.rex('(.+.+)+y').match('a' * 29 + 'y'))
Ejemplo n.º 4
0
 def test_will_match_zero_width_string(self):
     self.assertEqual(None, pyrex.rex('a*').match(''))
     self.assertEqual(None, pyrex.rex('a*').match('b'))
Ejemplo n.º 5
0
 def test_options_simple(self):
     self.assertEqual((0, 2), pyrex.rex('ab|aa').match('aab'))
Ejemplo n.º 6
0
 def test_double_literal(self):
     self.assertEqual((0, 2), pyrex.rex('aa').match('aa'))
Ejemplo n.º 7
0
 def test_simple_repetition(self):
     self.assertEqual((0, 2), pyrex.rex('a+').match('aa'))
Ejemplo n.º 8
0
 def test_single_literal(self):
     self.assertEqual((0, 1), pyrex.rex('a').match('a'))
Ejemplo n.º 9
0
 def test_options_simple(self):
     self.assertEqual((0, 2), pyrex.rex('ab|aa').match('aab'))
Ejemplo n.º 10
0
 def test_will_match_zero_width_string(self):
     self.assertEqual(None, pyrex.rex('a*').match(''))
     self.assertEqual(None, pyrex.rex('a*').match('b'))
Ejemplo n.º 11
0
 def test_simple_repetition_zero_or_one(self):
     self.assertEqual((0, 2), pyrex.rex('aa?').match('aa'))
Ejemplo n.º 12
0
 def test_simple_repetition_with_optional(self):
     self.assertEqual((0, 2), pyrex.rex('a+?').match('aa'))
Ejemplo n.º 13
0
 def test_simple_repetition(self):
     self.assertEqual((0, 2), pyrex.rex('a+').match('aa'))
Ejemplo n.º 14
0
 def test_double_literal_matching_multiple_times(self):
     self.assertEqual((0, 2), pyrex.rex('aa').match('aaaa'))
Ejemplo n.º 15
0
 def test_double_literal(self):
     self.assertEqual((0, 2), pyrex.rex('aa').match('aa'))
Ejemplo n.º 16
0
 def test_grouping_simple(self):
     self.assertEqual((0, 5), pyrex.rex('a(ab)+').match('aabab'))
Ejemplo n.º 17
0
 def test_single_literal_in_the_middle(self):
     self.assertEqual((1, 2), pyrex.rex('a').match('ba'))
Ejemplo n.º 18
0
 def test_would_never_end(self):
     self.assertEqual(None, pyrex.rex('(.+.+)+y').match('a'*29))
Ejemplo n.º 19
0
 def test_double_literal_matching_multiple_times(self):
     self.assertEqual((0, 2), pyrex.rex('aa').match('aaaa'))
Ejemplo n.º 20
0
 def test_will_match_greedly(self):
     self.assertEqual((0, 30), pyrex.rex('(.+.+)+y').match('a'*29+'y'))
Ejemplo n.º 21
0
 def test_simple_repetition_with_optional(self):
     self.assertEqual((0, 2), pyrex.rex('a+?').match('aa'))
Ejemplo n.º 22
0
 def test_will_match_greedly_in_the_middle(self):
     self.assertEqual((1, 31), pyrex.rex('a(.+.+)+y').match('b' + 'a'*29+ 'yb'))
Ejemplo n.º 23
0
 def test_simple_repetition_zero_or_one(self):
     self.assertEqual((0, 2), pyrex.rex('aa?').match('aa'))
Ejemplo n.º 24
0
 def test_article_specially_crafted_regex(self, n=100):
     self.assertEqual((0, n), pyrex.rex('a?'*n+'a'*n).match('a'*n))
Ejemplo n.º 25
0
 def test_grouping_simple(self):
     self.assertEqual((0, 5), pyrex.rex('a(ab)+').match('aabab'))
Ejemplo n.º 26
0
 def test_regex_source(self):
     self.assertEqual('0000: CONSUME a\n0001: JUMP (1, 2)\n0002: CONSUME b\n0003: MATCH!', str(pyrex.rex('ab?')))
Ejemplo n.º 27
0
 def test_single_literal(self):
     self.assertEqual((0, 1), pyrex.rex('a').match('a'))
Ejemplo n.º 28
0
try: input = raw_input
except NameError: pass

def simulate(machine, string):
    for i, answer, state in machine.matcher(string):
        best = (string[answer[0]:answer[1]] + ' ' + str(answer) if answer else '<none>')
        print('Best answer: ' + best)
        print('Input: ' + string)
        print('       ' + i*' ' + '^' + ' (' + str(i) + ')')
        print()
        
        per_line = {item: str(start)+' >' for start, item in state}
        
        for j, line in enumerate(machine.source()):
            print('{:>5}{:04d}: {}'.format(per_line.get(j, ''), j, line))

        print('-----')
        input()
        
if __name__ == '__main__':
    if len(sys.argv) != 3:
        print('usage: simulator.py <regex> <input>')
        sys.exit(1)
        
    print('-----')
    print('Matching pattern "{}" against input "{}"'.format(*sys.argv[1:]))
    print('-----')
    print()
    simulate(pyrex.rex(sys.argv[1]), sys.argv[2])
Ejemplo n.º 29
0
 def test_will_match_greedly_in_the_middle(self):
     self.assertEqual((1, 31),
                      pyrex.rex('a(.+.+)+y').match('b' + 'a' * 29 + 'yb'))
Ejemplo n.º 30
0

def simulate(machine, string):
    for i, answer, state in machine.matcher(string):
        best = (string[answer[0]:answer[1]] + ' ' +
                str(answer) if answer else '<none>')
        print('Best answer: ' + best)
        print('Input: ' + string)
        print('       ' + i * ' ' + '^' + ' (' + str(i) + ')')
        print()

        per_line = {item: str(start) + ' >' for start, item in state}

        for j, line in enumerate(machine.source()):
            print('{:>5}{:04d}: {}'.format(per_line.get(j, ''), j, line))

        print('-----')
        input()


if __name__ == '__main__':
    if len(sys.argv) != 3:
        print('usage: simulator.py <regex> <input>')
        sys.exit(1)

    print('-----')
    print('Matching pattern "{}" against input "{}"'.format(*sys.argv[1:]))
    print('-----')
    print()
    simulate(pyrex.rex(sys.argv[1]), sys.argv[2])
Ejemplo n.º 31
0
 def test_regex_source(self):
     self.assertEqual(
         '0000: CONSUME a\n0001: JUMP (1, 2)\n0002: CONSUME b\n0003: MATCH!',
         str(pyrex.rex('ab?')))
Ejemplo n.º 32
0
 def test_single_literal_in_the_middle(self):
     self.assertEqual((1, 2), pyrex.rex('a').match('ba'))