from __future__ import absolute_import from __future__ import print_function from typing import Callable, Optional from six.moves import range import re class TemplateParserException(Exception): # TODO: Have callers pass in line numbers. pass class TokenizerState(object): def __init__(self): # type: () -> None self.i = 0 self.line = 1 self.col = 1 class Token(object): def __init__(self, kind, s, tag, line, col): # type: (str, str, str, int, int) -> None self.kind = kind self.s = s self.tag = tag self.line = line self.col = col def tokenize(text): # type: (str) -> List[Token] def advance(n): # type: (int) -> None for _ in range(n): state.i += 1 if state.i >= 0 and text[state.i - 1] == '\n': state.line += 1 state.col = 1 else: state.col += 1 def looking_at(s): # type: (str) -> bool return text[state.i:state.i+len(s)] == s def looking_at_comment(): # type: () -> bool return looking_at("': return text[i:end] end += 1 raise TemplateParserException('Unclosed comment')