Back to index

python3.2  3.2.2
Defines | Functions
parser.c File Reference
#include "Python.h"
#include "pgenheaders.h"
#include "token.h"
#include "grammar.h"
#include "node.h"
#include "parser.h"
#include "errcode.h"

Go to the source code of this file.

Defines

#define D(x)
#define s_empty(s)   ((s)->s_top == &(s)->s_base[MAXSTACK])
#define s_pop(s)   (s)->s_top++

Functions

static void s_reset (stack *)
static int s_push (register stack *s, dfa *d, node *parent)
parser_statePyParser_New (grammar *g, int start)
void PyParser_Delete (parser_state *ps)
static int shift (register stack *s, int type, char *str, int newstate, int lineno, int col_offset)
static int push (register stack *s, int type, dfa *d, int newstate, int lineno, int col_offset)
static int classify (parser_state *ps, int type, char *str)
int PyParser_AddToken (register parser_state *ps, register int type, char *str, int lineno, int col_offset, int *expected_ret)

Define Documentation

#define D (   x)

Definition at line 21 of file parser.c.

#define s_empty (   s)    ((s)->s_top == &(s)->s_base[MAXSTACK])

Definition at line 35 of file parser.c.

#define s_pop (   s)    (s)->s_top++

Definition at line 64 of file parser.c.


Function Documentation

static int classify ( parser_state ps,
int  type,
char *  str 
) [static]

Definition at line 137 of file parser.c.

{
    grammar *g = ps->p_grammar;
    register int n = g->g_ll.ll_nlabels;

    if (type == NAME) {
        register char *s = str;
        register label *l = g->g_ll.ll_label;
        register int i;
        for (i = n; i > 0; i--, l++) {
            if (l->lb_type != NAME || l->lb_str == NULL ||
                l->lb_str[0] != s[0] ||
                strcmp(l->lb_str, s) != 0)
                continue;
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
#if 0
            /* Leaving this in as an example */
            if (!(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) {
                if (s[0] == 'w' && strcmp(s, "with") == 0)
                    break; /* not a keyword yet */
                else if (s[0] == 'a' && strcmp(s, "as") == 0)
                    break; /* not a keyword yet */
            }
#endif
#endif
            D(printf("It's a keyword\n"));
            return n - i;
        }
    }

    {
        register label *l = g->g_ll.ll_label;
        register int i;
        for (i = n; i > 0; i--, l++) {
            if (l->lb_type == type && l->lb_str == NULL) {
                D(printf("It's a token we know\n"));
                return n - i;
            }
        }
    }

    D(printf("Illegal token\n"));
    return -1;
}

Here is the caller graph for this function:

static int push ( register stack s,
int  type,
dfa d,
int  newstate,
int  lineno,
int  col_offset 
) [static]

Definition at line 120 of file parser.c.

{
    int err;
    register node *n;
    n = s->s_top->s_parent;
    assert(!s_empty(s));
    err = PyNode_AddChild(n, type, (char *)NULL, lineno, col_offset);
    if (err)
        return err;
    s->s_top->s_state = newstate;
    return s_push(s, d, CHILD(n, NCH(n)-1));
}

Here is the call graph for this function:

Here is the caller graph for this function:

int PyParser_AddToken ( register parser_state ps,
register int  type,
char *  str,
int  lineno,
int  col_offset,
int expected_ret 
)

Definition at line 228 of file parser.c.

{
    register int ilabel;
    int err;

    D(printf("Token %s/'%s' ... ", _PyParser_TokenNames[type], str));

    /* Find out which label this token is */
    ilabel = classify(ps, type, str);
    if (ilabel < 0)
        return E_SYNTAX;

    /* Loop until the token is shifted or an error occurred */
    for (;;) {
        /* Fetch the current dfa and state */
        register dfa *d = ps->p_stack.s_top->s_dfa;
        register state *s = &d->d_state[ps->p_stack.s_top->s_state];

        D(printf(" DFA '%s', state %d:",
            d->d_name, ps->p_stack.s_top->s_state));

        /* Check accelerator */
        if (s->s_lower <= ilabel && ilabel < s->s_upper) {
            register int x = s->s_accel[ilabel - s->s_lower];
            if (x != -1) {
                if (x & (1<<7)) {
                    /* Push non-terminal */
                    int nt = (x >> 8) + NT_OFFSET;
                    int arrow = x & ((1<<7)-1);
                    dfa *d1 = PyGrammar_FindDFA(
                        ps->p_grammar, nt);
                    if ((err = push(&ps->p_stack, nt, d1,
                        arrow, lineno, col_offset)) > 0) {
                        D(printf(" MemError: push\n"));
                        return err;
                    }
                    D(printf(" Push ...\n"));
                    continue;
                }

                /* Shift the token */
                if ((err = shift(&ps->p_stack, type, str,
                                x, lineno, col_offset)) > 0) {
                    D(printf(" MemError: shift.\n"));
                    return err;
                }
                D(printf(" Shift.\n"));
                /* Pop while we are in an accept-only state */
                while (s = &d->d_state
                                [ps->p_stack.s_top->s_state],
                    s->s_accept && s->s_narcs == 1) {
                    D(printf("  DFA '%s', state %d: "
                             "Direct pop.\n",
                             d->d_name,
                             ps->p_stack.s_top->s_state));
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
#if 0
                    if (d->d_name[0] == 'i' &&
                        strcmp(d->d_name,
                           "import_stmt") == 0)
                        future_hack(ps);
#endif
#endif
                    s_pop(&ps->p_stack);
                    if (s_empty(&ps->p_stack)) {
                        D(printf("  ACCEPT.\n"));
                        return E_DONE;
                    }
                    d = ps->p_stack.s_top->s_dfa;
                }
                return E_OK;
            }
        }

        if (s->s_accept) {
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
#if 0
            if (d->d_name[0] == 'i' &&
                strcmp(d->d_name, "import_stmt") == 0)
                future_hack(ps);
#endif
#endif
            /* Pop this dfa and try again */
            s_pop(&ps->p_stack);
            D(printf(" Pop ...\n"));
            if (s_empty(&ps->p_stack)) {
                D(printf(" Error: bottom of stack.\n"));
                return E_SYNTAX;
            }
            continue;
        }

        /* Stuck, report syntax error */
        D(printf(" Error.\n"));
        if (expected_ret) {
            if (s->s_lower == s->s_upper - 1) {
                /* Only one possible expected token */
                *expected_ret = ps->p_grammar->
                    g_ll.ll_label[s->s_lower].lb_type;
            }
            else
                *expected_ret = -1;
        }
        return E_SYNTAX;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 96 of file parser.c.

{
    /* NB If you want to save the parse tree,
       you must set p_tree to NULL before calling delparser! */
    PyNode_Free(ps->p_tree);
    PyMem_FREE(ps);
}

Here is the call graph for this function:

Here is the caller graph for this function:

parser_state* PyParser_New ( grammar g,
int  start 
)

Definition at line 72 of file parser.c.

{
    parser_state *ps;

    if (!g->g_accel)
        PyGrammar_AddAccelerators(g);
    ps = (parser_state *)PyMem_MALLOC(sizeof(parser_state));
    if (ps == NULL)
        return NULL;
    ps->p_grammar = g;
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
    ps->p_flags = 0;
#endif
    ps->p_tree = PyNode_New(start);
    if (ps->p_tree == NULL) {
        PyMem_FREE(ps);
        return NULL;
    }
    s_reset(&ps->p_stack);
    (void) s_push(&ps->p_stack, PyGrammar_FindDFA(g, start), ps->p_tree);
    return ps;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int s_push ( register stack s,
dfa d,
node parent 
) [static]

Definition at line 38 of file parser.c.

{
    register stackentry *top;
    if (s->s_top == s->s_base) {
        fprintf(stderr, "s_push: parser stack overflow\n");
        return E_NOMEM;
    }
    top = --s->s_top;
    top->s_dfa = d;
    top->s_parent = parent;
    top->s_state = 0;
    return 0;
}

Here is the caller graph for this function:

static void s_reset ( stack s) [static]

Definition at line 30 of file parser.c.

{
    s->s_top = &s->s_base[MAXSTACK];
}

Here is the caller graph for this function:

static int shift ( register stack s,
int  type,
char *  str,
int  newstate,
int  lineno,
int  col_offset 
) [static]

Definition at line 108 of file parser.c.

{
    int err;
    assert(!s_empty(s));
    err = PyNode_AddChild(s->s_top->s_parent, type, str, lineno, col_offset);
    if (err)
        return err;
    s->s_top->s_state = newstate;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function: