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