mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 05:14:29 +08:00
Fix the syntax errors from f-strings
This commit is contained in:
@@ -846,26 +846,12 @@ class _TryStmtRule(SyntaxRule):
|
|||||||
@ErrorFinder.register_rule(type='fstring')
|
@ErrorFinder.register_rule(type='fstring')
|
||||||
class _FStringRule(SyntaxRule):
|
class _FStringRule(SyntaxRule):
|
||||||
_fstring_grammar = None
|
_fstring_grammar = None
|
||||||
message_single_closing = "f-string: single '}' is not allowed" # f'}'
|
|
||||||
message_nested = "f-string: expressions nested too deeply"
|
message_nested = "f-string: expressions nested too deeply"
|
||||||
message_conversion = "f-string: invalid conversion character: expected 's', 'r', or 'a'"
|
message_conversion = "f-string: invalid conversion character: expected 's', 'r', or 'a'"
|
||||||
|
|
||||||
def _check_format_spec(self, format_spec, depth):
|
def _check_format_spec(self, format_spec, depth):
|
||||||
self._check_fstring_contents(format_spec.children[1:], depth)
|
self._check_fstring_contents(format_spec.children[1:], depth)
|
||||||
|
|
||||||
def _check_string_part(self, fstring_string):
|
|
||||||
index = -1
|
|
||||||
value = fstring_string.value
|
|
||||||
while True:
|
|
||||||
index = value.find('}', index + 1)
|
|
||||||
if index == -1:
|
|
||||||
break # No further } found, we're finished.
|
|
||||||
elif index + 1 != len(value) and value[index + 1]:
|
|
||||||
# It's }}, which is totally ok.
|
|
||||||
index += 1
|
|
||||||
else:
|
|
||||||
self.add_issue(fstring_string, message=self.message_single_closing)
|
|
||||||
|
|
||||||
def _check_fstring_expr(self, fstring_expr, depth):
|
def _check_fstring_expr(self, fstring_expr, depth):
|
||||||
if depth >= 2:
|
if depth >= 2:
|
||||||
self.add_issue(fstring_expr, message=self.message_nested)
|
self.add_issue(fstring_expr, message=self.message_nested)
|
||||||
@@ -885,10 +871,7 @@ class _FStringRule(SyntaxRule):
|
|||||||
|
|
||||||
def _check_fstring_contents(self, children, depth=0):
|
def _check_fstring_contents(self, children, depth=0):
|
||||||
for fstring_content in children:
|
for fstring_content in children:
|
||||||
if fstring_content.type == 'fstring_string':
|
if fstring_content.type == 'fstring_expr':
|
||||||
self._check_string_part(fstring_content)
|
|
||||||
else:
|
|
||||||
assert fstring_content.type == 'fstring_expr'
|
|
||||||
self._check_fstring_expr(fstring_content, depth)
|
self._check_fstring_expr(fstring_content, depth)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -122,12 +122,14 @@ def _get_actual_exception(code):
|
|||||||
"SyntaxError: EOL while scanning string literal",
|
"SyntaxError: EOL while scanning string literal",
|
||||||
"SyntaxError: unexpected character after line continuation character",
|
"SyntaxError: unexpected character after line continuation character",
|
||||||
], line_nr
|
], line_nr
|
||||||
|
elif wanted == "SyntaxError: f-string: expecting '}'":
|
||||||
|
wanted = 'SyntaxError: EOL while scanning string literal'
|
||||||
elif wanted == 'SyntaxError: f-string: empty expression not allowed':
|
elif wanted == 'SyntaxError: f-string: empty expression not allowed':
|
||||||
wanted = 'SyntaxError: invalid syntax'
|
wanted = 'SyntaxError: invalid syntax'
|
||||||
elif wanted == "SyntaxError: f-string expression part cannot include '#'":
|
elif wanted == "SyntaxError: f-string expression part cannot include '#'":
|
||||||
wanted = 'SyntaxError: invalid syntax'
|
wanted = 'SyntaxError: invalid syntax'
|
||||||
elif wanted == "SyntaxError: f-string: expecting '}'":
|
elif wanted == "SyntaxError: f-string: single '}' is not allowed":
|
||||||
wanted = 'SyntaxError: EOL while scanning string literal'
|
wanted = 'SyntaxError: invalid syntax'
|
||||||
return [wanted], line_nr
|
return [wanted], line_nr
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user