001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.1 */
020/* JavaCCOptions:STATIC=false */
021package org.dllearner.parser;
022
023/**
024 * An implementation of interface CharStream, where the stream is assumed to
025 * contain only ASCII characters (without unicode processing).
026 */
027
028@SuppressWarnings("all")
029public class SimpleCharStream
030{
031/** Whether parser is static. */
032  public static final boolean staticFlag = false;
033  int bufsize;
034  int available;
035  int tokenBegin;
036/** Position in buffer. */
037  public int bufpos = -1;
038  protected int bufline[];
039  protected int bufcolumn[];
040
041  protected int column = 0;
042  protected int line = 1;
043
044  protected boolean prevCharIsCR = false;
045  protected boolean prevCharIsLF = false;
046
047  protected java.io.Reader inputStream;
048
049  protected char[] buffer;
050  protected int maxNextCharInd = 0;
051  protected int inBuf = 0;
052  protected int tabSize = 8;
053
054  protected void setTabSize(int i) { tabSize = i; }
055  protected int getTabSize(int i) { return tabSize; }
056
057  protected void ExpandBuff(boolean wrapAround)
058  {
059     char[] newbuffer = new char[bufsize + 2048];
060     int newbufline[] = new int[bufsize + 2048];
061     int newbufcolumn[] = new int[bufsize + 2048];
062
063     try
064     {
065        if (wrapAround)
066        {
067           System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
068           System.arraycopy(buffer, 0, newbuffer,
069                                             bufsize - tokenBegin, bufpos);
070           buffer = newbuffer;
071
072           System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
073           System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
074           bufline = newbufline;
075
076           System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
077           System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
078           bufcolumn = newbufcolumn;
079
080           maxNextCharInd = (bufpos += (bufsize - tokenBegin));
081        }
082        else
083        {
084           System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
085           buffer = newbuffer;
086
087           System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
088           bufline = newbufline;
089
090           System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
091           bufcolumn = newbufcolumn;
092
093           maxNextCharInd = (bufpos -= tokenBegin);
094        }
095     }
096     catch (Throwable t)
097     {
098        throw new Error(t.getMessage());
099     }
100
101     bufsize += 2048;
102     available = bufsize;
103     tokenBegin = 0;
104  }
105
106  protected void FillBuff() throws java.io.IOException
107  {
108     if (maxNextCharInd == available)
109     {
110        if (available == bufsize)
111        {
112           if (tokenBegin > 2048)
113           {
114              bufpos = maxNextCharInd = 0;
115              available = tokenBegin;
116           }
117           else if (tokenBegin < 0)
118              bufpos = maxNextCharInd = 0;
119           else
120              ExpandBuff(false);
121        }
122        else if (available > tokenBegin)
123           available = bufsize;
124        else if ((tokenBegin - available) < 2048)
125           ExpandBuff(true);
126        else
127           available = tokenBegin;
128     }
129
130     int i;
131     try {
132        if ((i = inputStream.read(buffer, maxNextCharInd,
133                                    available - maxNextCharInd)) == -1)
134        {
135           inputStream.close();
136           throw new java.io.IOException();
137        }
138        else
139           maxNextCharInd += i;
140        return;
141     }
142     catch(java.io.IOException e) {
143        --bufpos;
144        backup(0);
145        if (tokenBegin == -1)
146           tokenBegin = bufpos;
147        throw e;
148     }
149  }
150
151/** Start. */
152  public char BeginToken() throws java.io.IOException
153  {
154     tokenBegin = -1;
155     char c = readChar();
156     tokenBegin = bufpos;
157
158     return c;
159  }
160
161  protected void UpdateLineColumn(char c)
162  {
163     column++;
164
165     if (prevCharIsLF)
166     {
167        prevCharIsLF = false;
168        line += (column = 1);
169     }
170     else if (prevCharIsCR)
171     {
172        prevCharIsCR = false;
173        if (c == '\n')
174        {
175           prevCharIsLF = true;
176        }
177        else
178           line += (column = 1);
179     }
180
181     switch (c)
182     {
183        case '\r' :
184           prevCharIsCR = true;
185           break;
186        case '\n' :
187           prevCharIsLF = true;
188           break;
189        case '\t' :
190           column--;
191           column += (tabSize - (column % tabSize));
192           break;
193        default :
194           break;
195     }
196
197     bufline[bufpos] = line;
198     bufcolumn[bufpos] = column;
199  }
200
201/** Read a character. */
202  public char readChar() throws java.io.IOException
203  {
204     if (inBuf > 0)
205     {
206        --inBuf;
207
208        if (++bufpos == bufsize)
209           bufpos = 0;
210
211        return buffer[bufpos];
212     }
213
214     if (++bufpos >= maxNextCharInd)
215        FillBuff();
216
217     char c = buffer[bufpos];
218
219     UpdateLineColumn(c);
220     return c;
221  }
222
223  /**
224   * @deprecated 
225   * @see #getEndColumn
226   */
227
228  public int getColumn() {
229     return bufcolumn[bufpos];
230  }
231
232  /**
233   * @deprecated 
234   * @see #getEndLine
235   */
236
237  public int getLine() {
238     return bufline[bufpos];
239  }
240
241  /** Get token end column number. */
242  public int getEndColumn() {
243     return bufcolumn[bufpos];
244  }
245
246  /** Get token end line number. */
247  public int getEndLine() {
248     return bufline[bufpos];
249  }
250
251  /** Get token beginning column number. */
252  public int getBeginColumn() {
253     return bufcolumn[tokenBegin];
254  }
255
256  /** Get token beginning line number. */
257  public int getBeginLine() {
258     return bufline[tokenBegin];
259  }
260
261/** Backup a number of characters. */
262  public void backup(int amount) {
263
264    inBuf += amount;
265    if ((bufpos -= amount) < 0)
266       bufpos += bufsize;
267  }
268
269  /** Constructor. */
270  public SimpleCharStream(java.io.Reader dstream, int startline,
271  int startcolumn, int buffersize)
272  {
273    inputStream = dstream;
274    line = startline;
275    column = startcolumn - 1;
276
277    available = bufsize = buffersize;
278    buffer = new char[buffersize];
279    bufline = new int[buffersize];
280    bufcolumn = new int[buffersize];
281  }
282
283  /** Constructor. */
284  public SimpleCharStream(java.io.Reader dstream, int startline,
285                          int startcolumn)
286  {
287     this(dstream, startline, startcolumn, 4096);
288  }
289
290  /** Constructor. */
291  public SimpleCharStream(java.io.Reader dstream)
292  {
293     this(dstream, 1, 1, 4096);
294  }
295
296  /** Reinitialise. */
297  public void ReInit(java.io.Reader dstream, int startline,
298  int startcolumn, int buffersize)
299  {
300    inputStream = dstream;
301    line = startline;
302    column = startcolumn - 1;
303
304    if (buffer == null || buffersize != buffer.length)
305    {
306      available = bufsize = buffersize;
307      buffer = new char[buffersize];
308      bufline = new int[buffersize];
309      bufcolumn = new int[buffersize];
310    }
311    prevCharIsLF = prevCharIsCR = false;
312    tokenBegin = inBuf = maxNextCharInd = 0;
313    bufpos = -1;
314  }
315
316  /** Reinitialise. */
317  public void ReInit(java.io.Reader dstream, int startline,
318                     int startcolumn)
319  {
320     ReInit(dstream, startline, startcolumn, 4096);
321  }
322
323  /** Reinitialise. */
324  public void ReInit(java.io.Reader dstream)
325  {
326     ReInit(dstream, 1, 1, 4096);
327  }
328  /** Constructor. */
329  public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
330  int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
331  {
332     this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
333  }
334
335  /** Constructor. */
336  public SimpleCharStream(java.io.InputStream dstream, int startline,
337  int startcolumn, int buffersize)
338  {
339     this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
340  }
341
342  /** Constructor. */
343  public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
344                          int startcolumn) throws java.io.UnsupportedEncodingException
345  {
346     this(dstream, encoding, startline, startcolumn, 4096);
347  }
348
349  /** Constructor. */
350  public SimpleCharStream(java.io.InputStream dstream, int startline,
351                          int startcolumn)
352  {
353     this(dstream, startline, startcolumn, 4096);
354  }
355
356  /** Constructor. */
357  public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
358  {
359     this(dstream, encoding, 1, 1, 4096);
360  }
361
362  /** Constructor. */
363  public SimpleCharStream(java.io.InputStream dstream)
364  {
365     this(dstream, 1, 1, 4096);
366  }
367
368  /** Reinitialise. */
369  public void ReInit(java.io.InputStream dstream, String encoding, int startline,
370                          int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
371  {
372     ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
373  }
374
375  /** Reinitialise. */
376  public void ReInit(java.io.InputStream dstream, int startline,
377                          int startcolumn, int buffersize)
378  {
379     ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
380  }
381
382  /** Reinitialise. */
383  public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
384  {
385     ReInit(dstream, encoding, 1, 1, 4096);
386  }
387
388  /** Reinitialise. */
389  public void ReInit(java.io.InputStream dstream)
390  {
391     ReInit(dstream, 1, 1, 4096);
392  }
393  /** Reinitialise. */
394  public void ReInit(java.io.InputStream dstream, String encoding, int startline,
395                     int startcolumn) throws java.io.UnsupportedEncodingException
396  {
397     ReInit(dstream, encoding, startline, startcolumn, 4096);
398  }
399  /** Reinitialise. */
400  public void ReInit(java.io.InputStream dstream, int startline,
401                     int startcolumn)
402  {
403     ReInit(dstream, startline, startcolumn, 4096);
404  }
405  /** Get token literal value. */
406  public String GetImage()
407  {
408     if (bufpos >= tokenBegin)
409        return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
410     else
411        return new String(buffer, tokenBegin, bufsize - tokenBegin) +
412                              new String(buffer, 0, bufpos + 1);
413  }
414
415  /** Get the suffix. */
416  public char[] GetSuffix(int len)
417  {
418     char[] ret = new char[len];
419
420     if ((bufpos + 1) >= len)
421        System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
422     else
423     {
424        System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
425                                                          len - bufpos - 1);
426        System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
427     }
428
429     return ret;
430  }
431
432  /** Reset buffer when finished. */
433  public void Done()
434  {
435     buffer = null;
436     bufline = null;
437     bufcolumn = null;
438  }
439
440  /**
441   * Method to adjust line and column numbers for the start of a token.
442   */
443  public void adjustBeginLineColumn(int newLine, int newCol)
444  {
445     int start = tokenBegin;
446     int len;
447
448     if (bufpos >= tokenBegin)
449     {
450        len = bufpos - tokenBegin + inBuf + 1;
451     }
452     else
453     {
454        len = bufsize - tokenBegin + bufpos + 1 + inBuf;
455     }
456
457     int i = 0, j = 0, k = 0;
458     int nextColDiff = 0, columnDiff = 0;
459
460     while (i < len &&
461            bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
462     {
463        bufline[j] = newLine;
464        nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
465        bufcolumn[j] = newCol + columnDiff;
466        columnDiff = nextColDiff;
467        i++;
468     } 
469
470     if (i < len)
471     {
472        bufline[j] = newLine++;
473        bufcolumn[j] = newCol + columnDiff;
474
475        while (i++ < len)
476        {
477           if (bufline[j = start % bufsize] != bufline[++start % bufsize])
478              bufline[j] = newLine++;
479           else
480              bufline[j] = newLine;
481        }
482     }
483
484     line = bufline[j];
485     column = bufcolumn[j];
486  }
487
488}
489/* JavaCC - OriginalChecksum=4dc2726dec9c445d0f0f7d31e4258576 (do not edit this line) */