bpo-42997: Improve error message for missing : before suites (GH-24292)

* Add to the peg generator a new directive ('&&') that allows to expect
  a token and hard fail the parsing if the token is not found. This
  allows to quickly emmit syntax errors for missing tokens.

* Use the new grammar element to hard-fail if the ':' is missing before
  suites.
This commit is contained in:
Pablo Galindo 2021-02-02 19:54:22 +00:00 • committed by GitHub
parent 802b645e81
commit 58fb156edd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 1219 additions and 428 deletions

File diff suppressed because it is too large Load diff

View file

@ -782,7 +782,6 @@ _PyPegen_is_memoized(Parser *p, int type, void *pres)
return 0;
}
int
_PyPegen_lookahead_with_name(int positive, expr_ty (func)(Parser *), Parser *p)
{
@ -836,6 +835,28 @@ _PyPegen_expect_token(Parser *p, int type)
return t;
}
Token *
_PyPegen_expect_forced_token(Parser *p, int type, const char* expected) {
if (p->error_indicator == 1) {
return NULL;
}
if (p->mark == p->fill) {
if (_PyPegen_fill_token(p) < 0) {
p->error_indicator = 1;
return NULL;
}
}
Token *t = p->tokens[p->mark];
if (t->type != type) {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(t, "expected '%s'", expected);
return NULL;
}
p->mark += 1;
return t;
}
expr_ty
_PyPegen_expect_soft_keyword(Parser *p, const char *keyword)
{

View file

@ -102,10 +102,7 @@ typedef struct {
arg_ty kwarg;
} StarEtc;
typedef struct {
operator_ty kind;
} AugOperator;
typedef struct { operator_ty kind; } AugOperator;
typedef struct {
void *element;
int is_keyword;
@ -118,12 +115,14 @@ int _PyPegen_insert_memo(Parser *p, int mark, int type, void *node);
int _PyPegen_update_memo(Parser *p, int mark, int type, void *node);
int _PyPegen_is_memoized(Parser *p, int type, void *pres);
int _PyPegen_lookahead_with_name(int, expr_ty (func)(Parser *), Parser *);
int _PyPegen_lookahead_with_int(int, Token *(func)(Parser *, int), Parser *, int);
int _PyPegen_lookahead_with_string(int , expr_ty (func)(Parser *, const char*), Parser *, const char*);
int _PyPegen_lookahead(int, void *(func)(Parser *), Parser *);
Token *_PyPegen_expect_token(Parser *p, int type);
Token *_PyPegen_expect_forced_token(Parser *p, int type, const char* expected);
expr_ty _PyPegen_expect_soft_keyword(Parser *p, const char *keyword);
Token *_PyPegen_get_last_nonnwhitespace_token(Parser *);
int _PyPegen_fill_token(Parser *p);