org.vesalainen.parser.util.CharInput Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lpg Show documentation
Show all versions of lpg Show documentation
Java Lookahead Parser Generator. Generator produces LALR(k) parsers. Grammar
rules are entered using annotations. Rule annotation can be attached to reducer
method, which keeps rule and it's action together.
The newest version!
/*
* Copyright (C) 2014 Timo Vesalainen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.vesalainen.parser.util;
import java.io.IOException;
import java.io.Writer;
import java.nio.CharBuffer;
import java.util.Arrays;
import java.util.EnumSet;
import org.vesalainen.parser.ParserFeature;
/**
*
* @author Timo Vesalainen
* @param A class providing input.
*/
public abstract class CharInput extends Input
{
protected char[] array;
protected CharInput(int size, EnumSet features)
{
super(features);
this.size = size;
this.buffer1 = CharBuffer.allocate(size);
this.buffer2 = buffer1.duplicate();
this.array1 = new CharBuffer[] {buffer1};
this.array2 = new CharBuffer[] {buffer1, buffer2};
if (buffer1.hasArray())
{
this.array = buffer1.array();
}
}
protected CharInput(char[] array, EnumSet features)
{
super(features);
this.size = array.length;
this.buffer1 = CharBuffer.wrap(array);
this.buffer2 = buffer1.duplicate();
this.array1 = new CharBuffer[] {buffer1};
this.array2 = new CharBuffer[] {buffer1, buffer2};
if (buffer1.hasArray())
{
this.array = buffer1.array();
}
}
protected CharInput(CharSequence text, EnumSet features)
{
super(features);
this.size = text.length();
this.buffer1 = CharBuffer.wrap(text);
this.buffer2 = buffer1.duplicate();
this.array1 = new CharBuffer[] {buffer1};
this.array2 = new CharBuffer[] {buffer1, buffer2};
if (buffer1.hasArray())
{
this.array = buffer1.array();
}
}
@Override
protected int get(int index)
{
return buffer1.get(index % size);
}
@Override
public void reuse(CharSequence text)
{
this.size = text.length();
this.buffer1 = CharBuffer.wrap(text);
this.buffer2 = buffer1.duplicate();
this.array1 = new CharBuffer[] {buffer1};
this.array2 = new CharBuffer[] {buffer1, buffer2};
if (buffer1.hasArray())
{
this.array = buffer1.array();
}
end = size;
cursor = 0;
length = 0;
findSkip = 0;
findMark = -1;
waterMark = 0;
}
@Override
protected void set(int index, int value)
{
buffer1.put(index % size, (char)value);
}
@Override
public void write(int start, int length, Writer writer) throws IOException
{
if (array != null)
{
if (start < end-size)
{
throw new IllegalArgumentException("buffer too small");
}
int ps = start % size;
int es = (start+length) % size;
if (ps <= es)
{
writer.write(array, ps, length);
}
else
{
writer.write(array, ps, size-ps);
writer.write(array, 0, es);
}
}
else
{
for (int ii=0;ii= size - (end-cursor))
{
throw new IOException(Arrays.toString(text)+" doesn't fit in the buffer");
}
if (cursor != end)
{
makeRoom(ln);
}
if (array != null)
{
int cms = cursor % size;
if (size - cms < text.length)
{
System.arraycopy(text, 0, array, cms, size - cms);
System.arraycopy(text, size - cms, array, 0, text.length - (size - cms));
}
else
{
System.arraycopy(text, 0, array, cms, text.length);
}
}
else
{
for (int ii=0;ii= size - (end-cursor))
{
throw new IOException(text+" doesn't fit in the buffer");
}
if (cursor != end)
{
makeRoom(ln);
}
for (int ii=0;ii= cms)
{
spaceAtEndOfBuffer = size - ems;
}
int needToWrap = Math.min(ln - spaceAtEndOfBuffer, size - cms);
if (needToWrap > 0)
{
src = size - spaceAtEndOfBuffer - needToWrap;
dst = ln-needToWrap - spaceAtEndOfBuffer;
len = needToWrap;
System.arraycopy(array, src, array, dst, len);
}
src = cms;
if (ems < cms)
{
len = (size - cms) - needToWrap;
}
else
{
len = (ems - cms) - needToWrap;
}
dst = Math.min(cms + ln, size-1);
System.arraycopy(array, src, array, dst, len);
}
else
{
int len = end-cursor;
for (int ii=0;ii