]> pd.if.org Git - pccts/blob - h/DLexerBase.h
auto commit for import
[pccts] / h / DLexerBase.h
1 /* DLGLexerBase.h
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
30 #ifndef DLGX_H
31 #define DLGX_H
32
33 #include <stdio.h>
34 #include "config.h"
35 #include ATOKEN_H
36 #include ATOKENSTREAM_H
37
38 /* must define what a char looks like; can make this a class too */
39 typedef char DLGChar;
40
41 /*  Can have it as a class too: (ack this looks weird; is it right?)
42 class DLGChar {
43 private:
44         int c;
45 public:
46         DLGChar(int ch) { c = ch; }
47         int atom() { return c; }
48 };
49 */
50
51 /* user must subclass this */
52 class DLGInputStream {
53 public:
54         virtual int nextChar() = 0;
55 };
56
57 /* Predefined char stream: Input from FILE */
58 class DLGFileInput : public DLGInputStream {
59 private:
60         int found_eof;
61         FILE *input;
62 public:
63         DLGFileInput(FILE *f) { input = f; found_eof = 0; }
64         int nextChar() {
65                         int c;
66                         if ( found_eof ) return EOF;
67                         else {
68                                 c=getc(input);
69                                 if ( c==EOF ) found_eof = 1;
70                                 return c;
71                         }
72                 }
73 };
74
75 /* Predefined char stream: Input from string */
76 class DLGStringInput : public DLGInputStream {
77 private:
78         DLGChar *input;
79         DLGChar *p;
80 public:
81         DLGStringInput(DLGChar *s) { input = s; p = &input[0];}
82         int nextChar()
83                 {
84                         if (*p) return (int) *p++;
85                         else return EOF;
86                 }
87 };
88
89 class DLGState {
90 public:
91         DLGInputStream *input;
92         int interactive;
93         int track_columns;
94         int auto_num;
95         int add_erase;
96         int lookc;
97         int char_full;
98         int begcol, endcol;
99         int line;
100         DLGChar *lextext, *begexpr, *endexpr;
101         int bufsize;
102         int bufovf;
103         DLGChar *nextpos;
104         int     class_num;
105 };
106
107 /* user must subclass this */
108 class DLGLexerBase : public ANTLRTokenStream {
109 public:
110         virtual ANTLRTokenType erraction();
111
112 protected:
113         DLGInputStream *input;
114         int interactive;
115         int track_columns;
116         DLGChar *_lextext;      /* text of most recently matched token */
117         DLGChar *_begexpr;      /* beginning of last reg expr recogn. */
118         DLGChar *_endexpr;      /* beginning of last reg expr recogn. */
119         int     _bufsize;               /* number of characters in lextext */
120         int     _begcol;                /* column that first character of token is in*/
121         int     _endcol;                /* column that last character of token is in */
122         int     _line;                  /* line current token is on */
123         int     ch;                             /* character to determine next state */
124         int     bufovf;                 /* indicates that buffer too small for text */
125         int     charfull;
126         DLGChar *nextpos;       /* points to next available position in lextext*/
127         int     cl;
128         int automaton;
129         int     add_erase;
130         DLGChar ebuf[70];
131         _ANTLRTokenPtr token_to_fill;
132
133         virtual _ANTLRTokenPtr getToken();
134
135 public:
136         virtual void advance(void) = 0;
137         void    skip(void);             /* erase lextext, look for antoher token */
138         void    more(void);             /* keep lextext, look for another token */
139         void    mode(int k);    /* switch to automaton 'k' */
140         void    saveState(DLGState *);
141         void    restoreState(DLGState *);
142         virtual ANTLRTokenType nextTokenType(void)=0;/* get next token */
143         void    replchar(DLGChar c);    /* replace last recognized reg. expr. with
144                                                                          a character */
145         void    replstr(DLGChar *s);    /* replace last recognized reg. expr. with
146                                                                          a string */
147         int             err_in();
148         void    errstd(char *);
149
150         int             line()          { return _line; }
151         virtual void newline()  { _line++; }
152         DLGChar *lextext()      { return _lextext; }
153
154         int             begcol()        { return _begcol; }
155         int             endcol()        { return _endcol; }
156         void    set_begcol(int a)       { _begcol=a; }
157         void    set_endcol(int a)       { _endcol=a; }
158         DLGChar *begexpr()      { return _begexpr; }
159         DLGChar *endexpr()      { return _endexpr; }
160         int             bufsize()       { return _bufsize; }
161
162         void    setToken(ANTLRAbstractToken *t) { token_to_fill = t; }
163
164         void    setInputStream(DLGInputStream *);
165         DLGLexerBase(DLGInputStream *in,
166                                  unsigned bufsize=2000,
167                                  int interactive=0,
168                                  int track_columns=0);
169         virtual ~DLGLexerBase() { delete [] _lextext; }
170         void    panic(char *msg);
171
172         void    trackColumns() {
173                                 track_columns = 1;
174                                 this->_begcol = 0;
175                                 this->_endcol = 0;
176                         }
177 };
178
179 #endif