Python-2.7.3/Parser/listnode.c

No issues found

 1 /* List a node on a file */
 2 
 3 #include "pgenheaders.h"
 4 #include "token.h"
 5 #include "node.h"
 6 
 7 /* Forward */
 8 static void list1node(FILE *, node *);
 9 static void listnode(FILE *, node *);
10 
11 void
12 PyNode_ListTree(node *n)
13 {
14     listnode(stdout, n);
15 }
16 
17 static int level, atbol;
18 
19 static void
20 listnode(FILE *fp, node *n)
21 {
22     level = 0;
23     atbol = 1;
24     list1node(fp, n);
25 }
26 
27 static void
28 list1node(FILE *fp, node *n)
29 {
30     if (n == 0)
31         return;
32     if (ISNONTERMINAL(TYPE(n))) {
33         int i;
34         for (i = 0; i < NCH(n); i++)
35             list1node(fp, CHILD(n, i));
36     }
37     else if (ISTERMINAL(TYPE(n))) {
38         switch (TYPE(n)) {
39         case INDENT:
40             ++level;
41             break;
42         case DEDENT:
43             --level;
44             break;
45         default:
46             if (atbol) {
47                 int i;
48                 for (i = 0; i < level; ++i)
49                     fprintf(fp, "\t");
50                 atbol = 0;
51             }
52             if (TYPE(n) == NEWLINE) {
53                 if (STR(n) != NULL)
54                     fprintf(fp, "%s", STR(n));
55                 fprintf(fp, "\n");
56                 atbol = 1;
57             }
58             else
59                 fprintf(fp, "%s ", STR(n));
60             break;
61         }
62     }
63     else
64         fprintf(fp, "? ");
65 }