Skip to content

Commit dd5b683

Browse files
committed
gh-153568: Keep NOTEQUAL spelling in token text
1 parent ac85f97 commit dd5b683

3 files changed

Lines changed: 9 additions & 8 deletions

File tree

Parser/action_helpers.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,14 +946,16 @@ _PyPegen_add_type_comment_to_arg(Parser *p, arg_ty a, Token *tc)
946946
0 indicates success and nonzero indicates failure (an exception may be set) */
947947
int
948948
_PyPegen_check_barry_as_flufl(Parser *p, Token* t) {
949+
assert(t->bytes != NULL);
949950
assert(t->type == NOTEQUAL);
950951

951-
if (p->flags & PyPARSE_BARRY_AS_BDFL && !t->is_barry) {
952+
const char* tok_str = PyBytes_AS_STRING(t->bytes);
953+
if (p->flags & PyPARSE_BARRY_AS_BDFL && strcmp(tok_str, "<>") != 0) {
952954
RAISE_SYNTAX_ERROR("with Barry as BDFL, use '<>' instead of '!='");
953955
return -1;
954956
}
955957
if (!(p->flags & PyPARSE_BARRY_AS_BDFL)) {
956-
return t->is_barry;
958+
return strcmp(tok_str, "!=");
957959
}
958960
return 0;
959961
}

Parser/pegen.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ _get_keyword_or_name_type(Parser *p, const char *text, Py_ssize_t length)
197197
return NAME;
198198
}
199199

200-
// Only names, literals and type comments need their token text. Keyword
201-
// error actions use fixed spellings; NOTEQUAL keeps its spelling in is_barry.
200+
// Keep text only where grammar actions or helpers read it. Keyword error
201+
// actions use fixed spellings, so keywords do not need their text.
202202
static inline int
203203
token_needs_text(int type)
204204
{
@@ -213,6 +213,7 @@ token_needs_text(int type)
213213
case TSTRING_MIDDLE:
214214
case TSTRING_END:
215215
case TYPE_COMMENT:
216+
case NOTEQUAL: // _PyPegen_check_barry_as_flufl() distinguishes != and <>.
216217
return 1;
217218
default:
218219
return 0;
@@ -252,7 +253,6 @@ initialize_token(Parser *p, Token *parser_token, struct token *new_token, int to
252253

253254
parser_token->level = new_token->level;
254255
parser_token->is_raw = new_token->is_raw;
255-
parser_token->is_barry = token_type == NOTEQUAL && text[0] == '<';
256256
parser_token->lineno = new_token->start_loc.lineno;
257257
parser_token->col_offset = new_token->end_loc.lineno == p->starting_lineno
258258
? p->starting_col_offset + new_token->start_loc.byte_col

Parser/pegen.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ typedef struct _memo {
3838

3939
typedef struct {
4040
int type;
41-
// Text for names, literals and type comments; otherwise NULL.
41+
// Text required by grammar actions or helpers; otherwise NULL.
4242
PyObject *bytes;
4343
int level;
44-
unsigned int is_raw : 1;
45-
unsigned int is_barry : 1; // The '<>' spelling of NOTEQUAL.
44+
int is_raw;
4645
int lineno, col_offset, end_lineno, end_col_offset;
4746
Memo *memo;
4847
// Filter over the rule types present in `memo` (bit `type & 63` is set

0 commit comments

Comments
 (0)