1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | ''' |
---|
5 | $Id: tester4.py 979 2012-06-26 05:14:24Z jemian $ |
---|
6 | |
---|
7 | develop regular expression to match macro declarations |
---|
8 | ''' |
---|
9 | |
---|
10 | |
---|
11 | import re |
---|
12 | import os |
---|
13 | |
---|
14 | |
---|
15 | spec_macro_declaration_match_re = re.compile( |
---|
16 | r'^' # line start |
---|
17 | + r'\s*?' # optional blank space |
---|
18 | + r'(r?def)' # 0: def_type (rdef | def) |
---|
19 | + r'\s*?' # optional blank space |
---|
20 | + r'([a-zA-Z_][\w_]*)' # 1: macro_name |
---|
21 | + r'(.*?)' # 2: optional arguments |
---|
22 | + r'(#.*?)?' # 3: optional comment |
---|
23 | + r'$' # line end |
---|
24 | ) |
---|
25 | spec_cdef_declaration_match_re = re.compile( |
---|
26 | r'^' # line start |
---|
27 | + r'\s*?' # optional blank space |
---|
28 | + r'(cdef)' # 0: cdef |
---|
29 | + r'\(' # opening parenthesis |
---|
30 | + r'(.*?)' # 1: args (anything between the parentheses) |
---|
31 | + r'\)' # closing parenthesis |
---|
32 | + r'\s*?' # optional blank space |
---|
33 | + r'(#.*?)?' # 2: optional comment |
---|
34 | + r'$' # line end |
---|
35 | ) |
---|
36 | |
---|
37 | spec_function_declaration_match_re = re.compile( |
---|
38 | r'^' # line start |
---|
39 | + r'\s*?' # optional blank space |
---|
40 | + r'(r?def)' # 0: def_type (rdef | def) |
---|
41 | + r'\s*?' # optional blank space |
---|
42 | + r'([a-zA-Z_][\w_]*)' # 1: function_name |
---|
43 | + r'\(' # opening parenthesis |
---|
44 | + r'(.*?)' # 2: args (anything between the parentheses) |
---|
45 | + r'\)' # closing parenthesis |
---|
46 | + r'\s*?' # optional blank space |
---|
47 | + r'\'' # open macro content |
---|
48 | + r'(.*?)' # 3: args (anything between the parentheses) |
---|
49 | + r'(#.*?)?' # 4: more_content |
---|
50 | + r'$' # line end |
---|
51 | ) |
---|
52 | |
---|
53 | TEST_DIR = os.path.join('..', 'macros') |
---|
54 | |
---|
55 | tests = { |
---|
56 | 'macro': spec_macro_declaration_match_re, |
---|
57 | 'cdef': spec_cdef_declaration_match_re, |
---|
58 | 'function': spec_function_declaration_match_re, |
---|
59 | } |
---|
60 | |
---|
61 | for f in sorted(os.listdir(TEST_DIR)): |
---|
62 | if f.endswith('.mac'): |
---|
63 | print '\n', f |
---|
64 | for linenumber, line in enumerate(open(os.path.join(TEST_DIR, f)).readlines()): |
---|
65 | for name in ('cdef', 'function', 'macro'): |
---|
66 | m = tests[name].match(line) |
---|
67 | if m is not None: |
---|
68 | print name, linenumber+1, m.groups() |
---|
69 | break |
---|