Skip to content

Commit 92e502a

Browse files
committed
add helper function in parser
1 parent 9e22cfa commit 92e502a

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

Grammar/python.gram

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,15 @@ invalid_type_params:
16421642
"Type parameter list cannot be empty")}
16431643

16441644
invalid_bitwise_and:
1645-
| a=bitwise_and b='&' c='&' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(b, c, "invalid syntax. Maybe you meant 'and' or '&' instead of '&&'?") }
1645+
| a=bitwise_and b='&' c='&' {
1646+
_PyPegen_tokens_are_adjacent(b, c)
1647+
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(b, c, "invalid syntax. Maybe you meant 'and' or '&' instead of '&&'?")
1648+
: NULL
1649+
}
16461650

16471651
invalid_bitwise_or:
1648-
| a=bitwise_or b='|' c='|' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(b, c, "invalid syntax. Maybe you meant 'or' or '|' instead of '||'?") }
1652+
| a=bitwise_or b='|' c='|' {
1653+
_PyPegen_tokens_are_adjacent(b, c)
1654+
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(b, c, "invalid syntax. Maybe you meant 'or' or '|' instead of '||'?")
1655+
: NULL
1656+
}

Parser/parser.c

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Parser/pegen.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ RAISE_ERROR_KNOWN_LOCATION(Parser *p, PyObject *errtype,
209209
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, (a)->lineno, (a)->col_offset, CURRENT_POS, CURRENT_POS, msg, ##__VA_ARGS__)
210210
#define RAISE_SYNTAX_ERROR_INVALID_TARGET(type, e) _RAISE_SYNTAX_ERROR_INVALID_TARGET(p, type, e)
211211

212+
Py_LOCAL_INLINE(int)
213+
_PyPegen_tokens_are_adjacent(Token *a, Token *b)
214+
{
215+
return (a->end_lineno == b->lineno) && (a->end_col_offset == b->col_offset);
216+
}
217+
212218
Py_LOCAL_INLINE(void *)
213219
CHECK_CALL(Parser *p, void *result)
214220
{

0 commit comments

Comments
 (0)