[antlr-interest] Feature request: output to any Appendable object from TokenRewriteStream

David-Sarah Hopwood david-sarah at jacaranda.org
Sat Jul 25 12:48:47 PDT 2009


It would be convenient to be able to direct the output of TokenRewriteStream
to any Appendable object, rather than just to a String. This would avoid
unnecessary copying when constructing output that includes the rewritten
text, but may include more than one "program", or text from another
source.

The implementation would be really simple -- add or change the following
methods in TokenRewriteStream:

  public String toString(String programName, int start, int end) {
      return appendTo(new StringBuilder(), programName, start, end)
        .toString();
  }

  public Appendable appendTo(Appendable buf) {
      return appendTo(buf, MIN_TOKEN_INDEX, size()-1);
  }

  public Appendable appendTo(Appendable buf, String programName) {
      return appendTo(buf, programName, MIN_TOKEN_INDEX, size()-1);
  }

  public Appendable appendTo(Appendable buf, int start, int end) {
      return appendTo(buf, DEFAULT_PROGRAM_NAME, start, end);
  }

  public Appendable appendTo(Appendable buf, String programName,
                             int start, int end) {
      // implementation of existing toString(programName, start, end)
      // method, but without "StringBuffer buf = new StringBuffer();",
      // and returning buf.
  }

  public String toOriginalString(int start, int end) {
      return appendOriginalTo(new StringBuilder(), start, end).toString();
  }

  public Appendable appendOriginalTo(Appendable buf) {
      return appendOriginalTo(buf, MIN_TOKEN_INDEX, size()-1);
  }

  public Appendable appendOriginalTo(Appendable buf, int start, int end) {
      for (int i=start; i>=MIN_TOKEN_INDEX && i<=end && i<tokens.size(); i++) {
          buf.append(get(i).getText());
      }
      return buf;
  }

-- 
David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com



More information about the antlr-interest mailing list