Commit 25417282 authored by Jelle Zijlstra's avatar Jelle Zijlstra Committed by Jelle Zijlstra

f-strings: bugfixes

parent 6c7c6dfa
...@@ -808,7 +808,7 @@ def p_cat_string_literal(s): ...@@ -808,7 +808,7 @@ def p_cat_string_literal(s):
continue continue
elif next_kind != kind: elif next_kind != kind:
# concatenating f strings and normal strings is allowed and leads to an f string # concatenating f strings and normal strings is allowed and leads to an f string
if {kind, next_kind} == {'f', 'u'}: if {kind, next_kind} == {'f', 'u'} or {kind, next_kind} == {'f', ''}:
kind = 'f' kind = 'f'
else: else:
error(pos, "Cannot mix string literals of different types, expected %s'', got %s''" % error(pos, "Cannot mix string literals of different types, expected %s'', got %s''" %
...@@ -937,7 +937,7 @@ def p_string_literal(s, kind_override=None): ...@@ -937,7 +937,7 @@ def p_string_literal(s, kind_override=None):
else: else:
s.error("Invalid hex escape '%s'" % systr, s.error("Invalid hex escape '%s'" % systr,
fatal=False) fatal=False)
elif c in u'NUu' and kind in ('u', ''): # \uxxxx, \Uxxxxxxxx, \N{...} elif c in u'NUu' and kind in ('u', 'f', ''): # \uxxxx, \Uxxxxxxxx, \N{...}
chrval = -1 chrval = -1
if c == u'N': if c == u'N':
try: try:
...@@ -999,7 +999,10 @@ def p_f_string(s, unicode_value, pos): ...@@ -999,7 +999,10 @@ def p_f_string(s, unicode_value, pos):
while i < size: while i < size:
c = unicode_value[i] c = unicode_value[i]
if c == '}': if c == '}':
s.error("single '}' encountered in format string") if i + 1 >= size or unicode_value[i + 1] != '}':
s.error("single '}' encountered in format string")
else:
i += 2
elif c == '{': elif c == '{':
# double { escapes it # double { escapes it
if i + 1 < size and unicode_value[i + 1] == '{': if i + 1 < size and unicode_value[i + 1] == '{':
...@@ -1015,7 +1018,7 @@ def p_f_string(s, unicode_value, pos): ...@@ -1015,7 +1018,7 @@ def p_f_string(s, unicode_value, pos):
encoded_str = EncodedString(unicode_value[current_literal_start:]) encoded_str = EncodedString(unicode_value[current_literal_start:])
values.append(ExprNodes.UnicodeNode(pos, value = encoded_str)) values.append(ExprNodes.UnicodeNode(pos, value = encoded_str))
print "F-STRING VALUES", values print("F-STRING VALUES", values)
return values return values
...@@ -1061,6 +1064,7 @@ def p_f_string_expr(s, unicode_value, pos, starting_index): ...@@ -1061,6 +1064,7 @@ def p_f_string_expr(s, unicode_value, pos, starting_index):
elif nested_depth == 0 and c in '!:}': elif nested_depth == 0 and c in '!:}':
# allow != as a special case # allow != as a special case
if c == '!' and i + 1 < size and unicode_value[i + 1] == '=': if c == '!' and i + 1 < size and unicode_value[i + 1] == '=':
i += 1
continue continue
terminal_char = c terminal_char = c
...@@ -1105,7 +1109,7 @@ def p_f_string_expr(s, unicode_value, pos, starting_index): ...@@ -1105,7 +1109,7 @@ def p_f_string_expr(s, unicode_value, pos, starting_index):
if terminal_char != '}': if terminal_char != '}':
s.error("missing '}' in format string expression'") s.error("missing '}' in format string expression'")
print 'expr=%r, conversion_char=%r, format_spec=%r' % (expr_str, conversion_char, format_spec_str) print('expr=%r, conversion_char=%r, format_spec=%r' % (expr_str, conversion_char, format_spec_str))
# parse the expression # parse the expression
name = 'format string expression' name = 'format string expression'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment