home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fns101.zip / Add-Ons / Fnorb / src / bisonmodule.c < prev    next >
C/C++ Source or Header  |  1999-06-28  |  6KB  |  219 lines

  1. /***************************************************************
  2.  
  3.   Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1997, 1998. 1999
  4.   Unpublished work.  All Rights Reserved.
  5.  
  6.   The software contained on this media is the property of the
  7.   DSTC Pty Ltd.  Use of this software is strictly in accordance
  8.   with the license agreement in the accompanying LICENSE.HTML
  9.   file.  If your distribution of this software does not contain
  10.   a LICENSE.HTML file then you have no rights to use this
  11.   software in any manner and should contact DSTC at the address
  12.   below to determine an appropriate licensing arrangement.
  13.  
  14.      DSTC Pty Ltd
  15.      Level 7, GP South
  16.      Staff House Road
  17.      University of Queensland
  18.      St Lucia, 4072
  19.      Australia
  20.      Tel: +61 7 3365 4310
  21.      Fax: +61 7 3365 4311
  22.      Email: enquiries@dstc.edu.au
  23.  
  24.   This software is being provided "AS IS" without warranty of
  25.   any kind.  In no event shall DSTC Pty Ltd be liable for
  26.   damage of any kind arising out of or in connection with
  27.   the use or performance of this software.
  28.  
  29.  
  30.   Project:      Fnorb
  31.   File:         $Source: /units/arch/src/Fnorb/src/RCS/bisonmodule.c,v $
  32.   Version:      @(#)$RCSfile: bisonmodule.c,v $ $Revision: 1.5 $
  33.  
  34.  
  35.   Description:    Python interface to bison/flex parsing.
  36.  
  37. ****************************************************************/
  38.  
  39. #if !defined(lint)
  40. static const char rcsid[] = "@(#)$RCSfile: bisonmodule.c,v $ $Revision: 1.5 $";
  41. #endif
  42.  
  43. /*
  44.  * Standard library includes (e.g., stdio)
  45.  */
  46.  
  47. /*
  48.  * Application library includes (e.g., X, DCE)
  49.  */
  50. #include "Python.h"                /* Python API        */
  51.  
  52. /*
  53.  * DSTC library includes (e.g., dstc_stdlib.h)
  54.  */
  55.  
  56. /*
  57.  * Project library includes              
  58.  */
  59.  
  60. /*
  61.  * Local file includes
  62.  */
  63.  
  64. /*
  65.  * The lexer's input stream, and the current line number.
  66.  */
  67. extern FILE* yyin;
  68. extern int yylineno;
  69.  
  70. /*
  71.  * The Python instance that implements the parser's semantic actions.
  72.  */
  73. extern PyObject* py_parser;
  74.  
  75. /*
  76.  * The parsing function generated by 'bison'.
  77.  */
  78. int yyparse(void);
  79.  
  80. /*
  81.  * Exception types.
  82.  */
  83. PyObject* Py_YYERROR;
  84. PyObject* Py_YYABORT;
  85.  
  86.  
  87. static PyObject*
  88. bison_yyparse(PyObject* self, PyObject* args)
  89. {
  90.     PyObject* py_yyin;            /* IDL input stream.        */
  91.  
  92.     /* Make sure we that have the correct number and type of arguments (in this
  93.      * case two Python objects ;^).
  94.      *
  95.      * The first argument is the Python instance that implements the parser's
  96.      * semantic actions.
  97.      *
  98.      * The second argument is an open file object that is used as the lexer's
  99.      * input stream.
  100.      */
  101.     if (!PyArg_ParseTuple(args, "OO!", &py_parser, &PyFile_Type, &py_yyin))
  102.     {
  103.     return NULL;
  104.     }
  105.  
  106.     /* Set the input stream used by the lexer. */
  107.     yyin = PyFile_AsFile(py_yyin);
  108.  
  109.     /* Parse it!
  110.      *
  111.      * 'yyparse' returns 0 if the parse was successful, 1 otherwise.
  112.      */
  113.     return PyInt_FromLong((long) yyparse());
  114. }
  115.  
  116. static PyObject*
  117. bison_yyin(PyObject* self, PyObject* args)
  118. {
  119.     PyObject* py_yyin;            /* Python value for 'yyin'.    */
  120.     PyObject* result;            /* Return value.        */
  121.  
  122.     /* Make sure we that have the correct number and type of arguments (in this
  123.      * case either none or one!).
  124.      */
  125.     if (PyTuple_Size(args) == 0)
  126.     {
  127.     /* Accessor.
  128.      *
  129.      * Create a Python file object from 'yyin'.
  130.      */
  131.     result = PyFile_FromFile(yyin, "yyin", "rw", NULL);
  132.     }
  133.     else if (PyArg_ParseTuple(args, "O!", &PyFile_Type, &py_yyin))
  134.     {
  135.     /* Modifier.
  136.      *
  137.      * Set 'yyin'.
  138.      */
  139.     yyin = PyFile_AsFile(py_yyin);
  140.     
  141.     Py_INCREF(Py_None);
  142.     result = Py_None;
  143.     }
  144.     else
  145.     {
  146.     /* Invalid arguments. */
  147.     result = NULL;
  148.     }
  149.  
  150.     return result;
  151. }
  152.  
  153. static PyObject*
  154. bison_yylineno(PyObject* self, PyObject* args)
  155. {
  156.     PyObject* py_yylineno;        /* Python value for 'yylineno'.    */
  157.     PyObject* result;            /* Return value.        */
  158.  
  159.     /* Make sure we that have the correct number and type of arguments (in this
  160.      * case either none or one!).
  161.      */
  162.     if (PyTuple_Size(args) == 0)
  163.     {
  164.     /* Accessor.
  165.      *
  166.      * Create a Python integer object from 'yylineno'.
  167.      */
  168.     result = PyInt_FromLong((long) yylineno);
  169.     }
  170.     else if (PyArg_ParseTuple(args, "O!", &PyInt_Type, &py_yylineno))
  171.     {
  172.     /* Modifier.
  173.      *
  174.      * Set 'yylineno'.
  175.      */
  176.     yylineno = (int) PyInt_AsLong(py_yylineno);
  177.  
  178.     Py_INCREF(Py_None);
  179.     result = Py_None;
  180.     }
  181.     else
  182.     {
  183.     /* Invalid arguments. */
  184.     result = NULL;
  185.     }
  186.  
  187.     return result;
  188. }
  189.  
  190. static PyMethodDef BisonMethods [] =
  191. {
  192.     {"yyparse",        bison_yyparse,    1},
  193.     {"yyin",        bison_yyin,    1},
  194.     {"yylineno",    bison_yylineno,    1},
  195.     {NULL, NULL, (int) NULL} /* Sentinel. */
  196. };
  197.  
  198. void
  199. initbison()
  200. {
  201.     PyObject* module;
  202.     PyObject* dictionary;
  203.     
  204.     module = Py_InitModule("bison", BisonMethods);
  205.     dictionary = PyModule_GetDict(module);
  206.  
  207.     /* Add the 'YYERROR' and 'YYABORT' exception types to the module. */
  208.     Py_YYERROR = PyErr_NewException("bison.YYERROR", NULL, NULL);
  209.     if (PyDict_SetItemString(dictionary, "YYERROR", Py_YYERROR) != 0)
  210.     Py_FatalError ("Unable to add YYERROR to the module dictionary.");
  211.  
  212.     Py_YYABORT = PyErr_NewException("bison.YYABORT", NULL, NULL);
  213.     if (PyDict_SetItemString(dictionary, "YYABORT", Py_YYABORT) != 0)
  214.     Py_FatalError ("Unable to add YYABORT to the module dictionary.");
  215. }
  216.  
  217. /***************************************************************/
  218. /* end of bisonmodule.c */
  219.