878ddf1f |
1 | /* DLGLexerBase.c\r |
2 | *\r |
3 | * SOFTWARE RIGHTS\r |
4 | *\r |
5 | * We reserve no LEGAL rights to the Purdue Compiler Construction Tool\r |
6 | * Set (PCCTS) -- PCCTS is in the public domain. An individual or\r |
7 | * company may do whatever they wish with source code distributed with\r |
8 | * PCCTS or the code generated by PCCTS, including the incorporation of\r |
9 | * PCCTS, or its output, into commerical software.\r |
10 | *\r |
11 | * We encourage users to develop software with PCCTS. However, we do ask\r |
12 | * that credit is given to us for developing PCCTS. By "credit",\r |
13 | * we mean that if you incorporate our source code into one of your\r |
14 | * programs (commercial product, research project, or otherwise) that you\r |
15 | * acknowledge this fact somewhere in the documentation, research report,\r |
16 | * etc... If you like PCCTS and have developed a nice tool with the\r |
17 | * output, please mention that you developed it using PCCTS. In\r |
18 | * addition, we ask that this header remain intact in our source code.\r |
19 | * As long as these guidelines are kept, we expect to continue enhancing\r |
20 | * this system and expect to make other tools available as they are\r |
21 | * completed.\r |
22 | *\r |
23 | * ANTLR 1.33\r |
24 | * Terence Parr\r |
25 | * Parr Research Corporation\r |
26 | * with Purdue University and AHPCRC, University of Minnesota\r |
27 | * 1989-2000\r |
28 | */\r |
29 | \r |
30 | #include "pcctscfg.h"\r |
31 | \r |
32 | #include "pccts_stdio.h"\r |
33 | #include "pccts_stdlib.h"\r |
34 | \r |
35 | PCCTS_NAMESPACE_STD\r |
36 | \r |
37 | /* I have to put this here due to C++ limitation\r |
38 | * that you can't have a 'forward' decl for enums.\r |
39 | * I hate C++!!!!!!!!!!!!!!!\r |
40 | */\r |
41 | \r |
42 | // MR1\r |
43 | // MR1 10-Apr-97 133MR1 Prevent use of varying sizes for the\r |
44 | // MR1 ANTLRTokenType enum\r |
45 | // MR1\r |
46 | \r |
47 | enum ANTLRTokenType { TER_HATES_CPP=0, ITS_UTTER_GARBAGE, // MR1\r |
48 | WITH_SOME_GOOD_IDEAS=9999}; // MR1\r |
49 | \r |
50 | #define ANTLR_SUPPORT_CODE\r |
51 | \r |
52 | #include "pcctscfg.h"\r |
53 | #include DLEXERBASE_H\r |
54 | #include APARSER_H // MR23\r |
55 | \r |
56 | DLGLexerBase::\r |
57 | DLGLexerBase(DLGInputStream *in,\r |
58 | unsigned bufsize,\r |
59 | int _interactive,\r |
60 | int _track_columns)\r |
61 | {\r |
62 | this->_bufsize = bufsize;\r |
63 | this->_lextext = new DLGChar[_bufsize];\r |
64 | if ( this->_lextext==NULL ) {\r |
65 | panic("text buffer is NULL");\r |
66 | }\r |
67 | this->_begexpr = this->_endexpr = NULL;\r |
68 | this->ch = this->bufovf = 0;\r |
69 | this->nextpos = NULL;\r |
70 | this->cl = 0;\r |
71 | this->add_erase = 0;\r |
72 | this->input = in;\r |
73 | this->_begcol = 0;\r |
74 | this->_endcol = 0;\r |
75 | this->_line = 1;\r |
76 | this->charfull = 0;\r |
77 | this->automaton = 0;\r |
78 | this->token_to_fill = NULL;\r |
79 | this->interactive = _interactive;\r |
80 | this->track_columns = _track_columns;\r |
81 | this->debugLexerFlag = 0; // MR1\r |
82 | this->parser = NULL; // MR1\r |
83 | this->lexErrCount=0; // MR11\r |
84 | }\r |
85 | \r |
86 | // MR19 THM \r |
87 | \r |
88 | void DLGLexerBase::reset()\r |
89 | {\r |
90 | this->charfull = 0;\r |
91 | this->_begcol = 0;\r |
92 | this->_endcol = 0;\r |
93 | this->automaton = 0;\r |
94 | this->_line=1;\r |
95 | this->lexErrCount=0;\r |
96 | }\r |
97 | \r |
98 | void DLGLexerBase::\r |
99 | setInputStream( DLGInputStream *in )\r |
100 | {\r |
101 | this->input = in;\r |
102 | _line = 1;\r |
103 | charfull = 0;\r |
104 | }\r |
105 | \r |
106 | /* saves dlg state, but not what feeds dlg (such as file position) */\r |
107 | void DLGLexerBase::\r |
108 | saveState(DLGState *state)\r |
109 | {\r |
110 | state->input = input;\r |
111 | state->interactive = interactive;\r |
112 | state->track_columns = track_columns;\r |
113 | state->auto_num = automaton;\r |
114 | state->add_erase = add_erase;\r |
115 | state->lookc = ch;\r |
116 | state->char_full = charfull;\r |
117 | state->begcol = _begcol;\r |
118 | state->endcol = _endcol;\r |
119 | state->line = _line;\r |
120 | state->lextext = _lextext;\r |
121 | state->begexpr = _begexpr;\r |
122 | state->endexpr = _endexpr;\r |
123 | state->bufsize = _bufsize;\r |
124 | state->bufovf = bufovf;\r |
125 | state->nextpos = nextpos;\r |
126 | state->class_num = cl;\r |
127 | state->debugLexerFlag = debugLexerFlag; // MR1\r |
128 | state->parser = parser; // MR1\r |
129 | }\r |
130 | \r |
131 | void DLGLexerBase::\r |
132 | restoreState(DLGState *state)\r |
133 | {\r |
134 | input = state->input;\r |
135 | interactive = state->interactive;\r |
136 | track_columns = state->track_columns;\r |
137 | automaton = state->auto_num;\r |
138 | add_erase = state->add_erase;\r |
139 | ch = state->lookc;\r |
140 | charfull = state->char_full;\r |
141 | _begcol = state->begcol;\r |
142 | _endcol = state->endcol;\r |
143 | _line = state->line;\r |
144 | _lextext = state->lextext;\r |
145 | _begexpr = state->begexpr;\r |
146 | _endexpr = state->endexpr;\r |
147 | _bufsize = state->bufsize;\r |
148 | bufovf = state->bufovf;\r |
149 | nextpos = state->nextpos;\r |
150 | cl = state->class_num;\r |
151 | debugLexerFlag = state->debugLexerFlag; // MR1\r |
152 | parser = state->parser; // MR1\r |
153 | }\r |
154 | \r |
155 | /* erase what is currently in the buffer, and get a new reg. expr */\r |
156 | void DLGLexerBase::\r |
157 | skip()\r |
158 | {\r |
159 | add_erase = 1;\r |
160 | }\r |
161 | \r |
162 | /* don't erase what is in the lextext buffer, add on to it */\r |
163 | void DLGLexerBase::\r |
164 | more()\r |
165 | {\r |
166 | add_erase = 2;\r |
167 | }\r |
168 | \r |
169 | /* substitute c for the reg. expr last matched and is in the buffer */\r |
170 | void DLGLexerBase::\r |
171 | replchar(DLGChar c)\r |
172 | {\r |
173 | /* can't allow overwriting null at end of string */\r |
174 | if (_begexpr < &_lextext[_bufsize-1]){\r |
175 | *_begexpr = c;\r |
176 | *(_begexpr+1) = '\0';\r |
177 | }\r |
178 | _endexpr = _begexpr;\r |
179 | if (c != '\0') {\r |
180 | nextpos = _begexpr + 1;\r |
181 | }\r |
182 | else {\r |
183 | nextpos = _begexpr; /* MR30 Zero terminates string. */\r |
184 | }\r |
185 | }\r |
186 | \r |
187 | /* replace the string s for the reg. expr last matched and in the buffer */\r |
188 | \r |
189 | #ifdef _MSC_VER // MR23\r |
190 | //Turn off "assignment within conditional expression" warning\r |
191 | #pragma warning(disable : 4706)\r |
192 | #endif\r |
193 | void DLGLexerBase::\r |
194 | replstr(const DLGChar *s) /* MR20 const */\r |
195 | {\r |
196 | register DLGChar *l= &_lextext[_bufsize -1];\r |
197 | \r |
198 | nextpos = _begexpr;\r |
199 | if (s){\r |
200 | while ((nextpos <= l) && (*(nextpos++) = *(s++))){\r |
201 | /* empty */\r |
202 | }\r |
203 | /* correct for NULL at end of string */\r |
204 | nextpos--;\r |
205 | }\r |
206 | if ((nextpos <= l) && (*(--s) == 0)){\r |
207 | bufovf = 0;\r |
208 | }else{\r |
209 | bufovf = 1;\r |
210 | }\r |
211 | *(nextpos) = '\0';\r |
212 | _endexpr = nextpos - 1;\r |
213 | }\r |
214 | #ifdef _MSC_VER // MR23\r |
215 | #pragma warning(default: 4706)\r |
216 | #endif\r |
217 | \r |
218 | void DLGLexerBase::\r |
219 | errstd(const char *s) /* MR20 const */\r |
220 | {\r |
221 | lexErrCount++; /* MR11 */\r |
222 | /* MR23 */ printMessage(stderr,\r |
223 | "%s near line %d (text was '%s')\n",\r |
224 | ((s == NULL) ? "Lexical error" : s),\r |
225 | _line,_lextext);\r |
226 | }\r |
227 | \r |
228 | int DLGLexerBase::\r |
229 | err_in()\r |
230 | {\r |
231 | /* MR23 */ printMessage(stderr,"No input stream, function, or string\n");\r |
232 | /* return eof to get out gracefully */\r |
233 | return EOF;\r |
234 | }\r |
235 | \r |
236 | ANTLRTokenType DLGLexerBase::\r |
237 | erraction()\r |
238 | {\r |
239 | errstd("invalid token");\r |
240 | advance();\r |
241 | skip();\r |
242 | return (ANTLRTokenType) 0; // bogus, but satisfies compiler\r |
243 | }\r |
244 | \r |
245 | _ANTLRTokenPtr DLGLexerBase::\r |
246 | getToken()\r |
247 | {\r |
248 | if ( token_to_fill==NULL ) panic("NULL token_to_fill");\r |
249 | ANTLRTokenType tt = nextTokenType();\r |
250 | _ANTLRTokenPtr tk = token_to_fill->makeToken(tt, _lextext,_line);\r |
251 | return tk;\r |
252 | }\r |
253 | \r |
254 | void DLGLexerBase::\r |
255 | panic(const char *msg) /* MR20 const */\r |
256 | {\r |
257 | if (parser) //MR23\r |
258 | parser->panic(msg); //MR23\r |
259 | else //MR23\r |
260 | {\r |
261 | /* MR23 */ printMessage(stderr, "DLG panic: %s\n", msg);\r |
262 | //\r |
263 | // 7-Apr-97 133MR1\r |
264 | //\r |
265 | exit(PCCTS_EXIT_FAILURE); // MR1\r |
266 | }\r |
267 | }\r |
268 | \r |
269 | ANTLRParser * DLGLexerBase:: // MR1\r |
270 | setParser(ANTLRParser *p) { // MR1\r |
271 | ANTLRParser *oldValue=parser; // MR1\r |
272 | parser=p; // MR1\r |
273 | return oldValue; // MR1\r |
274 | } // MR1\r |
275 | // MR1\r |
276 | ANTLRParser * DLGLexerBase:: // MR1\r |
277 | getParser() { // MR1\r |
278 | return parser; // MR1\r |
279 | } // MR1\r |
280 | // MR1\r |
281 | int DLGLexerBase:: // MR1\r |
282 | debugLexer(int newValue) { // MR1\r |
283 | int oldValue=debugLexerFlag; // MR1\r |
284 | debugLexerFlag=newValue; // MR1\r |
285 | return oldValue; // MR1\r |
286 | } // MR1\r |
287 | \r |
288 | //MR23\r |
289 | int DLGLexerBase::printMessage(FILE* pFile, const char* pFormat, ...)\r |
290 | {\r |
291 | va_list marker;\r |
292 | va_start( marker, pFormat );\r |
293 | \r |
294 | int iRet = 0;\r |
295 | if (parser)\r |
296 | parser->printMessageV(pFile, pFormat, marker);\r |
297 | else\r |
298 | iRet = vfprintf(pFile, pFormat, marker);\r |
299 | \r |
300 | va_end( marker );\r |
301 | return iRet;\r |
302 | }\r |