summaryrefslogtreecommitdiff
path: root/Modules
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2019-04-03 14:34:59 -0400
committerGitHub <noreply@github.com>2019-04-03 14:34:59 -0400
commit513d142993bb8c13e6803727fa086e44eafc360f (patch)
tree5f05e77b87f6256d569415c6d6abbb958949de7d /Modules
parent9c08eeb30ca0e551323467b62ae40e08e30839b3 (diff)
downloadcpython-git-513d142993bb8c13e6803727fa086e44eafc360f.tar.gz
[3.7] bpo-36440: include node names in ParserError messages, instead of numeric IDs (GH-12565) (GH-12671)
The error messages in the parser module are referring to numeric IDs for the nodes. To improve readability, use the node names when reporting errors.. (cherry picked from commit cb0748d3939c31168ab5d3b80e3677494497d5e3) Co-authored-by: tyomitch <tyomitch@gmail.com>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/parsermodule.c17
1 files changed, 11 insertions, 6 deletions
diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c
index 67c874267f..799a813468 100644
--- a/Modules/parsermodule.c
+++ b/Modules/parsermodule.c
@@ -24,10 +24,6 @@
* Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
* look like "NOTE(...)".
*
- * To debug parser errors like
- * "parser.ParserError: Expected node type 12, got 333."
- * decode symbol numbers using the automatically-generated files
- * Lib/symbol.h and Include/token.h.
*/
#include "Python.h" /* general Python API */
@@ -663,6 +659,13 @@ validate_node(node *tree)
for (pos = 0; pos < nch; ++pos) {
node *ch = CHILD(tree, pos);
int ch_type = TYPE(ch);
+ if ((ch_type >= NT_OFFSET + _PyParser_Grammar.g_ndfas)
+ || (ISTERMINAL(ch_type) && (ch_type >= N_TOKENS))
+ || (ch_type < 0)
+ ) {
+ PyErr_Format(parser_error, "Unrecognized node type %d.", ch_type);
+ return 0;
+ }
for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
short a_label = dfa_state->s_arc[arc].a_lbl;
assert(a_label < _PyParser_Grammar.g_ll.ll_nlabels);
@@ -691,8 +694,10 @@ validate_node(node *tree)
const char *expected_str = _PyParser_Grammar.g_ll.ll_label[a_label].lb_str;
if (ISNONTERMINAL(next_type)) {
- PyErr_Format(parser_error, "Expected node type %d, got %d.",
- next_type, ch_type);
+ PyErr_Format(parser_error, "Expected %s, got %s.",
+ _PyParser_Grammar.g_dfa[next_type - NT_OFFSET].d_name,
+ ISTERMINAL(ch_type) ? _PyParser_TokenNames[ch_type] :
+ _PyParser_Grammar.g_dfa[ch_type - NT_OFFSET].d_name);
}
else if (expected_str != NULL) {
PyErr_Format(parser_error, "Illegal terminal: expected '%s'.",