Path: blob/master/elisp/emacs-for-python/rope-dist/rope/base/simplify.py
1419 views
"""A module to ease code analysis12This module is here to help source code analysis.3"""4import re56from rope.base import codeanalyze, utils789@utils.cached(7)10def real_code(source):11"""Simplify `source` for analysis1213It replaces:1415* comments with spaces16* strs with a new str filled with spaces17* implicit and explicit continuations with spaces18* tabs and semicolons with spaces1920The resulting code is a lot easier to analyze if we are interested21only in offsets.22"""23collector = codeanalyze.ChangeCollector(source)24for start, end in ignored_regions(source):25if source[start] == '#':26replacement = ' ' * (end - start)27else:28replacement = '"%s"' % (' ' * (end - start - 2))29collector.add_change(start, end, replacement)30source = collector.get_changed() or source31collector = codeanalyze.ChangeCollector(source)32parens = 033for match in _parens.finditer(source):34i = match.start()35c = match.group()36if c in '({[':37parens += 138if c in ')}]':39parens -= 140if c == '\n' and parens > 0:41collector.add_change(i, i + 1, ' ')42source = collector.get_changed() or source43return source.replace('\\\n', ' ').replace('\t', ' ').replace(';', '\n')444546@utils.cached(7)47def ignored_regions(source):48"""Return ignored regions like strings and comments in `source` """49return [(match.start(), match.end()) for match in _str.finditer(source)]505152_str = re.compile('%s|%s' % (codeanalyze.get_comment_pattern(),53codeanalyze.get_string_pattern()))54_parens = re.compile(r'[\({\[\]}\)\n]')555657