package sicel.compiler.parser.internal;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import sicel.compiler.parser.Lexer;
import sicel.compiler.parser.Token;
import sicel.compiler.parser.exceptions.ParserError;
public abstract class AbstractLexer implements Lexer
{
private BufferedReader reader;
protected LinkedList<Token> tokenBuffer = new LinkedList<Token>();
protected boolean eof = false;
protected final StringBuffer buffer = new StringBuffer();
protected int linePos;
protected int charPos;
protected String fileName;
protected int stateStartCharPos;
protected abstract boolean processChar( char c, boolean eof ) throws ParserError;
public final void initialize( File file )
{
fileName = file.getName();
linePos = 1;
charPos = 0;
buffer.setLength( 0 );
try
{
reader = new BufferedReader( new FileReader( file ) );
}
catch (FileNotFoundException e)
{
throw new ParserError( e );
}
}
@Override
public final Token take() throws ParserError
{
while( tokenBuffer.size() < 1 && !eof )
{
process();
}
return tokenBuffer.poll();
}
@Override
public final Token peek() throws ParserError
{
return lookAhead( 0 );
}
@Override
public final Token lookAhead( int depth ) throws ParserError
{
while( tokenBuffer.size() <= depth && !eof ) process();
if( depth >= tokenBuffer.size() ) return null;
return tokenBuffer.get( depth );
}
private final void process() throws ParserError
{
char[] chars = new char[32];
int rlen;
try
{
rlen = reader.read( chars );
}
catch (IOException e)
{
throw new ParserError( e );
}
if( rlen == -1 )
{
processChar( ' ', true );
eof = true;
return;
}
for( int i = 0; i < rlen; i++ )
{
while( ! processChar( chars[i], false ) );
}
}
}