aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Parser/listnode.c
blob: c806b98e48c35ed6dacc2699d76f84e1a2fd73e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* List a node on a file */

#include "Python.h"
#include "pycore_interp.h"        // PyInterpreterState.parser
#include "pycore_pystate.h"       // _PyInterpreterState_GET()
#include "token.h"
#include "node.h"

/* Forward */
static void list1node(FILE *, node *);
static void listnode(FILE *, node *);

void
PyNode_ListTree(node *n)
{
    listnode(stdout, n);
}

static void
listnode(FILE *fp, node *n)
{
    PyInterpreterState *interp = _PyInterpreterState_GET();

    interp->parser.listnode.level = 0;
    interp->parser.listnode.atbol = 1;
    list1node(fp, n);
}

static void
list1node(FILE *fp, node *n)
{
    PyInterpreterState *interp;

    if (n == NULL)
        return;
    if (ISNONTERMINAL(TYPE(n))) {
        int i;
        for (i = 0; i < NCH(n); i++)
            list1node(fp, CHILD(n, i));
    }
    else if (ISTERMINAL(TYPE(n))) {
        interp = _PyInterpreterState_GET();
        switch (TYPE(n)) {
        case INDENT:
            interp->parser.listnode.level++;
            break;
        case DEDENT:
            interp->parser.listnode.level--;
            break;
        default:
            if (interp->parser.listnode.atbol) {
                int i;
                for (i = 0; i < interp->parser.listnode.level; ++i)
                    fprintf(fp, "\t");
                interp->parser.listnode.atbol = 0;
            }
            if (TYPE(n) == NEWLINE) {
                if (STR(n) != NULL)
                    fprintf(fp, "%s", STR(n));
                fprintf(fp, "\n");
                interp->parser.listnode.atbol = 1;
            }
            else
                fprintf(fp, "%s ", STR(n));
            break;
        }
    }
    else
        fprintf(fp, "? ");
}