Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Tagged String syntax -- RAISES SYNTAX ERROR
  • Loading branch information
gvanrossum committed May 3, 2022
commit 1b61a5e3c65ed8d505958f955cba063762dd57b5
1 change: 1 addition & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ slice[expr_ty]:
| a=named_expression { a }

atom[expr_ty]:
| a=NAME &STRING b=strings { _PyPegen_tag_string(p, a, b) }
| NAME
| 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
| 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
Expand Down
14 changes: 11 additions & 3 deletions Include/internal/pycore_ast.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Include/internal/pycore_ast_state.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Parser/Python.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ module Python
| Call(expr func, expr* args, keyword* keywords)
| FormattedValue(expr value, int conversion, expr? format_spec)
| JoinedStr(expr* values)
| TagString(expr tag, expr str)
| Constant(constant value, string? kind)

-- the following expression can appear in assignment context
Expand Down
13 changes: 13 additions & 0 deletions Parser/action_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,19 @@ _PyPegen_seq_delete_starred_exprs(Parser *p, asdl_seq *kwargs)
return new_seq;
}

expr_ty
_PyPegen_tag_string(Parser *p, expr_ty tag, expr_ty str)
{
// No prefixes (f, r, b, u)
// Parse like fstring
// Create a node similar to f-string AST
return _PyAST_TagString(tag, str,
tag->lineno, tag->col_offset, str->end_lineno, str->end_col_offset,
p->arena);

}


expr_ty
_PyPegen_concatenate_strings(Parser *p, asdl_seq *strings)
{
Expand Down
30 changes: 30 additions & 0 deletions Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Parser/pegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ expr_ty _PyPegen_collect_call_seqs(Parser *, asdl_expr_seq *, asdl_seq *,
int lineno, int col_offset, int end_lineno,
int end_col_offset, PyArena *arena);
expr_ty _PyPegen_concatenate_strings(Parser *p, asdl_seq *);
expr_ty _PyPegen_tag_string(Parser *p, expr_ty, expr_ty);
expr_ty _PyPegen_ensure_imaginary(Parser *p, expr_ty);
expr_ty _PyPegen_ensure_real(Parser *p, expr_ty);
asdl_seq *_PyPegen_join_sequences(Parser *, asdl_seq *, asdl_seq *);
Expand Down
106 changes: 106 additions & 0 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ validate_expr(struct validator *state, expr_ty exp, expr_context_ty ctx)
case JoinedStr_kind:
ret = validate_exprs(state, exp->v.JoinedStr.values, Load, 0);
break;
case TagString_kind:
ret = validate_expr(state, exp->v.TagString.tag, Load) &&
validate_expr(state, exp->v.TagString.str, Load);
break;
case FormattedValue_kind:
if (validate_expr(state, exp->v.FormattedValue.value, Load) == 0)
return 0;
Expand Down
5 changes: 5 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4691,6 +4691,7 @@ check_caller(struct compiler *c, expr_ty e)
case SetComp_kind:
case GeneratorExp_kind:
case JoinedStr_kind:
case TagString_kind:
case FormattedValue_kind:
return compiler_warn(c, "'%.200s' object is not callable; "
"perhaps you missed a comma?",
Expand Down Expand Up @@ -4750,6 +4751,7 @@ check_index(struct compiler *c, expr_ty e, expr_ty s)
case List_kind:
case ListComp_kind:
case JoinedStr_kind:
case TagString_kind:
case FormattedValue_kind:
return compiler_warn(c, "%.200s indices must be integers or slices, "
"not %.200s; "
Expand Down Expand Up @@ -5835,6 +5837,9 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
break;
case JoinedStr_kind:
return compiler_joined_str(c, e);
case TagString_kind:
return compiler_error(c, "TagString not yet supported");
break;
case FormattedValue_kind:
return compiler_formatted_value(c, e);
/* The following exprs can be assignment targets. */
Expand Down