blob: 71300ae908ae155753da4b088633a296d390fa60 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* List a node on a file */
3
Guido van Rossum3f5da241990-12-20 15:06:42 +00004#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005#include "token.h"
6#include "node.h"
7
Guido van Rossum3f5da241990-12-20 15:06:42 +00008/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +00009static void list1node(FILE *, node *);
10static void listnode(FILE *, node *);
Guido van Rossum3f5da241990-12-20 15:06:42 +000011
12void
Thomas Wouters23c9e002000-07-22 19:20:54 +000013PyNode_ListTree(node *n)
Guido van Rossum3f5da241990-12-20 15:06:42 +000014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000015 listnode(stdout, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +000016}
17
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018static int level, atbol;
19
Guido van Rossum9e90a671993-06-24 11:10:19 +000020static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000021listnode(FILE *fp, node *n)
Guido van Rossum3f5da241990-12-20 15:06:42 +000022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 level = 0;
24 atbol = 1;
25 list1node(fp, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +000026}
27
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000029list1node(FILE *fp, node *n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030{
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +020031 if (n == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 return;
33 if (ISNONTERMINAL(TYPE(n))) {
34 int i;
35 for (i = 0; i < NCH(n); i++)
36 list1node(fp, CHILD(n, i));
37 }
38 else if (ISTERMINAL(TYPE(n))) {
39 switch (TYPE(n)) {
40 case INDENT:
41 ++level;
42 break;
43 case DEDENT:
44 --level;
45 break;
46 default:
47 if (atbol) {
48 int i;
49 for (i = 0; i < level; ++i)
50 fprintf(fp, "\t");
51 atbol = 0;
52 }
53 if (TYPE(n) == NEWLINE) {
54 if (STR(n) != NULL)
55 fprintf(fp, "%s", STR(n));
56 fprintf(fp, "\n");
57 atbol = 1;
58 }
59 else
60 fprintf(fp, "%s ", STR(n));
61 break;
62 }
63 }
64 else
65 fprintf(fp, "? ");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066}