source: specdomain/src/specdomain/test/tester.py @ 934

Last change on this file since 934 was 934, checked in by jemian, 11 years ago

working to recognize cdef, this is testing the regular expression, looks like the parsing of cdef will be in steps

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1#!/usr/bin/env python
2
3#  $Id: tester.py 934 2012-06-15 00:00:14Z jemian $
4
5
6import re
7
8#  http://regexpal.com/
9#http://www.txt2re.com/index-python.php3
10
11def 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
25re_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
39test_cases = '''
40global  BCDA_GM[]
41
42   global    theta[]
43   global    2theta[]  # this will not be found
44   global    _motor[]
45
46global kohzu_PV kohzuMV_PV UND_PV Und_Off UNDE_TRACK_ON
47global       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
55tester(test_cases, re_spec_global_declaration)
56
57##################################################################################
58
59py_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
67test_cases = '''
68the.parent.class.myFunc(a, b) -> answer
69f2()
70f4(1, None, "four", four, 4, 4.0, "4.0")
71'''
72
73tester(test_cases, py_sig_re)
74
75##################################################################################
76
77
78match_all = '.*'
79non_greedy_filler = match_all+'?'
80double_quote_string = '("'+non_greedy_filler+'")'
81word_match = '((?:[a-z_][\w]*))'
82cdef_match = '(cdef)'
83
84regexp_str = ''
85regexp_str += non_greedy_filler + double_quote_string
86regexp_str += non_greedy_filler + word_match+'?'
87regexp_str += non_greedy_filler + word_match+'?'
88regexp_str += non_greedy_filler + word_match+'?'
89
90spec_func_re = re.compile(regexp_str, re.IGNORECASE|re.DOTALL)
91
92test_cases = '''
93cdef("macro_name", "commands", "partname", "delete")
94cdef("macro_name", "commands", "partname", flags)
95cdef("cleanup_once", sprintf("dscan_cleanup $1 %s;", _c1), "dscan")
96cdef("macro_name", "commands", "partname")
97cdef("macro_name")
98thing(arg1,arg2)
99afunc("tires")
100Afunc()
101aFunc(parm1)
102cdef("geo_ub_default", "", "ub.mac")
103cdef("config_mac", "{PLOT_CNTRS_MAX = COUNTERS}", "PLOT_Y", 0x10 )
104'''
105f = open('cdef-examples.mac', 'r')
106buf = f.read()
107f.close()
108test_cases += buf
109tester(test_cases, spec_func_re)
110
111cdef1_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)
120func_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
123tester(test_cases, cdef1_re)
Note: See TracBrowser for help on using the repository browser.