]>
Commit | Line | Data |
---|---|---|
3eb9473e | 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-1998\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 | \r | |
55 | DLGLexerBase::\r | |
56 | DLGLexerBase(DLGInputStream *in,\r | |
57 | unsigned bufsize,\r | |
58 | int _interactive,\r | |
59 | int _track_columns)\r | |
60 | {\r | |
61 | this->_bufsize = bufsize;\r | |
62 | this->_lextext = new DLGChar[_bufsize];\r | |
63 | if ( this->_lextext==NULL ) {\r | |
64 | panic("text buffer is NULL");\r | |
65 | }\r | |
66 | this->_begexpr = this->_endexpr = NULL;\r | |
67 | this->ch = this->bufovf = 0;\r | |
68 | this->nextpos = NULL;\r | |
69 | this->cl = 0;\r | |
70 | this->add_erase = 0;\r | |
71 | this->input = in;\r | |
72 | this->_begcol = 0;\r | |
73 | this->_endcol = 0;\r | |
74 | this->_line = 1;\r | |
75 | this->charfull = 0;\r | |
76 | this->automaton = 0;\r | |
77 | this->token_to_fill = NULL;\r | |
78 | this->interactive = _interactive;\r | |
79 | this->track_columns = _track_columns;\r | |
80 | this->debugLexerFlag = 0; // MR1\r | |
81 | this->parser = NULL; // MR1\r | |
82 | this->lexErrCount=0; // MR11\r | |
83 | }\r | |
84 | \r | |
85 | // MR19 THM \r | |
86 | \r | |
87 | void DLGLexerBase::reset()\r | |
88 | {\r | |
89 | this->charfull = 0;\r | |
90 | this->_begcol = 0;\r | |
91 | this->_endcol = 0;\r | |
92 | this->automaton = 0;\r | |
93 | this->_line=1;\r | |
94 | this->lexErrCount=0;\r | |
95 | }\r | |
96 | \r | |
97 | void DLGLexerBase::\r | |
98 | setInputStream( DLGInputStream *in )\r | |
99 | {\r | |
100 | this->input = in;\r | |
101 | _line = 1;\r | |
102 | charfull = 0;\r | |
103 | }\r | |
104 | \r | |
105 | /* saves dlg state, but not what feeds dlg (such as file position) */\r | |
106 | void DLGLexerBase::\r | |
107 | saveState(DLGState *state)\r | |
108 | {\r | |
109 | state->input = input;\r | |
110 | state->interactive = interactive;\r | |
111 | state->track_columns = track_columns;\r | |
112 | state->auto_num = automaton;\r | |
113 | state->add_erase = add_erase;\r | |
114 | state->lookc = ch;\r | |
115 | state->char_full = charfull;\r | |
116 | state->begcol = _begcol;\r | |
117 | state->endcol = _endcol;\r | |
118 | state->line = _line;\r | |
119 | state->lextext = _lextext;\r | |
120 | state->begexpr = _begexpr;\r | |
121 | state->endexpr = _endexpr;\r | |
122 | state->bufsize = _bufsize;\r | |
123 | state->bufovf = bufovf;\r | |
124 | state->nextpos = nextpos;\r | |
125 | state->class_num = cl;\r | |
126 | state->debugLexerFlag = debugLexerFlag; // MR1\r | |
127 | state->parser = parser; // MR1\r | |
128 | }\r | |
129 | \r | |
130 | void DLGLexerBase::\r | |
131 | restoreState(DLGState *state)\r | |
132 | {\r | |
133 | input = state->input;\r | |
134 | interactive = state->interactive;\r | |
135 | track_columns = state->track_columns;\r | |
136 | automaton = state->auto_num;\r | |
137 | add_erase = state->add_erase;\r | |
138 | ch = state->lookc;\r | |
139 | charfull = state->char_full;\r | |
140 | _begcol = state->begcol;\r | |
141 | _endcol = state->endcol;\r | |
142 | _line = state->line;\r | |
143 | _lextext = state->lextext;\r | |
144 | _begexpr = state->begexpr;\r | |
145 | _endexpr = state->endexpr;\r | |
146 | _bufsize = state->bufsize;\r | |
147 | bufovf = state->bufovf;\r | |
148 | nextpos = state->nextpos;\r | |
149 | cl = state->class_num;\r | |
150 | debugLexerFlag = state->debugLexerFlag; // MR1\r | |
151 | parser = state->parser; // MR1\r | |
152 | }\r | |
153 | \r | |
154 | /* erase what is currently in the buffer, and get a new reg. expr */\r | |
155 | void DLGLexerBase::\r | |
156 | skip()\r | |
157 | {\r | |
158 | add_erase = 1;\r | |
159 | }\r | |
160 | \r | |
161 | /* don't erase what is in the lextext buffer, add on to it */\r | |
162 | void DLGLexerBase::\r | |
163 | more()\r | |
164 | {\r | |
165 | add_erase = 2;\r | |
166 | }\r | |
167 | \r | |
168 | /* substitute c for the reg. expr last matched and is in the buffer */\r | |
169 | void DLGLexerBase::\r | |
170 | replchar(DLGChar c)\r | |
171 | {\r | |
172 | /* can't allow overwriting null at end of string */\r | |
173 | if (_begexpr < &_lextext[_bufsize-1]){\r | |
174 | *_begexpr = c;\r | |
175 | *(_begexpr+1) = '\0';\r | |
176 | }\r | |
177 | _endexpr = _begexpr;\r | |
178 | nextpos = _begexpr + 1;\r | |
179 | }\r | |
180 | \r | |
181 | /* replace the string s for the reg. expr last matched and in the buffer */\r | |
182 | \r | |
183 | void DLGLexerBase::\r | |
184 | replstr(const DLGChar *s) /* MR20 const */\r | |
185 | {\r | |
186 | register DLGChar *l= &_lextext[_bufsize -1];\r | |
187 | \r | |
188 | nextpos = _begexpr;\r | |
189 | if (s){\r | |
190 | while ((nextpos <= l) && (*(nextpos++) = *(s++))){\r | |
191 | /* empty */\r | |
192 | }\r | |
193 | /* correct for NULL at end of string */\r | |
194 | nextpos--;\r | |
195 | }\r | |
196 | if ((nextpos <= l) && (*(--s) == 0)){\r | |
197 | bufovf = 0;\r | |
198 | }else{\r | |
199 | bufovf = 1;\r | |
200 | }\r | |
201 | *(nextpos) = '\0';\r | |
202 | _endexpr = nextpos - 1;\r | |
203 | }\r | |
204 | \r | |
205 | void DLGLexerBase::\r | |
206 | errstd(const char *s) /* MR20 const */\r | |
207 | {\r | |
208 | lexErrCount++; /* MR11 */\r | |
209 | fprintf(stderr,\r | |
210 | "%s near line %d (text was '%s')\n",\r | |
211 | ((s == NULL) ? "Lexical error" : s),\r | |
212 | _line,_lextext);\r | |
213 | }\r | |
214 | \r | |
215 | int DLGLexerBase::\r | |
216 | err_in()\r | |
217 | {\r | |
218 | fprintf(stderr,"No input stream, function, or string\n");\r | |
219 | /* return eof to get out gracefully */\r | |
220 | return EOF;\r | |
221 | }\r | |
222 | \r | |
223 | ANTLRTokenType DLGLexerBase::\r | |
224 | erraction()\r | |
225 | {\r | |
226 | errstd("invalid token");\r | |
227 | advance();\r | |
228 | skip();\r | |
229 | return (ANTLRTokenType) 0; // bogus, but satisfies compiler\r | |
230 | }\r | |
231 | \r | |
232 | _ANTLRTokenPtr DLGLexerBase::\r | |
233 | getToken()\r | |
234 | {\r | |
235 | if ( token_to_fill==NULL ) panic("NULL token_to_fill");\r | |
236 | ANTLRTokenType tt = nextTokenType();\r | |
237 | _ANTLRTokenPtr tk = token_to_fill->makeToken(tt, _lextext,_line);\r | |
238 | return tk;\r | |
239 | }\r | |
240 | \r | |
241 | void DLGLexerBase::\r | |
242 | panic(const char *msg) /* MR20 const */\r | |
243 | {\r | |
244 | fprintf(stderr, "DLG panic: %s\n", msg);\r | |
245 | //\r | |
246 | // 7-Apr-97 133MR1\r | |
247 | //\r | |
248 | exit(PCCTS_EXIT_FAILURE); // MR1\r | |
249 | }\r | |
250 | \r | |
251 | ANTLRParser * DLGLexerBase:: // MR1\r | |
252 | setParser(ANTLRParser *p) { // MR1\r | |
253 | ANTLRParser *oldValue=parser; // MR1\r | |
254 | parser=p; // MR1\r | |
255 | return oldValue; // MR1\r | |
256 | } // MR1\r | |
257 | // MR1\r | |
258 | ANTLRParser * DLGLexerBase:: // MR1\r | |
259 | getParser() { // MR1\r | |
260 | return parser; // MR1\r | |
261 | } // MR1\r | |
262 | // MR1\r | |
263 | int DLGLexerBase:: // MR1\r | |
264 | debugLexer(int newValue) { // MR1\r | |
265 | int oldValue=debugLexerFlag; // MR1\r | |
266 | debugLexerFlag=newValue; // MR1\r | |
267 | return oldValue; // MR1\r | |
268 | } // MR1\r |