Skip to content

Commit

Permalink
add comment check in file grep. fixed #115
Browse files Browse the repository at this point in the history
  • Loading branch information
LoRexxar committed Dec 1, 2020
1 parent c636cae commit fdb5c19
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Kunlun_M/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
"html": ['.html'],
}

ext_comment_dict = {
"php": ['//', '/*'],
"javascript": ['//', '/*'],
}

default_black_list = ['.crx_files', 'vendor']

# base result class
Expand Down
64 changes: 61 additions & 3 deletions utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,64 @@ def check_filepath(target, filepath):


class FileParseAll:
def __init__(self, filelist, target, language=None):
def __init__(self, filelist, target, language='php'):
self.filelist = filelist
self.t_filelist = file_list_parse(filelist, language)
self.target = target
self.language = language

def check_comment(self, content):
backstr = ""

if self.language in ['php', 'javascript']:

lastchar = ""
isinlinecomment = False
isduolinecomment = False

for char in content:
if char == '/' and lastchar == '/':
backstr = backstr[:-1]
isinlinecomment = True
lastchar = ""
continue

if isinlinecomment:
if char == '\n':
isinlinecomment = False

lastchar = ''
backstr += '\n'
continue

if char == '\n':
backstr += '\n'
continue

# 多行注释
if char == '*' and lastchar == '/':
isduolinecomment = True
backstr = backstr[:-1]
lastchar = ""
continue

if isduolinecomment:

if char == '/' and lastchar == '*':
isduolinecomment = False
lastchar = ""
continue

lastchar = char
continue

lastchar = char
backstr += char

return backstr

else:
return content

def grep(self, reg):
"""
Expand All @@ -143,9 +197,11 @@ def grep(self, reg):
line_number += 1
content += line

if i < 5:
if i < 10:
continue

content = self.check_comment(content)

i = 0
# print line, line_number
if re.search(reg, content, re.I):
Expand All @@ -164,12 +220,14 @@ def grep(self, reg):

LRnumber = " ".join(split_data).count('\n')

match_numer = line_number - 5 + LRnumber
match_numer = line_number - 10 + LRnumber

result.append((filepath, str(match_numer), data))

content = ""

content = self.check_comment(content)

# 如果退出循环的时候没有清零,则还要检查一次
if i > 0:
if re.search(reg, content, re.I):
Expand Down

0 comments on commit fdb5c19

Please sign in to comment.