Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
ESC
  : '\\'
     ( 'n'             {this.setText("\n");}
     | 't'             {this.setText("\t");}
     | 'v'             {this.setText("\013");}
     | 'b'             {this.setText("\b");}
     | 'r'             {this.setText("\r");}
     | 'f'             {this.setText("\r");}
     | 'a'             {this.setText("\007");}
     | '\\'            {this.setText("\\");}
     | '?'             {this.setText("?");}
     | '\''            {this.setText("'");}
     | '"'             {this.setText("\"");}
     | OCTDIGIT (OCTDIGIT? OCTDIGIT)?
       {
	 char[] realc = new char[1];
	 realc[0] = (char) Integer.valueOf($text, 8).intValue();
	 this.setText(new String(realc));
       }
     | 'x' HEXDIGIT HEXDIGIT?
       {
         char[] realc = new char[1];
         realc[0] = (char) Integer.valueOf($text.substring(1), 16).intValue();
         this.setText(new String(realc));
       }
     | 'u' HEXDIGIT ((HEXDIGIT? HEXDIGIT)? HEXDIGIT)?
       {
         char[] realc = new char[1];
         realc[0] = (char) Integer.valueOf($text.substring(1), 16).intValue();
         this.setText(new String(realc));
       }
     )
     ;

An alternative approach that may be useful.

Code Block

fragment 
MARKER : '"' ;
ESCCHAR : '\\' ;
LITERAL : MARKER (options {greedy=false;}: ESCCHAR . | .)* MARKER ;

By the parser?

Code Block
UNICODE_LITERAL : '\\u' HEXDIGIT ((HEXDIGIT? HEXDIGIT)? HEXDIGIT)? ;
literal returns [char value] : UNICODE_LITERAL
  { $value = (char)Integer.valueOf($text.substring(1), 16).intValue(); } ;

...