Fixed all remaining TODOs

This commit is contained in:
Holger Rapp 2011-07-24 19:16:49 +02:00
parent 7f12de5cd9
commit f7c12f9d55
2 changed files with 12 additions and 33 deletions

View File

@ -51,10 +51,6 @@ class _TextIterator(object):
@property @property
def pos(self): def pos(self):
return Position(self._line, self._col) return Position(self._line, self._col)
@property
def exhausted(self): # Only used in one place. Really neede? TODO
return self._idx >= len(self._text)
# End: Helper Classes }}} # End: Helper Classes }}}
# Helper functions {{{ # Helper functions {{{
def _parse_number(stream): def _parse_number(stream):
@ -63,7 +59,7 @@ def _parse_number(stream):
without consuming any more bytes without consuming any more bytes
""" """
rv = "" rv = ""
while stream.peek() in string.digits: while stream.peek() and stream.peek() in string.digits:
rv += stream.next() rv += stream.next()
return int(rv) return int(rv)
@ -78,7 +74,7 @@ def _parse_till_closing_brace(stream):
rv = "" rv = ""
in_braces = 1 in_braces = 1
while True: while True:
if EscapeCharToken.check(stream, '{}'): if EscapeCharToken.starts_here(stream, '{}'):
rv += stream.next() + stream.next() rv += stream.next() + stream.next()
else: else:
c = stream.next() c = stream.next()
@ -96,7 +92,7 @@ def _parse_till_unescaped_char(stream, char):
""" """
rv = "" rv = ""
while True: while True:
if EscapeCharToken.check(stream, char): if EscapeCharToken.starts_here(stream, char):
rv += stream.next() + stream.next() rv += stream.next() + stream.next()
else: else:
c = stream.next() c = stream.next()
@ -117,8 +113,7 @@ class TabStopToken(Token):
CHECK = re.compile(r'^\${\d+[:}]') CHECK = re.compile(r'^\${\d+[:}]')
@classmethod @classmethod
def check(klass, stream): def starts_here(klass, stream):
# TODO: bad name for function
return klass.CHECK.match(stream.peek(10)) != None return klass.CHECK.match(stream.peek(10)) != None
def _parse(self, stream, indent): def _parse(self, stream, indent):
@ -140,8 +135,7 @@ class TransformationToken(Token):
CHECK = re.compile(r'^\${\d+\/') CHECK = re.compile(r'^\${\d+\/')
@classmethod @classmethod
def check(klass, stream): def starts_here(klass, stream):
# TODO: bad name for function
return klass.CHECK.match(stream.peek(10)) != None return klass.CHECK.match(stream.peek(10)) != None
def _parse(self, stream, indent): def _parse(self, stream, indent):
@ -165,17 +159,12 @@ class MirrorToken(Token):
CHECK = re.compile(r'^\$\d+') CHECK = re.compile(r'^\$\d+')
@classmethod @classmethod
def check(klass, stream): def starts_here(klass, stream):
# TODO: bad name for function
return klass.CHECK.match(stream.peek(10)) != None return klass.CHECK.match(stream.peek(10)) != None
def _parse(self, stream, indent): def _parse(self, stream, indent):
# TODO: why not parse number?
self.no = ""
stream.next() # $ stream.next() # $
while not stream.exhausted and stream.peek() in string.digits: self.no = _parse_number(stream)
self.no += stream.next()
self.no = int(self.no)
def __repr__(self): def __repr__(self):
return "MirrorToken(%r,%r,%r)" % ( return "MirrorToken(%r,%r,%r)" % (
@ -184,7 +173,7 @@ class MirrorToken(Token):
class EscapeCharToken(Token): class EscapeCharToken(Token):
@classmethod @classmethod
def check(klass, stream, chars = '{}\$`'): def starts_here(klass, stream, chars = '{}\$`'):
cs = stream.peek(2) cs = stream.peek(2)
if len(cs) == 2 and cs[0] == '\\' and cs[1] in chars: if len(cs) == 2 and cs[0] == '\\' and cs[1] in chars:
return True return True
@ -200,25 +189,23 @@ class EscapeCharToken(Token):
class ShellCodeToken(Token): class ShellCodeToken(Token):
@classmethod @classmethod
def check(klass, stream): def starts_here(klass, stream):
return stream.peek(1) == '`' return stream.peek(1) == '`'
def _parse(self, stream, indent): def _parse(self, stream, indent):
stream.next() # ` stream.next() # `
self.code = _parse_till_unescaped_char(stream, '`') self.code = _parse_till_unescaped_char(stream, '`')
# TODO: get rid of those __repr__ maybe
def __repr__(self): def __repr__(self):
return "ShellCodeToken(%r,%r,%r)" % ( return "ShellCodeToken(%r,%r,%r)" % (
self.start, self.end, self.code self.start, self.end, self.code
) )
# TODO: identical to VimLCodeToken
class PythonCodeToken(Token): class PythonCodeToken(Token):
CHECK = re.compile(r'^`!p\s') CHECK = re.compile(r'^`!p\s')
@classmethod @classmethod
def check(klass, stream): def starts_here(klass, stream):
return klass.CHECK.match(stream.peek(4)) is not None return klass.CHECK.match(stream.peek(4)) is not None
def _parse(self, stream, indent): def _parse(self, stream, indent):
@ -229,9 +216,6 @@ class PythonCodeToken(Token):
code = _parse_till_unescaped_char(stream, '`') code = _parse_till_unescaped_char(stream, '`')
# TODO: stupid to pass the indent down even if only python
# needs it. Stupid to indent beforehand.
# Strip the indent if any # Strip the indent if any
if len(indent): if len(indent):
lines = code.splitlines() lines = code.splitlines()
@ -242,18 +226,16 @@ class PythonCodeToken(Token):
self.code = code self.code = code
self.indent = indent self.indent = indent
# TODO: get rid of those __repr__ maybe
def __repr__(self): def __repr__(self):
return "PythonCodeToken(%r,%r,%r)" % ( return "PythonCodeToken(%r,%r,%r)" % (
self.start, self.end, self.code self.start, self.end, self.code
) )
class VimLCodeToken(Token): class VimLCodeToken(Token):
CHECK = re.compile(r'^`!v\s') CHECK = re.compile(r'^`!v\s')
@classmethod @classmethod
def check(klass, stream): def starts_here(klass, stream):
return klass.CHECK.match(stream.peek(4)) is not None return klass.CHECK.match(stream.peek(4)) is not None
def _parse(self, stream, indent): def _parse(self, stream, indent):
@ -271,14 +253,13 @@ __ALLOWED_TOKENS = [
EscapeCharToken, TransformationToken, TabStopToken, MirrorToken, EscapeCharToken, TransformationToken, TabStopToken, MirrorToken,
PythonCodeToken, VimLCodeToken, ShellCodeToken PythonCodeToken, VimLCodeToken, ShellCodeToken
] ]
def tokenize(text, indent): def tokenize(text, indent):
stream = _TextIterator(text) stream = _TextIterator(text)
while True: while True:
done_something = False done_something = False
for t in __ALLOWED_TOKENS: for t in __ALLOWED_TOKENS:
if t.check(stream): if t.starts_here(stream):
yield t(stream, indent) yield t(stream, indent)
done_something = True done_something = True
break break

View File

@ -110,7 +110,6 @@ class _CleverReplace(object):
return self._unescape(tv.decode("string-escape")) return self._unescape(tv.decode("string-escape"))
class _TOParser(object): class _TOParser(object):
def __init__(self, parent_to, text, indent): def __init__(self, parent_to, text, indent):
self._indent = indent self._indent = indent
@ -145,7 +144,6 @@ class _TOParser(object):
if token.no not in seen_ts: if token.no not in seen_ts:
raise RuntimeError("Tabstop %i is not known but is used by a Transformation" % t._ts) raise RuntimeError("Tabstop %i is not known but is used by a Transformation" % t._ts)
Transformation(parent, seen_ts[token.no], token) Transformation(parent, seen_ts[token.no], token)
# TODO: check if all associations have been done properly. Also add a testcase for this!
def _do_parse(self, all_tokens, seen_ts): def _do_parse(self, all_tokens, seen_ts):
tokens = list(tokenize(self._text, self._indent)) tokens = list(tokenize(self._text, self._indent))