]> pd.if.org Git - pccts/blob - h/DLexerBase.cpp
auto commit for import
[pccts] / h / DLexerBase.cpp
1 /* DLGLexerBase.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.33
24  * Terence Parr
25  * Parr Research Corporation
26  * with Purdue University and AHPCRC, University of Minnesota
27  * 1989-1995
28  */
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 /* I have to put this here due to C++ limitation
33  * that you can't have a 'forward' decl for enums.
34  * I hate C++!!!!!!!!!!!!!!!
35  */
36 enum ANTLRTokenType { TER_HATES_CPP, ITS_UTTER_GARBAGE, WITH_SOME_GOOD_IDEAS };
37
38 #define ANTLR_SUPPORT_CODE
39
40 #include "config.h"
41 #include DLEXERBASE_H
42
43 DLGLexerBase::
44 DLGLexerBase(DLGInputStream *in,
45              unsigned bufsize,
46              int _interactive,
47              int _track_columns)
48 {
49         this->_bufsize = bufsize;
50         this->_lextext = new DLGChar[_bufsize];
51         if ( this->_lextext==NULL ) {
52             panic("text buffer is NULL");
53         }
54         this->_begexpr = this->_endexpr = NULL;
55         this->ch = this->bufovf = 0;
56         this->nextpos = NULL;
57         this->cl = 0;
58         this->add_erase = 0;
59         this->input = in;
60         this->_begcol = 0;
61         this->_endcol = 0;
62         this->_line = 1;
63         this->charfull = 0;
64         this->automaton = 0;
65         this->token_to_fill = NULL;
66         this->interactive = _interactive;
67         this->track_columns = _track_columns;
68 }
69
70 void DLGLexerBase::
71 setInputStream( DLGInputStream *in )
72 {
73         this->input = in;
74         _line = 1;
75         charfull = 0;
76 }
77
78 /* saves dlg state, but not what feeds dlg (such as file position) */
79 void DLGLexerBase::
80 saveState(DLGState *state)
81 {
82         state->input = input;
83         state->interactive = interactive;
84         state->track_columns = track_columns;
85         state->auto_num = automaton;
86         state->add_erase = add_erase;
87         state->lookc = ch;
88         state->char_full = charfull;
89         state->begcol = _begcol;
90         state->endcol = _endcol;
91         state->line = _line;
92         state->lextext = _lextext;
93         state->begexpr = _begexpr;
94         state->endexpr = _endexpr;
95         state->bufsize = _bufsize;
96         state->bufovf = bufovf;
97         state->nextpos = nextpos;
98         state->class_num = cl;
99 }
100
101 void DLGLexerBase::
102 restoreState(DLGState *state)
103 {
104         input = state->input;
105         interactive = state->interactive;
106         track_columns = state->track_columns;
107         automaton = state->auto_num;
108         add_erase = state->add_erase;
109         ch = state->lookc;
110         charfull = state->char_full;
111         _begcol = state->begcol;
112         _endcol = state->endcol;
113         _line = state->line;
114         _lextext = state->lextext;
115         _begexpr = state->begexpr;
116         _endexpr = state->endexpr;
117         _bufsize = state->bufsize;
118         bufovf = state->bufovf;
119         nextpos = state->nextpos;
120         cl = state->class_num;
121 }
122
123 /* erase what is currently in the buffer, and get a new reg. expr */
124 void DLGLexerBase::
125 skip()
126 {
127         add_erase = 1;
128 }
129
130 /* don't erase what is in the lextext buffer, add on to it */
131 void DLGLexerBase::
132 more()
133 {
134         add_erase = 2;
135 }
136
137 /* substitute c for the reg. expr last matched and is in the buffer */
138 void DLGLexerBase::
139 replchar(DLGChar c)
140 {
141         /* can't allow overwriting null at end of string */
142         if (_begexpr < &_lextext[_bufsize-1]){
143                 *_begexpr = c;
144                 *(_begexpr+1) = '\0';
145         }
146         _endexpr = _begexpr;
147         nextpos = _begexpr + 1;
148 }
149
150 /* replace the string s for the reg. expr last matched and in the buffer */
151 void DLGLexerBase::
152 replstr(register DLGChar *s)
153 {
154         register DLGChar *l= &_lextext[_bufsize -1];
155
156         nextpos = _begexpr;
157         if (s){
158                 while ((nextpos <= l) && (*(nextpos++) = *(s++))){
159                         /* empty */
160                 }
161                 /* correct for NULL at end of string */
162                 nextpos--;
163         }
164         if ((nextpos <= l) && (*(--s) == 0)){
165                 bufovf = 0;
166         }else{
167                 bufovf = 1;
168         }
169         *(nextpos) = '\0';
170         _endexpr = nextpos - 1;
171 }
172
173 void DLGLexerBase::
174 errstd(char *s)
175 {
176         fprintf(stderr,
177                 "%s near line %d (text was '%s')\n",
178                 ((s == NULL) ? "Lexical error" : s),
179                 _line,_lextext);
180 }
181
182 int DLGLexerBase::
183 err_in()
184 {
185         fprintf(stderr,"No input stream, function, or string\n");
186         /* return eof to get out gracefully */
187         return EOF;
188 }
189
190 ANTLRTokenType DLGLexerBase::
191 erraction()
192 {
193         errstd("invalid token");
194         advance();
195         skip();
196         return (ANTLRTokenType) 0;      // bogus, but satisfies compiler
197 }
198
199 _ANTLRTokenPtr DLGLexerBase::
200 getToken()
201 {
202         if ( token_to_fill==NULL ) panic("NULL token_to_fill");
203         ANTLRTokenType tt = nextTokenType();
204         _ANTLRTokenPtr tk = token_to_fill->makeToken(tt, _lextext,_line);
205         return tk;
206 }
207
208 void DLGLexerBase::
209 panic(char *msg)
210 {
211         fprintf(stderr, "DLG panic: %s\n", msg);
212         exit(EXIT_FAILURE);
213 }