If the input matched by your token is "Hello"
then the value of the image
field of the token will be a 7 character string whose first and last characters are double quote characters. They're not really extra they were they in the input. Say you write
void foo() : {
Token t ; }
{
t = <STRING>
{ System.out.println( t.image ) ; }
}
That'll print 7 characters and then a newline.
Now if you don't want those characters, well, @Bryan's answer will do it.
void foo() : {
Token t ; }
{
t = <STRING>
{ { String nakedImage = t.image.substring(1,t.image.length()-1) ;
System.out.println( nakedImage ) ; } }
}
It should be noted that no quotes are removed. String
objects in Java are immutable, meaning they can't be changed. What really happens is that a new String
object gets created and a reference to it is assign to the nakedImage
variable. The String
object that t.image
is a reference to remains the same.
Now you still have the problem of dealing with the back slashes. If the input is "Hello\tWorld", then t.image
will be 14 characters long and nakedImage
will be 12 characters long. What I do at this point is to run the string through a function builds a new string that has single characters where the nakedImage
has escape sequences. So the result of that function on this example would be 11 characters long.
void foo() : {
Token t ; }
{
t = <STRING>
{ { String nakedImage = t.image.substring(1,t.image.length()-1) ;
String unescapedImage = unescape( nakedImage ) ;
System.out.println( unescapedImage ) ; } }
}
Here's such a function, based on one I wrote for a Java compiler.
private static String unescape( String str ) {
StringBuffer result = new StringBuffer() ;
for( int i=0, len = str.length() ; i<len ; ) {
char ch = str.charAt(i) ;
// Set ch and increment i ;
if( ch == '\\' ) {
ch = str.charAt(i+1) ;
switch( ch ) {
case 'b' : ch = '\b' ; i += 2 ; break ;
case 't' : ch = '\t' ; i += 2 ; break ;
case 'n' : ch = '\n' ; i += 2 ; break ;
case 'f' : ch = '\f' ; i += 2 ; break ;
case 'r' : ch = '\r' ; i += 2 ; break ;
case '"' : case '\'' : case '\\' : i+= 2 ; break ;
default:
/*TODO Deal with errors. */ } }
else {
i += 1 ; }
result.append( ch ) ; }
return result.toString() ;
}