Skip to content
Myr

Improved usage of string interning. This should fix the problems with jython.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 330fbc0 · Browse files · 1 file+97 added−49 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+97 added−49 removed

  • other:+97 added−49 removed

1 file+97 added−49 removed

modified:jinja2/lexer.py+97 added-49 removed
32 unmodified lines
33name_re = re.compile(r'\b[a-zA-Z_][a-zA-Z0-9_]*\b')34float_re = re.compile(r'(?<!\.)\d+\.\d+')35newline_re = re.compile(r'(\r\n|\r|\n)')3637# internal the tokens and keep references to them38TOKEN_ADD = intern('add')39TOKEN_ASSIGN = intern('assign')40TOKEN_COLON = intern('colon')41TOKEN_COMMA = intern('comma')42TOKEN_DIV = intern('div')43TOKEN_DOT = intern('dot')44TOKEN_EQ = intern('eq')45TOKEN_FLOORDIV = intern('floordiv')46TOKEN_GT = intern('gt')47TOKEN_GTEQ = intern('gteq')48TOKEN_LBRACE = intern('lbrace')49TOKEN_LBRACKET = intern('lbracket')50TOKEN_LPAREN = intern('lparen')51TOKEN_LT = intern('lt')52TOKEN_LTEQ = intern('lteq')53TOKEN_MOD = intern('mod')54TOKEN_MUL = intern('mul')55TOKEN_NE = intern('ne')56TOKEN_PIPE = intern('pipe')57TOKEN_POW = intern('pow')58TOKEN_RBRACE = intern('rbrace')59TOKEN_RBRACKET = intern('rbracket')60TOKEN_RPAREN = intern('rparen')61TOKEN_SEMICOLON = intern('semicolon')62TOKEN_SUB = intern('sub')63TOKEN_TILDE = intern('tilde')64TOKEN_WHITESPACE = intern('whitespace')65TOKEN_FLOAT = intern('float')66TOKEN_INTEGER = intern('integer')67TOKEN_NAME = intern('name')68TOKEN_STRING = intern('string')69TOKEN_OPERATOR = intern('operator')70TOKEN_BLOCK_BEGIN = intern('block_begin')71TOKEN_BLOCK_END = intern('block_end')72TOKEN_VARIABLE_BEGIN = intern('variable_begin')73TOKEN_VARIABLE_END = intern('variable_end')74TOKEN_RAW_BEGIN = intern('raw_begin')75TOKEN_RAW_END = intern('raw_end')76TOKEN_COMMENT_BEGIN = intern('comment_begin')77TOKEN_COMMENT_END = intern('comment_end')78TOKEN_COMMENT = intern('comment')79TOKEN_LINESTATEMENT_BEGIN = intern('linestatement_begin')80TOKEN_LINESTATEMENT_END = intern('linestatement_end')81TOKEN_DATA = intern('data')82TOKEN_INITIAL = intern('initial')83TOKEN_EOF = intern('eof')8485# bind operators to token types86operators = {39 '+': 'add',40 '-': 'sub',41 '/': 'div',42 '//': 'floordiv',43 '*': 'mul',44 '%': 'mod',45 '**': 'pow',46 '~': 'tilde',47 '[': 'lbracket',48 ']': 'rbracket',49 '(': 'lparen',50 ')': 'rparen',51 '{': 'lbrace',52 '}': 'rbrace',53 '==': 'eq',54 '!=': 'ne',55 '>': 'gt',56 '>=': 'gteq',57 '<': 'lt',58 '<=': 'lteq',59 '=': 'assign',60 '.': 'dot',61 ':': 'colon',62 '|': 'pipe',63 ',': 'comma',64 ';': 'semicolon'87 '+': TOKEN_ADD,88 '-': TOKEN_SUB,89 '/': TOKEN_DIV,90 '//': TOKEN_FLOORDIV,91 '*': TOKEN_MUL,92 '%': TOKEN_MOD,93 '**': TOKEN_POW,94 '~': TOKEN_TILDE,95 '[': TOKEN_LBRACKET,96 ']': TOKEN_RBRACKET,97 '(': TOKEN_LPAREN,98 ')': TOKEN_RPAREN,99 '{': TOKEN_LBRACE,100 '}': TOKEN_RBRACE,101 '==': TOKEN_EQ,102 '!=': TOKEN_NE,103 '>': TOKEN_GT,104 '>=': TOKEN_GTEQ,105 '<': TOKEN_LT,106 '<=': TOKEN_LTEQ,107 '=': TOKEN_ASSIGN,108 '.': TOKEN_DOT,109 ':': TOKEN_COLON,110 '|': TOKEN_PIPE,111 ',': TOKEN_COMMA,112 ';': TOKEN_SEMICOLON113}114115reverse_operators = dict([(v, k) for k, v in operators.iteritems()])116assert len(operators) == len(reverse_operators), 'operators dropped'
76 unmodified lines
193 return self194195 def next(self):196 token = self.stream.current149 if token.type == 'eof':197 if token.type is TOKEN_EOF:198 self.stream.close()199 raise StopIteration()200 self.stream.next()201 return token
10 unmodified lines
212 self._pushed = deque()213 self.name = name214 self.filename = filename215 self.closed = False168 self.current = Token(1, 'initial', '')216 self.current = Token(1, TOKEN_INITIAL, '')217 self.next()218219 def __iter__(self):220 return TokenStreamIterator(self)221222 def __nonzero__(self):223 """Are we at the end of the stream?"""176 return bool(self._pushed) or self.current.type != 'eof'224 return bool(self._pushed) or self.current.type is not TOKEN_EOF225226 eos = property(lambda x: not x.__nonzero__(), doc=__nonzero__.__doc__)227228 def push(self, token):
28 unmodified lines
257 """Go one token ahead and return the old one"""258 rv = self.current259 if self._pushed:260 self.current = self._pushed.popleft()213 elif self.current.type != 'eof':261 elif self.current.type is not TOKEN_EOF:262 try:263 self.current = self._next()264 except StopIteration:265 self.close()266 return rv267268 def close(self):269 """Close the stream."""222 self.current = Token(self.current.lineno, 'eof', '')270 self.current = Token(self.current.lineno, TOKEN_EOF, '')271 self._next = None272 self.closed = True273274 def expect(self, expr):
2 unmodified lines
277 """278 if not self.current.test(expr):279 if ':' in expr:280 expr = expr.split(':')[1]233 if self.current.type == 'eof':281 if self.current.type is TOKEN_EOF:282 raise TemplateSyntaxError('unexpected end of template, '283 'expected %r.' % expr,284 self.current.lineno,285 self.name, self.filename)
39 unmodified lines
325 e = re.escape326327 # lexing rules for tags328 tag_rules = [281 (whitespace_re, 'whitespace', None),282 (float_re, 'float', None),283 (integer_re, 'integer', None),284 (name_re, 'name', None),285 (string_re, 'string', None),286 (operator_re, 'operator', None)329 (whitespace_re, TOKEN_WHITESPACE, None),330 (float_re, TOKEN_FLOAT, None),331 (integer_re, TOKEN_INTEGER, None),332 (name_re, TOKEN_NAME, None),333 (string_re, TOKEN_STRING, None),334 (operator_re, TOKEN_OPERATOR, None)335 ]336337 # assamble the root lexing rule. because "|" is ungreedy338 # we have to sort by length so that the lexer continues working
34 unmodified lines
373 e(environment.block_end_string)374 )] + [375 '(?P<%s_begin>\s*%s\-|%s)' % (n, r, r)376 for n, r in root_tag_rules329 ])), ('data', '#bygroup'), '#bygroup'),377 ])), (TOKEN_DATA, '#bygroup'), '#bygroup'),378 # data379 (c('.+'), 'data', None)380 ],381 # comments334 'comment_begin': [382 TOKEN_COMMENT_BEGIN: [383 (c(r'(.*?)((?:\-%s\s*|%s)%s)' % (384 e(environment.comment_end_string),385 e(environment.comment_end_string),386 block_suffix_re339 )), ('comment', 'comment_end'), '#pop'),387 )), (TOKEN_COMMENT, TOKEN_COMMENT_END), '#pop'),388 (c('(.)'), (Failure('Missing end of comment tag'),), None)389 ],390 # blocks343 'block_begin': [391 TOKEN_BLOCK_BEGIN: [392 (c('(?:\-%s\s*|%s)%s' % (393 e(environment.block_end_string),394 e(environment.block_end_string),395 block_suffix_re348 )), 'block_end', '#pop'),396 )), TOKEN_BLOCK_END, '#pop'),397 ] + tag_rules,398 # variables351 'variable_begin': [399 TOKEN_VARIABLE_BEGIN: [400 (c('\-%s\s*|%s' % (401 e(environment.variable_end_string),402 e(environment.variable_end_string)355 )), 'variable_end', '#pop')403 )), TOKEN_VARIABLE_END, '#pop')404 ] + tag_rules,405 # raw block358 'raw_begin': [406 TOKEN_RAW_BEGIN: [407 (c('(.*?)((?:\s*%s\-|%s)\s*endraw\s*(?:\-%s\s*|%s%s))' % (408 e(environment.block_start_string),409 e(environment.block_start_string),410 e(environment.block_end_string),411 e(environment.block_end_string),412 block_suffix_re365 )), ('data', 'raw_end'), '#pop'),413 )), (TOKEN_DATA, TOKEN_RAW_END), '#pop'),414 (c('(.)'), (Failure('Missing end of raw directive'),), None)415 ],416 # line statements369 'linestatement_begin': [370 (c(r'\s*(\n|$)'), 'linestatement_end', '#pop')417 TOKEN_LINESTATEMENT_BEGIN: [418 (c(r'\s*(\n|$)'), TOKEN_LINESTATEMENT_END, '#pop')419 ] + tag_rules420 }421422 def _normalize_newlines(self, value):
187 unmodified lines