CharStream.cc
Go to the documentation of this file.
1 /* Generated By:JavaCC: Do not edit this line. CharStream.cc Version 6.2 */
2 /* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
3 #include "CharStream.h"
4 
5 namespace {
6 template <class T>
7 void ArrayCopy(T* src, int src_offset, T* dest, int dest_offset, int len) {
8  for (int i = 0; i < len; i++) {
9  dest[dest_offset + i] = src[src_offset + i];
10  }
11 }
12 
13 class StringReaderStream : public ReaderStream {
14  public:
15  StringReaderStream(const JAVACC_STRING_TYPE& str) : str_(str), cur_(0), max_(str.size()) {}
16  virtual size_t read(JAVACC_CHAR_TYPE *bufptr, int offset, size_t len) {
17  size_t count = str_.copy(bufptr + offset, len > max_ ? max_ : len, cur_);
18  cur_ += count;
19  max_ -= count;
20  return count;
21  }
22  virtual ~StringReaderStream() {}
23  virtual bool endOfInput() {
24  return max_ == 0;
25  }
26 
27  private:
28  const JAVACC_STRING_TYPE str_;
29  size_t cur_;
30  size_t max_;
31 };
32 }
33 
34 namespace vhdl {
35 namespace parser {
36 void CharStream::ReInit(const JAVACC_STRING_TYPE& str, int startline,
37  int startcolumn, int buffersize) {
38  StringReaderStream *stream = new StringReaderStream(str);
39  ReInit(stream, startline, startcolumn, buffersize);
40  deleteStream = true;
41 }
42 
43 void CharStream::ReInit(ReaderStream *input_stream, int startline,
44  int startcolumn, int buffersize) {
45  if (deleteStream) {
46  delete inputStream;
47  }
48 
49  if (buffer != NULL) {
50  DeleteBuffers();
51  }
52 
53  available = bufsize = buffersize;
54  buffer = new JAVACC_CHAR_TYPE[buffersize];
55  bufline = new int[buffersize];
56  bufcolumn = new int[buffersize];
57 
58  column = startcolumn - 1;
59  inputStream = input_stream;
60  line = startline;
61  prevCharIsLF = prevCharIsCR = false;
62  tokenBegin = inBuf = maxNextCharInd = 0;
63  bufpos = -1;
64  deleteStream = false;
65 }
66 
67 void CharStream::DeleteBuffers() {
68  delete[] buffer;
69  delete[] bufline;
70  delete[] bufcolumn;
71 }
72 
73 void CharStream::adjustBeginLineColumn(int newLine, int newCol) {
74  int start = tokenBegin;
75  int len;
76 
77  if (bufpos >= tokenBegin) {
78  len = bufpos - tokenBegin + inBuf + 1;
79  } else {
80  len = bufsize - tokenBegin + bufpos + 1 + inBuf;
81  }
82 
83  int i = 0, j = 0, k = 0;
84  int nextColDiff = 0, columnDiff = 0;
85 
86  while (i < len && bufline[j = start % bufsize] ==
87  bufline[k = (start + 1) % bufsize]) {
88  bufline[j] = newLine;
89  nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
90  bufcolumn[j] = newCol + columnDiff;
91  columnDiff = nextColDiff;
92  i++;
93  start++;
94  }
95 
96  if (i < len) {
97  bufline[j] = newLine++;
98  bufcolumn[j] = newCol + columnDiff;
99 
100  while (i++ < len) {
101  if (bufline[j = start % bufsize] != bufline[(start + 1) % bufsize])
102  bufline[j] = newLine++;
103  else
104  bufline[j] = newLine;
105  start++;
106  }
107  }
108 
109  line = bufline[j];
110  column = bufcolumn[j];
111 }
112 
113 void CharStream::ExpandBuff(bool wrapAround) {
114  JAVACC_CHAR_TYPE *newbuffer = new JAVACC_CHAR_TYPE[bufsize + 2048];
115  int *newbufline = new int[bufsize + 2048];
116  int *newbufcolumn = new int[bufsize + 2048];
117 
118  if (wrapAround) {
119  ArrayCopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
120  ArrayCopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
121  ArrayCopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
122  ArrayCopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
123  ArrayCopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
124  ArrayCopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
125  bufpos += (bufsize - tokenBegin);
126  } else {
127  ArrayCopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
128  ArrayCopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
129  ArrayCopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
130  bufpos -= tokenBegin;
131  }
132 
133  maxNextCharInd = bufpos;
134  DeleteBuffers();
135  buffer = newbuffer;
136  bufline = newbufline;
137  bufcolumn = newbufcolumn;
138  bufsize += 2048;
139  available = bufsize;
140  tokenBegin = 0;
141 }
142 
143 void CharStream::FillBuff() {
144  if (maxNextCharInd == available) {
145  if (available == bufsize) {
146  if (tokenBegin > 2048) {
147  bufpos = maxNextCharInd = 0;
148  available = tokenBegin;
149  } else if (tokenBegin < 0) {
150  bufpos = maxNextCharInd = 0;
151  } else {
152  ExpandBuff(false);
153  }
154  } else if (available > tokenBegin) {
155  available = bufsize;
156  } else if ((tokenBegin - available) < 2048) {
157  ExpandBuff(true);
158  } else {
159  available = tokenBegin;
160  }
161  }
162 
163  int i = inputStream->read(buffer, maxNextCharInd, available - maxNextCharInd);
164  if (i > 0) {
165  maxNextCharInd += i;
166  } else {
167  --bufpos;
168  backup(0);
169  if (tokenBegin == -1) {
170  tokenBegin = bufpos;
171  }
172  }
173 }
174 
175 void CharStream::UpdateLineColumn(JAVACC_CHAR_TYPE c) {
176  column++;
177  if (prevCharIsLF) {
178  prevCharIsLF = false;
179  column = 1;
180  line++;
181  } else if (prevCharIsCR) {
182  prevCharIsCR = false;
183  if (c == '\n') {
184  prevCharIsLF = true;
185  } else {
186  column = 1;
187  line++;
188  }
189  }
190 
191  switch (c) {
192  case '\r' :
193  prevCharIsCR = true;
194  break;
195  case '\n' :
196  prevCharIsLF = true;
197  break;
198  case '\t' :
199  column--;
200  column += (tabSize - (column % tabSize));
201  break;
202  default :
203  break;
204  }
205 
206  bufline[bufpos] = line;
207  bufcolumn[bufpos] = column;
208 }
209 
210 }
211 }
212 /* JavaCC - OriginalChecksum=ade3c1b57a731a003629de593814ffa6 (do not edit this line) */
#define JAVACC_STRING_TYPE
Definition: JavaCC.h:16
static void newLine()
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
#define JAVACC_CHAR_TYPE
Definition: JavaCC.h:12
virtual size_t read(JAVACC_CHAR_TYPE *, int, size_t)
Definition: JavaCC.h:28
virtual bool endOfInput()
Definition: JavaCC.h:29
void line(double t, double *p, double &x, double &y, double &z)
std::vector< std::string > column
static QCString str