3 # Copyright 2014 The Rust Project Developers. See the COPYRIGHT
4 # file at the top-level directory of this distribution and at
5 # http://rust-lang.org/COPYRIGHT.
7 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 # option. This file may not be copied, modified, or distributed
11 # except according to those terms.
13 from __future__ import absolute_import, division, print_function
16 import os.path as path
19 def print_tests(tests):
20 print('\n'.join([test_tostr(t) for t in tests]))
24 basename, _ = path.splitext(path.basename(f))
26 for lineno, line in enumerate(open(f), 1):
27 fields = filter(None, map(str.strip, line.split('\t')))
28 if not (4 <= len(fields) <= 5) \
29 or 'E' not in fields[0] or fields[0][0] == '#':
32 opts, pat, text, sgroups = fields[0:4]
33 groups = [] # groups as integer ranges
34 if sgroups == 'NOMATCH':
37 noparen = map(lambda s: s.strip('()'), sgroups.split(')('))
39 s, e = map(str.strip, g.split(','))
40 if s == '?' and e == '?':
43 groups.append((int(s), int(e)))
45 # This skips tests that should result in an error.
46 # There aren't many, so I think we can just capture those
47 # manually. Possibly fix this in future.
53 pat = pat.decode('string_escape')
54 text = text.decode('string_escape')
58 name = '%s_%d' % (basename, lineno)
59 tests.append((name, pat, text, groups))
64 lineno, pat, text, groups = t
65 options = map(group_tostr, groups)
66 return 'mat!{match_%s, r"%s", r"%s", %s}' \
67 % (lineno, pat, '' if text == "NULL" else text, ', '.join(options))
74 return 'Some((%d, %d))' % (g[0], g[1])
77 if __name__ == '__main__':
78 parser = argparse.ArgumentParser(
79 description='Generate match tests from an AT&T POSIX test file.')
80 aa = parser.add_argument
81 aa('files', nargs='+',
82 help='A list of dat AT&T POSIX test files. See src/libregexp/testdata')
83 args = parser.parse_args()
87 tests += read_tests(f)
89 tpl = '''// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
90 // file at the top-level directory of this distribution and at
91 // http://rust-lang.org/COPYRIGHT.
93 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
94 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
95 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
96 // option. This file may not be copied, modified, or distributed
97 // except according to those terms.
99 // ignore-tidy-linelength
101 // DO NOT EDIT. Automatically generated by 'src/etc/regexp-match-tests'
104 print(tpl.format(date=str(datetime.datetime.now())))
107 print('// Tests from %s' % path.basename(f))
108 print_tests(read_tests(f))