forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_tokens.py
More file actions
38 lines (34 loc) · 1.05 KB
/
gen_tokens.py
File metadata and controls
38 lines (34 loc) · 1.05 KB
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
import sys
import tokenize
import token
def printtoken(type, token, start, end, _):
# Use Python 3 tokenize style output, regardless of version
if tokenize.tok_name[type] not in ("ENCODING", "NL"):
token_range = "%d,%d-%d,%d:" % (start + end)
print("%-20s%-15s%r" %
(token_range, tokenize.tok_name[type], token)
)
OP_TYPES = {
"(" : token.LPAR,
")" : token.RPAR,
"[" : token.LSQB,
"]" : token.RSQB,
"{" : token.LBRACE,
"}" : token.RBRACE,
":" : token.COLON,
"," : token.COMMA,
"." : token.DOT,
"@" : token.AT,
}
def main():
readline = open(sys.argv[1], "rb").readline
if sys.version < "3":
tokenize.tokenize(readline, printtoken)
else:
for type, token, start, end, _ in tokenize.tokenize(readline):
if tokenize.tok_name[type] == "OP":
type = OP_TYPES.get(token, type)
if tokenize.tok_name[type] not in ("ENCODING", "NL"):
printtoken(type, token, start, end, _)
if __name__ == "__main__":
main()