]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | #!/usr/bin/env python2 |
2 | ||
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. | |
6 | # | |
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. | |
12 | ||
13 | from __future__ import absolute_import, division, print_function | |
14 | import argparse | |
15 | import datetime | |
16 | import os.path as path | |
17 | ||
18 | ||
19 | def print_tests(tests): | |
20 | print('\n'.join([test_tostr(t) for t in tests])) | |
21 | ||
22 | ||
23 | def read_tests(f): | |
24 | basename, _ = path.splitext(path.basename(f)) | |
25 | tests = [] | |
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] == '#': | |
30 | continue | |
31 | ||
32 | opts, pat, text, sgroups = fields[0:4] | |
33 | groups = [] # groups as integer ranges | |
34 | if sgroups == 'NOMATCH': | |
35 | groups = [None] | |
36 | elif ',' in sgroups: | |
37 | noparen = map(lambda s: s.strip('()'), sgroups.split(')(')) | |
38 | for g in noparen: | |
39 | s, e = map(str.strip, g.split(',')) | |
40 | if s == '?' and e == '?': | |
41 | groups.append(None) | |
42 | else: | |
43 | groups.append((int(s), int(e))) | |
44 | else: | |
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. | |
48 | continue | |
49 | ||
50 | if pat == 'SAME': | |
51 | pat = tests[-1][1] | |
52 | if '$' in opts: | |
53 | pat = pat.decode('string_escape') | |
54 | text = text.decode('string_escape') | |
55 | if 'i' in opts: | |
56 | pat = '(?i)%s' % pat | |
57 | ||
58 | name = '%s_%d' % (basename, lineno) | |
59 | tests.append((name, pat, text, groups)) | |
60 | return tests | |
61 | ||
62 | ||
63 | def test_tostr(t): | |
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)) | |
68 | ||
69 | ||
70 | def group_tostr(g): | |
71 | if g is None: | |
72 | return 'None' | |
73 | else: | |
74 | return 'Some((%d, %d))' % (g[0], g[1]) | |
75 | ||
76 | ||
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() | |
84 | ||
85 | tests = [] | |
86 | for f in args.files: | |
87 | tests += read_tests(f) | |
88 | ||
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. | |
92 | // | |
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. | |
98 | ||
99 | // ignore-tidy-linelength | |
100 | ||
101 | // DO NOT EDIT. Automatically generated by 'src/etc/regexp-match-tests' | |
102 | // on {date}. | |
103 | ''' | |
104 | print(tpl.format(date=str(datetime.datetime.now()))) | |
105 | ||
106 | for f in args.files: | |
107 | print('// Tests from %s' % path.basename(f)) | |
108 | print_tests(read_tests(f)) | |
109 | print('') |