-
Notifications
You must be signed in to change notification settings - Fork 2
/
scanner.py
75 lines (55 loc) · 1.93 KB
/
scanner.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import re
class NoIdentifierFoundError(Exception):
pass
_BASE_REGEX_STRING = '^\s*goog\.%s\(\s*[\'"](.+)[\'"]\s*\)'
_PROVIDE_REGEX = re.compile(_BASE_REGEX_STRING % 'provide')
_REQUIRES_REGEX = re.compile(_BASE_REGEX_STRING % 'require')
def YieldProvides(source):
for line in source.splitlines():
match = _PROVIDE_REGEX.match(line)
if match:
yield match.group(1)
def YieldRequires(source):
for line in source.splitlines():
match = _REQUIRES_REGEX.match(line)
if match:
yield match.group(1)
def ExtractDocumentedSymbols(script):
for comment_match in FindJsDocComments(script):
identifier_match = None
if re.match('\b@fileoverview\b', comment_match.group()):
# This is a file overview comment.
pass
else:
identifier_match = FindCommentTarget(script, comment_match.end())
if not identifier_match:
raise NoIdentiferFoundError(
'Found no identifier for comment: ' + identifier_match.group())
yield comment_match, identifier_match
def FindJsDocComments(script):
return re.finditer('/\*\*.*?\*/', script, re.DOTALL)
def FindCommentTarget(script, pos=0):
# Find an opening parethesis or an identifier.
# \w and $ should cover all valid identifiers.
identifier_regex = re.compile('\(|(?:[$\w]+\s*\.\s*)*[$\w]+')
return identifier_regex.search(script, pos=pos)
def StripWhitespace(original_string):
return re.sub('\s*', '', original_string)
def ExtractTextFromJsDocComment(comment):
comment = comment.strip()
# Strip the leading "/**"
assert comment.startswith('/**')
comment = comment[3:]
assert comment.endswith('*/')
comment = comment[:-2]
comment = comment.strip()
lines = comment.splitlines(True)
output_lines = []
for line in lines:
line = line.lstrip()
if line.startswith('*'):
line = line[1:]
while line.startswith(' '):
line = line[1:]
output_lines.append(line)
return ''.join(output_lines)