home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pccts.zip / pccts / h / dlgx.C < prev    next >
C/C++ Source or Header  |  1994-03-31  |  5KB  |  205 lines

  1. /* dlgx.c
  2.  *
  3.  * SOFTWARE RIGHTS
  4.  *
  5.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  6.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  7.  * company may do whatever they wish with source code distributed with
  8.  * PCCTS or the code generated by PCCTS, including the incorporation of
  9.  * PCCTS, or its output, into commerical software.
  10.  * 
  11.  * We encourage users to develop software with PCCTS.  However, we do ask
  12.  * that credit is given to us for developing PCCTS.  By "credit",
  13.  * we mean that if you incorporate our source code into one of your
  14.  * programs (commercial product, research project, or otherwise) that you
  15.  * acknowledge this fact somewhere in the documentation, research report,
  16.  * etc...  If you like PCCTS and have developed a nice tool with the
  17.  * output, please mention that you developed it using PCCTS.  In
  18.  * addition, we ask that this header remain intact in our source code.
  19.  * As long as these guidelines are kept, we expect to continue enhancing
  20.  * this system and expect to make other tools available as they are
  21.  * completed.
  22.  *
  23.  * ANTLR 1.20
  24.  * Terence Parr
  25.  * Purdue University
  26.  * With AHPCRC, University of Minnesota
  27.  * 1989-1994
  28.  */
  29. #include <stdio.h>
  30.  
  31. /* I have to put this here due to C++ limitation
  32.  * that you can't have a 'forward' decl for enums.
  33.  * I hate C++!!!!!!!!!!!!!!!
  34.  */
  35. enum TokenType { TER_HATES_CPP, ITS_UTTER_GARBAGE, WITH_SOME_GOOD_IDEAS };
  36.  
  37. #include "dlgx.h"
  38.  
  39. DLGLexerBase::
  40. DLGLexerBase(DLGInputStream *in, unsigned bufsize, int interactive=0, int track_columns=0)
  41. {
  42.     this->_bufsize = bufsize;
  43.     this->_lextext = new DLGChar[_bufsize];
  44.     if ( this->_lextext==NULL ) {
  45.         /* FATAL: what to do? */
  46.     }
  47.     this->_begexpr = this->_endexpr = NULL;
  48.     this->ch = this->bufovf = 0;
  49.     this->nextpos = NULL;
  50.     this->cl = 0;
  51.     this->add_erase = 0;
  52.     this->input = in;
  53.     this->_begcol = 0;
  54.     this->_endcol = 0;
  55.     this->_line = 1;
  56.     this->charfull = 0;
  57.     this->automaton = 0;
  58.     this->token_to_fill = NULL;
  59.     this->interactive = interactive;
  60.     this->track_columns = track_columns;
  61. }
  62.  
  63. void DLGLexerBase::
  64. setInputStream( DLGInputStream *in )
  65. {
  66.     this->input = in;
  67.     _line = 1;
  68.     charfull = 0;
  69. }
  70.  
  71. /* saves dlg state, but not what feeds dlg (such as file position) */
  72. void DLGLexerBase::
  73. saveState(DLGState *state)
  74. {
  75.     state->input = input;
  76.     state->interactive = interactive;
  77.     state->track_columns = track_columns;
  78.     state->auto_num = automaton;
  79.     state->add_erase = add_erase;
  80.     state->lookc = ch;
  81.     state->char_full = charfull;
  82.     state->begcol = _begcol;
  83.     state->endcol = _endcol;
  84.     state->line = _line;
  85.     state->lextext = _lextext;
  86.     state->begexpr = _begexpr;
  87.     state->endexpr = _endexpr;
  88.     state->bufsize = _bufsize;
  89.     state->bufovf = bufovf;
  90.     state->nextpos = nextpos;
  91.     state->class_num = cl;
  92. }
  93.  
  94. void DLGLexerBase::
  95. restoreState(DLGState *state)
  96. {
  97.     input = state->input;
  98.     interactive = state->interactive;
  99.     track_columns = state->track_columns;
  100.     automaton = state->auto_num;
  101.     add_erase = state->add_erase;
  102.     ch = state->lookc;
  103.     charfull = state->char_full;
  104.     _begcol = state->begcol;
  105.     _endcol = state->endcol;
  106.     _line = state->line;
  107.     _lextext = state->lextext;
  108.     _begexpr = state->begexpr;
  109.     _endexpr = state->endexpr;
  110.     _bufsize = state->bufsize;
  111.     bufovf = state->bufovf;
  112.     nextpos = state->nextpos;
  113.     cl = state->class_num;
  114. }
  115.  
  116. /* erase what is currently in the buffer, and get a new reg. expr */
  117. void DLGLexerBase::
  118. skip()
  119. {
  120.     add_erase = 1;
  121. }
  122.  
  123. /* don't erase what is in the lextext buffer, add on to it */
  124. void DLGLexerBase::
  125. more()
  126. {
  127.     add_erase = 2;
  128. }
  129.  
  130. /* substitute c for the reg. expr last matched and is in the buffer */
  131. void DLGLexerBase::
  132. replchar(DLGChar c)
  133. {
  134.     /* can't allow overwriting null at end of string */
  135.     if (_begexpr < &_lextext[_bufsize-1]){
  136.         *_begexpr = c;
  137.         *(_begexpr+1) = '\0';
  138.     }
  139.     _endexpr = _begexpr;
  140.     nextpos = _begexpr + 1;
  141. }
  142.  
  143. /* replace the string s for the reg. expr last matched and in the buffer */
  144. void DLGLexerBase::
  145. replstr(register DLGChar *s)
  146. {
  147.     register DLGChar *l= &_lextext[_bufsize -1];
  148.  
  149.     nextpos = _begexpr;
  150.     if (s){
  151.          while ((nextpos <= l) && (*(nextpos++) = *(s++))){
  152.             /* empty */
  153.         }
  154.         /* correct for NULL at end of string */
  155.         nextpos--;
  156.     }
  157.     if ((nextpos <= l) && (*(--s) == 0)){
  158.         bufovf = 0;
  159.     }else{
  160.         bufovf = 1;
  161.     }
  162.     *(nextpos) = '\0';
  163.     _endexpr = nextpos - 1;
  164. }
  165.  
  166. void DLGLexerBase::
  167. errstd(DLGChar *s)
  168. {
  169.         fprintf(stderr,
  170.                 "%s near line %d (text was '%s')\n",
  171.                 ((s == NULL) ? (DLGChar *)"Lexical error" : s),
  172.                 _line,_lextext);
  173. }
  174.  
  175. int DLGLexerBase::
  176. err_in()
  177. {
  178.     fprintf(stderr,"No input stream, function, or string\n");
  179.     /* return eof to get out gracefully */
  180.     return EOF;
  181. }
  182.  
  183. void DLGLexerBase::
  184. erraction()
  185. {
  186.     errstd("invalid token");
  187.     advance();
  188.     skip();
  189. }
  190.  
  191. ANTLRTokenBase *DLGLexerBase::
  192. nextToken()
  193. {
  194.     if ( token_to_fill==NULL ) DLGPanic("NULL token_to_fill");
  195.     token_to_fill->makeToken(gettok(), _lextext);
  196.     return token_to_fill;
  197. }
  198.  
  199. void DLGLexerBase::
  200. DLGPanic(DLGChar *msg)
  201. {
  202.     fprintf(stderr, "DLG panic: %s\n", msg);
  203.     exit(-1);
  204. }
  205.