1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.parser;
15 * An implementation of interface CharStream, where the stream is assumed to
16 * contain only ASCII characters (without unicode processing).
19 public class SimpleCharStream
21 /** Whether parser is static. */
22 public static final boolean staticFlag = false;
26 /** Position in buffer. */
27 public int bufpos = -1;
28 protected int bufline[];
29 protected int bufcolumn[];
31 protected int column = 0;
32 protected int line = 1;
34 protected boolean prevCharIsCR = false;
35 protected boolean prevCharIsLF = false;
37 protected java.io.Reader inputStream;
39 protected char[] buffer;
40 protected int maxNextCharInd = 0;
41 protected int inBuf = 0;
42 protected int tabSize = 8;
44 protected void setTabSize(int i) { tabSize = i; }
45 protected int getTabSize(int i) { return tabSize; }
48 protected void ExpandBuff(boolean wrapAround)
50 char[] newbuffer = new char[bufsize + 2048];
51 int newbufline[] = new int[bufsize + 2048];
52 int newbufcolumn[] = new int[bufsize + 2048];
58 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
59 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
62 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
63 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
66 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
67 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
68 bufcolumn = newbufcolumn;
70 maxNextCharInd = (bufpos += (bufsize - tokenBegin));
74 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
77 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
80 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
81 bufcolumn = newbufcolumn;
83 maxNextCharInd = (bufpos -= tokenBegin);
88 throw new Error(t.getMessage());
97 protected void FillBuff() throws java.io.IOException
99 if (maxNextCharInd == available)
101 if (available == bufsize)
103 if (tokenBegin > 2048)
105 bufpos = maxNextCharInd = 0;
106 available = tokenBegin;
108 else if (tokenBegin < 0)
109 bufpos = maxNextCharInd = 0;
113 else if (available > tokenBegin)
115 else if ((tokenBegin - available) < 2048)
118 available = tokenBegin;
123 if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1)
126 throw new java.io.IOException();
132 catch(java.io.IOException e) {
135 if (tokenBegin == -1)
142 public char BeginToken() throws java.io.IOException
151 protected void UpdateLineColumn(char c)
157 prevCharIsLF = false;
158 line += (column = 1);
160 else if (prevCharIsCR)
162 prevCharIsCR = false;
168 line += (column = 1);
181 column += (tabSize - (column % tabSize));
187 bufline[bufpos] = line;
188 bufcolumn[bufpos] = column;
191 /** Read a character. */
192 public char readChar() throws java.io.IOException
198 if (++bufpos == bufsize)
201 return buffer[bufpos];
204 if (++bufpos >= maxNextCharInd)
207 char c = buffer[bufpos];
219 public int getColumn() {
220 return bufcolumn[bufpos];
229 public int getLine() {
230 return bufline[bufpos];
233 /** Get token end column number. */
234 public int getEndColumn() {
235 return bufcolumn[bufpos];
238 /** Get token end line number. */
239 public int getEndLine() {
240 return bufline[bufpos];
243 /** Get token beginning column number. */
244 public int getBeginColumn() {
245 return bufcolumn[tokenBegin];
248 /** Get token beginning line number. */
249 public int getBeginLine() {
250 return bufline[tokenBegin];
253 /** Backup a number of characters. */
254 public void backup(int amount) {
257 if ((bufpos -= amount) < 0)
262 public SimpleCharStream(java.io.Reader dstream, int startline,
263 int startcolumn, int buffersize)
265 inputStream = dstream;
267 column = startcolumn - 1;
269 available = bufsize = buffersize;
270 buffer = new char[buffersize];
271 bufline = new int[buffersize];
272 bufcolumn = new int[buffersize];
276 public SimpleCharStream(java.io.Reader dstream, int startline,
279 this(dstream, startline, startcolumn, 4096);
283 public SimpleCharStream(java.io.Reader dstream)
285 this(dstream, 1, 1, 4096);
289 public void ReInit(java.io.Reader dstream, int startline,
290 int startcolumn, int buffersize)
292 inputStream = dstream;
294 column = startcolumn - 1;
296 if (buffer == null || buffersize != buffer.length)
298 available = bufsize = buffersize;
299 buffer = new char[buffersize];
300 bufline = new int[buffersize];
301 bufcolumn = new int[buffersize];
303 prevCharIsLF = prevCharIsCR = false;
304 tokenBegin = inBuf = maxNextCharInd = 0;
309 public void ReInit(java.io.Reader dstream, int startline,
312 ReInit(dstream, startline, startcolumn, 4096);
316 public void ReInit(java.io.Reader dstream)
318 ReInit(dstream, 1, 1, 4096);
321 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
322 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
324 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
328 public SimpleCharStream(java.io.InputStream dstream, int startline,
329 int startcolumn, int buffersize)
331 this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
335 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
336 int startcolumn) throws java.io.UnsupportedEncodingException
338 this(dstream, encoding, startline, startcolumn, 4096);
342 public SimpleCharStream(java.io.InputStream dstream, int startline,
345 this(dstream, startline, startcolumn, 4096);
349 public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
351 this(dstream, encoding, 1, 1, 4096);
355 public SimpleCharStream(java.io.InputStream dstream)
357 this(dstream, 1, 1, 4096);
361 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
362 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
364 ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
368 public void ReInit(java.io.InputStream dstream, int startline,
369 int startcolumn, int buffersize)
371 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
375 public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
377 ReInit(dstream, encoding, 1, 1, 4096);
381 public void ReInit(java.io.InputStream dstream)
383 ReInit(dstream, 1, 1, 4096);
386 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
387 int startcolumn) throws java.io.UnsupportedEncodingException
389 ReInit(dstream, encoding, startline, startcolumn, 4096);
392 public void ReInit(java.io.InputStream dstream, int startline,
395 ReInit(dstream, startline, startcolumn, 4096);
397 /** Get token literal value. */
398 public String GetImage()
400 if (bufpos >= tokenBegin)
401 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
403 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
404 new String(buffer, 0, bufpos + 1);
407 /** Get the suffix. */
408 public char[] GetSuffix(int len)
410 char[] ret = new char[len];
412 if ((bufpos + 1) >= len)
413 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
416 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
418 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
424 /** Reset buffer when finished. */
433 * Method to adjust line and column numbers for the start of a token.
435 public void adjustBeginLineColumn(int newLine, int newCol)
437 int start = tokenBegin;
440 if (bufpos >= tokenBegin)
442 len = bufpos - tokenBegin + inBuf + 1;
446 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
449 int i = 0, j = 0, k = 0;
450 int nextColDiff = 0, columnDiff = 0;
452 while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
454 bufline[j] = newLine;
455 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
456 bufcolumn[j] = newCol + columnDiff;
457 columnDiff = nextColDiff;
463 bufline[j] = newLine++;
464 bufcolumn[j] = newCol + columnDiff;
468 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
469 bufline[j] = newLine++;
471 bufline[j] = newLine;
476 column = bufcolumn[j];
480 /* JavaCC - OriginalChecksum=a27b2601f30a31b2eea422d3e11ea15d (do not edit this line) */