Refs #32919 -- Simplified Lexer.create_token() by reorganizing blocks.

This commit is contained in:
Chris Jerdonek 2021-08-03 11:21:22 -04:00 committed by Mariusz Felisiak
parent 7ff72b5909
commit 55cf9e93b5
1 changed files with 27 additions and 21 deletions

View File

@ -367,27 +367,33 @@ class Lexer:
If in_tag is True, we are processing something that matched a tag, If in_tag is True, we are processing something that matched a tag,
otherwise it should be treated as a literal string. otherwise it should be treated as a literal string.
""" """
token_start = token_string[0:2] if in_tag:
if in_tag and token_start == BLOCK_TAG_START: # The [0:2] and [2:-2] ranges below strip off *_TAG_START and
# The [2:-2] ranges below strip off *_TAG_START and *_TAG_END. # *_TAG_END. The 2's are hard-coded for performance. Using
# We could do len(BLOCK_TAG_START) to be more "correct", but we've # len(BLOCK_TAG_START) would permit BLOCK_TAG_START to be
# hard-coded the 2s here for performance. And it's not like # different, but it's not likely that the TAG_START values will
# the TAG_START values are going to change anytime, anyway. # change anytime soon.
block_content = token_string[2:-2].strip() token_start = token_string[0:2]
if self.verbatim and block_content == self.verbatim: if token_start == BLOCK_TAG_START:
self.verbatim = False content = token_string[2:-2].strip()
if in_tag and not self.verbatim: if self.verbatim:
if token_start == VARIABLE_TAG_START: # Then a verbatim block is being processed.
return Token(TokenType.VAR, token_string[2:-2].strip(), position, lineno) if content != self.verbatim:
elif token_start == BLOCK_TAG_START: return Token(TokenType.TEXT, token_string, position, lineno)
if block_content[:9] in ('verbatim', 'verbatim '): # Otherwise, the current verbatim block is ending.
self.verbatim = 'end%s' % block_content self.verbatim = False
return Token(TokenType.BLOCK, block_content, position, lineno) elif content[:9] in ('verbatim', 'verbatim '):
assert token_start == COMMENT_TAG_START # Then a verbatim block is starting.
content = token_string[2:-2].strip() self.verbatim = 'end%s' % content
return Token(TokenType.COMMENT, content, position, lineno) return Token(TokenType.BLOCK, content, position, lineno)
else: if not self.verbatim:
return Token(TokenType.TEXT, token_string, position, lineno) content = token_string[2:-2].strip()
if token_start == VARIABLE_TAG_START:
return Token(TokenType.VAR, content, position, lineno)
# BLOCK_TAG_START was handled above.
assert token_start == COMMENT_TAG_START
return Token(TokenType.COMMENT, content, position, lineno)
return Token(TokenType.TEXT, token_string, position, lineno)
class DebugLexer(Lexer): class DebugLexer(Lexer):