blob: c806b98e48c35ed6dacc2699d76f84e1a2fd73e7 [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
Pablo Galindof2cf1e32019-04-13 17:05:14 +01004#include "Python.h"
Victor Stinnere5014be2020-04-14 17:52:15 +02005#include "pycore_interp.h" // PyInterpreterState.parser
6#include "pycore_pystate.h" // _PyInterpreterState_GET()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007#include "token.h"
8#include "node.h"
9
Guido van Rossum3f5da241990-12-20 15:06:42 +000010/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000011static void list1node(FILE *, node *);
12static void listnode(FILE *, node *);
Guido van Rossum3f5da241990-12-20 15:06:42 +000013
14void
Thomas Wouters23c9e002000-07-22 19:20:54 +000015PyNode_ListTree(node *n)
Guido van Rossum3f5da241990-12-20 15:06:42 +000016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017 listnode(stdout, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +000018}
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{
Victor Stinner81a7be32020-04-14 15:14:01 +020023 PyInterpreterState *interp = _PyInterpreterState_GET();
Vinay Sajip9def81a2019-11-07 10:08:58 +000024
25 interp->parser.listnode.level = 0;
26 interp->parser.listnode.atbol = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 list1node(fp, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +000028}
29
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000031list1node(FILE *fp, node *n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032{
Vinay Sajip9def81a2019-11-07 10:08:58 +000033 PyInterpreterState *interp;
34
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +020035 if (n == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 return;
37 if (ISNONTERMINAL(TYPE(n))) {
38 int i;
39 for (i = 0; i < NCH(n); i++)
40 list1node(fp, CHILD(n, i));
41 }
42 else if (ISTERMINAL(TYPE(n))) {
Victor Stinner81a7be32020-04-14 15:14:01 +020043 interp = _PyInterpreterState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 switch (TYPE(n)) {
45 case INDENT:
Vinay Sajip9def81a2019-11-07 10:08:58 +000046 interp->parser.listnode.level++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 break;
48 case DEDENT:
Vinay Sajip9def81a2019-11-07 10:08:58 +000049 interp->parser.listnode.level--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 break;
51 default:
Vinay Sajip9def81a2019-11-07 10:08:58 +000052 if (interp->parser.listnode.atbol) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 int i;
Vinay Sajip9def81a2019-11-07 10:08:58 +000054 for (i = 0; i < interp->parser.listnode.level; ++i)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 fprintf(fp, "\t");
Vinay Sajip9def81a2019-11-07 10:08:58 +000056 interp->parser.listnode.atbol = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 }
58 if (TYPE(n) == NEWLINE) {
59 if (STR(n) != NULL)
60 fprintf(fp, "%s", STR(n));
61 fprintf(fp, "\n");
Vinay Sajip9def81a2019-11-07 10:08:58 +000062 interp->parser.listnode.atbol = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 }
64 else
65 fprintf(fp, "%s ", STR(n));
66 break;
67 }
68 }
69 else
70 fprintf(fp, "? ");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071}