Blame Modules/parsermodule.c

rpm-build 2bd099
/*  parsermodule.c
rpm-build 2bd099
 *
rpm-build 2bd099
 *  Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
rpm-build 2bd099
 *  Institute and State University, Blacksburg, Virginia, USA.
rpm-build 2bd099
 *  Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
rpm-build 2bd099
 *  Amsterdam, The Netherlands.  Copying is permitted under the terms
rpm-build 2bd099
 *  associated with the main Python distribution, with the additional
rpm-build 2bd099
 *  restriction that this additional notice be included and maintained
rpm-build 2bd099
 *  on all distributed copies.
rpm-build 2bd099
 *
rpm-build 2bd099
 *  This module serves to replace the original parser module written
rpm-build 2bd099
 *  by Guido.  The functionality is not matched precisely, but the
rpm-build 2bd099
 *  original may be implemented on top of this.  This is desirable
rpm-build 2bd099
 *  since the source of the text to be parsed is now divorced from
rpm-build 2bd099
 *  this interface.
rpm-build 2bd099
 *
rpm-build 2bd099
 *  Unlike the prior interface, the ability to give a parse tree
rpm-build 2bd099
 *  produced by Python code as a tuple to the compiler is enabled by
rpm-build 2bd099
 *  this module.  See the documentation for more details.
rpm-build 2bd099
 *
rpm-build 2bd099
 *  I've added some annotations that help with the lint code-checking
rpm-build 2bd099
 *  program, but they're not complete by a long shot.  The real errors
rpm-build 2bd099
 *  that lint detects are gone, but there are still warnings with
rpm-build 2bd099
 *  Py_[X]DECREF() and Py_[X]INCREF() macros.  The lint annotations
rpm-build 2bd099
 *  look like "NOTE(...)".
rpm-build 2bd099
 *
rpm-build 2bd099
 *  To debug parser errors like
rpm-build 2bd099
 *      "parser.ParserError: Expected node type 12, got 333."
rpm-build 2bd099
 *  decode symbol numbers using the automatically-generated files
rpm-build 2bd099
 *  Lib/symbol.h and Include/token.h.
rpm-build 2bd099
 */
rpm-build 2bd099
rpm-build 2bd099
#include "Python.h"                     /* general Python API             */
rpm-build 2bd099
#include "Python-ast.h"                 /* mod_ty */
rpm-build 2bd099
#include "graminit.h"                   /* symbols defined in the grammar */
rpm-build 2bd099
#include "node.h"                       /* internal parser structure      */
rpm-build 2bd099
#include "errcode.h"                    /* error codes for PyNode_*()     */
rpm-build 2bd099
#include "token.h"                      /* token definitions              */
rpm-build 2bd099
#include "grammar.h"
rpm-build 2bd099
#include "parsetok.h"
rpm-build 2bd099
                                        /* ISTERMINAL() / ISNONTERMINAL() */
rpm-build 2bd099
#undef Yield
rpm-build 2bd099
#include "ast.h"
rpm-build 2bd099
rpm-build 2bd099
extern grammar _PyParser_Grammar; /* From graminit.c */
rpm-build 2bd099
rpm-build 2bd099
#ifdef lint
rpm-build 2bd099
#include <note.h>
rpm-build 2bd099
#else
rpm-build 2bd099
#define NOTE(x)
rpm-build 2bd099
#endif
rpm-build 2bd099
rpm-build 2bd099
/*  String constants used to initialize module attributes.
rpm-build 2bd099
 *
rpm-build 2bd099
 */
rpm-build 2bd099
static const char parser_copyright_string[] =
rpm-build 2bd099
"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
rpm-build 2bd099
University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
rpm-build 2bd099
Virginia, USA.  Portions copyright 1991-1995 by Stichting Mathematisch\n\
rpm-build 2bd099
Centrum, Amsterdam, The Netherlands.";
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
PyDoc_STRVAR(parser_doc_string,
rpm-build 2bd099
"This is an interface to Python's internal parser.");
rpm-build 2bd099
rpm-build 2bd099
static const char parser_version_string[] = "0.5";
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
typedef PyObject* (*SeqMaker) (Py_ssize_t length);
rpm-build 2bd099
typedef int (*SeqInserter) (PyObject* sequence,
rpm-build 2bd099
                            Py_ssize_t index,
rpm-build 2bd099
                            PyObject* element);
rpm-build 2bd099
rpm-build 2bd099
/*  The function below is copyrighted by Stichting Mathematisch Centrum.  The
rpm-build 2bd099
 *  original copyright statement is included below, and continues to apply
rpm-build 2bd099
 *  in full to the function immediately following.  All other material is
rpm-build 2bd099
 *  original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
rpm-build 2bd099
 *  Institute and State University.  Changes were made to comply with the
rpm-build 2bd099
 *  new naming conventions.  Added arguments to provide support for creating
rpm-build 2bd099
 *  lists as well as tuples, and optionally including the line numbers.
rpm-build 2bd099
 */
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
static PyObject*
rpm-build 2bd099
node2tuple(node *n,                     /* node to convert               */
rpm-build 2bd099
           SeqMaker mkseq,              /* create sequence               */
rpm-build 2bd099
           SeqInserter addelem,         /* func. to add elem. in seq.    */
rpm-build 2bd099
           int lineno,                  /* include line numbers?         */
rpm-build 2bd099
           int col_offset)              /* include column offsets?       */
rpm-build 2bd099
{
rpm-build 2bd099
    PyObject *result = NULL, *w;
rpm-build 2bd099
rpm-build 2bd099
    if (n == NULL) {
rpm-build 2bd099
        Py_INCREF(Py_None);
rpm-build 2bd099
        return Py_None;
rpm-build 2bd099
    }
rpm-build 2bd099
rpm-build 2bd099
    if (ISNONTERMINAL(TYPE(n))) {
rpm-build 2bd099
        int i;
rpm-build 2bd099
rpm-build 2bd099
        result = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
rpm-build 2bd099
        if (result == NULL)
rpm-build 2bd099
            goto error;
rpm-build 2bd099
rpm-build 2bd099
        w = PyLong_FromLong(TYPE(n));
rpm-build 2bd099
        if (w == NULL)
rpm-build 2bd099
            goto error;
rpm-build 2bd099
        (void) addelem(result, 0, w);
rpm-build 2bd099
rpm-build 2bd099
        for (i = 0; i < NCH(n); i++) {
rpm-build 2bd099
            w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
rpm-build 2bd099
            if (w == NULL)
rpm-build 2bd099
                goto error;
rpm-build 2bd099
            (void) addelem(result, i+1, w);
rpm-build 2bd099
        }
rpm-build 2bd099
rpm-build 2bd099
        if (TYPE(n) == encoding_decl) {
rpm-build 2bd099
            w = PyUnicode_FromString(STR(n));
rpm-build 2bd099
            if (w == NULL)
rpm-build 2bd099
                goto error;
rpm-build 2bd099
            (void) addelem(result, i+1, w);
rpm-build 2bd099
        }
rpm-build 2bd099
    }
rpm-build 2bd099
    else if (ISTERMINAL(TYPE(n))) {
rpm-build 2bd099
        result = mkseq(2 + lineno + col_offset);
rpm-build 2bd099
        if (result == NULL)
rpm-build 2bd099
            goto error;
rpm-build 2bd099
rpm-build 2bd099
        w = PyLong_FromLong(TYPE(n));
rpm-build 2bd099
        if (w == NULL)
rpm-build 2bd099
            goto error;
rpm-build 2bd099
        (void) addelem(result, 0, w);
rpm-build 2bd099
rpm-build 2bd099
        w = PyUnicode_FromString(STR(n));
rpm-build 2bd099
        if (w == NULL)
rpm-build 2bd099
            goto error;
rpm-build 2bd099
        (void) addelem(result, 1, w);
rpm-build 2bd099
rpm-build 2bd099
        if (lineno == 1) {
rpm-build 2bd099
            w = PyLong_FromLong(n->n_lineno);
rpm-build 2bd099
            if (w == NULL)
rpm-build 2bd099
                goto error;
rpm-build 2bd099
            (void) addelem(result, 2, w);
rpm-build 2bd099
        }
rpm-build 2bd099
rpm-build 2bd099
        if (col_offset == 1) {
rpm-build 2bd099
            w = PyLong_FromLong(n->n_col_offset);
rpm-build 2bd099
            if (w == NULL)
rpm-build 2bd099
                goto error;
rpm-build 2bd099
            (void) addelem(result, 3, w);
rpm-build 2bd099
        }
rpm-build 2bd099
    }
rpm-build 2bd099
    else {
rpm-build 2bd099
        PyErr_SetString(PyExc_SystemError,
rpm-build 2bd099
                        "unrecognized parse tree node type");
rpm-build 2bd099
        return ((PyObject*) NULL);
rpm-build 2bd099
    }
rpm-build 2bd099
    return result;
rpm-build 2bd099
rpm-build 2bd099
error:
rpm-build 2bd099
    Py_XDECREF(result);
rpm-build 2bd099
    return NULL;
rpm-build 2bd099
}
rpm-build 2bd099
/*
rpm-build 2bd099
 *  End of material copyrighted by Stichting Mathematisch Centrum.
rpm-build 2bd099
 */
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/*  There are two types of intermediate objects we're interested in:
rpm-build 2bd099
 *  'eval' and 'exec' types.  These constants can be used in the st_type
rpm-build 2bd099
 *  field of the object type to identify which any given object represents.
rpm-build 2bd099
 *  These should probably go in an external header to allow other extensions
rpm-build 2bd099
 *  to use them, but then, we really should be using C++ too.  ;-)
rpm-build 2bd099
 */
rpm-build 2bd099
rpm-build 2bd099
#define PyST_EXPR  1
rpm-build 2bd099
#define PyST_SUITE 2
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/*  These are the internal objects and definitions required to implement the
rpm-build 2bd099
 *  ST type.  Most of the internal names are more reminiscent of the 'old'
rpm-build 2bd099
 *  naming style, but the code uses the new naming convention.
rpm-build 2bd099
 */
rpm-build 2bd099
rpm-build 2bd099
static PyObject*
rpm-build 2bd099
parser_error = 0;
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
typedef struct {
rpm-build 2bd099
    PyObject_HEAD                       /* standard object header           */
rpm-build 2bd099
    node* st_node;                      /* the node* returned by the parser */
rpm-build 2bd099
    int   st_type;                      /* EXPR or SUITE ?                  */
rpm-build 2bd099
    PyCompilerFlags st_flags;           /* Parser and compiler flags        */
rpm-build 2bd099
} PyST_Object;
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
static void parser_free(PyST_Object *st);
rpm-build 2bd099
static PyObject* parser_sizeof(PyST_Object *, void *);
rpm-build 2bd099
static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op);
rpm-build 2bd099
static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
rpm-build 2bd099
static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
rpm-build 2bd099
static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
rpm-build 2bd099
static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
rpm-build 2bd099
static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
rpm-build 2bd099
rpm-build 2bd099
#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
rpm-build 2bd099
rpm-build 2bd099
static PyMethodDef parser_methods[] = {
rpm-build 2bd099
    {"compile",         (PyCFunction)parser_compilest,  PUBLIC_METHOD_TYPE,
rpm-build 2bd099
        PyDoc_STR("Compile this ST object into a code object.")},
rpm-build 2bd099
    {"isexpr",          (PyCFunction)parser_isexpr,     PUBLIC_METHOD_TYPE,
rpm-build 2bd099
        PyDoc_STR("Determines if this ST object was created from an expression.")},
rpm-build 2bd099
    {"issuite",         (PyCFunction)parser_issuite,    PUBLIC_METHOD_TYPE,
rpm-build 2bd099
        PyDoc_STR("Determines if this ST object was created from a suite.")},
rpm-build 2bd099
    {"tolist",          (PyCFunction)parser_st2list,    PUBLIC_METHOD_TYPE,
rpm-build 2bd099
        PyDoc_STR("Creates a list-tree representation of this ST.")},
rpm-build 2bd099
    {"totuple",         (PyCFunction)parser_st2tuple,   PUBLIC_METHOD_TYPE,
rpm-build 2bd099
        PyDoc_STR("Creates a tuple-tree representation of this ST.")},
rpm-build 2bd099
    {"__sizeof__",      (PyCFunction)parser_sizeof,     METH_NOARGS,
rpm-build 2bd099
        PyDoc_STR("Returns size in memory, in bytes.")},
rpm-build 2bd099
    {NULL, NULL, 0, NULL}
rpm-build 2bd099
};
rpm-build 2bd099
rpm-build 2bd099
static
rpm-build 2bd099
PyTypeObject PyST_Type = {
rpm-build 2bd099
    PyVarObject_HEAD_INIT(NULL, 0)
rpm-build 2bd099
    "parser.st",                        /* tp_name              */
rpm-build 2bd099
    (int) sizeof(PyST_Object),          /* tp_basicsize         */
rpm-build 2bd099
    0,                                  /* tp_itemsize          */
rpm-build 2bd099
    (destructor)parser_free,            /* tp_dealloc           */
rpm-build 2bd099
    0,                                  /* tp_print             */
rpm-build 2bd099
    0,                                  /* tp_getattr           */
rpm-build 2bd099
    0,                                  /* tp_setattr           */
rpm-build 2bd099
    0,                                  /* tp_reserved          */
rpm-build 2bd099
    0,                                  /* tp_repr              */
rpm-build 2bd099
    0,                                  /* tp_as_number         */
rpm-build 2bd099
    0,                                  /* tp_as_sequence       */
rpm-build 2bd099
    0,                                  /* tp_as_mapping        */
rpm-build 2bd099
    0,                                  /* tp_hash              */
rpm-build 2bd099
    0,                                  /* tp_call              */
rpm-build 2bd099
    0,                                  /* tp_str               */
rpm-build 2bd099
    0,                                  /* tp_getattro          */
rpm-build 2bd099
    0,                                  /* tp_setattro          */
rpm-build 2bd099
rpm-build 2bd099
    /* Functions to access object as input/output buffer */
rpm-build 2bd099
    0,                                  /* tp_as_buffer         */
rpm-build 2bd099
rpm-build 2bd099
    Py_TPFLAGS_DEFAULT,                 /* tp_flags             */
rpm-build 2bd099
rpm-build 2bd099
    /* __doc__ */
rpm-build 2bd099
    "Intermediate representation of a Python parse tree.",
rpm-build 2bd099
    0,                                  /* tp_traverse */
rpm-build 2bd099
    0,                                  /* tp_clear */
rpm-build 2bd099
    parser_richcompare,                 /* tp_richcompare */
rpm-build 2bd099
    0,                                  /* tp_weaklistoffset */
rpm-build 2bd099
    0,                                  /* tp_iter */
rpm-build 2bd099
    0,                                  /* tp_iternext */
rpm-build 2bd099
    parser_methods,                     /* tp_methods */
rpm-build 2bd099
};  /* PyST_Type */
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/* PyST_Type isn't subclassable, so just check ob_type */
rpm-build 2bd099
#define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type)
rpm-build 2bd099
rpm-build 2bd099
static int
rpm-build 2bd099
parser_compare_nodes(node *left, node *right)
rpm-build 2bd099
{
rpm-build 2bd099
    int j;
rpm-build 2bd099
rpm-build 2bd099
    if (TYPE(left) < TYPE(right))
rpm-build 2bd099
        return (-1);
rpm-build 2bd099
rpm-build 2bd099
    if (TYPE(right) < TYPE(left))
rpm-build 2bd099
        return (1);
rpm-build 2bd099
rpm-build 2bd099
    if (ISTERMINAL(TYPE(left)))
rpm-build 2bd099
        return (strcmp(STR(left), STR(right)));
rpm-build 2bd099
rpm-build 2bd099
    if (NCH(left) < NCH(right))
rpm-build 2bd099
        return (-1);
rpm-build 2bd099
rpm-build 2bd099
    if (NCH(right) < NCH(left))
rpm-build 2bd099
        return (1);
rpm-build 2bd099
rpm-build 2bd099
    for (j = 0; j < NCH(left); ++j) {
rpm-build 2bd099
        int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
rpm-build 2bd099
rpm-build 2bd099
        if (v != 0)
rpm-build 2bd099
            return (v);
rpm-build 2bd099
    }
rpm-build 2bd099
    return (0);
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
/*  parser_richcompare(PyObject* left, PyObject* right, int op)
rpm-build 2bd099
 *
rpm-build 2bd099
 *  Comparison function used by the Python operators ==, !=, <, >, <=, >=
rpm-build 2bd099
 *  This really just wraps a call to parser_compare_nodes() with some easy
rpm-build 2bd099
 *  checks and protection code.
rpm-build 2bd099
 *
rpm-build 2bd099
 */
rpm-build 2bd099
rpm-build 2bd099
#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
rpm-build 2bd099
rpm-build 2bd099
static PyObject *
rpm-build 2bd099
parser_richcompare(PyObject *left, PyObject *right, int op)
rpm-build 2bd099
{
rpm-build 2bd099
    int result;
rpm-build 2bd099
    PyObject *v;
rpm-build 2bd099
rpm-build 2bd099
    /* neither argument should be NULL, unless something's gone wrong */
rpm-build 2bd099
    if (left == NULL || right == NULL) {
rpm-build 2bd099
        PyErr_BadInternalCall();
rpm-build 2bd099
        return NULL;
rpm-build 2bd099
    }
rpm-build 2bd099
rpm-build 2bd099
    /* both arguments should be instances of PyST_Object */
rpm-build 2bd099
    if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
rpm-build 2bd099
        v = Py_NotImplemented;
rpm-build 2bd099
        goto finished;
rpm-build 2bd099
    }
rpm-build 2bd099
rpm-build 2bd099
    if (left == right)
rpm-build 2bd099
        /* if arguments are identical, they're equal */
rpm-build 2bd099
        result = 0;
rpm-build 2bd099
    else
rpm-build 2bd099
        result = parser_compare_nodes(((PyST_Object *)left)->st_node,
rpm-build 2bd099
                                      ((PyST_Object *)right)->st_node);
rpm-build 2bd099
rpm-build 2bd099
    /* Convert return value to a Boolean */
rpm-build 2bd099
    switch (op) {
rpm-build 2bd099
      case Py_EQ:
rpm-build 2bd099
        v = TEST_COND(result == 0);
rpm-build 2bd099
        break;
rpm-build 2bd099
      case Py_NE:
rpm-build 2bd099
        v = TEST_COND(result != 0);
rpm-build 2bd099
        break;
rpm-build 2bd099
      case Py_LE:
rpm-build 2bd099
        v = TEST_COND(result <= 0);
rpm-build 2bd099
        break;
rpm-build 2bd099
      case Py_GE:
rpm-build 2bd099
        v = TEST_COND(result >= 0);
rpm-build 2bd099
        break;
rpm-build 2bd099
      case Py_LT:
rpm-build 2bd099
        v = TEST_COND(result < 0);
rpm-build 2bd099
        break;
rpm-build 2bd099
      case Py_GT:
rpm-build 2bd099
        v = TEST_COND(result > 0);
rpm-build 2bd099
        break;
rpm-build 2bd099
      default:
rpm-build 2bd099
        PyErr_BadArgument();
rpm-build 2bd099
        return NULL;
rpm-build 2bd099
    }
rpm-build 2bd099
  finished:
rpm-build 2bd099
    Py_INCREF(v);
rpm-build 2bd099
    return v;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
/*  parser_newstobject(node* st)
rpm-build 2bd099
 *
rpm-build 2bd099
 *  Allocates a new Python object representing an ST.  This is simply the
rpm-build 2bd099
 *  'wrapper' object that holds a node* and allows it to be passed around in
rpm-build 2bd099
 *  Python code.
rpm-build 2bd099
 *
rpm-build 2bd099
 */
rpm-build 2bd099
static PyObject*
rpm-build 2bd099
parser_newstobject(node *st, int type)
rpm-build 2bd099
{
rpm-build 2bd099
    PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
rpm-build 2bd099
rpm-build 2bd099
    if (o != 0) {
rpm-build 2bd099
        o->st_node = st;
rpm-build 2bd099
        o->st_type = type;
rpm-build 2bd099
        o->st_flags.cf_flags = 0;
rpm-build 2bd099
    }
rpm-build 2bd099
    else {
rpm-build 2bd099
        PyNode_Free(st);
rpm-build 2bd099
    }
rpm-build 2bd099
    return ((PyObject*)o);
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/*  void parser_free(PyST_Object* st)
rpm-build 2bd099
 *
rpm-build 2bd099
 *  This is called by a del statement that reduces the reference count to 0.
rpm-build 2bd099
 *
rpm-build 2bd099
 */
rpm-build 2bd099
static void
rpm-build 2bd099
parser_free(PyST_Object *st)
rpm-build 2bd099
{
rpm-build 2bd099
    PyNode_Free(st->st_node);
rpm-build 2bd099
    PyObject_Del(st);
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
static PyObject *
rpm-build 2bd099
parser_sizeof(PyST_Object *st, void *unused)
rpm-build 2bd099
{
rpm-build 2bd099
    Py_ssize_t res;
rpm-build 2bd099
rpm-build 2bd099
    res = _PyObject_SIZE(Py_TYPE(st)) + _PyNode_SizeOf(st->st_node);
rpm-build 2bd099
    return PyLong_FromSsize_t(res);
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/*  parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
rpm-build 2bd099
 *
rpm-build 2bd099
 *  This provides conversion from a node* to a tuple object that can be
rpm-build 2bd099
 *  returned to the Python-level caller.  The ST object is not modified.
rpm-build 2bd099
 *
rpm-build 2bd099
 */
rpm-build 2bd099
static PyObject*
rpm-build 2bd099
parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
rpm-build 2bd099
{
rpm-build 2bd099
    int line_info = 0;
rpm-build 2bd099
    int col_info = 0;
rpm-build 2bd099
    PyObject *res = 0;
rpm-build 2bd099
    int ok;
rpm-build 2bd099
rpm-build 2bd099
    static char *keywords[] = {"st", "line_info", "col_info", NULL};
rpm-build 2bd099
rpm-build 2bd099
    if (self == NULL || PyModule_Check(self)) {
rpm-build 2bd099
        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2tuple", keywords,
rpm-build 2bd099
                                         &PyST_Type, &self, &line_info,
rpm-build 2bd099
                                         &col_info);
rpm-build 2bd099
    }
rpm-build 2bd099
    else
rpm-build 2bd099
        ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:totuple", &keywords[1],
rpm-build 2bd099
                                         &line_info, &col_info);
rpm-build 2bd099
    if (ok != 0) {
rpm-build 2bd099
        /*
rpm-build 2bd099
         *  Convert ST into a tuple representation.  Use Guido's function,
rpm-build 2bd099
         *  since it's known to work already.
rpm-build 2bd099
         */
rpm-build 2bd099
        res = node2tuple(((PyST_Object*)self)->st_node,
rpm-build 2bd099
                         PyTuple_New, PyTuple_SetItem, line_info, col_info);
rpm-build 2bd099
    }
rpm-build 2bd099
    return (res);
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/*  parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
rpm-build 2bd099
 *
rpm-build 2bd099
 *  This provides conversion from a node* to a list object that can be
rpm-build 2bd099
 *  returned to the Python-level caller.  The ST object is not modified.
rpm-build 2bd099
 *
rpm-build 2bd099
 */
rpm-build 2bd099
static PyObject*
rpm-build 2bd099
parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
rpm-build 2bd099
{
rpm-build 2bd099
    int line_info = 0;
rpm-build 2bd099
    int col_info = 0;
rpm-build 2bd099
    PyObject *res = 0;
rpm-build 2bd099
    int ok;
rpm-build 2bd099
rpm-build 2bd099
    static char *keywords[] = {"st", "line_info", "col_info", NULL};
rpm-build 2bd099
rpm-build 2bd099
    if (self == NULL || PyModule_Check(self))
rpm-build 2bd099
        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2list", keywords,
rpm-build 2bd099
                                         &PyST_Type, &self, &line_info,
rpm-build 2bd099
                                         &col_info);
rpm-build 2bd099
    else
rpm-build 2bd099
        ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:tolist", &keywords[1],
rpm-build 2bd099
                                         &line_info, &col_info);
rpm-build 2bd099
    if (ok) {
rpm-build 2bd099
        /*
rpm-build 2bd099
         *  Convert ST into a tuple representation.  Use Guido's function,
rpm-build 2bd099
         *  since it's known to work already.
rpm-build 2bd099
         */
rpm-build 2bd099
        res = node2tuple(self->st_node,
rpm-build 2bd099
                         PyList_New, PyList_SetItem, line_info, col_info);
rpm-build 2bd099
    }
rpm-build 2bd099
    return (res);
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/*  parser_compilest(PyObject* self, PyObject* args)
rpm-build 2bd099
 *
rpm-build 2bd099
 *  This function creates code objects from the parse tree represented by
rpm-build 2bd099
 *  the passed-in data object.  An optional file name is passed in as well.
rpm-build 2bd099
 *
rpm-build 2bd099
 */
rpm-build 2bd099
static PyObject*
rpm-build 2bd099
parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
rpm-build 2bd099
{
rpm-build 2bd099
    PyObject*     res = NULL;
rpm-build 2bd099
    PyArena*      arena = NULL;
rpm-build 2bd099
    mod_ty        mod;
rpm-build 2bd099
    PyObject*     filename = NULL;
rpm-build 2bd099
    int ok;
rpm-build 2bd099
rpm-build 2bd099
    static char *keywords[] = {"st", "filename", NULL};
rpm-build 2bd099
rpm-build 2bd099
    if (self == NULL || PyModule_Check(self))
rpm-build 2bd099
        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O&:compilest", keywords,
rpm-build 2bd099
                                         &PyST_Type, &self,
rpm-build 2bd099
                                         PyUnicode_FSDecoder, &filename);
rpm-build 2bd099
    else
rpm-build 2bd099
        ok = PyArg_ParseTupleAndKeywords(args, kw, "|O&:compile", &keywords[1],
rpm-build 2bd099
                                         PyUnicode_FSDecoder, &filename);
rpm-build 2bd099
    if (!ok)
rpm-build 2bd099
        goto error;
rpm-build 2bd099
rpm-build 2bd099
    if (filename == NULL) {
rpm-build 2bd099
        filename = PyUnicode_FromString("<syntax-tree>");
rpm-build 2bd099
        if (filename == NULL)
rpm-build 2bd099
            goto error;
rpm-build 2bd099
    }
rpm-build 2bd099
rpm-build 2bd099
    arena = PyArena_New();
rpm-build 2bd099
    if (!arena)
rpm-build 2bd099
        goto error;
rpm-build 2bd099
rpm-build 2bd099
    mod = PyAST_FromNodeObject(self->st_node, &self->st_flags,
rpm-build 2bd099
                               filename, arena);
rpm-build 2bd099
    if (!mod)
rpm-build 2bd099
        goto error;
rpm-build 2bd099
rpm-build 2bd099
    res = (PyObject *)PyAST_CompileObject(mod, filename,
rpm-build 2bd099
                                          &self->st_flags, -1, arena);
rpm-build 2bd099
error:
rpm-build 2bd099
    Py_XDECREF(filename);
rpm-build 2bd099
    if (arena != NULL)
rpm-build 2bd099
        PyArena_Free(arena);
rpm-build 2bd099
    return res;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/*  PyObject* parser_isexpr(PyObject* self, PyObject* args)
rpm-build 2bd099
 *  PyObject* parser_issuite(PyObject* self, PyObject* args)
rpm-build 2bd099
 *
rpm-build 2bd099
 *  Checks the passed-in ST object to determine if it is an expression or
rpm-build 2bd099
 *  a statement suite, respectively.  The return is a Python truth value.
rpm-build 2bd099
 *
rpm-build 2bd099
 */
rpm-build 2bd099
static PyObject*
rpm-build 2bd099
parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
rpm-build 2bd099
{
rpm-build 2bd099
    PyObject* res = 0;
rpm-build 2bd099
    int ok;
rpm-build 2bd099
rpm-build 2bd099
    static char *keywords[] = {"st", NULL};
rpm-build 2bd099
rpm-build 2bd099
    if (self == NULL || PyModule_Check(self))
rpm-build 2bd099
        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
rpm-build 2bd099
                                         &PyST_Type, &self);
rpm-build 2bd099
    else
rpm-build 2bd099
        ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
rpm-build 2bd099
rpm-build 2bd099
    if (ok) {
rpm-build 2bd099
        /* Check to see if the ST represents an expression or not. */
rpm-build 2bd099
        res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
rpm-build 2bd099
        Py_INCREF(res);
rpm-build 2bd099
    }
rpm-build 2bd099
    return (res);
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
static PyObject*
rpm-build 2bd099
parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
rpm-build 2bd099
{
rpm-build 2bd099
    PyObject* res = 0;
rpm-build 2bd099
    int ok;
rpm-build 2bd099
rpm-build 2bd099
    static char *keywords[] = {"st", NULL};
rpm-build 2bd099
rpm-build 2bd099
    if (self == NULL || PyModule_Check(self))
rpm-build 2bd099
        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
rpm-build 2bd099
                                         &PyST_Type, &self);
rpm-build 2bd099
    else
rpm-build 2bd099
        ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
rpm-build 2bd099
rpm-build 2bd099
    if (ok) {
rpm-build 2bd099
        /* Check to see if the ST represents an expression or not. */
rpm-build 2bd099
        res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
rpm-build 2bd099
        Py_INCREF(res);
rpm-build 2bd099
    }
rpm-build 2bd099
    return (res);
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/*  err_string(const char* message)
rpm-build 2bd099
 *
rpm-build 2bd099
 *  Sets the error string for an exception of type ParserError.
rpm-build 2bd099
 *
rpm-build 2bd099
 */
rpm-build 2bd099
static void
rpm-build 2bd099
err_string(const char *message)
rpm-build 2bd099
{
rpm-build 2bd099
    PyErr_SetString(parser_error, message);
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/*  PyObject* parser_do_parse(PyObject* args, int type)
rpm-build 2bd099
 *
rpm-build 2bd099
 *  Internal function to actually execute the parse and return the result if
rpm-build 2bd099
 *  successful or set an exception if not.
rpm-build 2bd099
 *
rpm-build 2bd099
 */
rpm-build 2bd099
static PyObject*
rpm-build 2bd099
parser_do_parse(PyObject *args, PyObject *kw, const char *argspec, int type)
rpm-build 2bd099
{
rpm-build 2bd099
    char*     string = 0;
rpm-build 2bd099
    PyObject* res    = 0;
rpm-build 2bd099
    int flags        = 0;
rpm-build 2bd099
    perrdetail err;
rpm-build 2bd099
rpm-build 2bd099
    static char *keywords[] = {"source", NULL};
rpm-build 2bd099
rpm-build 2bd099
    if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
rpm-build 2bd099
        node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
rpm-build 2bd099
                                                       &_PyParser_Grammar,
rpm-build 2bd099
                                                      (type == PyST_EXPR)
rpm-build 2bd099
                                                      ? eval_input : file_input,
rpm-build 2bd099
                                                      &err, &flags);
rpm-build 2bd099
rpm-build 2bd099
        if (n) {
rpm-build 2bd099
            res = parser_newstobject(n, type);
rpm-build 2bd099
            if (res)
rpm-build 2bd099
                ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
rpm-build 2bd099
        }
rpm-build 2bd099
        else {
rpm-build 2bd099
            PyParser_SetError(&err;;
rpm-build 2bd099
        }
rpm-build 2bd099
        PyParser_ClearError(&err;;
rpm-build 2bd099
    }
rpm-build 2bd099
    return (res);
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/*  PyObject* parser_expr(PyObject* self, PyObject* args)
rpm-build 2bd099
 *  PyObject* parser_suite(PyObject* self, PyObject* args)
rpm-build 2bd099
 *
rpm-build 2bd099
 *  External interfaces to the parser itself.  Which is called determines if
rpm-build 2bd099
 *  the parser attempts to recognize an expression ('eval' form) or statement
rpm-build 2bd099
 *  suite ('exec' form).  The real work is done by parser_do_parse() above.
rpm-build 2bd099
 *
rpm-build 2bd099
 */
rpm-build 2bd099
static PyObject*
rpm-build 2bd099
parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
rpm-build 2bd099
{
rpm-build 2bd099
    NOTE(ARGUNUSED(self))
rpm-build 2bd099
    return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
static PyObject*
rpm-build 2bd099
parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
rpm-build 2bd099
{
rpm-build 2bd099
    NOTE(ARGUNUSED(self))
rpm-build 2bd099
    return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/*  This is the messy part of the code.  Conversion from a tuple to an ST
rpm-build 2bd099
 *  object requires that the input tuple be valid without having to rely on
rpm-build 2bd099
 *  catching an exception from the compiler.  This is done to allow the
rpm-build 2bd099
 *  compiler itself to remain fast, since most of its input will come from
rpm-build 2bd099
 *  the parser directly, and therefore be known to be syntactically correct.
rpm-build 2bd099
 *  This validation is done to ensure that we don't core dump the compile
rpm-build 2bd099
 *  phase, returning an exception instead.
rpm-build 2bd099
 *
rpm-build 2bd099
 *  Two aspects can be broken out in this code:  creating a node tree from
rpm-build 2bd099
 *  the tuple passed in, and verifying that it is indeed valid.  It may be
rpm-build 2bd099
 *  advantageous to expand the number of ST types to include funcdefs and
rpm-build 2bd099
 *  lambdadefs to take advantage of the optimizer, recognizing those STs
rpm-build 2bd099
 *  here.  They are not necessary, and not quite as useful in a raw form.
rpm-build 2bd099
 *  For now, let's get expressions and suites working reliably.
rpm-build 2bd099
 */
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
static node* build_node_tree(PyObject *tuple);
rpm-build 2bd099
rpm-build 2bd099
static int
rpm-build 2bd099
validate_node(node *tree)
rpm-build 2bd099
{
rpm-build 2bd099
    int type = TYPE(tree);
rpm-build 2bd099
    int nch = NCH(tree);
rpm-build 2bd099
    dfa *nt_dfa;
rpm-build 2bd099
    state *dfa_state;
rpm-build 2bd099
    int pos, arc;
rpm-build 2bd099
rpm-build 2bd099
    assert(ISNONTERMINAL(type));
rpm-build 2bd099
    type -= NT_OFFSET;
rpm-build 2bd099
    if (type >= _PyParser_Grammar.g_ndfas) {
rpm-build 2bd099
        PyErr_Format(parser_error, "Unrecognized node type %d.", TYPE(tree));
rpm-build 2bd099
        return 0;
rpm-build 2bd099
    }
rpm-build 2bd099
    nt_dfa = &_PyParser_Grammar.g_dfa[type];
rpm-build 2bd099
    REQ(tree, nt_dfa->d_type);
rpm-build 2bd099
rpm-build 2bd099
    /* Run the DFA for this nonterminal. */
rpm-build 2bd099
    dfa_state = &nt_dfa->d_state[nt_dfa->d_initial];
rpm-build 2bd099
    for (pos = 0; pos < nch; ++pos) {
rpm-build 2bd099
        node *ch = CHILD(tree, pos);
rpm-build 2bd099
        int ch_type = TYPE(ch);
rpm-build 2bd099
        for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
rpm-build 2bd099
            short a_label = dfa_state->s_arc[arc].a_lbl;
rpm-build 2bd099
            assert(a_label < _PyParser_Grammar.g_ll.ll_nlabels);
rpm-build 2bd099
            if (_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type) {
rpm-build 2bd099
                /* The child is acceptable; if non-terminal, validate it recursively. */
rpm-build 2bd099
                if (ISNONTERMINAL(ch_type) && !validate_node(ch))
rpm-build 2bd099
                    return 0;
rpm-build 2bd099
rpm-build 2bd099
                /* Update the state, and move on to the next child. */
rpm-build 2bd099
                dfa_state = &nt_dfa->d_state[dfa_state->s_arc[arc].a_arrow];
rpm-build 2bd099
                goto arc_found;
rpm-build 2bd099
            }
rpm-build 2bd099
        }
rpm-build 2bd099
        /* What would this state have accepted? */
rpm-build 2bd099
        {
rpm-build 2bd099
            short a_label = dfa_state->s_arc->a_lbl;
rpm-build 2bd099
            int next_type;
rpm-build 2bd099
            if (!a_label) /* Wouldn't accept any more children */
rpm-build 2bd099
                goto illegal_num_children;
rpm-build 2bd099
rpm-build 2bd099
            next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type;
rpm-build 2bd099
            if (ISNONTERMINAL(next_type))
rpm-build 2bd099
                PyErr_Format(parser_error, "Expected node type %d, got %d.",
rpm-build 2bd099
                             next_type, ch_type);
rpm-build 2bd099
            else
rpm-build 2bd099
                PyErr_Format(parser_error, "Illegal terminal: expected %s.",
rpm-build 2bd099
                             _PyParser_TokenNames[next_type]);
rpm-build 2bd099
            return 0;
rpm-build 2bd099
        }
rpm-build 2bd099
rpm-build 2bd099
arc_found:
rpm-build 2bd099
        continue;
rpm-build 2bd099
    }
rpm-build 2bd099
    /* Are we in a final state? If so, return 1 for successful validation. */
rpm-build 2bd099
    for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
rpm-build 2bd099
        if (!dfa_state->s_arc[arc].a_lbl) {
rpm-build 2bd099
            return 1;
rpm-build 2bd099
        }
rpm-build 2bd099
    }
rpm-build 2bd099
rpm-build 2bd099
illegal_num_children:
rpm-build 2bd099
    PyErr_Format(parser_error,
rpm-build 2bd099
                 "Illegal number of children for %s node.", nt_dfa->d_name);
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
/*  PyObject* parser_tuple2st(PyObject* self, PyObject* args)
rpm-build 2bd099
 *
rpm-build 2bd099
 *  This is the public function, called from the Python code.  It receives a
rpm-build 2bd099
 *  single tuple object from the caller, and creates an ST object if the
rpm-build 2bd099
 *  tuple can be validated.  It does this by checking the first code of the
rpm-build 2bd099
 *  tuple, and, if acceptable, builds the internal representation.  If this
rpm-build 2bd099
 *  step succeeds, the internal representation is validated as fully as
rpm-build 2bd099
 *  possible with the recursive validate_node() routine defined above.
rpm-build 2bd099
 *
rpm-build 2bd099
 *  This function must be changed if support is to be added for PyST_FRAGMENT
rpm-build 2bd099
 *  ST objects.
rpm-build 2bd099
 *
rpm-build 2bd099
 */
rpm-build 2bd099
static PyObject*
rpm-build 2bd099
parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
rpm-build 2bd099
{
rpm-build 2bd099
    NOTE(ARGUNUSED(self))
rpm-build 2bd099
    PyObject *st = 0;
rpm-build 2bd099
    PyObject *tuple;
rpm-build 2bd099
    node *tree;
rpm-build 2bd099
rpm-build 2bd099
    static char *keywords[] = {"sequence", NULL};
rpm-build 2bd099
rpm-build 2bd099
    if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
rpm-build 2bd099
                                     &tuple))
rpm-build 2bd099
        return (0);
rpm-build 2bd099
    if (!PySequence_Check(tuple)) {
rpm-build 2bd099
        PyErr_SetString(PyExc_ValueError,
rpm-build 2bd099
                        "sequence2st() requires a single sequence argument");
rpm-build 2bd099
        return (0);
rpm-build 2bd099
    }
rpm-build 2bd099
    /*
rpm-build 2bd099
     *  Convert the tree to the internal form before checking it.
rpm-build 2bd099
     */
rpm-build 2bd099
    tree = build_node_tree(tuple);
rpm-build 2bd099
    if (tree != 0) {
rpm-build 2bd099
        node *validation_root = NULL;
rpm-build 2bd099
        int tree_type = 0;
rpm-build 2bd099
        switch (TYPE(tree)) {
rpm-build 2bd099
        case eval_input:
rpm-build 2bd099
            /*  Might be an eval form.  */
rpm-build 2bd099
            tree_type = PyST_EXPR;
rpm-build 2bd099
            validation_root = tree;
rpm-build 2bd099
            break;
rpm-build 2bd099
        case encoding_decl:
rpm-build 2bd099
            /* This looks like an encoding_decl so far. */
rpm-build 2bd099
            if (NCH(tree) == 1) {
rpm-build 2bd099
                tree_type = PyST_SUITE;
rpm-build 2bd099
                validation_root = CHILD(tree, 0);
rpm-build 2bd099
            }
rpm-build 2bd099
            else {
rpm-build 2bd099
                err_string("Error Parsing encoding_decl");
rpm-build 2bd099
            }
rpm-build 2bd099
            break;
rpm-build 2bd099
        case file_input:
rpm-build 2bd099
            /*  This looks like an exec form so far.  */
rpm-build 2bd099
            tree_type = PyST_SUITE;
rpm-build 2bd099
            validation_root = tree;
rpm-build 2bd099
            break;
rpm-build 2bd099
        default:
rpm-build 2bd099
            /*  This is a fragment, at best. */
rpm-build 2bd099
            err_string("parse tree does not use a valid start symbol");
rpm-build 2bd099
        }
rpm-build 2bd099
rpm-build 2bd099
        if (validation_root != NULL && validate_node(validation_root))
rpm-build 2bd099
            st = parser_newstobject(tree, tree_type);
rpm-build 2bd099
        else
rpm-build 2bd099
            PyNode_Free(tree);
rpm-build 2bd099
    }
rpm-build 2bd099
    /*  Make sure we raise an exception on all errors.  We should never
rpm-build 2bd099
     *  get this, but we'd do well to be sure something is done.
rpm-build 2bd099
     */
rpm-build 2bd099
    if (st == NULL && !PyErr_Occurred())
rpm-build 2bd099
        err_string("unspecified ST error occurred");
rpm-build 2bd099
rpm-build 2bd099
    return st;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/*  node* build_node_children()
rpm-build 2bd099
 *
rpm-build 2bd099
 *  Iterate across the children of the current non-terminal node and build
rpm-build 2bd099
 *  their structures.  If successful, return the root of this portion of
rpm-build 2bd099
 *  the tree, otherwise, 0.  Any required exception will be specified already,
rpm-build 2bd099
 *  and no memory will have been deallocated.
rpm-build 2bd099
 *
rpm-build 2bd099
 */
rpm-build 2bd099
static node*
rpm-build 2bd099
build_node_children(PyObject *tuple, node *root, int *line_num)
rpm-build 2bd099
{
rpm-build 2bd099
    Py_ssize_t len = PyObject_Size(tuple);
rpm-build 2bd099
    Py_ssize_t i;
rpm-build 2bd099
    int  err;
rpm-build 2bd099
rpm-build 2bd099
    if (len < 0) {
rpm-build 2bd099
        return NULL;
rpm-build 2bd099
    }
rpm-build 2bd099
    for (i = 1; i < len; ++i) {
rpm-build 2bd099
        /* elem must always be a sequence, however simple */
rpm-build 2bd099
        PyObject* elem = PySequence_GetItem(tuple, i);
rpm-build 2bd099
        int ok = elem != NULL;
rpm-build 2bd099
        int type = 0;
rpm-build 2bd099
        char *strn = 0;
rpm-build 2bd099
rpm-build 2bd099
        if (ok)
rpm-build 2bd099
            ok = PySequence_Check(elem);
rpm-build 2bd099
        if (ok) {
rpm-build 2bd099
            PyObject *temp = PySequence_GetItem(elem, 0);
rpm-build 2bd099
            if (temp == NULL)
rpm-build 2bd099
                ok = 0;
rpm-build 2bd099
            else {
rpm-build 2bd099
                ok = PyLong_Check(temp);
rpm-build 2bd099
                if (ok) {
rpm-build 2bd099
                    type = _PyLong_AsInt(temp);
rpm-build 2bd099
                    if (type == -1 && PyErr_Occurred()) {
rpm-build 2bd099
                        Py_DECREF(temp);
rpm-build 2bd099
                        Py_DECREF(elem);
rpm-build 2bd099
                        return NULL;
rpm-build 2bd099
                    }
rpm-build 2bd099
                }
rpm-build 2bd099
                Py_DECREF(temp);
rpm-build 2bd099
            }
rpm-build 2bd099
        }
rpm-build 2bd099
        if (!ok) {
rpm-build 2bd099
            PyObject *err = Py_BuildValue("Os", elem,
rpm-build 2bd099
                                          "Illegal node construct.");
rpm-build 2bd099
            PyErr_SetObject(parser_error, err);
rpm-build 2bd099
            Py_XDECREF(err);
rpm-build 2bd099
            Py_XDECREF(elem);
rpm-build 2bd099
            return NULL;
rpm-build 2bd099
        }
rpm-build 2bd099
        if (ISTERMINAL(type)) {
rpm-build 2bd099
            Py_ssize_t len = PyObject_Size(elem);
rpm-build 2bd099
            PyObject *temp;
rpm-build 2bd099
            const char *temp_str;
rpm-build 2bd099
rpm-build 2bd099
            if ((len != 2) && (len != 3)) {
rpm-build 2bd099
                err_string("terminal nodes must have 2 or 3 entries");
rpm-build 2bd099
                Py_DECREF(elem);
rpm-build 2bd099
                return NULL;
rpm-build 2bd099
            }
rpm-build 2bd099
            temp = PySequence_GetItem(elem, 1);
rpm-build 2bd099
            if (temp == NULL) {
rpm-build 2bd099
                Py_DECREF(elem);
rpm-build 2bd099
                return NULL;
rpm-build 2bd099
            }
rpm-build 2bd099
            if (!PyUnicode_Check(temp)) {
rpm-build 2bd099
                PyErr_Format(parser_error,
rpm-build 2bd099
                             "second item in terminal node must be a string,"
rpm-build 2bd099
                             " found %s",
rpm-build 2bd099
                             Py_TYPE(temp)->tp_name);
rpm-build 2bd099
                Py_DECREF(temp);
rpm-build 2bd099
                Py_DECREF(elem);
rpm-build 2bd099
                return NULL;
rpm-build 2bd099
            }
rpm-build 2bd099
            if (len == 3) {
rpm-build 2bd099
                PyObject *o = PySequence_GetItem(elem, 2);
rpm-build 2bd099
                if (o == NULL) {
rpm-build 2bd099
                    Py_DECREF(temp);
rpm-build 2bd099
                    Py_DECREF(elem);
rpm-build 2bd099
                    return NULL;
rpm-build 2bd099
                }
rpm-build 2bd099
                if (PyLong_Check(o)) {
rpm-build 2bd099
                    int num = _PyLong_AsInt(o);
rpm-build 2bd099
                    if (num == -1 && PyErr_Occurred()) {
rpm-build 2bd099
                        Py_DECREF(o);
rpm-build 2bd099
                        Py_DECREF(temp);
rpm-build 2bd099
                        Py_DECREF(elem);
rpm-build 2bd099
                        return NULL;
rpm-build 2bd099
                    }
rpm-build 2bd099
                    *line_num = num;
rpm-build 2bd099
                }
rpm-build 2bd099
                else {
rpm-build 2bd099
                    PyErr_Format(parser_error,
rpm-build 2bd099
                                 "third item in terminal node must be an"
rpm-build 2bd099
                                 " integer, found %s",
rpm-build 2bd099
                                 Py_TYPE(temp)->tp_name);
rpm-build 2bd099
                    Py_DECREF(o);
rpm-build 2bd099
                    Py_DECREF(temp);
rpm-build 2bd099
                    Py_DECREF(elem);
rpm-build 2bd099
                    return NULL;
rpm-build 2bd099
                }
rpm-build 2bd099
                Py_DECREF(o);
rpm-build 2bd099
            }
rpm-build 2bd099
            temp_str = PyUnicode_AsUTF8AndSize(temp, &len;;
rpm-build 2bd099
            if (temp_str == NULL) {
rpm-build 2bd099
                Py_DECREF(temp);
rpm-build 2bd099
                Py_DECREF(elem);
rpm-build 2bd099
                return NULL;
rpm-build 2bd099
            }
rpm-build 2bd099
            strn = (char *)PyObject_MALLOC(len + 1);
rpm-build 2bd099
            if (strn == NULL) {
rpm-build 2bd099
                Py_DECREF(temp);
rpm-build 2bd099
                Py_DECREF(elem);
rpm-build 2bd099
                PyErr_NoMemory();
rpm-build 2bd099
                return NULL;
rpm-build 2bd099
            }
rpm-build 2bd099
            (void) memcpy(strn, temp_str, len + 1);
rpm-build 2bd099
            Py_DECREF(temp);
rpm-build 2bd099
        }
rpm-build 2bd099
        else if (!ISNONTERMINAL(type)) {
rpm-build 2bd099
            /*
rpm-build 2bd099
             *  It has to be one or the other; this is an error.
rpm-build 2bd099
             *  Raise an exception.
rpm-build 2bd099
             */
rpm-build 2bd099
            PyObject *err = Py_BuildValue("Os", elem, "unknown node type.");
rpm-build 2bd099
            PyErr_SetObject(parser_error, err);
rpm-build 2bd099
            Py_XDECREF(err);
rpm-build 2bd099
            Py_DECREF(elem);
rpm-build 2bd099
            return NULL;
rpm-build 2bd099
        }
rpm-build 2bd099
        err = PyNode_AddChild(root, type, strn, *line_num, 0);
rpm-build 2bd099
        if (err == E_NOMEM) {
rpm-build 2bd099
            Py_DECREF(elem);
rpm-build 2bd099
            PyObject_FREE(strn);
rpm-build 2bd099
            PyErr_NoMemory();
rpm-build 2bd099
            return NULL;
rpm-build 2bd099
        }
rpm-build 2bd099
        if (err == E_OVERFLOW) {
rpm-build 2bd099
            Py_DECREF(elem);
rpm-build 2bd099
            PyObject_FREE(strn);
rpm-build 2bd099
            PyErr_SetString(PyExc_ValueError,
rpm-build 2bd099
                            "unsupported number of child nodes");
rpm-build 2bd099
            return NULL;
rpm-build 2bd099
        }
rpm-build 2bd099
rpm-build 2bd099
        if (ISNONTERMINAL(type)) {
rpm-build 2bd099
            node* new_child = CHILD(root, i - 1);
rpm-build 2bd099
rpm-build 2bd099
            if (new_child != build_node_children(elem, new_child, line_num)) {
rpm-build 2bd099
                Py_DECREF(elem);
rpm-build 2bd099
                return NULL;
rpm-build 2bd099
            }
rpm-build 2bd099
        }
rpm-build 2bd099
        else if (type == NEWLINE) {     /* It's true:  we increment the     */
rpm-build 2bd099
            ++(*line_num);              /* line number *after* the newline! */
rpm-build 2bd099
        }
rpm-build 2bd099
        Py_DECREF(elem);
rpm-build 2bd099
    }
rpm-build 2bd099
    return root;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
static node*
rpm-build 2bd099
build_node_tree(PyObject *tuple)
rpm-build 2bd099
{
rpm-build 2bd099
    node* res = 0;
rpm-build 2bd099
    PyObject *temp = PySequence_GetItem(tuple, 0);
rpm-build 2bd099
    long num = -1;
rpm-build 2bd099
rpm-build 2bd099
    if (temp != NULL)
rpm-build 2bd099
        num = PyLong_AsLong(temp);
rpm-build 2bd099
    Py_XDECREF(temp);
rpm-build 2bd099
    if (ISTERMINAL(num)) {
rpm-build 2bd099
        /*
rpm-build 2bd099
         *  The tuple is simple, but it doesn't start with a start symbol.
rpm-build 2bd099
         *  Raise an exception now and be done with it.
rpm-build 2bd099
         */
rpm-build 2bd099
        tuple = Py_BuildValue("Os", tuple,
rpm-build 2bd099
                    "Illegal syntax-tree; cannot start with terminal symbol.");
rpm-build 2bd099
        PyErr_SetObject(parser_error, tuple);
rpm-build 2bd099
        Py_XDECREF(tuple);
rpm-build 2bd099
    }
rpm-build 2bd099
    else if (ISNONTERMINAL(num)) {
rpm-build 2bd099
        /*
rpm-build 2bd099
         *  Not efficient, but that can be handled later.
rpm-build 2bd099
         */
rpm-build 2bd099
        int line_num = 0;
rpm-build 2bd099
        PyObject *encoding = NULL;
rpm-build 2bd099
rpm-build 2bd099
        if (num == encoding_decl) {
rpm-build 2bd099
            encoding = PySequence_GetItem(tuple, 2);
rpm-build 2bd099
            if (encoding == NULL) {
rpm-build 2bd099
                PyErr_SetString(parser_error, "missed encoding");
rpm-build 2bd099
                return NULL;
rpm-build 2bd099
            }
rpm-build 2bd099
            if (!PyUnicode_Check(encoding)) {
rpm-build 2bd099
                PyErr_Format(parser_error,
rpm-build 2bd099
                             "encoding must be a string, found %.200s",
rpm-build 2bd099
                             Py_TYPE(encoding)->tp_name);
rpm-build 2bd099
                Py_DECREF(encoding);
rpm-build 2bd099
                return NULL;
rpm-build 2bd099
            }
rpm-build 2bd099
            /* tuple isn't borrowed anymore here, need to DECREF */
rpm-build 2bd099
            tuple = PySequence_GetSlice(tuple, 0, 2);
rpm-build 2bd099
            if (tuple == NULL) {
rpm-build 2bd099
                Py_DECREF(encoding);
rpm-build 2bd099
                return NULL;
rpm-build 2bd099
            }
rpm-build 2bd099
        }
rpm-build 2bd099
        res = PyNode_New(num);
rpm-build 2bd099
        if (res != NULL) {
rpm-build 2bd099
            if (res != build_node_children(tuple, res, &line_num)) {
rpm-build 2bd099
                PyNode_Free(res);
rpm-build 2bd099
                res = NULL;
rpm-build 2bd099
            }
rpm-build 2bd099
            if (res && encoding) {
rpm-build 2bd099
                Py_ssize_t len;
rpm-build 2bd099
                const char *temp;
rpm-build 2bd099
                temp = PyUnicode_AsUTF8AndSize(encoding, &len;;
rpm-build 2bd099
                if (temp == NULL) {
rpm-build 2bd099
                    PyNode_Free(res);
rpm-build 2bd099
                    Py_DECREF(encoding);
rpm-build 2bd099
                    Py_DECREF(tuple);
rpm-build 2bd099
                    return NULL;
rpm-build 2bd099
                }
rpm-build 2bd099
                res->n_str = (char *)PyObject_MALLOC(len + 1);
rpm-build 2bd099
                if (res->n_str == NULL) {
rpm-build 2bd099
                    PyNode_Free(res);
rpm-build 2bd099
                    Py_DECREF(encoding);
rpm-build 2bd099
                    Py_DECREF(tuple);
rpm-build 2bd099
                    PyErr_NoMemory();
rpm-build 2bd099
                    return NULL;
rpm-build 2bd099
                }
rpm-build 2bd099
                (void) memcpy(res->n_str, temp, len + 1);
rpm-build 2bd099
            }
rpm-build 2bd099
        }
rpm-build 2bd099
        if (encoding != NULL) {
rpm-build 2bd099
            Py_DECREF(encoding);
rpm-build 2bd099
            Py_DECREF(tuple);
rpm-build 2bd099
        }
rpm-build 2bd099
    }
rpm-build 2bd099
    else {
rpm-build 2bd099
        /*  The tuple is illegal -- if the number is neither TERMINAL nor
rpm-build 2bd099
         *  NONTERMINAL, we can't use it.  Not sure the implementation
rpm-build 2bd099
         *  allows this condition, but the API doesn't preclude it.
rpm-build 2bd099
         */
rpm-build 2bd099
        PyObject *err = Py_BuildValue("Os", tuple,
rpm-build 2bd099
                                      "Illegal component tuple.");
rpm-build 2bd099
        PyErr_SetObject(parser_error, err);
rpm-build 2bd099
        Py_XDECREF(err);
rpm-build 2bd099
    }
rpm-build 2bd099
rpm-build 2bd099
    return (res);
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
static PyObject*
rpm-build 2bd099
pickle_constructor = NULL;
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
static PyObject*
rpm-build 2bd099
parser__pickler(PyObject *self, PyObject *args)
rpm-build 2bd099
{
rpm-build 2bd099
    NOTE(ARGUNUSED(self))
rpm-build 2bd099
    PyObject *result = NULL;
rpm-build 2bd099
    PyObject *st = NULL;
rpm-build 2bd099
    PyObject *empty_dict = NULL;
rpm-build 2bd099
rpm-build 2bd099
    if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
rpm-build 2bd099
        PyObject *newargs;
rpm-build 2bd099
        PyObject *tuple;
rpm-build 2bd099
rpm-build 2bd099
        if ((empty_dict = PyDict_New()) == NULL)
rpm-build 2bd099
            goto finally;
rpm-build 2bd099
        if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
rpm-build 2bd099
            goto finally;
rpm-build 2bd099
        tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
rpm-build 2bd099
        if (tuple != NULL) {
rpm-build 2bd099
            result = Py_BuildValue("O(O)", pickle_constructor, tuple);
rpm-build 2bd099
            Py_DECREF(tuple);
rpm-build 2bd099
        }
rpm-build 2bd099
        Py_DECREF(newargs);
rpm-build 2bd099
    }
rpm-build 2bd099
  finally:
rpm-build 2bd099
    Py_XDECREF(empty_dict);
rpm-build 2bd099
rpm-build 2bd099
    return (result);
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/*  Functions exported by this module.  Most of this should probably
rpm-build 2bd099
 *  be converted into an ST object with methods, but that is better
rpm-build 2bd099
 *  done directly in Python, allowing subclasses to be created directly.
rpm-build 2bd099
 *  We'd really have to write a wrapper around it all anyway to allow
rpm-build 2bd099
 *  inheritance.
rpm-build 2bd099
 */
rpm-build 2bd099
static PyMethodDef parser_functions[] =  {
rpm-build 2bd099
    {"compilest",      (PyCFunction)parser_compilest,  PUBLIC_METHOD_TYPE,
rpm-build 2bd099
        PyDoc_STR("Compiles an ST object into a code object.")},
rpm-build 2bd099
    {"expr",            (PyCFunction)parser_expr,      PUBLIC_METHOD_TYPE,
rpm-build 2bd099
        PyDoc_STR("Creates an ST object from an expression.")},
rpm-build 2bd099
    {"isexpr",          (PyCFunction)parser_isexpr,    PUBLIC_METHOD_TYPE,
rpm-build 2bd099
        PyDoc_STR("Determines if an ST object was created from an expression.")},
rpm-build 2bd099
    {"issuite",         (PyCFunction)parser_issuite,   PUBLIC_METHOD_TYPE,
rpm-build 2bd099
        PyDoc_STR("Determines if an ST object was created from a suite.")},
rpm-build 2bd099
    {"suite",           (PyCFunction)parser_suite,     PUBLIC_METHOD_TYPE,
rpm-build 2bd099
        PyDoc_STR("Creates an ST object from a suite.")},
rpm-build 2bd099
    {"sequence2st",     (PyCFunction)parser_tuple2st,  PUBLIC_METHOD_TYPE,
rpm-build 2bd099
        PyDoc_STR("Creates an ST object from a tree representation.")},
rpm-build 2bd099
    {"st2tuple",        (PyCFunction)parser_st2tuple,  PUBLIC_METHOD_TYPE,
rpm-build 2bd099
        PyDoc_STR("Creates a tuple-tree representation of an ST.")},
rpm-build 2bd099
    {"st2list",         (PyCFunction)parser_st2list,   PUBLIC_METHOD_TYPE,
rpm-build 2bd099
        PyDoc_STR("Creates a list-tree representation of an ST.")},
rpm-build 2bd099
    {"tuple2st",        (PyCFunction)parser_tuple2st,  PUBLIC_METHOD_TYPE,
rpm-build 2bd099
        PyDoc_STR("Creates an ST object from a tree representation.")},
rpm-build 2bd099
rpm-build 2bd099
    /* private stuff: support pickle module */
rpm-build 2bd099
    {"_pickler",        (PyCFunction)parser__pickler,  METH_VARARGS,
rpm-build 2bd099
        PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
rpm-build 2bd099
rpm-build 2bd099
    {NULL, NULL, 0, NULL}
rpm-build 2bd099
    };
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
static struct PyModuleDef parsermodule = {
rpm-build 2bd099
        PyModuleDef_HEAD_INIT,
rpm-build 2bd099
        "parser",
rpm-build 2bd099
        NULL,
rpm-build 2bd099
        -1,
rpm-build 2bd099
        parser_functions,
rpm-build 2bd099
        NULL,
rpm-build 2bd099
        NULL,
rpm-build 2bd099
        NULL,
rpm-build 2bd099
        NULL
rpm-build 2bd099
};
rpm-build 2bd099
rpm-build 2bd099
PyMODINIT_FUNC PyInit_parser(void);  /* supply a prototype */
rpm-build 2bd099
rpm-build 2bd099
PyMODINIT_FUNC
rpm-build 2bd099
PyInit_parser(void)
rpm-build 2bd099
{
rpm-build 2bd099
    PyObject *module, *copyreg;
rpm-build 2bd099
rpm-build 2bd099
    if (PyType_Ready(&PyST_Type) < 0)
rpm-build 2bd099
        return NULL;
rpm-build 2bd099
    module = PyModule_Create(&parsermodule);
rpm-build 2bd099
    if (module == NULL)
rpm-build 2bd099
        return NULL;
rpm-build 2bd099
rpm-build 2bd099
    if (parser_error == 0)
rpm-build 2bd099
        parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
rpm-build 2bd099
rpm-build 2bd099
    if (parser_error == 0)
rpm-build 2bd099
        return NULL;
rpm-build 2bd099
    /* CAUTION:  The code next used to skip bumping the refcount on
rpm-build 2bd099
     * parser_error.  That's a disaster if PyInit_parser() gets called more
rpm-build 2bd099
     * than once.  By incref'ing, we ensure that each module dict that
rpm-build 2bd099
     * gets created owns its reference to the shared parser_error object,
rpm-build 2bd099
     * and the file static parser_error vrbl owns a reference too.
rpm-build 2bd099
     */
rpm-build 2bd099
    Py_INCREF(parser_error);
rpm-build 2bd099
    if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
rpm-build 2bd099
        return NULL;
rpm-build 2bd099
rpm-build 2bd099
    Py_INCREF(&PyST_Type);
rpm-build 2bd099
    PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
rpm-build 2bd099
rpm-build 2bd099
    PyModule_AddStringConstant(module, "__copyright__",
rpm-build 2bd099
                               parser_copyright_string);
rpm-build 2bd099
    PyModule_AddStringConstant(module, "__doc__",
rpm-build 2bd099
                               parser_doc_string);
rpm-build 2bd099
    PyModule_AddStringConstant(module, "__version__",
rpm-build 2bd099
                               parser_version_string);
rpm-build 2bd099
rpm-build 2bd099
    /* Register to support pickling.
rpm-build 2bd099
     * If this fails, the import of this module will fail because an
rpm-build 2bd099
     * exception will be raised here; should we clear the exception?
rpm-build 2bd099
     */
rpm-build 2bd099
    copyreg = PyImport_ImportModuleNoBlock("copyreg");
rpm-build 2bd099
    if (copyreg != NULL) {
rpm-build 2bd099
        PyObject *func, *pickler;
rpm-build 2bd099
        _Py_IDENTIFIER(pickle);
rpm-build 2bd099
        _Py_IDENTIFIER(sequence2st);
rpm-build 2bd099
        _Py_IDENTIFIER(_pickler);
rpm-build 2bd099
rpm-build 2bd099
        func = _PyObject_GetAttrId(copyreg, &PyId_pickle);
rpm-build 2bd099
        pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st);
rpm-build 2bd099
        pickler = _PyObject_GetAttrId(module, &PyId__pickler);
rpm-build 2bd099
        Py_XINCREF(pickle_constructor);
rpm-build 2bd099
        if ((func != NULL) && (pickle_constructor != NULL)
rpm-build 2bd099
            && (pickler != NULL)) {
rpm-build 2bd099
            PyObject *res;
rpm-build 2bd099
rpm-build 2bd099
            res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
rpm-build 2bd099
                                               pickle_constructor, NULL);
rpm-build 2bd099
            Py_XDECREF(res);
rpm-build 2bd099
        }
rpm-build 2bd099
        Py_XDECREF(func);
rpm-build 2bd099
        Py_XDECREF(pickle_constructor);
rpm-build 2bd099
        Py_XDECREF(pickler);
rpm-build 2bd099
        Py_DECREF(copyreg);
rpm-build 2bd099
    }
rpm-build 2bd099
    return module;
rpm-build 2bd099
}