buffer.append( c );
return true;
}
else
{
throw new SyntaxError(
"Unexpected input: Expected '`', got '" + c + "'",
fileName, linePos, charPos );
}
case NUMBER:
if( c == 'L' || c == 'l' || c == 'd' || c == 'D' || c == 'f' || c == 'F' )
{
buffer.append( c );
return true;
}
else if( ( ! ( Character.isDigit( c ) || c == '.' || c == 'E' ) ) || eof )
{
String res = buffer.toString();
if( res.matches( "-?[1-9][0-9]*" ) )
{
int value = Integer.parseInt( res );
tokenBuffer.offer( new T_INT_LITERAL( value, linePos, stateStartCharPos ) );
}
else if( res.matches( "-?[1-9][0-9]*L" ) )
{
long value = Long.parseLong( res );
tokenBuffer.offer( new T_LONG_LITERAL( value, linePos, stateStartCharPos ) );
}
else if( res.matches( "-?([1-9][0-9]*|0)\\.[0-9]*[dD]?" ) ||
res.matches( "-?[0-9]\\.[0-9]+E[1-9][0-9]*" ) )
{
double value = Double.parseDouble( res );
tokenBuffer.offer( new T_DOUBLE_LITERAL( value, linePos, stateStartCharPos ) );
}
else if( res.matches( "-?([1-9][0-9]*|0)\\.[0-9]*[fF]" ) )
{
float value = Float.parseFloat( res );
tokenBuffer.offer( new T_FLOAT_LITERAL( value, linePos, stateStartCharPos ) );
}
else
{
throw new SyntaxError( "Unrecognized number format: '" + res + "'",
fileName, linePos, stateStartCharPos );
}
buffer.setLength( 0 );
lexerState = LexerState.INIT;
charPos--;
return false;
}
else
{
buffer.append( c );
return true;
}
case OPERATOR:
if( Arrays.binarySearch( opChars, c ) >= 0 )
{
buffer.append( c );
return true;
}
else
{
tokenBuffer.offer( new T_OPERATOR( buffer.toString(), linePos, stateStartCharPos ) );
buffer.setLength( 0 );
lexerState = LexerState.INIT;
charPos--;
return false;
}
case STRING:
if( c == '"' )
{
tokenBuffer.offer( new T_STRING_LITERAL( buffer.toString(), linePos, stateStartCharPos ) );
buffer.setLength( 0 );
lexerState = LexerState.INIT;
return true;
}
else if( c == '\\' )
{
buffer.append( '\\' );
lexerState = LexerState.STRING_ESCAPE;
return true;
}
else
{
buffer.append( c );
return true;
}
case STRING_ESCAPE:
buffer.append( c );
lexerState = LexerState.STRING;
return true;
case INIT:
if( c == '\n' )
{
tokenBuffer.offer( new T_EOL( linePos, charPos ) );
charPos = 0;
linePos++;
return true;
}
if( c == '\t' )
{
charPos += 3;
return true;
}
if( Character.isWhitespace( c ) ) { lexerState = LexerState.INIT; return true; }
if( Arrays.binarySearch( opChars, c ) >= 0 )
{
buffer.append( c );
stateStartCharPos = charPos;
lexerState = LexerState.OPERATOR;
return true;
}
if( c == '"' ) { stateStartCharPos = charPos; lexerState = LexerState.STRING; return true; }
if( c == '`' ) { stateStartCharPos = charPos; lexerState = LexerState.INFIX_WORD; return true; }
if( c == '-' ) { stateStartCharPos = charPos; lexerState = LexerState.DASH; return true; }
if( c == ',' ) { tokenBuffer.offer( new T_COMMA( linePos, charPos ) ); return true; }
if( c == '{' ) { tokenBuffer.offer( new T_BRACE_OPEN( linePos, charPos ) ); return true; }
if( c == '}' ) { tokenBuffer.offer( new T_BRACE_CLOSE( linePos, charPos ) ); return true; }
if( c == '(' ) { tokenBuffer.offer( new T_PAREN_OPEN( linePos, charPos ) ); return true; }
if( c == ')' ) { tokenBuffer.offer( new T_PAREN_CLOSE( linePos, charPos ) ); return true; }
if( c == '[' ) { tokenBuffer.offer( new T_SQUARE_OPEN( linePos, charPos ) ); return true; }
if( c == ']' ) { tokenBuffer.offer( new T_SQUARE_CLOSE( linePos, charPos ) ); return true; }
if( c == '.' ) { tokenBuffer.offer( new T_DOT( linePos, charPos ) ); return true; }
if( Character.isLetter( c ) )
{
buffer.append( c );
stateStartCharPos = charPos;
lexerState = LexerState.WORD;
return true;
}
if( Character.isDigit( c ) && c != '0' )
{
buffer.append( c );
stateStartCharPos = charPos;
lexerState = LexerState.NUMBER;
return true;
}
throw new SyntaxError( "Unrecognized input '" + c + "'",
fileName, linePos, charPos );
default:
throw new SyntaxError( "Invalid parser state",
fileName, linePos, charPos );
}
}