add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / parser / JavaCharStream.java
1 /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 4.1 */
2 /* JavaCCOptions:STATIC=false */
3 package org.apache.lucene.queryParser.standard.parser;
4
5 /**
6  * An implementation of interface CharStream, where the stream is assumed to
7  * contain only ASCII characters (with java-like unicode escape processing).
8  */
9
10 public class JavaCharStream
11 {
12 /** Whether parser is static. */
13   public static final boolean staticFlag = false;
14   static final int hexval(char c) throws java.io.IOException {
15     switch(c)
16     {
17        case '0' :
18           return 0;
19        case '1' :
20           return 1;
21        case '2' :
22           return 2;
23        case '3' :
24           return 3;
25        case '4' :
26           return 4;
27        case '5' :
28           return 5;
29        case '6' :
30           return 6;
31        case '7' :
32           return 7;
33        case '8' :
34           return 8;
35        case '9' :
36           return 9;
37
38        case 'a' :
39        case 'A' :
40           return 10;
41        case 'b' :
42        case 'B' :
43           return 11;
44        case 'c' :
45        case 'C' :
46           return 12;
47        case 'd' :
48        case 'D' :
49           return 13;
50        case 'e' :
51        case 'E' :
52           return 14;
53        case 'f' :
54        case 'F' :
55           return 15;
56     }
57
58     throw new java.io.IOException(); // Should never come here
59   }
60
61 /** Position in buffer. */
62   public int bufpos = -1;
63   int bufsize;
64   int available;
65   int tokenBegin;
66   protected int bufline[];
67   protected int bufcolumn[];
68
69   protected int column = 0;
70   protected int line = 1;
71
72   protected boolean prevCharIsCR = false;
73   protected boolean prevCharIsLF = false;
74
75   protected java.io.Reader inputStream;
76
77   protected char[] nextCharBuf;
78   protected char[] buffer;
79   protected int maxNextCharInd = 0;
80   protected int nextCharInd = -1;
81   protected int inBuf = 0;
82   protected int tabSize = 8;
83
84   protected void setTabSize(int i) { tabSize = i; }
85   protected int getTabSize(int i) { return tabSize; }
86
87   protected void ExpandBuff(boolean wrapAround)
88   {
89      char[] newbuffer = new char[bufsize + 2048];
90      int newbufline[] = new int[bufsize + 2048];
91      int newbufcolumn[] = new int[bufsize + 2048];
92
93      try
94      {
95         if (wrapAround)
96         {
97            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
98            System.arraycopy(buffer, 0, newbuffer,
99                                              bufsize - tokenBegin, bufpos);
100            buffer = newbuffer;
101
102            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
103            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
104            bufline = newbufline;
105
106            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
107            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
108            bufcolumn = newbufcolumn;
109
110            bufpos += (bufsize - tokenBegin);
111         }
112         else
113         {
114            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
115            buffer = newbuffer;
116
117            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
118            bufline = newbufline;
119
120            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
121            bufcolumn = newbufcolumn;
122
123            bufpos -= tokenBegin;
124         }
125      }
126      catch (Throwable t)
127      {
128         throw new Error(t.getMessage());
129      }
130
131      available = (bufsize += 2048);
132      tokenBegin = 0;
133   }
134
135   protected void FillBuff() throws java.io.IOException
136   {
137      int i;
138      if (maxNextCharInd == 4096)
139         maxNextCharInd = nextCharInd = 0;
140
141      try {
142         if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
143                                             4096 - maxNextCharInd)) == -1)
144         {
145            inputStream.close();
146            throw new java.io.IOException();
147         }
148         else
149            maxNextCharInd += i;
150         return;
151      }
152      catch(java.io.IOException e) {
153         if (bufpos != 0)
154         {
155            --bufpos;
156            backup(0);
157         }
158         else
159         {
160            bufline[bufpos] = line;
161            bufcolumn[bufpos] = column;
162         }
163         throw e;
164      }
165   }
166
167   protected char ReadByte() throws java.io.IOException
168   {
169      if (++nextCharInd >= maxNextCharInd)
170         FillBuff();
171
172      return nextCharBuf[nextCharInd];
173   }
174
175 /** @return starting character for token. */
176   public char BeginToken() throws java.io.IOException
177   {
178      if (inBuf > 0)
179      {
180         --inBuf;
181
182         if (++bufpos == bufsize)
183            bufpos = 0;
184
185         tokenBegin = bufpos;
186         return buffer[bufpos];
187      }
188
189      tokenBegin = 0;
190      bufpos = -1;
191
192      return readChar();
193   }
194
195   protected void AdjustBuffSize()
196   {
197      if (available == bufsize)
198      {
199         if (tokenBegin > 2048)
200         {
201            bufpos = 0;
202            available = tokenBegin;
203         }
204         else
205            ExpandBuff(false);
206      }
207      else if (available > tokenBegin)
208         available = bufsize;
209      else if ((tokenBegin - available) < 2048)
210         ExpandBuff(true);
211      else
212         available = tokenBegin;
213   }
214
215   protected void UpdateLineColumn(char c)
216   {
217      column++;
218
219      if (prevCharIsLF)
220      {
221         prevCharIsLF = false;
222         line += (column = 1);
223      }
224      else if (prevCharIsCR)
225      {
226         prevCharIsCR = false;
227         if (c == '\n')
228         {
229            prevCharIsLF = true;
230         }
231         else
232            line += (column = 1);
233      }
234
235      switch (c)
236      {
237         case '\r' :
238            prevCharIsCR = true;
239            break;
240         case '\n' :
241            prevCharIsLF = true;
242            break;
243         case '\t' :
244            column--;
245            column += (tabSize - (column % tabSize));
246            break;
247         default :
248            break;
249      }
250
251      bufline[bufpos] = line;
252      bufcolumn[bufpos] = column;
253   }
254
255 /** Read a character. */
256   public char readChar() throws java.io.IOException
257   {
258      if (inBuf > 0)
259      {
260         --inBuf;
261
262         if (++bufpos == bufsize)
263            bufpos = 0;
264
265         return buffer[bufpos];
266      }
267
268      char c;
269
270      if (++bufpos == available)
271         AdjustBuffSize();
272
273      if ((buffer[bufpos] = c = ReadByte()) == '\\')
274      {
275         UpdateLineColumn(c);
276
277         int backSlashCnt = 1;
278
279         for (;;) // Read all the backslashes
280         {
281            if (++bufpos == available)
282               AdjustBuffSize();
283
284            try
285            {
286               if ((buffer[bufpos] = c = ReadByte()) != '\\')
287               {
288                  UpdateLineColumn(c);
289                  // found a non-backslash char.
290                  if ((c == 'u') && ((backSlashCnt & 1) == 1))
291                  {
292                     if (--bufpos < 0)
293                        bufpos = bufsize - 1;
294
295                     break;
296                  }
297
298                  backup(backSlashCnt);
299                  return '\\';
300               }
301            }
302            catch(java.io.IOException e)
303            {
304               if (backSlashCnt > 1)
305                  backup(backSlashCnt-1);
306
307               return '\\';
308            }
309
310            UpdateLineColumn(c);
311            backSlashCnt++;
312         }
313
314         // Here, we have seen an odd number of backslash's followed by a 'u'
315         try
316         {
317            while ((c = ReadByte()) == 'u')
318               ++column;
319
320            buffer[bufpos] = c = (char)(hexval(c) << 12 |
321                                        hexval(ReadByte()) << 8 |
322                                        hexval(ReadByte()) << 4 |
323                                        hexval(ReadByte()));
324
325            column += 4;
326         }
327         catch(java.io.IOException e)
328         {
329            throw new Error("Invalid escape character at line " + line +
330                                          " column " + column + ".");
331         }
332
333         if (backSlashCnt == 1)
334            return c;
335         else
336         {
337            backup(backSlashCnt - 1);
338            return '\\';
339         }
340      }
341      else
342      {
343         UpdateLineColumn(c);
344         return c;
345      }
346   }
347
348   @Deprecated
349   /**
350    * @deprecated
351    * @see #getEndColumn
352    */
353   public int getColumn() {
354      return bufcolumn[bufpos];
355   }
356
357   @Deprecated
358   /**
359    * @deprecated
360    * @see #getEndLine
361    */
362   public int getLine() {
363      return bufline[bufpos];
364   }
365
366 /** Get end column. */
367   public int getEndColumn() {
368      return bufcolumn[bufpos];
369   }
370
371 /** Get end line. */
372   public int getEndLine() {
373      return bufline[bufpos];
374   }
375
376 /** @return column of token start */
377   public int getBeginColumn() {
378      return bufcolumn[tokenBegin];
379   }
380
381 /** @return line number of token start */
382   public int getBeginLine() {
383      return bufline[tokenBegin];
384   }
385
386 /** Retreat. */
387   public void backup(int amount) {
388
389     inBuf += amount;
390     if ((bufpos -= amount) < 0)
391        bufpos += bufsize;
392   }
393
394 /** Constructor. */
395   public JavaCharStream(java.io.Reader dstream,
396                  int startline, int startcolumn, int buffersize)
397   {
398     inputStream = dstream;
399     line = startline;
400     column = startcolumn - 1;
401
402     available = bufsize = buffersize;
403     buffer = new char[buffersize];
404     bufline = new int[buffersize];
405     bufcolumn = new int[buffersize];
406     nextCharBuf = new char[4096];
407   }
408
409 /** Constructor. */
410   public JavaCharStream(java.io.Reader dstream,
411                                         int startline, int startcolumn)
412   {
413      this(dstream, startline, startcolumn, 4096);
414   }
415
416 /** Constructor. */
417   public JavaCharStream(java.io.Reader dstream)
418   {
419      this(dstream, 1, 1, 4096);
420   }
421 /** Reinitialise. */
422   public void ReInit(java.io.Reader dstream,
423                  int startline, int startcolumn, int buffersize)
424   {
425     inputStream = dstream;
426     line = startline;
427     column = startcolumn - 1;
428
429     if (buffer == null || buffersize != buffer.length)
430     {
431       available = bufsize = buffersize;
432       buffer = new char[buffersize];
433       bufline = new int[buffersize];
434       bufcolumn = new int[buffersize];
435       nextCharBuf = new char[4096];
436     }
437     prevCharIsLF = prevCharIsCR = false;
438     tokenBegin = inBuf = maxNextCharInd = 0;
439     nextCharInd = bufpos = -1;
440   }
441
442 /** Reinitialise. */
443   public void ReInit(java.io.Reader dstream,
444                                         int startline, int startcolumn)
445   {
446      ReInit(dstream, startline, startcolumn, 4096);
447   }
448
449 /** Reinitialise. */
450   public void ReInit(java.io.Reader dstream)
451   {
452      ReInit(dstream, 1, 1, 4096);
453   }
454 /** Constructor. */
455   public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
456   int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
457   {
458      this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
459   }
460
461 /** Constructor. */
462   public JavaCharStream(java.io.InputStream dstream, int startline,
463   int startcolumn, int buffersize)
464   {
465      this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
466   }
467
468 /** Constructor. */
469   public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
470                         int startcolumn) throws java.io.UnsupportedEncodingException
471   {
472      this(dstream, encoding, startline, startcolumn, 4096);
473   }
474
475 /** Constructor. */
476   public JavaCharStream(java.io.InputStream dstream, int startline,
477                         int startcolumn)
478   {
479      this(dstream, startline, startcolumn, 4096);
480   }
481
482 /** Constructor. */
483   public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
484   {
485      this(dstream, encoding, 1, 1, 4096);
486   }
487
488 /** Constructor. */
489   public JavaCharStream(java.io.InputStream dstream)
490   {
491      this(dstream, 1, 1, 4096);
492   }
493
494 /** Reinitialise. */
495   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
496   int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
497   {
498      ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
499   }
500
501 /** Reinitialise. */
502   public void ReInit(java.io.InputStream dstream, int startline,
503   int startcolumn, int buffersize)
504   {
505      ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
506   }
507 /** Reinitialise. */
508   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
509                      int startcolumn) throws java.io.UnsupportedEncodingException
510   {
511      ReInit(dstream, encoding, startline, startcolumn, 4096);
512   }
513 /** Reinitialise. */
514   public void ReInit(java.io.InputStream dstream, int startline,
515                      int startcolumn)
516   {
517      ReInit(dstream, startline, startcolumn, 4096);
518   }
519 /** Reinitialise. */
520   public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
521   {
522      ReInit(dstream, encoding, 1, 1, 4096);
523   }
524
525 /** Reinitialise. */
526   public void ReInit(java.io.InputStream dstream)
527   {
528      ReInit(dstream, 1, 1, 4096);
529   }
530
531   /** @return token image as String */
532   public String GetImage()
533   {
534      if (bufpos >= tokenBegin)
535         return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
536      else
537         return new String(buffer, tokenBegin, bufsize - tokenBegin) +
538                               new String(buffer, 0, bufpos + 1);
539   }
540
541   /** @return suffix */
542   public char[] GetSuffix(int len)
543   {
544      char[] ret = new char[len];
545
546      if ((bufpos + 1) >= len)
547         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
548      else
549      {
550         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
551                                                           len - bufpos - 1);
552         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
553      }
554
555      return ret;
556   }
557
558   /** Set buffers back to null when finished. */
559   public void Done()
560   {
561      nextCharBuf = null;
562      buffer = null;
563      bufline = null;
564      bufcolumn = null;
565   }
566
567   /**
568    * Method to adjust line and column numbers for the start of a token.
569    */
570   public void adjustBeginLineColumn(int newLine, int newCol)
571   {
572      int start = tokenBegin;
573      int len;
574
575      if (bufpos >= tokenBegin)
576      {
577         len = bufpos - tokenBegin + inBuf + 1;
578      }
579      else
580      {
581         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
582      }
583
584      int i = 0, j = 0, k = 0;
585      int nextColDiff = 0, columnDiff = 0;
586
587      while (i < len &&
588             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
589      {
590         bufline[j] = newLine;
591         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
592         bufcolumn[j] = newCol + columnDiff;
593         columnDiff = nextColDiff;
594         i++;
595      }
596
597      if (i < len)
598      {
599         bufline[j] = newLine++;
600         bufcolumn[j] = newCol + columnDiff;
601
602         while (i++ < len)
603         {
604            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
605               bufline[j] = newLine++;
606            else
607               bufline[j] = newLine;
608         }
609      }
610
611      line = bufline[j];
612      column = bufcolumn[j];
613   }
614
615 }
616 /* JavaCC - OriginalChecksum=f19c73b8f7faf94cc4a581e7b2933cc6 (do not edit this line) */