Holds 4 copies of all lines with different preprocessing applied to them. 1) elided member contains lines without strings and comments. 2) lines member contains lines without comments. 3) raw_lines member contains all the lines without processing. 4) lines_without_raw_strings member is same
| 1899 | |
| 1900 | |
| 1901 | class CleansedLines(object): |
| 1902 | """Holds 4 copies of all lines with different preprocessing applied to them. |
| 1903 | |
| 1904 | 1) elided member contains lines without strings and comments. |
| 1905 | 2) lines member contains lines without comments. |
| 1906 | 3) raw_lines member contains all the lines without processing. |
| 1907 | 4) lines_without_raw_strings member is same as raw_lines, but with C++11 raw |
| 1908 | strings removed. |
| 1909 | All these members are of <type 'list'>, and of the same length. |
| 1910 | """ |
| 1911 | |
| 1912 | def __init__(self, lines): |
| 1913 | self.elided = [] |
| 1914 | self.lines = [] |
| 1915 | self.raw_lines = lines |
| 1916 | self.num_lines = len(lines) |
| 1917 | self.lines_without_raw_strings = CleanseRawStrings(lines) |
| 1918 | for linenum in range(len(self.lines_without_raw_strings)): |
| 1919 | self.lines.append(CleanseComments( |
| 1920 | self.lines_without_raw_strings[linenum])) |
| 1921 | elided = self._CollapseStrings(self.lines_without_raw_strings[linenum]) |
| 1922 | self.elided.append(CleanseComments(elided)) |
| 1923 | |
| 1924 | def NumLines(self): |
| 1925 | """Returns the number of lines represented.""" |
| 1926 | return self.num_lines |
| 1927 | |
| 1928 | @staticmethod |
| 1929 | def _CollapseStrings(elided): |
| 1930 | """Collapses strings and chars on a line to simple "" or '' blocks. |
| 1931 | |
| 1932 | We nix strings first so we're not fooled by text like '"http://"' |
| 1933 | |
| 1934 | Args: |
| 1935 | elided: The line being processed. |
| 1936 | |
| 1937 | Returns: |
| 1938 | The line with collapsed strings. |
| 1939 | """ |
| 1940 | if _RE_PATTERN_INCLUDE.match(elided): |
| 1941 | return elided |
| 1942 | |
| 1943 | # Remove escaped characters first to make quote/single quote collapsing |
| 1944 | # basic. Things that look like escaped characters shouldn't occur |
| 1945 | # outside of strings and chars. |
| 1946 | elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided) |
| 1947 | |
| 1948 | # Replace quoted strings and digit separators. Both single quotes |
| 1949 | # and double quotes are processed in the same loop, otherwise |
| 1950 | # nested quotes wouldn't work. |
| 1951 | collapsed = '' |
| 1952 | while True: |
| 1953 | # Find the first quote character |
| 1954 | match = Match(r'^([^\'"]*)([\'"])(.*)$', elided) |
| 1955 | if not match: |
| 1956 | collapsed += elided |
| 1957 | break |
| 1958 | head, quote, tail = match.groups() |