1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | # $Id: tester.py 934 2012-06-15 00:00:14Z jemian $ |
---|
4 | |
---|
5 | |
---|
6 | import re |
---|
7 | |
---|
8 | # http://regexpal.com/ |
---|
9 | #http://www.txt2re.com/index-python.php3 |
---|
10 | |
---|
11 | def tester(test_cases, regexp): |
---|
12 | for s in test_cases.split('\n'): |
---|
13 | if len(s) > 0: |
---|
14 | print s, ":", |
---|
15 | m = regexp.match(s) |
---|
16 | if m is None: |
---|
17 | print "" |
---|
18 | else: |
---|
19 | parselist = m.groups() |
---|
20 | print parselist |
---|
21 | print "#" + "-"*59 |
---|
22 | |
---|
23 | ################################################################################## |
---|
24 | |
---|
25 | re_spec_global_declaration = re.compile( |
---|
26 | r''' |
---|
27 | ^ # start of line |
---|
28 | (\s*) # optional preceding white space |
---|
29 | global # "global" declaration |
---|
30 | # |
---|
31 | # FIXME: only finds the last symbol |
---|
32 | ([\s]+[@]?[a-zA-Z_][\w]*(\[\])?)+ # variable name(s) + optional array braces |
---|
33 | # |
---|
34 | (\s+\#.*)* # optional comments |
---|
35 | $ # end of line |
---|
36 | ''', re.VERBOSE |
---|
37 | ) |
---|
38 | |
---|
39 | test_cases = ''' |
---|
40 | global BCDA_GM[] |
---|
41 | |
---|
42 | global theta[] |
---|
43 | global 2theta[] # this will not be found |
---|
44 | global _motor[] |
---|
45 | |
---|
46 | global kohzu_PV kohzuMV_PV UND_PV Und_Off UNDE_TRACK_ON |
---|
47 | global kohzuStop_PV kohzuMode_PV kohzuMove_PV |
---|
48 | global CCD_OVERHEAD_SECS_MEASURED # measured readout time |
---|
49 | |
---|
50 | global @A_name[] @B_name[] |
---|
51 | unglobal @A_name |
---|
52 | unglobal @B_name |
---|
53 | ''' |
---|
54 | |
---|
55 | tester(test_cases, re_spec_global_declaration) |
---|
56 | |
---|
57 | ################################################################################## |
---|
58 | |
---|
59 | py_sig_re = re.compile( |
---|
60 | r'''^ ([\w.]*\.)? # class name(s) |
---|
61 | (\w+) \s* # thing name |
---|
62 | (?: \((.*)\) # optional: arguments |
---|
63 | (?:\s* -> \s* (.*))? # return annotation |
---|
64 | )? $ # and nothing more |
---|
65 | ''', re.VERBOSE) |
---|
66 | |
---|
67 | test_cases = ''' |
---|
68 | the.parent.class.myFunc(a, b) -> answer |
---|
69 | f2() |
---|
70 | f4(1, None, "four", four, 4, 4.0, "4.0") |
---|
71 | ''' |
---|
72 | |
---|
73 | tester(test_cases, py_sig_re) |
---|
74 | |
---|
75 | ################################################################################## |
---|
76 | |
---|
77 | |
---|
78 | match_all = '.*' |
---|
79 | non_greedy_filler = match_all+'?' |
---|
80 | double_quote_string = '("'+non_greedy_filler+'")' |
---|
81 | word_match = '((?:[a-z_][\w]*))' |
---|
82 | cdef_match = '(cdef)' |
---|
83 | |
---|
84 | regexp_str = '' |
---|
85 | regexp_str += non_greedy_filler + double_quote_string |
---|
86 | regexp_str += non_greedy_filler + word_match+'?' |
---|
87 | regexp_str += non_greedy_filler + word_match+'?' |
---|
88 | regexp_str += non_greedy_filler + word_match+'?' |
---|
89 | |
---|
90 | spec_func_re = re.compile(regexp_str, re.IGNORECASE|re.DOTALL) |
---|
91 | |
---|
92 | test_cases = ''' |
---|
93 | cdef("macro_name", "commands", "partname", "delete") |
---|
94 | cdef("macro_name", "commands", "partname", flags) |
---|
95 | cdef("cleanup_once", sprintf("dscan_cleanup $1 %s;", _c1), "dscan") |
---|
96 | cdef("macro_name", "commands", "partname") |
---|
97 | cdef("macro_name") |
---|
98 | thing(arg1,arg2) |
---|
99 | afunc("tires") |
---|
100 | Afunc() |
---|
101 | aFunc(parm1) |
---|
102 | cdef("geo_ub_default", "", "ub.mac") |
---|
103 | cdef("config_mac", "{PLOT_CNTRS_MAX = COUNTERS}", "PLOT_Y", 0x10 ) |
---|
104 | ''' |
---|
105 | f = open('cdef-examples.mac', 'r') |
---|
106 | buf = f.read() |
---|
107 | f.close() |
---|
108 | test_cases += buf |
---|
109 | tester(test_cases, spec_func_re) |
---|
110 | |
---|
111 | cdef1_re = re.compile(cdef_match+'\(' |
---|
112 | + '('+match_all+')' |
---|
113 | + '\)', |
---|
114 | re.IGNORECASE|re.DOTALL) |
---|
115 | #cdef2_re = re.compile(cdef_match+'\(' |
---|
116 | # + non_greedy_filler + word_match |
---|
117 | # + non_greedy_filler + word_match |
---|
118 | # +'\)', |
---|
119 | # re.IGNORECASE|re.DOTALL) |
---|
120 | func_start_re = re.compile(word_match+'\(', |
---|
121 | re.IGNORECASE|re.DOTALL) |
---|
122 | # cdef1_re is most general for recognizing any of the different cdef signatures |
---|
123 | tester(test_cases, cdef1_re) |
---|