Commit f57736e7 authored by Guido van Rossum's avatar Guido van Rossum

Translated to use re instead of regex. Also replaced one use of

L.append(a,b,c,d) with the correct L.append((a,b,c,d)).
parent 1b0ff2c3
...@@ -77,7 +77,7 @@ STEPSIZE = 8 ...@@ -77,7 +77,7 @@ STEPSIZE = 8
TABSIZE = 8 TABSIZE = 8
import os import os
import regex import re
import string import string
import sys import sys
...@@ -100,15 +100,15 @@ class PythonIndenter: ...@@ -100,15 +100,15 @@ class PythonIndenter:
self.tabsize = tabsize self.tabsize = tabsize
self.lineno = 0 self.lineno = 0
self.write = fpo.write self.write = fpo.write
self.kwprog = regex.symcomp( self.kwprog = re.compile(
'^[ \t]*\(<kw>[a-z]+\)' r'^\s*(?P<kw>[a-z]+)'
'\([ \t]+\(<id>[a-zA-Z_][a-zA-Z0-9_]*\)\)?' r'(\s+(?P<id>[a-zA-Z_]\w*))?'
'[^a-zA-Z0-9_]') r'[^\w]')
self.endprog = regex.symcomp( self.endprog = re.compile(
'^[ \t]*#?[ \t]*end[ \t]+\(<kw>[a-z]+\)' r'^\s*#?\s*end\s+(?P<kw>[a-z]+)'
'\([ \t]+\(<id>[a-zA-Z_][a-zA-Z0-9_]*\)\)?' r'(\s+(?P<id>[a-zA-Z_]\w*))?'
'[^a-zA-Z0-9_]') r'[^\w]')
self.wsprog = regex.compile('^[ \t]*') self.wsprog = re.compile(r'^[ \t]*')
# end def __init__ # end def __init__
def readline(self): def readline(self):
...@@ -142,7 +142,10 @@ class PythonIndenter: ...@@ -142,7 +142,10 @@ class PythonIndenter:
return return
# end if # end if
tabs, spaces = divmod(indent*self.indentsize, self.tabsize) tabs, spaces = divmod(indent*self.indentsize, self.tabsize)
i = max(0, self.wsprog.match(line)) i = 0
m = self.wsprog.match(line)
if m: i = m.end()
# end if
self.write('\t'*tabs + ' '*spaces + line[i:]) self.write('\t'*tabs + ' '*spaces + line[i:])
# end def putline # end def putline
...@@ -152,9 +155,10 @@ class PythonIndenter: ...@@ -152,9 +155,10 @@ class PythonIndenter:
line = self.getline() line = self.getline()
if not line: break # EOF if not line: break # EOF
# end if # end if
if self.endprog.match(line) >= 0: m = self.endprog.match(line)
if m:
kw = 'end' kw = 'end'
kw2 = self.endprog.group('kw') kw2 = m.group('kw')
if not stack: if not stack:
self.error('unexpected end') self.error('unexpected end')
elif stack[-1][0] != kw2: elif stack[-1][0] != kw2:
...@@ -164,8 +168,9 @@ class PythonIndenter: ...@@ -164,8 +168,9 @@ class PythonIndenter:
self.putline(line, len(stack)) self.putline(line, len(stack))
continue continue
# end if # end if
if self.kwprog.match(line) >= 0: m = self.kwprog.match(line)
kw = self.kwprog.group('kw') if m:
kw = m.group('kw')
if kw in start: if kw in start:
self.putline(line, len(stack)) self.putline(line, len(stack))
stack.append((kw, kw)) stack.append((kw, kw))
...@@ -195,18 +200,24 @@ class PythonIndenter: ...@@ -195,18 +200,24 @@ class PythonIndenter:
current, firstkw, lastkw, topid = 0, '', '', '' current, firstkw, lastkw, topid = 0, '', '', ''
while 1: while 1:
line = self.getline() line = self.getline()
i = max(0, self.wsprog.match(line)) i = 0
if self.endprog.match(line) >= 0: m = self.wsprog.match(line)
if m: i = m.end()
# end if
m = self.endprog.match(line)
if m:
thiskw = 'end' thiskw = 'end'
endkw = self.endprog.group('kw') endkw = m.group('kw')
thisid = self.endprog.group('id') thisid = m.group('id')
elif self.kwprog.match(line) >= 0: else:
thiskw = self.kwprog.group('kw') m = self.kwprog.match(line)
if m:
thiskw = m.group('kw')
if not next.has_key(thiskw): if not next.has_key(thiskw):
thiskw = '' thiskw = ''
# end if # end if
if thiskw in ('def', 'class'): if thiskw in ('def', 'class'):
thisid = self.kwprog.group('id') thisid = m.group('id')
else: else:
thisid = '' thisid = ''
# end if # end if
...@@ -216,6 +227,7 @@ class PythonIndenter: ...@@ -216,6 +227,7 @@ class PythonIndenter:
else: else:
thiskw = '' thiskw = ''
# end if # end if
# end if
indent = len(string.expandtabs(line[:i], self.tabsize)) indent = len(string.expandtabs(line[:i], self.tabsize))
while indent < current: while indent < current:
if firstkw: if firstkw:
...@@ -249,7 +261,7 @@ class PythonIndenter: ...@@ -249,7 +261,7 @@ class PythonIndenter:
# end if # end if
# end if # end if
if indent > current: if indent > current:
stack.append(current, firstkw, lastkw, topid) stack.append((current, firstkw, lastkw, topid))
if thiskw and thiskw not in start: if thiskw and thiskw not in start:
# error # error
thiskw = '' thiskw = ''
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment