1 /* Generated By:JavaCC: Do not edit this line. CharStream.h Version 7.0 */
2 /* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
3 #ifndef JAVACC_CHARSTREAM_H_
4 #define JAVACC_CHARSTREAM_H_
5 
6 #include "JavaCC.h"
7 
8 #ifndef INITIAL_BUFFER_SIZE
9 #define INITIAL_BUFFER_SIZE 4096
10 #endif
11 
12 namespace vhdl {
13 namespace parser {
14 
15 /**
16  * This class describes a character stream that maintains line and
17  * column number positions of the characters.  It also has the capability
18  * to backup the stream to some extent.  An implementation of this
19  * class is used in the TokenManager implementation generated by
20  * JavaCCParser.
21  *
22  * All the methods except backup can be implemented in any fashion. backup
23  * needs to be implemented correctly for the correct operation of the lexer.
24  * Rest of the methods are all used to get information like line number,
25  * column number and the string that constitutes a token and are not used
26  * by the lexer. Hence their implementation won't affect the generated lexer's
27  * operation.
28  */
29 
30 
31 class CharStream {
32 public:
setTabSize(int i)33    void setTabSize(int i) { tabSize = i; }
getTabSize(int i)34    int  getTabSize(int i) { return tabSize; }
35 
36 private:
getBufcolumn(int pos)37   int getBufcolumn(int pos) {
38     if (trackLineColumn && pos>=0) {
39       return bufcolumn[pos];
40     } else {
41       return -1;
42     }
43   }
getBufline(int pos)44   int getBufline(int pos) {
45     if (trackLineColumn && pos>=0) {
46       return bufline[pos];
47     } else {
48       return -1;
49     }
50   }
51 
52 public:
getColumn()53   virtual int getColumn()        { return getBufcolumn(bufpos); }
getLine()54   virtual int getLine()          { return getBufline(bufpos); }
getEndColumn()55   virtual int getEndColumn()     { return getBufcolumn(bufpos); }
getEndLine()56   virtual int getEndLine()       { return getBufline(bufpos); }
getBeginColumn()57   virtual int getBeginColumn()   { return getBufcolumn(tokenBegin); }
getBeginLine()58   virtual int getBeginLine()     { return getBufline(tokenBegin); }
59 
getTrackLineColumn()60   virtual bool getTrackLineColumn()         { return trackLineColumn; }
setTrackLineColumn(bool val)61   virtual void setTrackLineColumn(bool val) { trackLineColumn = val; }
62 
63 /**
64  * Backs up the input stream by amount steps. Lexer calls this method if it
65  * had already read some characters, but could not use them to match a
66  * (longer) token. So, they will be used again as the prefix of the next
67  * token and it is the implemetation's responsibility to do this right.
68  */
backup(int amount)69   virtual inline void backup(int amount) {
70     inBuf += amount;
71     bufpos -= amount;
72     if (bufpos < 0) {
73       bufpos += bufsize;
74     }
75   }
76 
77 /**
78  * Returns the next character that marks the beginning of the next token.
79  * All characters must remain in the buffer between two successive calls
80  * to this method to implement backup correctly.
81  */
BeginToken()82   virtual inline JJChar BeginToken() {
83     tokenBegin = -1;
84     JJChar c = readChar();
85     tokenBegin = bufpos;
86     return c;
87   }
88 
89 
90 /**
91  * Returns the next character from the selected input.  The method
92  * of selecting the input is the responsibility of the class
93  * implementing this class.
94  */
readChar()95   virtual inline JJChar readChar() {
96     if (inBuf > 0) {
97       --inBuf;
98       ++bufpos;
99       if (bufpos == bufsize) {
100         bufpos = 0;
101       }
102       return buffer[bufpos];
103     }
104 
105     ++bufpos;
106     if (bufpos >= maxNextCharInd) {
107       FillBuff();
108     }
109 
110     JJChar c = buffer[bufpos];
111 
112     if (trackLineColumn) {
113       UpdateLineColumn(c);
114     }
115 
116     return c;
117   }
118 
119 
120   virtual void ExpandBuff(bool wrapAround);
121   virtual void FillBuff();
122 
123   /**
124    * Returns a string made up of characters from the marked token beginning
125    * to the current buffer position. Implementations can return
126    * anything that they want to. For example, for efficiency, one might decide
127    * to just return NULL, which is a valid implementation.
128    */
GetImage()129   virtual JJString GetImage() {
130     if (bufpos >= tokenBegin)
131       return JJString(buffer + tokenBegin, bufpos - tokenBegin + 1);
132     else
133       return JJString(buffer + tokenBegin, bufsize - tokenBegin).append(buffer, bufpos + 1);
134   }
135 
136   /**
137    * Returns an array of characters that make up the suffix of length 'len' for
138    * the currently matched token. This is used to build up the matched string
139    * for use in actions in the case of MORE. A simple and inefficient
140    * implementation of this is as follows :
141    */
GetSuffix(int len)142   virtual JJString GetSuffix(int len) {
143     if ((bufpos + 1) >= len) {
144       return JJString(buffer + bufpos - len + 1, len);
145     }
146     return JJString(buffer + bufsize - (len - bufpos - 1), len - bufpos - 1).append(buffer, bufpos + 1);
147   }
148 
149   /**
150    * The lexer calls this function to indicate that it is done with the stream
151    * and hence implementations can free any resources held by this class.
152    */
153   virtual void DeleteBuffers();
154 
~CharStream()155   virtual ~CharStream() {
156     if (deleteStream) {
157      delete inputStream;
158     }
159     DeleteBuffers();
160   }
161 
endOfInput()162   bool endOfInput() {
163     return inBuf == 0 && bufpos + 1 >= maxNextCharInd && inputStream->endOfInput();
164   }
165 
CharStream(const JJChar * buf,int sz,int startline,int startcolumn,int buffersize)166   CharStream(const JJChar *buf, int sz, int startline,
167                       int startcolumn, int buffersize) :
168     bufline(nullptr), bufcolumn(nullptr), buffer(nullptr), bufpos(0), bufsize(0),
169     tokenBegin(0), column(0), line(0), prevCharIsCR(false), prevCharIsLF(false),
170     available(0), maxNextCharInd(0), inBuf(0), tabSize(1), trackLineColumn(true),
171     inputStream(nullptr), deleteStream(false) {
172     ReInit(JJString(buf, sz), startline, startcolumn, buffersize);
173   }
174 
CharStream(const JJChar * buf,int sz,int startline,int startcolumn)175   CharStream(const JJChar *buf, int sz, int startline, int startcolumn) :
176     bufline(nullptr), bufcolumn(nullptr), buffer(nullptr), bufpos(0), bufsize(0),
177     tokenBegin(0), column(0), line(0), prevCharIsCR(false), prevCharIsLF(false),
178     available(0), maxNextCharInd(0), inBuf(0), tabSize(1), trackLineColumn(true),
179     inputStream(nullptr), deleteStream(false) {
180     ReInit(JJString(buf, sz), startline, startcolumn, INITIAL_BUFFER_SIZE);
181   }
182 
CharStream(const JJString & str,int startline,int startcolumn,int buffersize)183   CharStream(const JJString& str, int startline,
184                       int startcolumn, int buffersize) :
185     bufline(nullptr), bufcolumn(nullptr), buffer(nullptr), bufpos(0), bufsize(0),
186     tokenBegin(0), column(0), line(0), prevCharIsCR(false), prevCharIsLF(false),
187     available(0), maxNextCharInd(0), inBuf(0), tabSize(1), trackLineColumn(true),
188     inputStream(nullptr), deleteStream(false) {
189     ReInit(str, startline, startcolumn, buffersize);
190   }
191 
CharStream(const JJString & str,int startline,int startcolumn)192   CharStream(const JJString& str, int startline, int startcolumn) :
193     bufline(nullptr), bufcolumn(nullptr), buffer(nullptr), bufpos(0), bufsize(0),
194     tokenBegin(0), column(0), line(0), prevCharIsCR(false), prevCharIsLF(false),
195     available(0), maxNextCharInd(0), inBuf(0), tabSize(1), trackLineColumn(true),
196     inputStream(nullptr), deleteStream(false) {
197     ReInit(str, startline, startcolumn, INITIAL_BUFFER_SIZE);
198   }
199 
CharStream(ReaderStream * input_stream,int startline,int startcolumn,int buffersize)200   CharStream(ReaderStream *input_stream, int startline,
201              int startcolumn, int buffersize) :
202     bufline(nullptr), bufcolumn(nullptr), buffer(nullptr), bufpos(0), bufsize(0),
203     tokenBegin(0), column(0), line(0), prevCharIsCR(false), prevCharIsLF(false),
204     available(0), maxNextCharInd(0), inBuf(0), tabSize(1), trackLineColumn(true),
205     inputStream(nullptr), deleteStream(false) {
206     ReInit(input_stream, startline, startcolumn, buffersize);
207   }
208 
CharStream(ReaderStream * input_stream,int startline,int startcolumn)209   CharStream(ReaderStream *input_stream, int startline, int startcolumn) :
210     bufline(nullptr), bufcolumn(nullptr), buffer(nullptr), bufpos(0), bufsize(0),
211     tokenBegin(0), column(0), line(0), prevCharIsCR(false), prevCharIsLF(false),
212     available(0), maxNextCharInd(0), inBuf(0), tabSize(1), trackLineColumn(true),
213     inputStream(nullptr), deleteStream(false) {
214     ReInit(input_stream, startline, startcolumn, INITIAL_BUFFER_SIZE);
215   }
216 
CharStream(ReaderStream * input_stream)217   CharStream(ReaderStream *input_stream) :
218     bufline(nullptr), bufcolumn(nullptr), buffer(nullptr), bufpos(0), bufsize(0),
219     tokenBegin(0), column(0), line(0), prevCharIsCR(false), prevCharIsLF(false),
220     available(0), maxNextCharInd(0), inBuf(0), tabSize(1), trackLineColumn(true),
221     inputStream(nullptr), deleteStream(false) {
222     ReInit(input_stream, 1, 1, INITIAL_BUFFER_SIZE);
223   }
224 
225   virtual void ReInit(ReaderStream *input_stream, int startline, int startcolumn, int buffersize);
226 
ReInit(ReaderStream * input_stream,int startline,int startcolumn)227   virtual void ReInit(ReaderStream *input_stream, int startline, int startcolumn) {
228     ReInit(input_stream, startline, startcolumn, INITIAL_BUFFER_SIZE);
229   }
230 
ReInit(ReaderStream * input_stream)231   virtual void ReInit(ReaderStream *input_stream) {
232     ReInit(input_stream, 1, 1, INITIAL_BUFFER_SIZE);
233   }
234 
235   virtual void ReInit(const JJString& str, int startline,
236                       int startcolumn, int buffersize);
237 
ReInit(const JJString & str,int startline,int startcolumn)238   virtual void ReInit(const JJString& str, int startline,
239                       int startcolumn) {
240     ReInit(str, startline, startcolumn, INITIAL_BUFFER_SIZE);
241   }
242 
243   virtual void adjustBeginLineColumn(int newLine, int newCol);
244 
245 protected:
246   virtual void UpdateLineColumn(JJChar c);
247 
248   int*               bufline;
249   int*               bufcolumn;
250   JJChar*  	         buffer;
251   int                bufpos;
252   int                bufsize;
253   int                tokenBegin;
254   int                column;
255   int                line;
256   bool               prevCharIsCR;
257   bool               prevCharIsLF;
258   int                available;
259   int                maxNextCharInd;
260   int                inBuf;
261   int                tabSize;
262   bool               trackLineColumn;
263   ReaderStream*      inputStream;
264   bool               deleteStream;
265 };
266 
267 }
268 }
269 
270 #endif
271 /* JavaCC - OriginalChecksum=c5b4b2e72393f865547f405cc9def169 (do not edit this line) */
272