Changeset 957


Ignore:
Timestamp:
Jun 21, 2012 1:08:07 AM (11 years ago)
Author:
jemian
Message:

refs #8, now parses global, local, and constant declarations, need to parse macro definitions next

File:
1 edited

Legend:

Unmodified
Added
Removed
  • specdomain/src/specdomain/test/parser.py

    r956 r957  
    1919
    2020
     21#   http://www.txt2re.com/index-python.php3
     22#  http://regexpal.com/
     23
    2124string_start                = r'^'
    2225string_end                  = r'$'
    2326match_all                   = r'.*'
    2427non_greedy_filler           = match_all + r'?'
    25 non_greedy_whitespace       = r'\w*?'
     28non_greedy_whitespace       = r'\s*?'
    2629double_quote_string_match   = r'("' + non_greedy_filler + r'")'
     30prog_name_match             = r'([a-z_]\w*)'
    2731word_match                  = r'((?:[a-z_]\w*))'
    2832cdef_match                  = r'(cdef)'
     
    3034extended_comment_match      = r'(' + extended_comment_marker + r')'
    3135
    32 spec_macro_sig_re = re.compile(
     36macro_sig_re = re.compile(
    3337                               r'''^ ([a-zA-Z_]\w*)         # macro name
    3438                               ''', re.VERBOSE)
    3539
    36 spec_func_sig_re = re.compile(word_match + r'\('
     40func_sig_re = re.compile(word_match + r'\('
    3741                      + r'(' + match_all + r')'
    3842                      + r'\)',
    3943                      re.IGNORECASE|re.DOTALL)
    4044
    41 spec_cdef_name_sig_re = re.compile(double_quote_string_match,
     45cdef_name_sig_re = re.compile(double_quote_string_match,
    4246                                   re.IGNORECASE|re.DOTALL)
    4347
    4448
    45 spec_extended_comment_flag_sig_re = re.compile(extended_comment_marker,
     49extended_comment_flag_sig_re = re.compile(extended_comment_marker,
    4650                                               re.IGNORECASE|re.DOTALL)
    47 spec_extended_comment_start_sig_re = re.compile(string_start
     51extended_comment_start_sig_re = re.compile(string_start
    4852                                                + non_greedy_whitespace
    4953                                                + extended_comment_match,
    5054                                                re.IGNORECASE|re.VERBOSE)
    51 spec_extended_comment_end_sig_re = re.compile(non_greedy_whitespace
     55extended_comment_end_sig_re = re.compile(non_greedy_whitespace
    5256                                                + extended_comment_match
    5357                                                + non_greedy_whitespace
    5458                                                + r'#' + non_greedy_filler
    55                                                 + r'$',
     59                                                + string_end,
    5660                                                re.IGNORECASE|re.VERBOSE)
    57 spec_extended_comment_block_sig_re = re.compile(string_start
     61extended_comment_block_sig_re = re.compile(string_start
    5862                                                + non_greedy_whitespace
    5963                                                + extended_comment_marker
     
    6367                                                + string_end,
    6468                                                re.IGNORECASE|re.DOTALL|re.MULTILINE)
     69lgc_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)
    6577
    6678
     
    114126            line_number += 1
    115127            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
    116151                # 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)
    118153                if m is not None:
    119154                    del m['start'], m['end'], m['line']
    120155                    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))
    124158                    continue
    125159               
    126160                # 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)
    128162                if m is not None:
    129163                    text = m['line'][m['end']:]
     
    139173            elif state == 'extended comment':
    140174                # 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)
    144176                if m is not None:
    145177                    text = m['line'][:m['start']]
     
    147179                    ec['text'] = '\n'.join(ec['text'])
    148180                    ec['end_line'] = line_number
    149                     self.findings.append(ec)
     181                    self.findings.append(dict(ec))
    150182                    state = state_stack.pop()
    151183                    del ec
     
    164196            'text':  m.group(1),
    165197            'line':  line,
     198            'filename':  self.filename,
    166199        }
    167200        return d
     
    171204        for r in self.findings:
    172205            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)
    174211            s.append( t )
    175212            s.append( r['text'] )
    176213        return '\n'.join(s)
    177214
     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
    178230
    179231if __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.