]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/parser/SimpleCharStream.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / parser / SimpleCharStream.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.parser;
13
14 /**
15  * An implementation of interface CharStream, where the stream is assumed to
16  * contain only ASCII characters (without unicode processing).
17  */
18
19 public class SimpleCharStream
20 {
21 /** Whether parser is static. */
22   public static final boolean staticFlag = false;
23   int bufsize;
24   int available;
25   int tokenBegin;
26 /** Position in buffer. */
27   public int bufpos = -1;
28   protected int bufline[];
29   protected int bufcolumn[];
30
31   protected int column = 0;
32   protected int line = 1;
33
34   protected boolean prevCharIsCR = false;
35   protected boolean prevCharIsLF = false;
36
37   protected java.io.Reader inputStream;
38
39   protected char[] buffer;
40   protected int maxNextCharInd = 0;
41   protected int inBuf = 0;
42   protected int tabSize = 8;
43
44   protected void setTabSize(int i) { tabSize = i; }
45   protected int getTabSize(int i) { return tabSize; }
46
47
48   protected void ExpandBuff(boolean wrapAround)
49   {
50     char[] newbuffer = new char[bufsize + 2048];
51     int newbufline[] = new int[bufsize + 2048];
52     int newbufcolumn[] = new int[bufsize + 2048];
53
54     try
55     {
56       if (wrapAround)
57       {
58         System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
59         System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
60         buffer = newbuffer;
61
62         System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
63         System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
64         bufline = newbufline;
65
66         System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
67         System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
68         bufcolumn = newbufcolumn;
69
70         maxNextCharInd = (bufpos += (bufsize - tokenBegin));
71       }
72       else
73       {
74         System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
75         buffer = newbuffer;
76
77         System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
78         bufline = newbufline;
79
80         System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
81         bufcolumn = newbufcolumn;
82
83         maxNextCharInd = (bufpos -= tokenBegin);
84       }
85     }
86     catch (Throwable t)
87     {
88       throw new Error(t.getMessage());
89     }
90
91
92     bufsize += 2048;
93     available = bufsize;
94     tokenBegin = 0;
95   }
96
97   protected void FillBuff() throws java.io.IOException
98   {
99     if (maxNextCharInd == available)
100     {
101       if (available == bufsize)
102       {
103         if (tokenBegin > 2048)
104         {
105           bufpos = maxNextCharInd = 0;
106           available = tokenBegin;
107         }
108         else if (tokenBegin < 0)
109           bufpos = maxNextCharInd = 0;
110         else
111           ExpandBuff(false);
112       }
113       else if (available > tokenBegin)
114         available = bufsize;
115       else if ((tokenBegin - available) < 2048)
116         ExpandBuff(true);
117       else
118         available = tokenBegin;
119     }
120
121     int i;
122     try {
123       if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1)
124       {
125         inputStream.close();
126         throw new java.io.IOException();
127       }
128       else
129         maxNextCharInd += i;
130       return;
131     }
132     catch(java.io.IOException e) {
133       --bufpos;
134       backup(0);
135       if (tokenBegin == -1)
136         tokenBegin = bufpos;
137       throw e;
138     }
139   }
140
141 /** Start. */
142   public char BeginToken() throws java.io.IOException
143   {
144     tokenBegin = -1;
145     char c = readChar();
146     tokenBegin = bufpos;
147
148     return c;
149   }
150
151   protected void UpdateLineColumn(char c)
152   {
153     column++;
154
155     if (prevCharIsLF)
156     {
157       prevCharIsLF = false;
158       line += (column = 1);
159     }
160     else if (prevCharIsCR)
161     {
162       prevCharIsCR = false;
163       if (c == '\n')
164       {
165         prevCharIsLF = true;
166       }
167       else
168         line += (column = 1);
169     }
170
171     switch (c)
172     {
173       case '\r' :
174         prevCharIsCR = true;
175         break;
176       case '\n' :
177         prevCharIsLF = true;
178         break;
179       case '\t' :
180         column--;
181         column += (tabSize - (column % tabSize));
182         break;
183       default :
184         break;
185     }
186
187     bufline[bufpos] = line;
188     bufcolumn[bufpos] = column;
189   }
190
191 /** Read a character. */
192   public char readChar() throws java.io.IOException
193   {
194     if (inBuf > 0)
195     {
196       --inBuf;
197
198       if (++bufpos == bufsize)
199         bufpos = 0;
200
201       return buffer[bufpos];
202     }
203
204     if (++bufpos >= maxNextCharInd)
205       FillBuff();
206
207     char c = buffer[bufpos];
208
209     UpdateLineColumn(c);
210     return c;
211   }
212
213   @Deprecated
214   /**
215    * @deprecated
216    * @see #getEndColumn
217    */
218
219   public int getColumn() {
220     return bufcolumn[bufpos];
221   }
222
223   @Deprecated
224   /**
225    * @deprecated
226    * @see #getEndLine
227    */
228
229   public int getLine() {
230     return bufline[bufpos];
231   }
232
233   /** Get token end column number. */
234   public int getEndColumn() {
235     return bufcolumn[bufpos];
236   }
237
238   /** Get token end line number. */
239   public int getEndLine() {
240      return bufline[bufpos];
241   }
242
243   /** Get token beginning column number. */
244   public int getBeginColumn() {
245     return bufcolumn[tokenBegin];
246   }
247
248   /** Get token beginning line number. */
249   public int getBeginLine() {
250     return bufline[tokenBegin];
251   }
252
253 /** Backup a number of characters. */
254   public void backup(int amount) {
255
256     inBuf += amount;
257     if ((bufpos -= amount) < 0)
258       bufpos += bufsize;
259   }
260
261   /** Constructor. */
262   public SimpleCharStream(java.io.Reader dstream, int startline,
263   int startcolumn, int buffersize)
264   {
265     inputStream = dstream;
266     line = startline;
267     column = startcolumn - 1;
268
269     available = bufsize = buffersize;
270     buffer = new char[buffersize];
271     bufline = new int[buffersize];
272     bufcolumn = new int[buffersize];
273   }
274
275   /** Constructor. */
276   public SimpleCharStream(java.io.Reader dstream, int startline,
277                           int startcolumn)
278   {
279     this(dstream, startline, startcolumn, 4096);
280   }
281
282   /** Constructor. */
283   public SimpleCharStream(java.io.Reader dstream)
284   {
285     this(dstream, 1, 1, 4096);
286   }
287
288   /** Reinitialise. */
289   public void ReInit(java.io.Reader dstream, int startline,
290   int startcolumn, int buffersize)
291   {
292     inputStream = dstream;
293     line = startline;
294     column = startcolumn - 1;
295
296     if (buffer == null || buffersize != buffer.length)
297     {
298       available = bufsize = buffersize;
299       buffer = new char[buffersize];
300       bufline = new int[buffersize];
301       bufcolumn = new int[buffersize];
302     }
303     prevCharIsLF = prevCharIsCR = false;
304     tokenBegin = inBuf = maxNextCharInd = 0;
305     bufpos = -1;
306   }
307
308   /** Reinitialise. */
309   public void ReInit(java.io.Reader dstream, int startline,
310                      int startcolumn)
311   {
312     ReInit(dstream, startline, startcolumn, 4096);
313   }
314
315   /** Reinitialise. */
316   public void ReInit(java.io.Reader dstream)
317   {
318     ReInit(dstream, 1, 1, 4096);
319   }
320   /** Constructor. */
321   public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
322   int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
323   {
324     this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
325   }
326
327   /** Constructor. */
328   public SimpleCharStream(java.io.InputStream dstream, int startline,
329   int startcolumn, int buffersize)
330   {
331     this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
332   }
333
334   /** Constructor. */
335   public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
336                           int startcolumn) throws java.io.UnsupportedEncodingException
337   {
338     this(dstream, encoding, startline, startcolumn, 4096);
339   }
340
341   /** Constructor. */
342   public SimpleCharStream(java.io.InputStream dstream, int startline,
343                           int startcolumn)
344   {
345     this(dstream, startline, startcolumn, 4096);
346   }
347
348   /** Constructor. */
349   public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
350   {
351     this(dstream, encoding, 1, 1, 4096);
352   }
353
354   /** Constructor. */
355   public SimpleCharStream(java.io.InputStream dstream)
356   {
357     this(dstream, 1, 1, 4096);
358   }
359
360   /** Reinitialise. */
361   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
362                           int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
363   {
364     ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
365   }
366
367   /** Reinitialise. */
368   public void ReInit(java.io.InputStream dstream, int startline,
369                           int startcolumn, int buffersize)
370   {
371     ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
372   }
373
374   /** Reinitialise. */
375   public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
376   {
377     ReInit(dstream, encoding, 1, 1, 4096);
378   }
379
380   /** Reinitialise. */
381   public void ReInit(java.io.InputStream dstream)
382   {
383     ReInit(dstream, 1, 1, 4096);
384   }
385   /** Reinitialise. */
386   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
387                      int startcolumn) throws java.io.UnsupportedEncodingException
388   {
389     ReInit(dstream, encoding, startline, startcolumn, 4096);
390   }
391   /** Reinitialise. */
392   public void ReInit(java.io.InputStream dstream, int startline,
393                      int startcolumn)
394   {
395     ReInit(dstream, startline, startcolumn, 4096);
396   }
397   /** Get token literal value. */
398   public String GetImage()
399   {
400     if (bufpos >= tokenBegin)
401       return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
402     else
403       return new String(buffer, tokenBegin, bufsize - tokenBegin) +
404                             new String(buffer, 0, bufpos + 1);
405   }
406
407   /** Get the suffix. */
408   public char[] GetSuffix(int len)
409   {
410     char[] ret = new char[len];
411
412     if ((bufpos + 1) >= len)
413       System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
414     else
415     {
416       System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
417                                                         len - bufpos - 1);
418       System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
419     }
420
421     return ret;
422   }
423
424   /** Reset buffer when finished. */
425   public void Done()
426   {
427     buffer = null;
428     bufline = null;
429     bufcolumn = null;
430   }
431
432   /**
433    * Method to adjust line and column numbers for the start of a token.
434    */
435   public void adjustBeginLineColumn(int newLine, int newCol)
436   {
437     int start = tokenBegin;
438     int len;
439
440     if (bufpos >= tokenBegin)
441     {
442       len = bufpos - tokenBegin + inBuf + 1;
443     }
444     else
445     {
446       len = bufsize - tokenBegin + bufpos + 1 + inBuf;
447     }
448
449     int i = 0, j = 0, k = 0;
450     int nextColDiff = 0, columnDiff = 0;
451
452     while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
453     {
454       bufline[j] = newLine;
455       nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
456       bufcolumn[j] = newCol + columnDiff;
457       columnDiff = nextColDiff;
458       i++;
459     }
460
461     if (i < len)
462     {
463       bufline[j] = newLine++;
464       bufcolumn[j] = newCol + columnDiff;
465
466       while (i++ < len)
467       {
468         if (bufline[j = start % bufsize] != bufline[++start % bufsize])
469           bufline[j] = newLine++;
470         else
471           bufline[j] = newLine;
472       }
473     }
474
475     line = bufline[j];
476     column = bufcolumn[j];
477   }
478
479 }
480 /* JavaCC - OriginalChecksum=a27b2601f30a31b2eea422d3e11ea15d (do not edit this line) */