Skip to content

Commit f63ac52

Browse files
committed
Refactor error function
1 parent e8bf6b9 commit f63ac52

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

Parser/pegen.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,16 @@ raise_decode_error(Parser *p)
265265
return -1;
266266
}
267267

268+
static inline void
269+
raise_unclosed_parentheses_error(Parser *p) {
270+
int error_lineno = p->tok->parenlinenostack[p->tok->level-1];
271+
int error_col = p->tok->parencolstack[p->tok->level-1];
272+
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError,
273+
error_lineno, error_col,
274+
"'%c' was never closed",
275+
p->tok->parenstack[p->tok->level-1]);
276+
}
277+
268278
static void
269279
raise_tokenizer_init_error(PyObject *filename)
270280
{
@@ -325,11 +335,7 @@ tokenizer_error(Parser *p)
325335
return -1;
326336
case E_EOF:
327337
if (p->tok->level) {
328-
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError,
329-
p->tok->parenlinenostack[p->tok->level-1],
330-
p->tok->parencolstack[p->tok->level-1],
331-
"'%c' was never closed",
332-
p->tok->parenstack[p->tok->level-1]);
338+
raise_unclosed_parentheses_error(p);
333339
} else {
334340
RAISE_SYNTAX_ERROR("unexpected EOF while parsing");
335341
}
@@ -1159,7 +1165,7 @@ reset_parser_state(Parser *p)
11591165
p->call_invalid_rules = 1;
11601166
}
11611167

1162-
int
1168+
static int
11631169
_PyPegen_check_tokenizer_errors(Parser *p) {
11641170
// Tokenize the whole input to see if there are any tokenization
11651171
// errors such as mistmatching parentheses. These will get priority
@@ -1187,12 +1193,8 @@ _PyPegen_check_tokenizer_errors(Parser *p) {
11871193
if (type == ERRORTOKEN) {
11881194
if (p->tok->level != 0) {
11891195
int error_lineno = p->tok->parenlinenostack[p->tok->level-1];
1190-
int error_col = p->tok->parencolstack[p->tok->level-1];
11911196
if (current_err_line > error_lineno) {
1192-
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError,
1193-
error_lineno, error_col,
1194-
"'%c' was never closed",
1195-
p->tok->parenstack[p->tok->level-1]);
1197+
raise_unclosed_parentheses_error(p);
11961198
return -1;
11971199
}
11981200
}
@@ -1221,11 +1223,7 @@ _PyPegen_run_parser(Parser *p)
12211223
}
12221224
else if (p->tok->done == E_EOF) {
12231225
if (p->tok->level) {
1224-
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError,
1225-
p->tok->parenlinenostack[p->tok->level-1],
1226-
p->tok->parencolstack[p->tok->level-1],
1227-
"'%c' was never closed",
1228-
p->tok->parenstack[p->tok->level-1]);
1226+
raise_unclosed_parentheses_error(p);
12291227
} else {
12301228
RAISE_SYNTAX_ERROR("unexpected EOF while parsing");
12311229
}

0 commit comments

Comments
 (0)