def _get_pattern(cls): if cls._match_pattern is None: pattern = codeanalyze.get_comment_pattern() + '|' + \ codeanalyze.get_string_pattern() + '|' + \ r'(?P<name>\$\{[^\s\$\}]*\})' cls._match_pattern = re.compile(pattern) return cls._match_pattern
def __init__(self, name, docs=False): self.name = name self.docs = docs self.comment_pattern = _TextualFinder.any('comment', [r'#[^\n]*']) self.string_pattern = _TextualFinder.any( 'string', [codeanalyze.get_string_pattern()]) self.pattern = self._get_occurrence_pattern(self.name)
def _get_pattern(cls): if cls._match_pattern is None: pattern = (codeanalyze.get_comment_pattern() + "|" + codeanalyze.get_string_pattern() + "|" + r"(?P<name>\$\{[^\s\$\}]*\})") cls._match_pattern = re.compile(pattern) return cls._match_pattern
def consume_string(self, end=None): if _Source._string_pattern is None: original = codeanalyze.get_string_pattern() pattern = r'(%s)((\s|\\\n|#[^\n]*\n)*(%s))*' % \ (original, original) _Source._string_pattern = re.compile(pattern) repattern = _Source._string_pattern return self._consume_pattern(repattern, end)
def __init__(self, name, docs=False): self.name = name self.docs = docs self.comment_pattern = _TextualFinder.any("comment", [r"#[^\n]*"]) self.string_pattern = _TextualFinder.any( "string", [codeanalyze.get_string_pattern()]) self.f_string_pattern = _TextualFinder.any( "fstring", [codeanalyze.get_formatted_string_pattern()]) self.pattern = self._get_occurrence_pattern(self.name)
def consume_string(self, end=None): if _Source._string_pattern is None: string_pattern = codeanalyze.get_string_pattern() formatted_string_pattern = codeanalyze.get_formatted_string_pattern() original = r"(?:%s)|(?:%s)" % (string_pattern, formatted_string_pattern) pattern = r"(%s)((\s|\\\n|#[^\n]*\n)*(%s))*" % (original, original) _Source._string_pattern = re.compile(pattern) repattern = _Source._string_pattern return self._consume_pattern(repattern, end)
def _get_pattern(cls): if cls._match_pattern is None: pattern = ( codeanalyze.get_comment_pattern() + "|" + codeanalyze.get_string_pattern() + "|" + r"(?P<name>\$\{[^\s\$\}]*\})" ) cls._match_pattern = re.compile(pattern) return cls._match_pattern
def _get_return_pattern(cls): if not hasattr(cls, "_return_pattern"): def named_pattern(name, list_): return "(?P<%s>" % name + "|".join(list_) + ")" comment_pattern = named_pattern("comment", [r"#[^\n]*"]) string_pattern = named_pattern("string", [codeanalyze.get_string_pattern()]) return_pattern = r"\b(?P<return>return)\b" cls._return_pattern = re.compile(comment_pattern + "|" + string_pattern + "|" + return_pattern) return cls._return_pattern
def _get_return_pattern(cls): if not hasattr(cls, '_return_pattern'): def named_pattern(name, list_): return "(?P<%s>" % name + "|".join(list_) + ")" comment_pattern = named_pattern('comment', [r'#[^\n]*']) string_pattern = named_pattern('string', [codeanalyze.get_string_pattern()]) return_pattern = r'\b(?P<return>return)\b' cls._return_pattern = re.compile(comment_pattern + "|" + string_pattern + "|" + return_pattern) return cls._return_pattern
if source[start] == '#': replacement = ' ' * (end - start) else: replacement = '"%s"' % (' ' * (end - start - 2)) collector.add_change(start, end, replacement) source = collector.get_changed() or source collector = codeanalyze.ChangeCollector(source) parens = 0 for match in _parens.finditer(source): i = match.start() c = match.group() if c in '({[': parens += 1 if c in ')}]': parens -= 1 if c == '\n' and parens > 0: collector.add_change(i, i + 1, ' ') source = collector.get_changed() or source return source.replace('\\\n', ' ').replace('\t', ' ').replace(';', '\n') @utils.cached(7) def ignored_regions(source): """Return ignored regions like strings and comments in `source` """ return [(match.start(), match.end()) for match in _str.finditer(source)] _str = re.compile('%s|%s' % (codeanalyze.get_comment_pattern(), codeanalyze.get_string_pattern())) _parens = re.compile(r'[\({\[\]}\)\n]')
replacement = ' ' * (end - start) else: replacement = '"%s"' % (' ' * (end - start - 2)) collector.add_change(start, end, replacement) source = collector.get_changed() or source collector = codeanalyze.ChangeCollector(source) parens = 0 for match in _parens.finditer(source): i = match.start() c = match.group() if c in '({[': parens += 1 if c in ')}]': parens -= 1 if c == '\n' and parens > 0: collector.add_change(i, i + 1, ' ') source = collector.get_changed() or source return source.replace('\\\n', ' ').replace('\t', ' ').replace(';', '\n') @utils.cached(7) def ignored_regions(source): """Return ignored regions like strings and comments in `source` """ return [(match.start(), match.end()) for match in _str.finditer(source)] _str = re.compile( '%s|%s' % (codeanalyze.get_comment_pattern(), codeanalyze.get_string_pattern())) _parens = re.compile(r'[\({\[\]}\)\n]')