title: Replace token in ANTLR
body: <p>I want to replace a token using ANTLR.</p>
  
  <p>I tried with TokenRewriteStream and replace, but it didn't work.</p>
  
  <p>Any suggestions?</p>
  
  <pre><code>  ANTLRStringStream in = new ANTLRStringStream(source);
    MyLexer lexer = new MyLexer(in);
    TokenRewriteStream tokens = new TokenRewriteStream(lexer);
    for(Object obj : tokens.getTokens()) {
       CommonToken token = (CommonToken)obj;  
       tokens.replace(token, "replacement");
    }
  </code></pre>
  
  <p>The lexer finds all occurences of single-line comments, and i want to replace them in the original source too.</p>
  
  <p>EDIT:</p>
  
  <p>This is the grammar:</p>
  
  <pre><code>grammar ANTLRTest;
  
  options {
    language = Java;
  }
  
  @header {
    package main;
  }
  
  @lexer::header {
    package main;
  }
  
  rule: SingleLineComment+;
  
  SingleLineComment
    :  '//' ~( '\r' | '\n' )* {setText("replacement");}
      ;
  </code></pre>
  
  <p>What i want to do is replace all single-line comments in a file, let's say.</p>