def fixline(line): # Quick check for easy case if '=' not in line: return line i, n = 0, len(line) stack = [] while i < n: j = tokenprog.match(line, i) if j < 0: # 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:] i, n = a + len('=='), len(line) elif token == '==' and not stack: print '(Warning: \'==\' at top level:)' print line, return line
def lex(text, textpos, filepath): offset = pos = 0 end = len(text) escaped = False while 1: if escaped: offset = text.find(PREFIX, offset + 2) escaped = False else: offset = text.find(PREFIX, pos) if offset < 0 or offset == end - 1: break next = text[offset + 1] if next == '{': if offset > pos: yield False, text[pos:offset] pos = offset + 2 level = 1 while level: match = tokenprog.match(text, pos) if match is None: raise TemplateSyntaxError('invalid syntax', filepath, *textpos[1:]) pos = match.end() tstart, tend = match.regs[3] token = text[tstart:tend] if token == '{': level += 1 elif token == '}': level -= 1 yield True, text[offset + 2:pos - 1] elif next in NAMESTART: if offset > pos: yield False, text[pos:offset] pos = offset pos += 1 while pos < end: char = text[pos] if char not in NAMECHARS: break pos += 1 yield True, text[offset + 1:pos].strip() elif not escaped and next == PREFIX: if offset > pos: yield False, text[pos:offset] pos = offset escaped = True pos = offset + 1 else: yield False, text[pos:offset + 1] pos = offset + 1 if pos < end: yield False, text[pos:]
def lex(text, textpos, filepath): offset = pos = 0 end = len(text) escaped = False while 1: if escaped: offset = text.find(PREFIX, offset + 2) escaped = False else: offset = text.find(PREFIX, pos) if offset < 0 or offset == end - 1: break next = text[offset + 1] if next == '{': if offset > pos: yield False, text[pos:offset] pos = offset + 2 level = 1 while level: match = tokenprog.match(text, pos) if match is None: raise TemplateSyntaxError('invalid syntax', filepath, *textpos[1:]) pos = match.end() tstart, tend = match.regs[3] token = text[tstart:tend] if token == '{': level += 1 elif token == '}': level -= 1 yield True, text[offset + 2:pos - 1] elif next in NAMESTART: if offset > pos: yield False, text[pos:offset] pos = offset pos += 1 while pos < end: char = text[pos] if char not in NAMECHARS: break pos += 1 yield True, text[offset + 1:pos].strip() elif not escaped and next == PREFIX: escaped = True pos = offset + 1 else: yield False, text[pos:offset + 1] pos = offset + 1 if pos < end: yield False, text[pos:]
def _matchorfail(text, pos): match = tokenprog.match(text, pos) if match is None: raise ValueError(text, pos) return match, match.end()
def match(self): match = tokenprog.match(self.text, self.pos) if match is None: raise _ItplError(self.text, self.pos) return match, match.end()
def match_or_fail(text, pos): """match_or_fail(text, pos)""" match = tokenprog.match(text, pos) if match is None: raise ItplError(text, pos) return match, match.end()
#! /usr/bin/env python
def matchorfail(text, pos): match = tokenprog.match(text, pos) # @UndefinedVariable if match is None: raise _ItplError(text, pos) return match, match.end()