]> pd.if.org Git - pccts/blob - h/dlgauto.h
auto commit for import
[pccts] / h / dlgauto.h
1 /* dlgauto.h automaton
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  * Will Cohen and Terence Parr
25  * Parr Research Corporation
26  * with Purdue University and AHPCRC, University of Minnesota
27  * 1989-1995
28  */
29
30 #ifndef ZZDEFAUTO_H
31 #define ZZDEFAUTO_H
32
33 zzchar_t        *zzlextext;     /* text of most recently matched token */
34 zzchar_t        *zzbegexpr;     /* beginning of last reg expr recogn. */
35 zzchar_t        *zzendexpr;     /* beginning of last reg expr recogn. */
36 int     zzbufsize;      /* number of characters in zzlextext */
37 int     zzbegcol = 0;   /* column that first character of token is in*/
38 int     zzendcol = 0;   /* column that last character of token is in */
39 int     zzline = 1;     /* line current token is on */
40 int     zzreal_line=1;  /* line of 1st portion of token that is not skipped */
41 int     zzchar;         /* character to determine next state */
42 int     zzbufovf;       /* indicates that buffer too small for text */
43 int     zzcharfull = 0;
44 static zzchar_t *zznextpos;/* points to next available position in zzlextext*/
45 static int      zzclass;
46
47 #ifdef __USE_PROTOS
48 void    zzerrstd(const char *);
49 void    (*zzerr)(const char *)=zzerrstd;/* pointer to error reporting function */
50 extern int      zzerr_in(void);
51 #else
52 void    zzerrstd();
53 void    (*zzerr)()=zzerrstd;    /* pointer to error reporting function */
54 extern int      zzerr_in();
55 #endif
56
57 static FILE     *zzstream_in=0;
58 static int      (*zzfunc_in)() = zzerr_in;
59 static zzchar_t *zzstr_in=0;
60
61 #ifdef USER_ZZMODE_STACK
62 int               zzauto = 0;
63 #else
64 static int     zzauto = 0;
65 #endif
66 static int      zzadd_erase;
67 static char     zzebuf[70];
68
69 #ifdef ZZCOL
70 #define ZZINC (++zzendcol)
71 #else
72 #define ZZINC
73 #endif
74
75
76 #define ZZGETC_STREAM {zzchar = getc(zzstream_in); zzclass = ZZSHIFT(zzchar);}
77 #define ZZGETC_FUNC {zzchar = (*zzfunc_in)(); zzclass = ZZSHIFT(zzchar);}
78 #define ZZGETC_STR {                    \
79         if (*zzstr_in){                         \
80                 zzchar = *zzstr_in;             \
81                 ++zzstr_in;                             \
82         }else{                                          \
83                 zzchar = EOF;                   \
84         }                                                       \
85         zzclass = ZZSHIFT(zzchar);      \
86 }
87
88 #define ZZNEWSTATE      (newstate = dfa[state][zzclass])
89
90 #ifndef ZZCOPY
91 #define ZZCOPY  \
92         /* Truncate matching buffer to size (not an error) */   \
93         if (zznextpos < lastpos){                               \
94                 *(zznextpos++) = zzchar;                        \
95         }else{                                                  \
96                 zzbufovf = 1;                                   \
97         }
98 #endif
99
100 void
101 #ifdef __USE_PROTOS
102 zzrdstream( FILE *f )
103 #else
104 zzrdstream( f )
105 FILE *f;
106 #endif
107 {
108         /* make sure that it is really set to something, otherwise just 
109            leave it be.
110         */
111         if (f){
112                 /* make sure that there is always someplace to get input
113                    before closing zzstream_in
114                 */
115 #if 0
116                 if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
117 #endif
118                 zzline = 1;
119                 zzstream_in = f;
120                 zzfunc_in = NULL;
121                 zzstr_in = 0;
122                 zzcharfull = 0;
123         }
124 }
125
126 void
127 #ifdef __USE_PROTOS
128 zzrdfunc( int (*f)() )
129 #else
130 zzrdfunc( f )
131 int (*f)();
132 #endif
133 {
134         /* make sure that it is really set to something, otherwise just 
135            leave it be.
136         */
137         if (f){
138                 /* make sure that there is always someplace to get input
139                    before closing zzstream_in
140                 */
141 #if 0
142                 if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
143 #endif
144                 zzline = 1;
145                 zzstream_in = NULL;
146                 zzfunc_in = f;
147                 zzstr_in = 0;
148                 zzcharfull = 0;
149         }
150 }
151
152
153 void
154 #ifdef __USE_PROTOS
155 zzrdstr( zzchar_t *s )
156 #else
157 zzrdstr( s )
158 zzchar_t *s;
159 #endif
160 {
161         /* make sure that it is really set to something, otherwise just 
162            leave it be.
163         */
164         if (s){
165                 /* make sure that there is always someplace to get input
166                    before closing zzstream_in
167                 */
168 #if 0
169                 if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
170 #endif
171                 zzline = 1;
172                 zzstream_in = NULL;
173                 zzfunc_in = 0;
174                 zzstr_in = s;
175                 zzcharfull = 0;
176         }
177 }
178
179
180 void
181 zzclose_stream()
182 {
183 #if 0
184         fclose( zzstream_in );
185         zzstream_in = NULL;
186         zzfunc_in = NULL;
187 #endif
188 }
189
190 /* saves dlg state, but not what feeds dlg (such as file position) */
191 void
192 #ifdef __USE_PROTOS
193 zzsave_dlg_state(struct zzdlg_state *state)
194 #else
195 zzsave_dlg_state(state)
196 struct zzdlg_state *state;
197 #endif
198 {
199         state->stream = zzstream_in;
200         state->func_ptr = zzfunc_in;
201         state->str = zzstr_in;
202         state->auto_num = zzauto;
203         state->add_erase = zzadd_erase;
204         state->lookc = zzchar;
205         state->char_full = zzcharfull;
206         state->begcol = zzbegcol;
207         state->endcol = zzendcol;
208         state->line = zzline;
209         state->lextext = zzlextext;
210         state->begexpr = zzbegexpr;
211         state->endexpr = zzendexpr;
212         state->bufsize = zzbufsize;
213         state->bufovf = zzbufovf;
214         state->nextpos = zznextpos;
215         state->class_num = zzclass;
216 }
217
218 void
219 #ifdef __USE_PROTOS
220 zzrestore_dlg_state(struct zzdlg_state *state)
221 #else
222 zzrestore_dlg_state(state)
223 struct zzdlg_state *state;
224 #endif
225 {
226         zzstream_in = state->stream;
227         zzfunc_in = state->func_ptr;
228         zzstr_in = state->str;
229         zzauto = state->auto_num;
230         zzadd_erase = state->add_erase;
231         zzchar = state->lookc;
232         zzcharfull = state->char_full;
233         zzbegcol = state->begcol;
234         zzendcol = state->endcol;
235         zzline = state->line;
236         zzlextext = state->lextext;
237         zzbegexpr = state->begexpr;
238         zzendexpr = state->endexpr;
239         zzbufsize = state->bufsize;
240         zzbufovf = state->bufovf;
241         zznextpos = state->nextpos;
242         zzclass = state->class_num;
243 }
244
245 void
246 #ifdef __USE_PROTOS
247 zzmode( int m )
248 #else
249 zzmode( m )
250 int m;
251 #endif
252 {
253         /* points to base of dfa table */
254         if (m<MAX_MODE){
255                 zzauto = m;
256                 /* have to redo class since using different compression */
257                 zzclass = ZZSHIFT(zzchar);
258         }else{
259                 sprintf(zzebuf,"Invalid automaton mode = %d ",m);
260                 zzerr(zzebuf);
261         }
262 }
263
264 /* erase what is currently in the buffer, and get a new reg. expr */
265 void
266 zzskip()
267 {
268         zzadd_erase = 1;
269 }
270
271 /* don't erase what is in the zzlextext buffer, add on to it */
272 void
273 zzmore()
274 {
275         zzadd_erase = 2;
276 }
277
278 /* substitute c for the reg. expr last matched and is in the buffer */
279 #ifdef __USE_PROTOS
280 void
281 zzreplchar(zzchar_t c)
282 #else
283 void
284 zzreplchar(c)
285 zzchar_t c;
286 #endif
287 {
288         /* can't allow overwriting null at end of string */
289         if (zzbegexpr < &zzlextext[zzbufsize-1]){
290                 *zzbegexpr = c;
291                 *(zzbegexpr+1) = '\0';
292         }
293         zzendexpr = zzbegexpr;
294         zznextpos = zzbegexpr + 1;
295 }
296
297 /* replace the string s for the reg. expr last matched and in the buffer */
298 void
299 #ifdef __USE_PROTOS
300 zzreplstr(register zzchar_t *s)
301 #else
302 zzreplstr(s)
303 register zzchar_t *s;
304 #endif
305 {
306         register zzchar_t *l= &zzlextext[zzbufsize -1];
307
308         zznextpos = zzbegexpr;
309         if (s){
310                 while ((zznextpos <= l) && (*(zznextpos++) = *(s++))!=0){
311                         /* empty */
312                 }
313                 /* correct for NULL at end of string */
314                 zznextpos--;
315         }
316         if ((zznextpos <= l) && (*(--s) == 0)){
317                 zzbufovf = 0;
318         }else{
319                 zzbufovf = 1;
320         }
321         *(zznextpos) = '\0';
322         zzendexpr = zznextpos - 1;
323 }
324
325 void
326 zzgettok()
327 {
328         register int state, newstate;
329         /* last space reserved for the null char */
330         register zzchar_t *lastpos;
331
332 skip:
333         zzreal_line = zzline;
334         zzbufovf = 0;
335         lastpos = &zzlextext[zzbufsize-1];
336         zznextpos = zzlextext;
337         zzbegcol = zzendcol+1;
338 more:
339         zzbegexpr = zznextpos;
340 #ifdef ZZINTERACTIVE
341         /* interactive version of automaton */
342         /* if there is something in zzchar, process it */
343         state = newstate = dfa_base[zzauto];
344         if (zzcharfull){
345                 ZZINC;
346                 ZZCOPY;
347                 ZZNEWSTATE;
348         }
349         if (zzstr_in)
350                 while (zzalternatives[newstate]){
351                         state = newstate;
352                         ZZGETC_STR;
353                         ZZINC;
354                         ZZCOPY;
355                         ZZNEWSTATE;
356                 }
357         else if (zzstream_in)
358                 while (zzalternatives[newstate]){
359                         state = newstate;
360                         ZZGETC_STREAM;
361                         ZZINC;
362                         ZZCOPY;
363                         ZZNEWSTATE;
364                 }
365         else if (zzfunc_in)
366                 while (zzalternatives[newstate]){
367                         state = newstate;
368                         ZZGETC_FUNC;
369                         ZZINC;
370                         ZZCOPY;
371                         ZZNEWSTATE;
372                 }
373         /* figure out if last character really part of token */
374         if ((state != dfa_base[zzauto]) && (newstate == DfaStates)){
375                 zzcharfull = 1;
376                 --zznextpos;
377         }else{
378                 zzcharfull = 0;
379                 state = newstate;
380         }
381         *(zznextpos) = '\0';
382         /* Able to transition out of start state to some non err state?*/
383         if ( state == dfa_base[zzauto] ){
384                 /* make sure doesn't get stuck */
385                 zzadvance();
386         }
387 #else
388         /* non-interactive version of automaton */
389         if (!zzcharfull)
390                 zzadvance();
391         else
392                 ZZINC;
393         state = dfa_base[zzauto];
394         if (zzstr_in)
395                 while (ZZNEWSTATE != DfaStates){
396                         state = newstate;
397                         ZZCOPY;
398                         ZZGETC_STR;
399                         ZZINC;
400                 }
401         else if (zzstream_in)
402                 while (ZZNEWSTATE != DfaStates){
403                         state = newstate;
404                         ZZCOPY;
405                         ZZGETC_STREAM;
406                         ZZINC;
407                 }
408         else if (zzfunc_in)
409                 while (ZZNEWSTATE != DfaStates){
410                         state = newstate;
411                         ZZCOPY;
412                         ZZGETC_FUNC;
413                         ZZINC;
414                 }
415         zzcharfull = 1;
416         if ( state == dfa_base[zzauto] ){
417                 if (zznextpos < lastpos){
418                         *(zznextpos++) = zzchar;
419                 }else{
420                         zzbufovf = 1;
421                 }
422                 *zznextpos = '\0';
423                 /* make sure doesn't get stuck */
424                 zzadvance();
425         }else{
426                 *zznextpos = '\0';
427         }
428 #endif
429 #ifdef ZZCOL
430         zzendcol -= zzcharfull;
431 #endif
432         zzendexpr = zznextpos -1;
433         zzadd_erase = 0;
434         (*actions[accepts[state]])();
435         switch (zzadd_erase) {
436                 case 1: goto skip;
437                 case 2: goto more;
438         }
439 }
440
441 void
442 zzadvance()
443 {
444         if (zzstream_in) { ZZGETC_STREAM; zzcharfull = 1; ZZINC;}
445         if (zzfunc_in) { ZZGETC_FUNC; zzcharfull = 1; ZZINC;}
446         if (zzstr_in) { ZZGETC_STR; zzcharfull = 1; ZZINC;}
447         if (!(zzstream_in || zzfunc_in || zzstr_in)){
448                 zzerr_in();
449         }
450 }
451
452 void
453 #ifdef __USE_PROTOS
454 zzerrstd(const char *s)
455 #else
456 zzerrstd(s)
457 char *s;
458 #endif
459 {
460         fprintf(stderr,
461                 "%s near line %d (text was '%s')\n",
462                 ((s == NULL) ? "Lexical error" : s),
463                 zzline,zzlextext);
464 }
465
466 int
467 zzerr_in()
468 {
469         fprintf(stderr,"No input stream, function, or string\n");
470         /* return eof to get out gracefully */
471         return EOF;
472 }
473
474 #endif