/Modules/parsermodule.c
http://unladen-swallow.googlecode.com/ · C · 3405 lines · 2521 code · 428 blank · 456 comment · 1103 complexity · b2835d6145228b011be75992b6438b0a MD5 · raw file
Large files are truncated click here to view the full file
- /* parsermodule.c
- *
- * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
- * Institute and State University, Blacksburg, Virginia, USA.
- * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
- * Amsterdam, The Netherlands. Copying is permitted under the terms
- * associated with the main Python distribution, with the additional
- * restriction that this additional notice be included and maintained
- * on all distributed copies.
- *
- * This module serves to replace the original parser module written
- * by Guido. The functionality is not matched precisely, but the
- * original may be implemented on top of this. This is desirable
- * since the source of the text to be parsed is now divorced from
- * this interface.
- *
- * Unlike the prior interface, the ability to give a parse tree
- * produced by Python code as a tuple to the compiler is enabled by
- * this module. See the documentation for more details.
- *
- * I've added some annotations that help with the lint code-checking
- * program, but they're not complete by a long shot. The real errors
- * that lint detects are gone, but there are still warnings with
- * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
- * look like "NOTE(...)".
- */
- #include "Python.h" /* general Python API */
- #include "Python-ast.h" /* mod_ty */
- #include "graminit.h" /* symbols defined in the grammar */
- #include "node.h" /* internal parser structure */
- #include "errcode.h" /* error codes for PyNode_*() */
- #include "token.h" /* token definitions */
- #include "grammar.h"
- #include "parsetok.h"
- /* ISTERMINAL() / ISNONTERMINAL() */
- #include "compile.h"
- #undef Yield
- #include "ast.h"
- #include "pyarena.h"
- extern grammar _PyParser_Grammar; /* From graminit.c */
- #ifdef lint
- #include <note.h>
- #else
- #define NOTE(x)
- #endif
- /* String constants used to initialize module attributes.
- *
- */
- static char parser_copyright_string[] =
- "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
- University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
- Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
- Centrum, Amsterdam, The Netherlands.";
- PyDoc_STRVAR(parser_doc_string,
- "This is an interface to Python's internal parser.");
- static char parser_version_string[] = "0.5";
- typedef PyObject* (*SeqMaker) (Py_ssize_t length);
- typedef int (*SeqInserter) (PyObject* sequence,
- Py_ssize_t index,
- PyObject* element);
- /* The function below is copyrighted by Stichting Mathematisch Centrum. The
- * original copyright statement is included below, and continues to apply
- * in full to the function immediately following. All other material is
- * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
- * Institute and State University. Changes were made to comply with the
- * new naming conventions. Added arguments to provide support for creating
- * lists as well as tuples, and optionally including the line numbers.
- */
- static PyObject*
- node2tuple(node *n, /* node to convert */
- SeqMaker mkseq, /* create sequence */
- SeqInserter addelem, /* func. to add elem. in seq. */
- int lineno, /* include line numbers? */
- int col_offset) /* include column offsets? */
- {
- if (n == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- if (ISNONTERMINAL(TYPE(n))) {
- int i;
- PyObject *v;
- PyObject *w;
- v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
- if (v == NULL)
- return (v);
- w = PyInt_FromLong(TYPE(n));
- if (w == NULL) {
- Py_DECREF(v);
- return ((PyObject*) NULL);
- }
- (void) addelem(v, 0, w);
- for (i = 0; i < NCH(n); i++) {
- w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
- if (w == NULL) {
- Py_DECREF(v);
- return ((PyObject*) NULL);
- }
- (void) addelem(v, i+1, w);
- }
- if (TYPE(n) == encoding_decl)
- (void) addelem(v, i+1, PyString_FromString(STR(n)));
- return (v);
- }
- else if (ISTERMINAL(TYPE(n))) {
- PyObject *result = mkseq(2 + lineno + col_offset);
- if (result != NULL) {
- (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
- (void) addelem(result, 1, PyString_FromString(STR(n)));
- if (lineno == 1)
- (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
- if (col_offset == 1)
- (void) addelem(result, 3, PyInt_FromLong(n->n_col_offset));
- }
- return (result);
- }
- else {
- PyErr_SetString(PyExc_SystemError,
- "unrecognized parse tree node type");
- return ((PyObject*) NULL);
- }
- }
- /*
- * End of material copyrighted by Stichting Mathematisch Centrum.
- */
- /* There are two types of intermediate objects we're interested in:
- * 'eval' and 'exec' types. These constants can be used in the st_type
- * field of the object type to identify which any given object represents.
- * These should probably go in an external header to allow other extensions
- * to use them, but then, we really should be using C++ too. ;-)
- */
- #define PyST_EXPR 1
- #define PyST_SUITE 2
- /* These are the internal objects and definitions required to implement the
- * ST type. Most of the internal names are more reminiscent of the 'old'
- * naming style, but the code uses the new naming convention.
- */
- static PyObject*
- parser_error = 0;
- typedef struct {
- PyObject_HEAD /* standard object header */
- node* st_node; /* the node* returned by the parser */
- int st_type; /* EXPR or SUITE ? */
- PyCompilerFlags st_flags; /* Parser and compiler flags */
- } PyST_Object;
- static void parser_free(PyST_Object *st);
- static int parser_compare(PyST_Object *left, PyST_Object *right);
- static PyObject *parser_getattr(PyObject *self, char *name);
- static
- PyTypeObject PyST_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "parser.st", /* tp_name */
- (int) sizeof(PyST_Object), /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor)parser_free, /* tp_dealloc */
- 0, /* tp_print */
- parser_getattr, /* tp_getattr */
- 0, /* tp_setattr */
- (cmpfunc)parser_compare, /* tp_compare */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- /* Functions to access object as input/output buffer */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- /* __doc__ */
- "Intermediate representation of a Python parse tree."
- }; /* PyST_Type */
- static int
- parser_compare_nodes(node *left, node *right)
- {
- int j;
- if (TYPE(left) < TYPE(right))
- return (-1);
- if (TYPE(right) < TYPE(left))
- return (1);
- if (ISTERMINAL(TYPE(left)))
- return (strcmp(STR(left), STR(right)));
- if (NCH(left) < NCH(right))
- return (-1);
- if (NCH(right) < NCH(left))
- return (1);
- for (j = 0; j < NCH(left); ++j) {
- int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
- if (v != 0)
- return (v);
- }
- return (0);
- }
- /* int parser_compare(PyST_Object* left, PyST_Object* right)
- *
- * Comparison function used by the Python operators ==, !=, <, >, <=, >=
- * This really just wraps a call to parser_compare_nodes() with some easy
- * checks and protection code.
- *
- */
- static int
- parser_compare(PyST_Object *left, PyST_Object *right)
- {
- if (left == right)
- return (0);
- if ((left == 0) || (right == 0))
- return (-1);
- return (parser_compare_nodes(left->st_node, right->st_node));
- }
- /* parser_newstobject(node* st)
- *
- * Allocates a new Python object representing an ST. This is simply the
- * 'wrapper' object that holds a node* and allows it to be passed around in
- * Python code.
- *
- */
- static PyObject*
- parser_newstobject(node *st, int type)
- {
- PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
- if (o != 0) {
- o->st_node = st;
- o->st_type = type;
- o->st_flags.cf_flags = 0;
- }
- else {
- PyNode_Free(st);
- }
- return ((PyObject*)o);
- }
- /* void parser_free(PyST_Object* st)
- *
- * This is called by a del statement that reduces the reference count to 0.
- *
- */
- static void
- parser_free(PyST_Object *st)
- {
- PyNode_Free(st->st_node);
- PyObject_Del(st);
- }
- /* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
- *
- * This provides conversion from a node* to a tuple object that can be
- * returned to the Python-level caller. The ST object is not modified.
- *
- */
- static PyObject*
- parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
- {
- PyObject *line_option = 0;
- PyObject *col_option = 0;
- PyObject *res = 0;
- int ok;
- static char *keywords[] = {"ast", "line_info", "col_info", NULL};
- if (self == NULL) {
- ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
- &PyST_Type, &self, &line_option,
- &col_option);
- }
- else
- ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
- &line_option, &col_option);
- if (ok != 0) {
- int lineno = 0;
- int col_offset = 0;
- if (line_option != NULL) {
- lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
- }
- if (col_option != NULL) {
- col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
- }
- /*
- * Convert ST into a tuple representation. Use Guido's function,
- * since it's known to work already.
- */
- res = node2tuple(((PyST_Object*)self)->st_node,
- PyTuple_New, PyTuple_SetItem, lineno, col_offset);
- }
- return (res);
- }
- static PyObject*
- parser_ast2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
- {
- if (PyErr_WarnPy3k("ast2tuple is removed in 3.x; use st2tuple", 1) < 0)
- return NULL;
- return parser_st2tuple(self, args, kw);
- }
- /* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
- *
- * This provides conversion from a node* to a list object that can be
- * returned to the Python-level caller. The ST object is not modified.
- *
- */
- static PyObject*
- parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
- {
- PyObject *line_option = 0;
- PyObject *col_option = 0;
- PyObject *res = 0;
- int ok;
- static char *keywords[] = {"ast", "line_info", "col_info", NULL};
- if (self == NULL)
- ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
- &PyST_Type, &self, &line_option,
- &col_option);
- else
- ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
- &line_option, &col_option);
- if (ok) {
- int lineno = 0;
- int col_offset = 0;
- if (line_option != 0) {
- lineno = PyObject_IsTrue(line_option) ? 1 : 0;
- }
- if (col_option != NULL) {
- col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
- }
- /*
- * Convert ST into a tuple representation. Use Guido's function,
- * since it's known to work already.
- */
- res = node2tuple(self->st_node,
- PyList_New, PyList_SetItem, lineno, col_offset);
- }
- return (res);
- }
- static PyObject*
- parser_ast2list(PyST_Object *self, PyObject *args, PyObject *kw)
- {
- if (PyErr_WarnPy3k("ast2list is removed in 3.x; use st2list", 1) < 0)
- return NULL;
- return parser_st2list(self, args, kw);
- }
- /* parser_compilest(PyObject* self, PyObject* args)
- *
- * This function creates code objects from the parse tree represented by
- * the passed-in data object. An optional file name is passed in as well.
- *
- */
- static PyObject*
- parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
- {
- PyObject* res = 0;
- PyArena* arena;
- mod_ty mod;
- char* str = "<syntax-tree>";
- int ok;
- static char *keywords[] = {"ast", "filename", NULL};
- if (self == NULL)
- ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
- &PyST_Type, &self, &str);
- else
- ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
- &str);
- if (ok) {
- arena = PyArena_New();
- if (arena) {
- mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
- if (mod) {
- res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
- }
- PyArena_Free(arena);
- }
- }
- return (res);
- }
- static PyObject*
- parser_compileast(PyST_Object *self, PyObject *args, PyObject *kw)
- {
- if (PyErr_WarnPy3k("compileast is removed in 3.x; use compilest", 1) < 0)
- return NULL;
- return parser_compilest(self, args, kw);
- }
- /* PyObject* parser_isexpr(PyObject* self, PyObject* args)
- * PyObject* parser_issuite(PyObject* self, PyObject* args)
- *
- * Checks the passed-in ST object to determine if it is an expression or
- * a statement suite, respectively. The return is a Python truth value.
- *
- */
- static PyObject*
- parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
- {
- PyObject* res = 0;
- int ok;
- static char *keywords[] = {"ast", NULL};
- if (self == NULL)
- ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
- &PyST_Type, &self);
- else
- ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
- if (ok) {
- /* Check to see if the ST represents an expression or not. */
- res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
- Py_INCREF(res);
- }
- return (res);
- }
- static PyObject*
- parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
- {
- PyObject* res = 0;
- int ok;
- static char *keywords[] = {"ast", NULL};
- if (self == NULL)
- ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
- &PyST_Type, &self);
- else
- ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
- if (ok) {
- /* Check to see if the ST represents an expression or not. */
- res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
- Py_INCREF(res);
- }
- return (res);
- }
- #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
- static PyMethodDef
- parser_methods[] = {
- {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
- PyDoc_STR("Compile this ST object into a code object.")},
- {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
- PyDoc_STR("Determines if this ST object was created from an expression.")},
- {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
- PyDoc_STR("Determines if this ST object was created from a suite.")},
- {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
- PyDoc_STR("Creates a list-tree representation of this ST.")},
- {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
- PyDoc_STR("Creates a tuple-tree representation of this ST.")},
- {NULL, NULL, 0, NULL}
- };
- static PyObject*
- parser_getattr(PyObject *self, char *name)
- {
- return (Py_FindMethod(parser_methods, self, name));
- }
- /* err_string(char* message)
- *
- * Sets the error string for an exception of type ParserError.
- *
- */
- static void
- err_string(char *message)
- {
- PyErr_SetString(parser_error, message);
- }
- /* PyObject* parser_do_parse(PyObject* args, int type)
- *
- * Internal function to actually execute the parse and return the result if
- * successful or set an exception if not.
- *
- */
- static PyObject*
- parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
- {
- char* string = 0;
- PyObject* res = 0;
- int flags = 0;
- perrdetail err;
- static char *keywords[] = {"source", NULL};
- if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
- node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
- &_PyParser_Grammar,
- (type == PyST_EXPR)
- ? eval_input : file_input,
- &err, &flags);
- if (n) {
- res = parser_newstobject(n, type);
- if (res)
- ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
- }
- else
- PyParser_SetError(&err);
- }
- return (res);
- }
- /* PyObject* parser_expr(PyObject* self, PyObject* args)
- * PyObject* parser_suite(PyObject* self, PyObject* args)
- *
- * External interfaces to the parser itself. Which is called determines if
- * the parser attempts to recognize an expression ('eval' form) or statement
- * suite ('exec' form). The real work is done by parser_do_parse() above.
- *
- */
- static PyObject*
- parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
- {
- NOTE(ARGUNUSED(self))
- return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
- }
- static PyObject*
- parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
- {
- NOTE(ARGUNUSED(self))
- return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
- }
- /* This is the messy part of the code. Conversion from a tuple to an ST
- * object requires that the input tuple be valid without having to rely on
- * catching an exception from the compiler. This is done to allow the
- * compiler itself to remain fast, since most of its input will come from
- * the parser directly, and therefore be known to be syntactically correct.
- * This validation is done to ensure that we don't core dump the compile
- * phase, returning an exception instead.
- *
- * Two aspects can be broken out in this code: creating a node tree from
- * the tuple passed in, and verifying that it is indeed valid. It may be
- * advantageous to expand the number of ST types to include funcdefs and
- * lambdadefs to take advantage of the optimizer, recognizing those STs
- * here. They are not necessary, and not quite as useful in a raw form.
- * For now, let's get expressions and suites working reliably.
- */
- static node* build_node_tree(PyObject *tuple);
- static int validate_expr_tree(node *tree);
- static int validate_file_input(node *tree);
- static int validate_encoding_decl(node *tree);
- /* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
- *
- * This is the public function, called from the Python code. It receives a
- * single tuple object from the caller, and creates an ST object if the
- * tuple can be validated. It does this by checking the first code of the
- * tuple, and, if acceptable, builds the internal representation. If this
- * step succeeds, the internal representation is validated as fully as
- * possible with the various validate_*() routines defined below.
- *
- * This function must be changed if support is to be added for PyST_FRAGMENT
- * ST objects.
- *
- */
- static PyObject*
- parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
- {
- NOTE(ARGUNUSED(self))
- PyObject *st = 0;
- PyObject *tuple;
- node *tree;
- static char *keywords[] = {"sequence", NULL};
- if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
- &tuple))
- return (0);
- if (!PySequence_Check(tuple)) {
- PyErr_SetString(PyExc_ValueError,
- "sequence2st() requires a single sequence argument");
- return (0);
- }
- /*
- * Convert the tree to the internal form before checking it.
- */
- tree = build_node_tree(tuple);
- if (tree != 0) {
- int start_sym = TYPE(tree);
- if (start_sym == eval_input) {
- /* Might be an eval form. */
- if (validate_expr_tree(tree))
- st = parser_newstobject(tree, PyST_EXPR);
- else
- PyNode_Free(tree);
- }
- else if (start_sym == file_input) {
- /* This looks like an exec form so far. */
- if (validate_file_input(tree))
- st = parser_newstobject(tree, PyST_SUITE);
- else
- PyNode_Free(tree);
- }
- else if (start_sym == encoding_decl) {
- /* This looks like an encoding_decl so far. */
- if (validate_encoding_decl(tree))
- st = parser_newstobject(tree, PyST_SUITE);
- else
- PyNode_Free(tree);
- }
- else {
- /* This is a fragment, at best. */
- PyNode_Free(tree);
- err_string("parse tree does not use a valid start symbol");
- }
- }
- /* Make sure we throw an exception on all errors. We should never
- * get this, but we'd do well to be sure something is done.
- */
- if (st == NULL && !PyErr_Occurred())
- err_string("unspecified ST error occurred");
- return st;
- }
- static PyObject*
- parser_tuple2ast(PyST_Object *self, PyObject *args, PyObject *kw)
- {
- if (PyErr_WarnPy3k("tuple2ast is removed in 3.x; use tuple2st", 1) < 0)
- return NULL;
- return parser_tuple2st(self, args, kw);
- }
- /* node* build_node_children()
- *
- * Iterate across the children of the current non-terminal node and build
- * their structures. If successful, return the root of this portion of
- * the tree, otherwise, 0. Any required exception will be specified already,
- * and no memory will have been deallocated.
- *
- */
- static node*
- build_node_children(PyObject *tuple, node *root, int *line_num)
- {
- Py_ssize_t len = PyObject_Size(tuple);
- Py_ssize_t i;
- int err;
- for (i = 1; i < len; ++i) {
- /* elem must always be a sequence, however simple */
- PyObject* elem = PySequence_GetItem(tuple, i);
- int ok = elem != NULL;
- long type = 0;
- char *strn = 0;
- if (ok)
- ok = PySequence_Check(elem);
- if (ok) {
- PyObject *temp = PySequence_GetItem(elem, 0);
- if (temp == NULL)
- ok = 0;
- else {
- ok = PyInt_Check(temp);
- if (ok)
- type = PyInt_AS_LONG(temp);
- Py_DECREF(temp);
- }
- }
- if (!ok) {
- PyObject *err = Py_BuildValue("os", elem,
- "Illegal node construct.");
- PyErr_SetObject(parser_error, err);
- Py_XDECREF(err);
- Py_XDECREF(elem);
- return (0);
- }
- if (ISTERMINAL(type)) {
- Py_ssize_t len = PyObject_Size(elem);
- PyObject *temp;
- if ((len != 2) && (len != 3)) {
- err_string("terminal nodes must have 2 or 3 entries");
- return 0;
- }
- temp = PySequence_GetItem(elem, 1);
- if (temp == NULL)
- return 0;
- if (!PyString_Check(temp)) {
- PyErr_Format(parser_error,
- "second item in terminal node must be a string,"
- " found %s",
- Py_TYPE(temp)->tp_name);
- Py_DECREF(temp);
- return 0;
- }
- if (len == 3) {
- PyObject *o = PySequence_GetItem(elem, 2);
- if (o != NULL) {
- if (PyInt_Check(o))
- *line_num = PyInt_AS_LONG(o);
- else {
- PyErr_Format(parser_error,
- "third item in terminal node must be an"
- " integer, found %s",
- Py_TYPE(temp)->tp_name);
- Py_DECREF(o);
- Py_DECREF(temp);
- return 0;
- }
- Py_DECREF(o);
- }
- }
- len = PyString_GET_SIZE(temp) + 1;
- strn = (char *)PyObject_MALLOC(len);
- if (strn != NULL)
- (void) memcpy(strn, PyString_AS_STRING(temp), len);
- Py_DECREF(temp);
- }
- else if (!ISNONTERMINAL(type)) {
- /*
- * It has to be one or the other; this is an error.
- * Throw an exception.
- */
- PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
- PyErr_SetObject(parser_error, err);
- Py_XDECREF(err);
- Py_XDECREF(elem);
- return (0);
- }
- err = PyNode_AddChild(root, type, strn, *line_num, 0);
- if (err == E_NOMEM) {
- PyObject_FREE(strn);
- return (node *) PyErr_NoMemory();
- }
- if (err == E_OVERFLOW) {
- PyObject_FREE(strn);
- PyErr_SetString(PyExc_ValueError,
- "unsupported number of child nodes");
- return NULL;
- }
- if (ISNONTERMINAL(type)) {
- node* new_child = CHILD(root, i - 1);
- if (new_child != build_node_children(elem, new_child, line_num)) {
- Py_XDECREF(elem);
- return (0);
- }
- }
- else if (type == NEWLINE) { /* It's true: we increment the */
- ++(*line_num); /* line number *after* the newline! */
- }
- Py_XDECREF(elem);
- }
- return root;
- }
- static node*
- build_node_tree(PyObject *tuple)
- {
- node* res = 0;
- PyObject *temp = PySequence_GetItem(tuple, 0);
- long num = -1;
- if (temp != NULL)
- num = PyInt_AsLong(temp);
- Py_XDECREF(temp);
- if (ISTERMINAL(num)) {
- /*
- * The tuple is simple, but it doesn't start with a start symbol.
- * Throw an exception now and be done with it.
- */
- tuple = Py_BuildValue("os", tuple,
- "Illegal syntax-tree; cannot start with terminal symbol.");
- PyErr_SetObject(parser_error, tuple);
- Py_XDECREF(tuple);
- }
- else if (ISNONTERMINAL(num)) {
- /*
- * Not efficient, but that can be handled later.
- */
- int line_num = 0;
- PyObject *encoding = NULL;
- if (num == encoding_decl) {
- encoding = PySequence_GetItem(tuple, 2);
- /* tuple isn't borrowed anymore here, need to DECREF */
- tuple = PySequence_GetSlice(tuple, 0, 2);
- }
- res = PyNode_New(num);
- if (res != NULL) {
- if (res != build_node_children(tuple, res, &line_num)) {
- PyNode_Free(res);
- res = NULL;
- }
- if (res && encoding) {
- Py_ssize_t len;
- len = PyString_GET_SIZE(encoding) + 1;
- res->n_str = (char *)PyObject_MALLOC(len);
- if (res->n_str != NULL)
- (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
- Py_DECREF(encoding);
- Py_DECREF(tuple);
- }
- }
- }
- else {
- /* The tuple is illegal -- if the number is neither TERMINAL nor
- * NONTERMINAL, we can't use it. Not sure the implementation
- * allows this condition, but the API doesn't preclude it.
- */
- PyObject *err = Py_BuildValue("os", tuple,
- "Illegal component tuple.");
- PyErr_SetObject(parser_error, err);
- Py_XDECREF(err);
- }
- return (res);
- }
- /*
- * Validation routines used within the validation section:
- */
- static int validate_terminal(node *terminal, int type, char *string);
- #define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
- #define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
- #define validate_colon(ch) validate_terminal(ch, COLON, ":")
- #define validate_comma(ch) validate_terminal(ch, COMMA, ",")
- #define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
- #define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
- #define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
- #define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
- #define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
- #define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
- #define validate_semi(ch) validate_terminal(ch, SEMI, ";")
- #define validate_star(ch) validate_terminal(ch, STAR, "*")
- #define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
- #define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
- #define validate_dot(ch) validate_terminal(ch, DOT, ".")
- #define validate_at(ch) validate_terminal(ch, AT, "@")
- #define validate_name(ch, str) validate_terminal(ch, NAME, str)
- #define VALIDATER(n) static int validate_##n(node *tree)
- VALIDATER(node); VALIDATER(small_stmt);
- VALIDATER(class); VALIDATER(node);
- VALIDATER(parameters); VALIDATER(suite);
- VALIDATER(testlist); VALIDATER(varargslist);
- VALIDATER(fpdef); VALIDATER(fplist);
- VALIDATER(stmt); VALIDATER(simple_stmt);
- VALIDATER(expr_stmt); VALIDATER(power);
- VALIDATER(print_stmt); VALIDATER(del_stmt);
- VALIDATER(return_stmt); VALIDATER(list_iter);
- VALIDATER(raise_stmt); VALIDATER(import_stmt);
- VALIDATER(import_name); VALIDATER(import_from);
- VALIDATER(global_stmt); VALIDATER(list_if);
- VALIDATER(assert_stmt); VALIDATER(list_for);
- VALIDATER(exec_stmt); VALIDATER(compound_stmt);
- VALIDATER(while); VALIDATER(for);
- VALIDATER(try); VALIDATER(except_clause);
- VALIDATER(test); VALIDATER(and_test);
- VALIDATER(not_test); VALIDATER(comparison);
- VALIDATER(comp_op); VALIDATER(expr);
- VALIDATER(xor_expr); VALIDATER(and_expr);
- VALIDATER(shift_expr); VALIDATER(arith_expr);
- VALIDATER(term); VALIDATER(factor);
- VALIDATER(atom); VALIDATER(lambdef);
- VALIDATER(trailer); VALIDATER(subscript);
- VALIDATER(subscriptlist); VALIDATER(sliceop);
- VALIDATER(exprlist); VALIDATER(dictmaker);
- VALIDATER(arglist); VALIDATER(argument);
- VALIDATER(listmaker); VALIDATER(yield_stmt);
- VALIDATER(testlist1); VALIDATER(gen_for);
- VALIDATER(gen_iter); VALIDATER(gen_if);
- VALIDATER(testlist_gexp); VALIDATER(yield_expr);
- VALIDATER(yield_or_testlist); VALIDATER(or_test);
- VALIDATER(old_test); VALIDATER(old_lambdef);
- #undef VALIDATER
- #define is_even(n) (((n) & 1) == 0)
- #define is_odd(n) (((n) & 1) == 1)
- static int
- validate_ntype(node *n, int t)
- {
- if (TYPE(n) != t) {
- PyErr_Format(parser_error, "Expected node type %d, got %d.",
- t, TYPE(n));
- return 0;
- }
- return 1;
- }
- /* Verifies that the number of child nodes is exactly 'num', raising
- * an exception if it isn't. The exception message does not indicate
- * the exact number of nodes, allowing this to be used to raise the
- * "right" exception when the wrong number of nodes is present in a
- * specific variant of a statement's syntax. This is commonly used
- * in that fashion.
- */
- static int
- validate_numnodes(node *n, int num, const char *const name)
- {
- if (NCH(n) != num) {
- PyErr_Format(parser_error,
- "Illegal number of children for %s node.", name);
- return 0;
- }
- return 1;
- }
- static int
- validate_terminal(node *terminal, int type, char *string)
- {
- int res = (validate_ntype(terminal, type)
- && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
- if (!res && !PyErr_Occurred()) {
- PyErr_Format(parser_error,
- "Illegal terminal: expected \"%s\"", string);
- }
- return (res);
- }
- /* X (',' X) [',']
- */
- static int
- validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
- const char *const name)
- {
- int nch = NCH(tree);
- int res = (nch && validate_ntype(tree, ntype)
- && vfunc(CHILD(tree, 0)));
- if (!res && !PyErr_Occurred())
- (void) validate_numnodes(tree, 1, name);
- else {
- if (is_even(nch))
- res = validate_comma(CHILD(tree, --nch));
- if (res && nch > 1) {
- int pos = 1;
- for ( ; res && pos < nch; pos += 2)
- res = (validate_comma(CHILD(tree, pos))
- && vfunc(CHILD(tree, pos + 1)));
- }
- }
- return (res);
- }
- /* validate_class()
- *
- * classdef:
- * 'class' NAME ['(' testlist ')'] ':' suite
- */
- static int
- validate_class(node *tree)
- {
- int nch = NCH(tree);
- int res = (validate_ntype(tree, classdef) &&
- ((nch == 4) || (nch == 6) || (nch == 7)));
- if (res) {
- res = (validate_name(CHILD(tree, 0), "class")
- && validate_ntype(CHILD(tree, 1), NAME)
- && validate_colon(CHILD(tree, nch - 2))
- && validate_suite(CHILD(tree, nch - 1)));
- }
- else {
- (void) validate_numnodes(tree, 4, "class");
- }
-
- if (res) {
- if (nch == 7) {
- res = ((validate_lparen(CHILD(tree, 2)) &&
- validate_testlist(CHILD(tree, 3)) &&
- validate_rparen(CHILD(tree, 4))));
- }
- else if (nch == 6) {
- res = (validate_lparen(CHILD(tree,2)) &&
- validate_rparen(CHILD(tree,3)));
- }
- }
- return (res);
- }
- /* if_stmt:
- * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
- */
- static int
- validate_if(node *tree)
- {
- int nch = NCH(tree);
- int res = (validate_ntype(tree, if_stmt)
- && (nch >= 4)
- && validate_name(CHILD(tree, 0), "if")
- && validate_test(CHILD(tree, 1))
- && validate_colon(CHILD(tree, 2))
- && validate_suite(CHILD(tree, 3)));
- if (res && ((nch % 4) == 3)) {
- /* ... 'else' ':' suite */
- res = (validate_name(CHILD(tree, nch - 3), "else")
- && validate_colon(CHILD(tree, nch - 2))
- && validate_suite(CHILD(tree, nch - 1)));
- nch -= 3;
- }
- else if (!res && !PyErr_Occurred())
- (void) validate_numnodes(tree, 4, "if");
- if ((nch % 4) != 0)
- /* Will catch the case for nch < 4 */
- res = validate_numnodes(tree, 0, "if");
- else if (res && (nch > 4)) {
- /* ... ('elif' test ':' suite)+ ... */
- int j = 4;
- while ((j < nch) && res) {
- res = (validate_name(CHILD(tree, j), "elif")
- && validate_colon(CHILD(tree, j + 2))
- && validate_test(CHILD(tree, j + 1))
- && validate_suite(CHILD(tree, j + 3)));
- j += 4;
- }
- }
- return (res);
- }
- /* parameters:
- * '(' [varargslist] ')'
- *
- */
- static int
- validate_parameters(node *tree)
- {
- int nch = NCH(tree);
- int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
- if (res) {
- res = (validate_lparen(CHILD(tree, 0))
- && validate_rparen(CHILD(tree, nch - 1)));
- if (res && (nch == 3))
- res = validate_varargslist(CHILD(tree, 1));
- }
- else {
- (void) validate_numnodes(tree, 2, "parameters");
- }
- return (res);
- }
- /* validate_suite()
- *
- * suite:
- * simple_stmt
- * | NEWLINE INDENT stmt+ DEDENT
- */
- static int
- validate_suite(node *tree)
- {
- int nch = NCH(tree);
- int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
- if (res && (nch == 1))
- res = validate_simple_stmt(CHILD(tree, 0));
- else if (res) {
- /* NEWLINE INDENT stmt+ DEDENT */
- res = (validate_newline(CHILD(tree, 0))
- && validate_indent(CHILD(tree, 1))
- && validate_stmt(CHILD(tree, 2))
- && validate_dedent(CHILD(tree, nch - 1)));
- if (res && (nch > 4)) {
- int i = 3;
- --nch; /* forget the DEDENT */
- for ( ; res && (i < nch); ++i)
- res = validate_stmt(CHILD(tree, i));
- }
- else if (nch < 4)
- res = validate_numnodes(tree, 4, "suite");
- }
- return (res);
- }
- static int
- validate_testlist(node *tree)
- {
- return (validate_repeating_list(tree, testlist,
- validate_test, "testlist"));
- }
- static int
- validate_testlist1(node *tree)
- {
- return (validate_repeating_list(tree, testlist1,
- validate_test, "testlist1"));
- }
- static int
- validate_testlist_safe(node *tree)
- {
- return (validate_repeating_list(tree, testlist_safe,
- validate_old_test, "testlist_safe"));
- }
- /* '*' NAME [',' '**' NAME] | '**' NAME
- */
- static int
- validate_varargslist_trailer(node *tree, int start)
- {
- int nch = NCH(tree);
- int res = 0;
- int sym;
- if (nch <= start) {
- err_string("expected variable argument trailer for varargslist");
- return 0;
- }
- sym = TYPE(CHILD(tree, start));
- if (sym == STAR) {
- /*
- * ('*' NAME [',' '**' NAME]
- */
- if (nch-start == 2)
- res = validate_name(CHILD(tree, start+1), NULL);
- else if (nch-start == 5)
- res = (validate_name(CHILD(tree, start+1), NULL)
- && validate_comma(CHILD(tree, start+2))
- && validate_doublestar(CHILD(tree, start+3))
- && validate_name(CHILD(tree, start+4), NULL));
- }
- else if (sym == DOUBLESTAR) {
- /*
- * '**' NAME
- */
- if (nch-start == 2)
- res = validate_name(CHILD(tree, start+1), NULL);
- }
- if (!res)
- err_string("illegal variable argument trailer for varargslist");
- return res;
- }
- /* validate_varargslist()
- *
- * varargslist:
- * (fpdef ['=' test] ',')*
- * ('*' NAME [',' '**' NAME]
- * | '**' NAME)
- * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
- *
- */
- static int
- validate_varargslist(node *tree)
- {
- int nch = NCH(tree);
- int res = validate_ntype(tree, varargslist) && (nch != 0);
- int sym;
- if (!res)
- return 0;
- if (nch < 1) {
- err_string("varargslist missing child nodes");
- return 0;
- }
- sym = TYPE(CHILD(tree, 0));
- if (sym == STAR || sym == DOUBLESTAR)
- /* whole thing matches:
- * '*' NAME [',' '**' NAME] | '**' NAME
- */
- res = validate_varargslist_trailer(tree, 0);
- else if (sym == fpdef) {
- int i = 0;
- sym = TYPE(CHILD(tree, nch-1));
- if (sym == NAME) {
- /*
- * (fpdef ['=' test] ',')+
- * ('*' NAME [',' '**' NAME]
- * | '**' NAME)
- */
- /* skip over (fpdef ['=' test] ',')+ */
- while (res && (i+2 <= nch)) {
- res = validate_fpdef(CHILD(tree, i));
- ++i;
- if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
- res = (validate_equal(CHILD(tree, i))
- && validate_test(CHILD(tree, i+1)));
- if (res)
- i += 2;
- }
- if (res && i < nch) {
- res = validate_comma(CHILD(tree, i));
- ++i;
- if (res && i < nch
- && (TYPE(CHILD(tree, i)) == DOUBLESTAR
- || TYPE(CHILD(tree, i)) == STAR))
- break;
- }
- }
- /* ... '*' NAME [',' '**' NAME] | '**' NAME
- * i --^^^
- */
- if (res)
- res = validate_varargslist_trailer(tree, i);
- }
- else {
- /*
- * fpdef ['=' test] (',' fpdef ['=' test])* [',']
- */
- /* strip trailing comma node */
- if (sym == COMMA) {
- res = validate_comma(CHILD(tree, nch-1));
- if (!res)
- return 0;
- --nch;
- }
- /*
- * fpdef ['=' test] (',' fpdef ['=' test])*
- */
- res = validate_fpdef(CHILD(tree, 0));
- ++i;
- if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
- res = (validate_equal(CHILD(tree, i))
- && validate_test(CHILD(tree, i+1)));
- i += 2;
- }
- /*
- * ... (',' fpdef ['=' test])*
- * i ---^^^
- */
- while (res && (nch - i) >= 2) {
- res = (validate_comma(CHILD(tree, i))
- && validate_fpdef(CHILD(tree, i+1)));
- i += 2;
- if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
- res = (validate_equal(CHILD(tree, i))
- && validate_test(CHILD(tree, i+1)));
- i += 2;
- }
- }
- if (res && nch - i != 0) {
- res = 0;
- err_string("illegal formation for varargslist");
- }
- }
- }
- return res;
- }
- /* list_iter: list_for | list_if
- */
- static int
- validate_list_iter(node *tree)
- {
- int res = (validate_ntype(tree, list_iter)
- && validate_numnodes(tree, 1, "list_iter"));
- if (res && TYPE(CHILD(tree, 0)) == list_for)
- res = validate_list_for(CHILD(tree, 0));
- else
- res = validate_list_if(CHILD(tree, 0));
- return res;
- }
- /* gen_iter: gen_for | gen_if
- */
- static int
- validate_gen_iter(node *tree)
- {
- int res = (validate_ntype(tree, gen_iter)
- && validate_numnodes(tree, 1, "gen_iter"));
- if (res && TYPE(CHILD(tree, 0)) == gen_for)
- res = validate_gen_for(CHILD(tree, 0));
- else
- res = validate_gen_if(CHILD(tree, 0));
- return res;
- }
- /* list_for: 'for' exprlist 'in' testlist [list_iter]
- */
- static int
- validate_list_for(node *tree)
- {
- int nch = NCH(tree);
- int res;
- if (nch == 5)
- res = validate_list_iter(CHILD(tree, 4));
- else
- res = validate_numnodes(tree, 4, "list_for");
- if (res)
- res = (validate_name(CHILD(tree, 0), "for")
- && validate_exprlist(CHILD(tree, 1))
- && validate_name(CHILD(tree, 2), "in")
- && validate_testlist_safe(CHILD(tree, 3)));
- return res;
- }
- /* gen_for: 'for' exprlist 'in' test [gen_iter]
- */
- static int
- validate_gen_for(node *tree)
- {
- int nch = NCH(tree);
- int res;
- if (nch == 5)
- res = validate_gen_iter(CHILD(tree, 4));
- else
- res = validate_numnodes(tree, 4, "gen_for");
- if (res)
- res = (validate_name(CHILD(tree, 0), "for")
- && validate_exprlist(CHILD(tree, 1))
- && validate_name(CHILD(tree, 2), "in")
- && validate_or_test(CHILD(tree, 3)));
- return res;
- }
- /* list_if: 'if' old_test [list_iter]
- */
- static int
- validate_list_if(node *tree)
- {
- int nch = NCH(tree);
- int res;
- if (nch == 3)
- res = validate_list_iter(CHILD(tree, 2));
- else
- res = validate_numnodes(tree, 2, "list_if");
- if (res)
- res = (validate_name(CHILD(tree, 0), "if")
- && validate_old_test(CHILD(tree, 1)));
- return res;
- }
- /* gen_if: 'if' old_test [gen_iter]
- */
- static int
- validate_gen_if(node *tree)
- {
- int nch = NCH(tree);
- int res;
- if (nch == 3)
- res = validate_gen_iter(CHILD(tree, 2));
- else
- res = validate_numnodes(tree, 2, "gen_if");
-
- if (res)
- res = (validate_name(CHILD(tree, 0), "if")
- && validate_old_test(CHILD(tree, 1)));
- return res;
- }
- /* validate_fpdef()
- *
- * fpdef:
- * NAME
- * | '(' fplist ')'
- */
- static int
- validate_fpdef(node *tree)
- {
- int nch = NCH(tree);
- int res = validate_ntype(tree, fpdef);
- if (res) {
- if (nch == 1)
- res = validate_ntype(CHILD(tree, 0), NAME);
- else if (nch == 3)
- res = (validate_lparen(CHILD(tree, 0))
- && validate_fplist(CHILD(tree, 1))
- && validate_rparen(CHILD(tree, 2)));
- else
- res = validate_numnodes(tree, 1, "fpdef");
- }
- return (res);
- }
- static int
- validate_fplist(node *tree)
- {
- return (validate_repeating_list(tree, fplist,
- validate_fpdef, "fplist"));
- }
- /* simple_stmt | compound_stmt
- *
- */
- static int
- validate_stmt(node *tree)
- {
- int res = (validate_ntype(tree, stmt)
- && validate_numnodes(tree, 1, "stmt"));
- if (res) {
- tree = CHILD(tree, 0);
- if (TYPE(tree) == simple_stmt)
- res = validate_simple_stmt(tree);
- else
- res = validate_compound_stmt(tree);
- }
- return (res);
- }
- /* small_stmt (';' small_stmt)* [';'] NEWLINE
- *
- */
- static int
- validate_simple_stmt(node *tree)
- {
- int nch = NCH(tree);
- int res = (validate_ntype(tree, simple_stmt)
- && (nch >= 2)
- && validate_small_stmt(CHILD(tree, 0))
- && validate_newline(CHILD(tree, nch - 1)));
- if (nch < 2)
- res = validate_numnodes(tree, 2, "simple_stmt");
- --nch; /* forget the NEWLINE */
- if (res && is_even(nch))
- res = validate_semi(CHILD(tree, --nch));
- if (res && (nch > 2)) {
- int i;
- for (i = 1; res && (i < nch); i += 2)
- res = (validate_semi(CHILD(tree, i))
- && validate_small_stmt(CHILD(tree, i + 1)));
- }
- return (res);
- }
- static int
- validate_small_stmt(node *tree)
- {
- int nch = NCH(tree);
- int res = validate_numnodes(tree, 1, "small_stmt");
- if (res) {
- int ntype = TYPE(CHILD(tree, 0));
- if ( (ntype == expr_stmt)
- || (ntype == print_stmt)
- || (ntype == del_stmt)
- || (ntype == pass_stmt)
- || (ntype == flow_stmt)
- || (ntype == import_stmt)
- || (ntype == global_stmt)
- || (ntype == assert_stmt)
- || (ntype == exec_stmt))
- res = validate_node(CHILD(tree, 0));
- else {
- res = 0;
- err_string("illegal small_stmt child type");
- }
- }
- else if (nch == 1) {
- res = 0;
- PyErr_Format(parser_error,
- "Unrecognized child node of small_stmt: %d.",
- TYPE(CHILD(tree, 0)));
- }
- return (res);
- }
- /* compound_stmt:
- * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
- */
- static int
- validate_compound_stmt(node *tree)
- {
- int res = (validate_ntype(tree, compound_stmt)
- && validate_numnodes(tree, 1, "compound_stmt"));
- int ntype;
- if (!res)
- return (0);
- tree = CHILD(tree, 0);
- ntype = TYPE(tree);
- if ( (ntype == if_stmt)
- || (ntype == while_stmt)
- || (ntype == for_stmt)
- || (ntype == try_stmt)
- || (ntype == with_stmt)
- || (ntype == funcdef)
- || (ntype == classdef)
- || (ntype == decorated))
- res = validate_node(tree);
- else {
- res = 0;
- PyErr_Format(parser_error,
- "Illegal compound statement type: %d.", TYPE(tree));
- }
- return (res);
- }
- static int
- validate_yield_or_testlist(node *tree)
- {
- if (TYPE(tree) == yield_expr)
- return validate_yield_expr(tree);
- else
- return validate_testlist(tree);
- }
- static int
- validate_expr_stmt(node *tree)
- {
- int j;
- int nch = NCH(tree);
- int res = (validate_ntype(tree, expr_stmt)
- && is_odd(nch)
- && validate_testlist(CHILD(tree, 0)));
- if (res && nch == 3
- && TYPE(CHILD(tree, 1)) == augassign) {
- res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
- && validate_yield_or_testlist(CHILD(tree, 2));
- if (res) {
- char *s = STR(CHILD(CHILD(tree, 1), 0));
- res = (strcmp(s, "+=") == 0
- || strcmp(s, "-=") == 0
- || strcmp(s, "*=") == 0
- || strcmp(s, "/=") == 0
- || strcmp(s, "//=") == 0
- || strcmp(s, "%=") == 0
- || strcmp(s, "&=") == 0
- || strcmp(s, "|=") == 0…