1
2 /* Grammar subroutines needed by parser */
3
4 #include "Python.h"
5 #include "pgenheaders.h"
6 #include "grammar.h"
7 #include "token.h"
8
9 /* Return the DFA for the given type */
10
11 dfa *
PyGrammar_FindDFA(grammar * g,int type)12 PyGrammar_FindDFA(grammar *g, int type)
13 {
14 dfa *d;
15 #if 1
16 /* Massive speed-up */
17 d = &g->g_dfa[type - NT_OFFSET];
18 assert(d->d_type == type);
19 return d;
20 #else
21 /* Old, slow version */
22 int i;
23
24 for (i = g->g_ndfas, d = g->g_dfa; --i >= 0; d++) {
25 if (d->d_type == type)
26 return d;
27 }
28 Py_UNREACHABLE();
29 #endif
30 }
31
32 const char *
PyGrammar_LabelRepr(label * lb)33 PyGrammar_LabelRepr(label *lb)
34 {
35 static char buf[100];
36
37 if (lb->lb_type == ENDMARKER)
38 return "EMPTY";
39 else if (ISNONTERMINAL(lb->lb_type)) {
40 if (lb->lb_str == NULL) {
41 PyOS_snprintf(buf, sizeof(buf), "NT%d", lb->lb_type);
42 return buf;
43 }
44 else
45 return lb->lb_str;
46 }
47 else if (lb->lb_type < N_TOKENS) {
48 if (lb->lb_str == NULL)
49 return _PyParser_TokenNames[lb->lb_type];
50 else {
51 PyOS_snprintf(buf, sizeof(buf), "%.32s(%.32s)",
52 _PyParser_TokenNames[lb->lb_type], lb->lb_str);
53 return buf;
54 }
55 }
56 else {
57 Py_FatalError("invalid label");
58 return NULL;
59 }
60 }
61