@@ -54,10 +54,7 @@ def match(self, regexp, flags=None):
5454 try :
5555 reg = _regexp_cache [(regexp , flags )]
5656 except KeyError :
57- if flags :
58- reg = re .compile (regexp , flags )
59- else :
60- reg = re .compile (regexp )
57+ reg = re .compile (regexp , flags ) if flags else re .compile (regexp )
6158 _regexp_cache [(regexp , flags )] = reg
6259
6360 return self .match_reg (reg )
@@ -75,21 +72,14 @@ def match_reg(self, reg):
7572 match = reg .match (self .text , self .match_position )
7673 if match :
7774 (start , end ) = match .span ()
78- if end == start :
79- self .match_position = end + 1
80- else :
81- self .match_position = end
75+ self .match_position = end + 1 if end == start else end
8276 self .matched_lineno = self .lineno
8377 lines = re .findall (r"\n" , self .text [mp : self .match_position ])
8478 cp = mp - 1
8579 while cp >= 0 and cp < self .textlength and self .text [cp ] != "\n " :
8680 cp -= 1
8781 self .matched_charpos = mp - cp
8882 self .lineno += len (lines )
89- # print "MATCHED:", match.group(0), "LINE START:",
90- # self.matched_lineno, "LINE END:", self.lineno
91- # print "MATCH:", regexp, "\n", self.text[mp : mp + 15], \
92- # (match and "TRUE" or "FALSE")
9383 return match
9484
9585 def parse_until_text (self , watch_nesting , * text ):
@@ -149,12 +139,15 @@ def append_node(self, nodecls, *args, **kwargs):
149139 if self .control_line :
150140 control_frame = self .control_line [- 1 ]
151141 control_frame .nodes .append (node )
152- if not (
153- isinstance (node , parsetree .ControlLine )
154- and control_frame .is_ternary (node .keyword )
142+ if (
143+ not (
144+ isinstance (node , parsetree .ControlLine )
145+ and control_frame .is_ternary (node .keyword )
146+ )
147+ and self .ternary_stack
148+ and self .ternary_stack [- 1 ]
155149 ):
156- if self .ternary_stack and self .ternary_stack [- 1 ]:
157- self .ternary_stack [- 1 ][- 1 ].nodes .append (node )
150+ self .ternary_stack [- 1 ][- 1 ].nodes .append (node )
158151 if isinstance (node , parsetree .Tag ):
159152 if len (self .tag ):
160153 node .parent = self .tag [- 1 ]
@@ -207,11 +200,7 @@ def decode_raw_stream(self, text, decode_raw, known_encoding, filename):
207200 )
208201 else :
209202 m = self ._coding_re .match (text .decode ("utf-8" , "ignore" ))
210- if m :
211- parsed_encoding = m .group (1 )
212- else :
213- parsed_encoding = known_encoding or "utf-8"
214-
203+ parsed_encoding = m .group (1 ) if m else known_encoding or "utf-8"
215204 if decode_raw :
216205 try :
217206 text = text .decode (parsed_encoding )
@@ -301,35 +290,34 @@ def match_tag_start(self):
301290 re .I | re .S | re .X ,
302291 )
303292
304- if match :
305- keyword , attr , isend = match .groups ()
306- self .keyword = keyword
307- attributes = {}
308- if attr :
309- for att in re .findall (
310- r"\s*(\w+)\s*=\s*(?:'([^']*)'|\"([^\"]*)\")" , attr
311- ):
312- key , val1 , val2 = att
313- text = val1 or val2
314- text = text .replace ("\r \n " , "\n " )
315- attributes [key ] = text
316- self .append_node (parsetree .Tag , keyword , attributes )
317- if isend :
318- self .tag .pop ()
319- else :
320- if keyword == "text" :
321- match = self .match (r"(.*?)(?=\</%text>)" , re .S )
322- if not match :
323- raise exceptions .SyntaxException (
324- "Unclosed tag: <%%%s>" % self .tag [- 1 ].keyword ,
325- ** self .exception_kwargs ,
326- )
327- self .append_node (parsetree .Text , match .group (1 ))
328- return self .match_tag_end ()
329- return True
330- else :
293+ if not match :
331294 return False
332295
296+ keyword , attr , isend = match .groups ()
297+ self .keyword = keyword
298+ attributes = {}
299+ if attr :
300+ for att in re .findall (
301+ r"\s*(\w+)\s*=\s*(?:'([^']*)'|\"([^\"]*)\")" , attr
302+ ):
303+ key , val1 , val2 = att
304+ text = val1 or val2
305+ text = text .replace ("\r \n " , "\n " )
306+ attributes [key ] = text
307+ self .append_node (parsetree .Tag , keyword , attributes )
308+ if isend :
309+ self .tag .pop ()
310+ elif keyword == "text" :
311+ match = self .match (r"(.*?)(?=\</%text>)" , re .S )
312+ if not match :
313+ raise exceptions .SyntaxException (
314+ "Unclosed tag: <%%%s>" % self .tag [- 1 ].keyword ,
315+ ** self .exception_kwargs
316+ )
317+ self .append_node (parsetree .Text , match .group (1 ))
318+ return self .match_tag_end ()
319+ return True
320+
333321 def match_tag_end (self ):
334322 match = self .match (r"\</%[\t ]*(.+?)[\t ]*>" )
335323 if match :
@@ -352,15 +340,15 @@ def match_tag_end(self):
352340
353341 def match_end (self ):
354342 match = self .match (r"\Z" , re .S )
355- if match :
356- string = match .group ()
357- if string :
358- return string
359- else :
360- return True
361- else :
343+ if not match :
362344 return False
363345
346+ string = match .group ()
347+ if string :
348+ return string
349+ else :
350+ return True
351+
364352 def match_text (self ):
365353 match = self .match (
366354 r"""
@@ -411,63 +399,63 @@ def match_python_block(self):
411399
412400 def match_expression (self ):
413401 match = self .match (r"\${" )
414- if match :
415- line , pos = self .matched_lineno , self .matched_charpos
416- text , end = self .parse_until_text (True , r"\|" , r"}" )
417- if end == "|" :
418- escapes , end = self .parse_until_text (True , r"}" )
419- else :
420- escapes = ""
421- text = text .replace ("\r \n " , "\n " )
422- self .append_node (
423- parsetree .Expression ,
424- text ,
425- escapes .strip (),
426- lineno = line ,
427- pos = pos ,
428- )
429- return True
430- else :
402+ if not match :
431403 return False
432404
405+ line , pos = self .matched_lineno , self .matched_charpos
406+ text , end = self .parse_until_text (True , r"\|" , r"}" )
407+ if end == "|" :
408+ escapes , end = self .parse_until_text (True , r"}" )
409+ else :
410+ escapes = ""
411+ text = text .replace ("\r \n " , "\n " )
412+ self .append_node (
413+ parsetree .Expression ,
414+ text ,
415+ escapes .strip (),
416+ lineno = line ,
417+ pos = pos ,
418+ )
419+ return True
420+
433421 def match_control_line (self ):
434422 match = self .match (
435423 r"(?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\r?\n)|[^\r\n])*)"
436424 r"(?:\r?\n|\Z)" ,
437425 re .M ,
438426 )
439- if match :
440- operator = match .group (1 )
441- text = match .group (2 )
442- if operator == "%" :
443- m2 = re .match (r"(end)?(\w+)\s*(.*)" , text )
444- if not m2 :
427+ if not match :
428+ return False
429+
430+ operator = match .group (1 )
431+ text = match .group (2 )
432+ if operator == "%" :
433+ m2 = re .match (r"(end)?(\w+)\s*(.*)" , text )
434+ if not m2 :
435+ raise exceptions .SyntaxException (
436+ "Invalid control line: '%s'" % text ,
437+ ** self .exception_kwargs
438+ )
439+ isend , keyword = m2 .group (1 , 2 )
440+ isend = isend is not None
441+
442+ if isend :
443+ if not len (self .control_line ):
445444 raise exceptions .SyntaxException (
446- "Invalid control line: '%s'" % text ,
447- ** self .exception_kwargs ,
445+ "No starting keyword '%s' for '%s'"
446+ % (keyword , text ),
447+ ** self .exception_kwargs
448448 )
449- isend , keyword = m2 .group (1 , 2 )
450- isend = isend is not None
451-
452- if isend :
453- if not len (self .control_line ):
454- raise exceptions .SyntaxException (
455- "No starting keyword '%s' for '%s'"
456- % (keyword , text ),
457- ** self .exception_kwargs ,
458- )
459- elif self .control_line [- 1 ].keyword != keyword :
460- raise exceptions .SyntaxException (
461- "Keyword '%s' doesn't match keyword '%s'"
462- % (text , self .control_line [- 1 ].keyword ),
463- ** self .exception_kwargs ,
464- )
465- self .append_node (parsetree .ControlLine , keyword , isend , text )
466- else :
467- self .append_node (parsetree .Comment , text )
468- return True
449+ elif self .control_line [- 1 ].keyword != keyword :
450+ raise exceptions .SyntaxException (
451+ "Keyword '%s' doesn't match keyword '%s'"
452+ % (text , self .control_line [- 1 ].keyword ),
453+ ** self .exception_kwargs
454+ )
455+ self .append_node (parsetree .ControlLine , keyword , isend , text )
469456 else :
470- return False
457+ self .append_node (parsetree .Comment , text )
458+ return True
471459
472460 def match_comment (self ):
473461 """matches the multiline version of a comment"""
0 commit comments