fr.umlv.tatoo.runtime.buffer.impl
Class UTF8Wrapper<T>

java.lang.Object
  extended by fr.umlv.tatoo.runtime.buffer.impl.UTF8Wrapper<T>
Type Parameters:
T - the type of the token view of the wrapper
All Implemented Interfaces:
LexerBuffer, TokenBuffer<T>

public class UTF8Wrapper<T>
extends Object
implements LexerBuffer, TokenBuffer<T>

The class provides a buffer for using with a lexer build with UTF-8 encoding (UTF8Encoding). It can wrap InputStream and ReadableByteChannel

Author:
Julien Cervelle

Method Summary
 void discard()
          Discards all characters already recognized by the lexer.
 LocationTracker getLocationProvider()
          Returns a location provider.
 boolean hasRemaining()
          Returns true if more characters are availables in the buffer.
 int lastChar()
          Returns last read char or -1 if not available.
 int next()
          Reads the next character from the buffer.
 boolean previousWasNewLine()
          Determines if the last unwinded character was an end of line.
 boolean read()
          Reads at some bytes from the stream.
 void reset()
          Moves current position back to first non unwinded character
 void restart()
          Moves current position back to first non discarded character
 void unwind(int count)
          Unwinds count characters from the buffer.
 T view()
          Return a view of the tokens recognized in the buffer.
static UTF8Wrapper<ByteBuffer> wrap(boolean direct, int capacity, int increment, int chunkSize, ReadableByteChannel readable, LocationTracker tracker)
          Construct a UTF8Wrapper used by the lexer to process the ReadableByteChannel.
static UTF8Wrapper<InputStream> wrap(InputStream inputStream, LocationTracker tracker)
          Constructs an UTF8Wrapper wrapper used by the lexer to process the InputStream.
static UTF8Wrapper<InputStream> wrap(int capacity, int increment, int chunkSize, InputStream inputStream, LocationTracker tracker)
          Construct a UTF8Wrapper used by the lexer to process the InputStream.
static UTF8Wrapper<ByteBuffer> wrap(ReadableByteChannel readable, LocationTracker tracker)
          Constructs an UTF8Wrapper wrapper used by the lexer to process the ReadableByteChannel.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

wrap

public static UTF8Wrapper<InputStream> wrap(int capacity,
                                            int increment,
                                            int chunkSize,
                                            InputStream inputStream,
                                            LocationTracker tracker)
Construct a UTF8Wrapper used by the lexer to process the InputStream. The parameters specify how the underlying buffer which stores data in allocated and reallocated when more space is needed.

Parameters:
capacity - the initial capacity
increment - how much to increment the size of the buffer when more space is required
chunkSize - how many bytes are read at once is the input
inputStream - the input
tracker - the location tracker

wrap

public static UTF8Wrapper<InputStream> wrap(InputStream inputStream,
                                            LocationTracker tracker)
Constructs an UTF8Wrapper wrapper used by the lexer to process the InputStream.

Parameters:
inputStream - the InputStream to wrap
tracker - the location tracker

wrap

public static UTF8Wrapper<ByteBuffer> wrap(boolean direct,
                                           int capacity,
                                           int increment,
                                           int chunkSize,
                                           ReadableByteChannel readable,
                                           LocationTracker tracker)
Construct a UTF8Wrapper used by the lexer to process the ReadableByteChannel. The parameters specify how the underlying buffer which stores data in allocated and reallocated when more space is needed.

Parameters:
direct - if the ByteBuffer must be direct
capacity - the initial capacity
increment - how much to increment the size of the buffer when more space is required
chunkSize - how many bytes are read at once is the input
readable - the input
tracker - the location tracker
Returns:
a new lexer buffer

wrap

public static UTF8Wrapper<ByteBuffer> wrap(ReadableByteChannel readable,
                                           LocationTracker tracker)
Constructs an UTF8Wrapper wrapper used by the lexer to process the ReadableByteChannel.

Parameters:
readable - the ReadableByteChannel to wrap
tracker - the location tracker
Returns:
a new lexer buffer

next

public int next()
Description copied from interface: LexerBuffer
Reads the next character from the buffer. Current position in the buffer is incremented.

Specified by:
next in interface LexerBuffer
Returns:
the next character in the buffer

hasRemaining

public boolean hasRemaining()
Description copied from interface: LexerBuffer
Returns true if more characters are availables in the buffer. If the end of file has been reached and backtracked by lexer this method must return true.

Specified by:
hasRemaining in interface LexerBuffer
Returns:
true if more characters are availables in the buffer

discard

public void discard()
Description copied from interface: LexerBuffer
Discards all characters already recognized by the lexer.

Specified by:
discard in interface LexerBuffer
Specified by:
discard in interface TokenBuffer<T>

restart

public void restart()
Description copied from interface: LexerBuffer
Moves current position back to first non discarded character

Specified by:
restart in interface LexerBuffer

unwind

public void unwind(int count)
Description copied from interface: LexerBuffer
Unwinds count characters from the buffer. count must not be 0.

Specified by:
unwind in interface LexerBuffer
Parameters:
count - the number of characters to unwind

reset

public void reset()
Description copied from interface: LexerBuffer
Moves current position back to first non unwinded character

Specified by:
reset in interface LexerBuffer

previousWasNewLine

public boolean previousWasNewLine()
Description copied from interface: LexerBuffer
Determines if the last unwinded character was an end of line.

Specified by:
previousWasNewLine in interface LexerBuffer
Returns:
true if the last unwinded character was an end of line; false otherwise

read

public boolean read()
             throws IOException
Description copied from interface: LexerBuffer
Reads at some bytes from the stream. This operation is optionnal and only used by SimpleLexer.run().

Specified by:
read in interface LexerBuffer
Returns:
false at end of stream
Throws:
IOException - if an i/o operation failed.

view

public T view()
Return a view of the tokens recognized in the buffer. If the view has methods to modify its limit, they must not be called.

Specified by:
view in interface TokenBuffer<T>
Returns:
a view of the tokens recognized in the buffer.
See Also:
ReadableByteChannelWrapper.view()

lastChar

public int lastChar()
Description copied from interface: LexerBuffer
Returns last read char or -1 if not available. This method is used for logging and error reporting. This operation is optional since this method can return -1 anytime

Specified by:
lastChar in interface LexerBuffer
Returns:
last read char or -1 if not available

getLocationProvider

public LocationTracker getLocationProvider()
Description copied from interface: LexerBuffer
Returns a location provider.

Specified by:
getLocationProvider in interface LexerBuffer
Returns:
the location provider or null if the buffer has no location provider.