Tokenizing version (see lib/tokenize.py).

This commit is contained in:
Guido van Rossum 1992-01-01 19:22:25 +00:00
parent 0a60ee1c69
commit 318a91c3f2

View file

@ -1,11 +1,12 @@
#! /ufs/guido/bin/sgi/python #! /ufs/guido/bin/sgi/python
#! /usr/local/python #! /usr/local/python
# Fix Python source files to use the new equality test operator, # Fix Python source files to use the new equality test operator, i.e.,
# i.e.,
# if x == y: ...
# instead of
# if x = y: ... # if x = y: ...
# is changed to
# if x == y: ...
# The script correctly tokenizes the Python program to reliably
# distinguish between assignments and equality tests.
# #
# Command line arguments are files or directories to be processed. # Command line arguments are files or directories to be processed.
# Directories are searched recursively for files whose name looks # Directories are searched recursively for files whose name looks
@ -77,7 +78,7 @@ def recursedown(dirname):
return bad return bad
def fix(filename): def fix(filename):
dbg('fix(' + `filename` + ')\n') ## dbg('fix(' + `filename` + ')\n')
try: try:
f = open(filename, 'r') f = open(filename, 'r')
except IOError, msg: except IOError, msg:
@ -144,25 +145,39 @@ def fix(filename):
# Return succes # Return succes
return 0 return 0
PAT1 = '\<\(if\|elif\|while\)\>[\0-\377]*[^<>!=]\(=\)[^=][\0-\377]*[^[]:[^]]'
# \2 \3 from tokenize import tokenprog
PAT2 = '\<return\>[\0-\377]*[^<>!=]\(=\)[^=]'
# \4 match = {'if':':', 'elif':':', 'while':':', 'return':'\n', \
PAT = '^[ \t]*\(' + PAT1 + '\|' + PAT2 + '\)' '(':')', '[':']', '{':'}', '`':'`'}
# \1
prog = regex.compile(PAT)
def fixline(line): def fixline(line):
while prog.match(line) >= 0: # Quick check for easy case
regs = prog.regs if '=' not in line: return line
if regs[3] == (-1, -1):
a, b = regs[4] i, n = 0, len(line)
else: stack = []
a, b = regs[3] while i < n:
if not 0 < a < b < len(line): j = tokenprog.match(line, i)
dbg('Weird: ' + line) if j < 0:
break # A bad token; forget about the rest of this line
print '(Syntax error:)'
print line,
return line
a, b = tokenprog.regs[3] # Location of the token proper
token = line[a:b]
i = i+j
if stack and token == stack[-1]:
del stack[-1]
elif match.has_key(token):
stack.append(match[token])
elif token == '=' and stack:
line = line[:a] + '==' + line[b:] line = line[:a] + '==' + line[b:]
i, n = a + len('=='), len(line)
elif token == '==' and not stack:
print '(Warning: \'==\' at top level:)'
print line,
return line return line
main() main()