diff options
author | Federico Caselli <cfederico87@gmail.com> | 2021-10-25 14:14:26 -0400 |
---|---|---|
committer | Federico Caselli <cfederico87@gmail.com> | 2021-10-25 21:21:58 +0200 |
commit | c47d172e5c6524c37ec91e5304c8279cc0132e54 (patch) | |
tree | 6987b09fe16485464c772b45681cb7ffaf4b21ba /mako/lexer.py | |
parent | cb94b678480fad8acf10ab896dd024d4357d7b30 (diff) | |
download | mako-c47d172e5c6524c37ec91e5304c8279cc0132e54.tar.gz |
Refactoring Code
## [Setup.py](setup.py)
- Use `with` when opening file to ensure closure
## [Util.py](mako/util.py)
- Remove unnecessary else after guard condition
- Replace if statement with if expression
- Remove unnecessary else after guard condition
- Replace unneeded comprehension with generator
- Inline variable that is immediately returned
## [Template.py](mako/template.py)
- Replace if statement with if expression, Simplify if expression by using or
- Replace list(), dict() or set() with comprehension
- Swap if/else branches, Merge else clause's nested if statement into elif
## [Runtime.py](mako/runtime.py)
- Remove unnecessary else after guard condition
## [PyParser.py](mako/pyparser.py)
- Replace yield inside for loop with yield from
- Lift code into else after jump in control flow, Merge else clause's nested if statement into elif
- Replace if statement with if expression
## [Pygen.py](mako/pygen.py)
- Merge nested if conditions
- Simplify conditional into return statement (removes comment)
- Replace if statement with if expression, Simplify boolean if expression
## [Parsetree.py](mako/parsetree.py)
- Replace unneeded comprehension with generator
- Merge else clause's nested if statement into elif
- Replace unneeded comprehension with generator
## [Lookup.py](mako/lookup.py)
- Swap if/else branches, Merge else clause's nested if statement into elif
- Swap if/else branches, Remove unnecessary else after guard condition
## [Lexer.py](mako/lexer.py)
- Replace if statement with if expression
- Merge nested if conditions
- Swap if/else branches, Remove unnecessary else after guard condition, Merge else clause's nested if statement into elif
- Swap if/else branches, Remove unnecessary else after guard condition
## [Exceptions.py](mako/exceptions.py)
- Replace if statement with if expression, Use `with` when opening file to ensure closure
## [Ast.py](mako/ast.py)
- Replace multiple comparisons of same variable with `in` operator
## [Pygmentplugin.py](mako/ext/pygmentplugin.py)
- Replace if statement with if expression
Closes: #335
Pull-request: https://github.com/sqlalchemy/mako/pull/335
Pull-request-sha: c6243b116441f4b14e1cff00f47ed72aee3d3133
Change-Id: I9093e2f5ca4bb59aa12536b1a0bdf2d58514aa40
Diffstat (limited to 'mako/lexer.py')
-rw-r--r-- | mako/lexer.py | 192 |
1 files changed, 90 insertions, 102 deletions
diff --git a/mako/lexer.py b/mako/lexer.py index 74fafa1..306ae4b 100644 --- a/mako/lexer.py +++ b/mako/lexer.py @@ -54,10 +54,7 @@ class Lexer: try: reg = _regexp_cache[(regexp, flags)] except KeyError: - if flags: - reg = re.compile(regexp, flags) - else: - reg = re.compile(regexp) + reg = re.compile(regexp, flags) if flags else re.compile(regexp) _regexp_cache[(regexp, flags)] = reg return self.match_reg(reg) @@ -75,10 +72,7 @@ class Lexer: match = reg.match(self.text, self.match_position) if match: (start, end) = match.span() - if end == start: - self.match_position = end + 1 - else: - self.match_position = end + self.match_position = end + 1 if end == start else end self.matched_lineno = self.lineno lines = re.findall(r"\n", self.text[mp : self.match_position]) cp = mp - 1 @@ -86,10 +80,6 @@ class Lexer: cp -= 1 self.matched_charpos = mp - cp self.lineno += len(lines) - # print "MATCHED:", match.group(0), "LINE START:", - # self.matched_lineno, "LINE END:", self.lineno - # print "MATCH:", regexp, "\n", self.text[mp : mp + 15], \ - # (match and "TRUE" or "FALSE") return match def parse_until_text(self, watch_nesting, *text): @@ -149,12 +139,15 @@ class Lexer: if self.control_line: control_frame = self.control_line[-1] control_frame.nodes.append(node) - if not ( - isinstance(node, parsetree.ControlLine) - and control_frame.is_ternary(node.keyword) + if ( + not ( + isinstance(node, parsetree.ControlLine) + and control_frame.is_ternary(node.keyword) + ) + and self.ternary_stack + and self.ternary_stack[-1] ): - if self.ternary_stack and self.ternary_stack[-1]: - self.ternary_stack[-1][-1].nodes.append(node) + self.ternary_stack[-1][-1].nodes.append(node) if isinstance(node, parsetree.Tag): if len(self.tag): node.parent = self.tag[-1] @@ -207,11 +200,7 @@ class Lexer: ) else: m = self._coding_re.match(text.decode("utf-8", "ignore")) - if m: - parsed_encoding = m.group(1) - else: - parsed_encoding = known_encoding or "utf-8" - + parsed_encoding = m.group(1) if m else known_encoding or "utf-8" if decode_raw: try: text = text.decode(parsed_encoding) @@ -301,35 +290,34 @@ class Lexer: re.I | re.S | re.X, ) - if match: - keyword, attr, isend = match.groups() - self.keyword = keyword - attributes = {} - if attr: - for att in re.findall( - r"\s*(\w+)\s*=\s*(?:'([^']*)'|\"([^\"]*)\")", attr - ): - key, val1, val2 = att - text = val1 or val2 - text = text.replace("\r\n", "\n") - attributes[key] = text - self.append_node(parsetree.Tag, keyword, attributes) - if isend: - self.tag.pop() - else: - if keyword == "text": - match = self.match(r"(.*?)(?=\</%text>)", re.S) - if not match: - raise exceptions.SyntaxException( - "Unclosed tag: <%%%s>" % self.tag[-1].keyword, - **self.exception_kwargs, - ) - self.append_node(parsetree.Text, match.group(1)) - return self.match_tag_end() - return True - else: + if not match: return False + keyword, attr, isend = match.groups() + self.keyword = keyword + attributes = {} + if attr: + for att in re.findall( + r"\s*(\w+)\s*=\s*(?:'([^']*)'|\"([^\"]*)\")", attr + ): + key, val1, val2 = att + text = val1 or val2 + text = text.replace("\r\n", "\n") + attributes[key] = text + self.append_node(parsetree.Tag, keyword, attributes) + if isend: + self.tag.pop() + elif keyword == "text": + match = self.match(r"(.*?)(?=\</%text>)", re.S) + if not match: + raise exceptions.SyntaxException( + "Unclosed tag: <%%%s>" % self.tag[-1].keyword, + **self.exception_kwargs + ) + self.append_node(parsetree.Text, match.group(1)) + return self.match_tag_end() + return True + def match_tag_end(self): match = self.match(r"\</%[\t ]*(.+?)[\t ]*>") if match: @@ -352,15 +340,15 @@ class Lexer: def match_end(self): match = self.match(r"\Z", re.S) - if match: - string = match.group() - if string: - return string - else: - return True - else: + if not match: return False + string = match.group() + if string: + return string + else: + return True + def match_text(self): match = self.match( r""" @@ -411,63 +399,63 @@ class Lexer: def match_expression(self): match = self.match(r"\${") - if match: - line, pos = self.matched_lineno, self.matched_charpos - text, end = self.parse_until_text(True, r"\|", r"}") - if end == "|": - escapes, end = self.parse_until_text(True, r"}") - else: - escapes = "" - text = text.replace("\r\n", "\n") - self.append_node( - parsetree.Expression, - text, - escapes.strip(), - lineno=line, - pos=pos, - ) - return True - else: + if not match: return False + line, pos = self.matched_lineno, self.matched_charpos + text, end = self.parse_until_text(True, r"\|", r"}") + if end == "|": + escapes, end = self.parse_until_text(True, r"}") + else: + escapes = "" + text = text.replace("\r\n", "\n") + self.append_node( + parsetree.Expression, + text, + escapes.strip(), + lineno=line, + pos=pos, + ) + return True + def match_control_line(self): match = self.match( r"(?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\r?\n)|[^\r\n])*)" r"(?:\r?\n|\Z)", re.M, ) - if match: - operator = match.group(1) - text = match.group(2) - if operator == "%": - m2 = re.match(r"(end)?(\w+)\s*(.*)", text) - if not m2: + if not match: + return False + + operator = match.group(1) + text = match.group(2) + if operator == "%": + m2 = re.match(r"(end)?(\w+)\s*(.*)", text) + if not m2: + raise exceptions.SyntaxException( + "Invalid control line: '%s'" % text, + **self.exception_kwargs + ) + isend, keyword = m2.group(1, 2) + isend = isend is not None + + if isend: + if not len(self.control_line): raise exceptions.SyntaxException( - "Invalid control line: '%s'" % text, - **self.exception_kwargs, + "No starting keyword '%s' for '%s'" + % (keyword, text), + **self.exception_kwargs ) - isend, keyword = m2.group(1, 2) - isend = isend is not None - - if isend: - if not len(self.control_line): - raise exceptions.SyntaxException( - "No starting keyword '%s' for '%s'" - % (keyword, text), - **self.exception_kwargs, - ) - elif self.control_line[-1].keyword != keyword: - raise exceptions.SyntaxException( - "Keyword '%s' doesn't match keyword '%s'" - % (text, self.control_line[-1].keyword), - **self.exception_kwargs, - ) - self.append_node(parsetree.ControlLine, keyword, isend, text) - else: - self.append_node(parsetree.Comment, text) - return True + elif self.control_line[-1].keyword != keyword: + raise exceptions.SyntaxException( + "Keyword '%s' doesn't match keyword '%s'" + % (text, self.control_line[-1].keyword), + **self.exception_kwargs + ) + self.append_node(parsetree.ControlLine, keyword, isend, text) else: - return False + self.append_node(parsetree.Comment, text) + return True def match_comment(self): """matches the multiline version of a comment""" |