summaryrefslogtreecommitdiffstats
path: root/vhdlparser/CharStream.cc
blob: 05003eff19648ec2edf1af08c82e4c25038f98e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* Generated By:JavaCC: Do not edit this line. CharStream.cc Version 7.0 */
/* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
#include "CharStream.h"

namespace {
template <class T>
void ArrayCopy(T* src, int src_offset, T* dest, int dest_offset, int len) {
 for (int i = 0; i < len; i++) {
   dest[dest_offset + i] = src[src_offset + i];
 }
}

class StringReaderStream : public ReaderStream {
 public:
  StringReaderStream(const JJString& str) : str_(str), cur_(0), max_(str.size()) {}
  virtual size_t read(JJChar *bufptr, int offset, size_t len) {
    size_t count = str_.copy(bufptr + offset, len > max_ ? max_ : len, cur_);
    cur_ += count;
    max_ -= count;
    return count;
  }
  virtual ~StringReaderStream() {}
  virtual bool endOfInput() {
    return max_ == 0;
  }

 private:
  const JJString str_;
  size_t cur_;
  size_t max_;
};
}

namespace vhdl {
namespace parser {
void CharStream::ReInit(const JJString& str, int startline,
                        int startcolumn, int buffersize) {
  StringReaderStream *stream = new StringReaderStream(str);
  ReInit(stream, startline, startcolumn, buffersize);
  deleteStream = true;
}

void CharStream::ReInit(ReaderStream *input_stream, int startline,
                        int startcolumn, int buffersize) {
  if (deleteStream) {
   delete inputStream;
  }

  if (buffer) {
    DeleteBuffers();
  }

  available = bufsize = buffersize;
  buffer = new JJChar[buffersize];
  bufline = new int[buffersize];
  bufcolumn = new int[buffersize];

  column = startcolumn - 1;
  inputStream = input_stream;
  line = startline;
  prevCharIsLF = prevCharIsCR = false;
  tokenBegin = inBuf = maxNextCharInd = 0;
  bufpos = -1;
  deleteStream = false;
}

void CharStream::DeleteBuffers() {
  delete[] buffer;
  delete[] bufline;
  delete[] bufcolumn;
}

void CharStream::adjustBeginLineColumn(int newLine, int newCol) {
  int start = tokenBegin;
  int len;

  if (bufpos >= tokenBegin) {
    len = bufpos - tokenBegin + inBuf + 1;
  } else {
    len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  }

  int i = 0, j = 0, k = 0;
  int nextColDiff = 0, columnDiff = 0;

  while (i < len && bufline[j = start % bufsize] ==
         bufline[k = (start + 1) % bufsize]) {
    bufline[j] = newLine;
    nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
    bufcolumn[j] = newCol + columnDiff;
    columnDiff = nextColDiff;
    i++;
    start++;
  }

  if (i < len) {
    bufline[j] = newLine++;
    bufcolumn[j] = newCol + columnDiff;

    while (i++ < len) {
      if (bufline[j = start % bufsize] != bufline[(start + 1) % bufsize])
        bufline[j] = newLine++;
      else
        bufline[j] = newLine;
      start++;
    }
  }

  line = bufline[j];
  column = bufcolumn[j];
}

void CharStream::ExpandBuff(bool wrapAround) {
  JJChar *newbuffer = new JJChar[bufsize + 2048];
  int *newbufline = new int[bufsize + 2048];
  int *newbufcolumn = new int[bufsize + 2048];

  if (wrapAround) {
    ArrayCopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
    ArrayCopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
    ArrayCopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
    ArrayCopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
    ArrayCopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
    ArrayCopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
    bufpos += (bufsize - tokenBegin);
  } else {
    ArrayCopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
    ArrayCopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
    ArrayCopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
    bufpos -= tokenBegin;
  }

  maxNextCharInd = bufpos;
  DeleteBuffers();
  buffer = newbuffer;
  bufline = newbufline;
  bufcolumn = newbufcolumn;
  bufsize += 2048;
  available = bufsize;
  tokenBegin = 0;
}

void CharStream::FillBuff() {
  if (maxNextCharInd == available) {
    if (available == bufsize) {
      if (tokenBegin > 2048) {
        bufpos = maxNextCharInd = 0;
        available = tokenBegin;
      } else if (tokenBegin < 0) {
        bufpos = maxNextCharInd = 0;
      } else {
        ExpandBuff(false);
      }
    } else if (available > tokenBegin) {
      available = bufsize;
    } else if ((tokenBegin - available) < 2048) {
      ExpandBuff(true);
    } else {
      available = tokenBegin;
    }
  }

  size_t i = inputStream->read(buffer, maxNextCharInd, available - maxNextCharInd);
  if (i > 0) {
    maxNextCharInd += i;
  } else {
    --bufpos;
    backup(0);
    if (tokenBegin == -1) {
      tokenBegin = bufpos;
    }
  }
}

void CharStream::UpdateLineColumn(JJChar c) {
  column++;
  if (prevCharIsLF) {
    prevCharIsLF = false;
    column = 1;
    line++;
  } else if (prevCharIsCR) {
    prevCharIsCR = false;
    if (c == '\n') {
      prevCharIsLF = true;
    } else {
      column = 1;
      line++;
    }
  }

  switch (c) {
    case '\r' :
      prevCharIsCR = true;
      break;
    case '\n' :
      prevCharIsLF = true;
      break;
    case '\t' :
      column--;
      column += (tabSize - (column % tabSize));
      break;
    default :
      break;
  }

  bufline[bufpos] = line;
  bufcolumn[bufpos] = column;
}

}
}
/* JavaCC - OriginalChecksum=0ba78d6c493d812eba61de6fa794fd15 (do not edit this line) */