Changeset 957
- Timestamp:
- Jun 21, 2012 1:08:07 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
specdomain/src/specdomain/test/parser.py
r956 r957 19 19 20 20 21 # http://www.txt2re.com/index-python.php3 22 # http://regexpal.com/ 23 21 24 string_start = r'^' 22 25 string_end = r'$' 23 26 match_all = r'.*' 24 27 non_greedy_filler = match_all + r'?' 25 non_greedy_whitespace = r'\ w*?'28 non_greedy_whitespace = r'\s*?' 26 29 double_quote_string_match = r'("' + non_greedy_filler + r'")' 30 prog_name_match = r'([a-z_]\w*)' 27 31 word_match = r'((?:[a-z_]\w*))' 28 32 cdef_match = r'(cdef)' … … 30 34 extended_comment_match = r'(' + extended_comment_marker + r')' 31 35 32 spec_macro_sig_re = re.compile(36 macro_sig_re = re.compile( 33 37 r'''^ ([a-zA-Z_]\w*) # macro name 34 38 ''', re.VERBOSE) 35 39 36 spec_func_sig_re = re.compile(word_match + r'\('40 func_sig_re = re.compile(word_match + r'\(' 37 41 + r'(' + match_all + r')' 38 42 + r'\)', 39 43 re.IGNORECASE|re.DOTALL) 40 44 41 spec_cdef_name_sig_re = re.compile(double_quote_string_match,45 cdef_name_sig_re = re.compile(double_quote_string_match, 42 46 re.IGNORECASE|re.DOTALL) 43 47 44 48 45 spec_extended_comment_flag_sig_re = re.compile(extended_comment_marker,49 extended_comment_flag_sig_re = re.compile(extended_comment_marker, 46 50 re.IGNORECASE|re.DOTALL) 47 spec_extended_comment_start_sig_re = re.compile(string_start51 extended_comment_start_sig_re = re.compile(string_start 48 52 + non_greedy_whitespace 49 53 + extended_comment_match, 50 54 re.IGNORECASE|re.VERBOSE) 51 spec_extended_comment_end_sig_re = re.compile(non_greedy_whitespace55 extended_comment_end_sig_re = re.compile(non_greedy_whitespace 52 56 + extended_comment_match 53 57 + non_greedy_whitespace 54 58 + r'#' + non_greedy_filler 55 + r'$',59 + string_end, 56 60 re.IGNORECASE|re.VERBOSE) 57 spec_extended_comment_block_sig_re = re.compile(string_start61 extended_comment_block_sig_re = re.compile(string_start 58 62 + non_greedy_whitespace 59 63 + extended_comment_marker … … 63 67 + string_end, 64 68 re.IGNORECASE|re.DOTALL|re.MULTILINE) 69 lgc_variable_sig_re = re.compile(string_start 70 + non_greedy_whitespace 71 + r'(local|global|constant)' 72 + r'((?:\s*@?[\w.eE+-]+\[?\]?)*)' 73 + non_greedy_whitespace 74 + r'#' + non_greedy_filler 75 + string_end, 76 re.VERBOSE) 65 77 66 78 … … 114 126 line_number += 1 115 127 if state == 'command level': 128 # test if local, global, or constant variable declaration 129 m = self._match(lgc_variable_sig_re, line) 130 if m is not None: 131 objtype, vars = lgc_variable_sig_re.match(line).groups() 132 pos = vars.find('#') 133 if pos > -1: 134 vars = vars[:pos] 135 if line_number > 220: 136 pass # TODO: test for multiple definitions on one line 137 m['objtype'] = objtype 138 m['start_line'] = m['end_line'] = line_number 139 del m['start'], m['end'], m['line'] 140 if objtype == 'constant': 141 var, value = vars.split() 142 m['text'] = var 143 self.findings.append(dict(m)) 144 else: 145 for var in vars.split(): 146 m['text'] = var 147 self.findings.append(dict(m)) 148 # TODO: to what is this local? 149 continue 150 116 151 # test if one-line extended comment 117 m = self._match( spec_extended_comment_block_sig_re, line)152 m = self._match(extended_comment_block_sig_re, line) 118 153 if m is not None: 119 154 del m['start'], m['end'], m['line'] 120 155 m['objtype'] = 'extended comment' 121 m['start_line'] = line_number 122 m['end_line'] = line_number 123 self.findings.append(m) 156 m['start_line'] = m['end_line'] = line_number 157 self.findings.append(dict(m)) 124 158 continue 125 159 126 160 # test if start of multiline extended comment 127 m = self._match( spec_extended_comment_start_sig_re, line)161 m = self._match(extended_comment_start_sig_re, line) 128 162 if m is not None: 129 163 text = m['line'][m['end']:] … … 139 173 elif state == 'extended comment': 140 174 # test if end of multiline extended comment 141 if line_number > 250: 142 pass 143 m = self._match(spec_extended_comment_end_sig_re, line) 175 m = self._match(extended_comment_end_sig_re, line) 144 176 if m is not None: 145 177 text = m['line'][:m['start']] … … 147 179 ec['text'] = '\n'.join(ec['text']) 148 180 ec['end_line'] = line_number 149 self.findings.append( ec)181 self.findings.append(dict(ec)) 150 182 state = state_stack.pop() 151 183 del ec … … 164 196 'text': m.group(1), 165 197 'line': line, 198 'filename': self.filename, 166 199 } 167 200 return d … … 171 204 for r in self.findings: 172 205 s.append( '' ) 173 t = '%s %s %d %d %s' % ('*'*20, r['objtype'], r['start_line'], r['end_line'], '*'*20) 206 t = '%s %s %d %d %s' % ('*'*20, 207 r['objtype'], 208 r['start_line'], 209 r['end_line'], 210 '*'*20) 174 211 s.append( t ) 175 212 s.append( r['text'] ) 176 213 return '\n'.join(s) 177 214 215 def ReST(self): 216 """create the ReStructured Text from what has been found""" 217 s = [] 218 for r in self.findings: 219 if r['objtype'] == 'extended comment': 220 s.append( '' ) 221 s.append( '.. %s %s %d %d' % (self.filename, 222 r['objtype'], 223 r['start_line'], 224 r['end_line']) ) 225 s.append( '' ) 226 s.append(r['text']) 227 # TODO: other objtypes 228 return '\n'.join(s) 229 178 230 179 231 if __name__ == '__main__': 180 print SpecMacrofileParser('test-battery.mac') 181 print SpecMacrofileParser('cdef-examples.mac') 232 p = SpecMacrofileParser('test-battery.mac') 233 #print p.ReST() 234 print p 235 p = SpecMacrofileParser('cdef-examples.mac') 236 #print p.ReST()
Note: See TracChangeset
for help on using the changeset viewer.