1 | ''' |
---|
2 | Created on Jul 11, 2012 |
---|
3 | |
---|
4 | @author: Pete |
---|
5 | ''' |
---|
6 | |
---|
7 | |
---|
8 | import re |
---|
9 | import pyparsing |
---|
10 | |
---|
11 | |
---|
12 | string_start = r'^' |
---|
13 | string_end = r'$' |
---|
14 | match_all = r'.*' |
---|
15 | non_greedy_filler = match_all + r'?' |
---|
16 | non_greedy_whitespace = r'\s*?' |
---|
17 | |
---|
18 | spec_cdef_declaration_match_re = re.compile( |
---|
19 | r'' |
---|
20 | + non_greedy_filler # optional any kind of preceding stuff, was \s*? (optional blank space) |
---|
21 | + non_greedy_whitespace |
---|
22 | + r'(cdef)' # 0: cdef |
---|
23 | + r'\(' # opening parenthesis |
---|
24 | # + r'(.*?)' # 1: args (anything between the parentheses) |
---|
25 | + r'(.*?)' # 1: args (anything between the parentheses) |
---|
26 | + r'\)', # closing parenthesis |
---|
27 | re.DOTALL | re.MULTILINE | re.VERBOSE |
---|
28 | ) |
---|
29 | |
---|
30 | |
---|
31 | barrage = """ |
---|
32 | cdef("test1") |
---|
33 | cdef("test2","\nwait(1);\n","waitmove_hack","0x20") |
---|
34 | cdef("test3", |
---|
35 | "\nwait(1);\n", |
---|
36 | "waitmove_hack", |
---|
37 | "0x20") |
---|
38 | cdef("test4", "", "scan_cleanup", "delete") |
---|
39 | cdef("test5", sprintf("dscan_cleanup $1 %s;", _c1), "dscan") |
---|
40 | """ |
---|
41 | |
---|
42 | |
---|
43 | for mo in spec_cdef_declaration_match_re.finditer(barrage): |
---|
44 | print mo.groups(), mo.start(), mo.end(), mo.span() |
---|
45 | |
---|
46 | print """ |
---|
47 | It is not possible to identify properly all these cases with a regular expression |
---|
48 | |
---|
49 | Instead, search for the beginnings of a potential cdef declaration |
---|
50 | 'cdef\(' and start watching the code, incrementing and decrementing |
---|
51 | on ( and ) until the counter gets back to zero. |
---|
52 | """ |
---|