Skip to content

Commit

Permalink
hopefully fix the templates and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
thatbirdguythatuknownot committed Jun 3, 2024
1 parent 302ff51 commit 52b7d60
Show file tree
Hide file tree
Showing 9 changed files with 1,670 additions and 1,607 deletions.
9 changes: 3 additions & 6 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1252,14 +1252,11 @@ atom[expr_ty]:
| 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
| 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
| 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
| a='$' b[int]=[pos_c_int_opt] {
b == -1 ? NULL :
b < p->subn ?
_PyAST_Template(b, 0, EXTRA) :
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "template index out of range") }
| t='$' lvl[int]=[pos_c_int_opt] {
lvl >= 0 ? _PyPegen_make_template(p, lvl, t, EXTRA) : NULL }
| &(STRING|FSTRING_START) strings
| NUMBER
| &'(' (compound_expr | tuplecomp | tuple | group | genexp)
| &'(' (compound_expr | tuplecomp | group | genexp | tuple)
| &'[' (listcomp | list)
| &'{' (dictcomp | setcomp | dict | set)
| '...' { _PyAST_Constant(Py_Ellipsis, NULL, EXTRA) }
Expand Down
4 changes: 4 additions & 0 deletions Include/cpython/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# error "this header file must not be included directly"
#endif

#define PY_MAX_TEMPLATE_SUBS 200

/* Public interface */
#define PyCF_MASK (CO_FUTURE_DIVISION | CO_FUTURE_ABSOLUTE_IMPORT | \
CO_FUTURE_WITH_STATEMENT | CO_FUTURE_PRINT_FUNCTION | \
Expand All @@ -24,6 +26,8 @@
PyCF_TYPE_COMMENTS | PyCF_DONT_IMPLY_DEDENT | \
PyCF_ALLOW_INCOMPLETE_INPUT | PyCF_OPTIMIZED_AST)



typedef struct {
int cf_flags; /* bitmask of CO_xxx flags relevant to future */
int cf_feature_version; /* minor Python version (PyCF_ONLY_AST) */
Expand Down
59 changes: 59 additions & 0 deletions Parser/action_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1749,3 +1749,62 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
assert(current_pos == n_elements);
return _PyAST_JoinedStr(values, lineno, col_offset, end_lineno, end_col_offset, p->arena);
}

expr_ty
_PyPegen_make_template(Parser *p, int level, Token *t,
int lineno, int col_offset, int end_lineno,
int end_col_offset, PyArena *arena)
{
if (level >= p->subn) {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(t, "template index out of range");
return NULL;
}

expr_ty node = _PyAST_Template(level, 0,
lineno, col_offset,
end_lineno, end_col_offset,
p->arena);
p->template_subs[p->subn - level - 1] = node;
return node;
}

int
_PyPegen_inc_subn(Parser *p)
{
if (p->subn >= PY_MAX_TEMPLATE_SUBS) {
int lineno = p->tok->lineno;
int col_offset = CURRENT_POS;
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError,
lineno, col_offset, lineno, col_offset,
"composition/comprehension depth exceeded %d",
PY_MAX_TEMPLATE_SUBS);
return 0;
}

/* Ensure that the data isn't garbage. */
p->template_subs[p->subn++] = NULL;
return 1;
}

int
_PyPegen_dec_subn(Parser *p, int success)
{
if (p->subn <= 0) {
int lineno = p->tok->lineno;
int col_offset = CURRENT_POS;
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError,
lineno, col_offset, lineno, col_offset,
"composition/comprehension depth underflow",
PY_MAX_TEMPLATE_SUBS);
return -1;
}

if (!success) {
p->template_subs[--p->subn] = NULL;
p->max_subn = p->subn;
}
else {
p->max_subn = p->subn--;
}
return success;
}
Loading

0 comments on commit 52b7d60

Please sign in to comment.