File: SSFilter.py

package info (click to toggle)
synopsis 0.8.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,112 kB
  • ctags: 12,996
  • sloc: cpp: 34,254; ansic: 33,620; python: 10,975; sh: 7,261; xml: 6,369; makefile: 773; asm: 445
file content (47 lines) | stat: -rw-r--r-- 1,290 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#
# Copyright (C) 2005 Stefan Seefeld
# All rights reserved.
# Licensed to the public under the terms of the GNU LGPL (>= 2),
# see the file COPYING for details.
#

from Synopsis import AST
from Filter import Filter

import re

class SSFilter(Filter):
    """A class that selects only // comments."""

    __re_star = r'/\*(.*?)\*/'
    __re_ss = r'^[ \t]*// ?(.*)$'

    def __init__(self):
        "Compiles the regular expressions"

        self.re_star = re.compile(SSFilter.__re_star, re.S)
        self.re_ss = re.compile(SSFilter.__re_ss, re.M)

    def filter_comment(self, comment):
        """Replaces the text in the comment. It calls strip_star() first to
        remove all multi-line star comments, then follows with filter_ss().
        """

        text = comment.text()
        text = self.filter_ss(self.strip_star(text))
        comment.set_text(text)

    def strip_star(self, str):
        """Strips all star-format comments from the string"""

        mo = self.re_star.search(str)
        while mo:
            str = str[:mo.start()] + str[mo.end():]
            mo = self.re_star.search(str)
        return str

    def filter_ss(self, str):
        """Filters str and returns just the lines that start with //"""

        return '\n'.join(self.re_ss.findall(str))