Staging
v0.5.1
https://github.com/python/cpython
Revision 24a7c298d47658295673dc04d1b6d59f2b200a7c authored by Lysandros Nikolaou on 28 October 2020, 00:14:15 UTC, committed by GitHub on 28 October 2020, 00:14:15 UTC
* Implement running the parser a second time for the errors messages

The first parser run is only responsible for detecting whether
there is a `SyntaxError` or not. If there isn't the AST gets returned.
Otherwise, the parser is run a second time with all the `invalid_*`
rules enabled so that all the customized error messages get produced.

(cherry picked from commit bca701403253379409dece03053dbd739c0bd059)
1 parent c4b58ce
Raw File
Tip revision: 24a7c298d47658295673dc04d1b6d59f2b200a7c authored by Lysandros Nikolaou on 28 October 2020, 00:14:15 UTC
[3.9] bpo-42123: Run the parser two times and only enable invalid rules on the second run (GH-22111) (GH-23011)
Tip revision: 24a7c29
listnode.c

/* 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, "? ");
}
back to top