@@ -0,0 +1,2425 @@
+package mclint.layout;%0A%0Aimport java.io.IOException;%0Aimport java.io.Reader;%0Aimport java.io.StringReader;%0Aimport java.util.List;%0A%0Aimport matlab.MatlabLexer;%0Aimport mclint.util.Parsing;%0A%0Aimport org.antlr.runtime.ANTLRReaderStream;%0Aimport org.antlr.runtime.Token;%0A%0Aimport ast.Program;%0A%0Aimport com.google.common.collect.HashBasedTable;%0Aimport com.google.common.collect.Lists;%0Aimport com.google.common.collect.Table;%0Aimport com.google.common.io.CharStreams;%0A%0A/**%0A * A utility for performing layout-preserving AST transformations.%0A * %0A * Given the text of an input program, this class maintains a token stream together with an AST%0A * and keeps them synchronized as the AST changes.%0A *%0A */%0Apublic class LayoutPreservingTransformer %7B%0A  private List%3CToken%3E tokens;%0A  private Table%3CInteger, Integer, Integer%3E tokensByPosition;%0A  private Program program;%0A%0A  public static LayoutPreservingTransformer of(Reader source) throws IOException %7B%0A    String code = CharStreams.toString(source);%0A    Program program = Parsing.string(code);%0A    List%3CToken%3E tokens = getTokens(new StringReader(code));%0A    Table%3CInteger, Integer, Integer%3E tokensByPosition = indexByPosition(tokens);%0A    return new LayoutPreservingTransformer(program, tokens, tokensByPosition);%0A  %7D%0A%0A  public Program getProgram() %7B%0A    return (Program) program.fullCopy();%0A  %7D%0A  %0A  public String reconstructText() %7B%0A    StringBuilder sb = new StringBuilder();%0A    for (Token token : tokens) %7B%0A      sb.append(token.getText());%0A    %7D%0A    return sb.toString();%0A  %7D%0A  %0A  private LayoutPreservingTransformer(Program program, List%3CToken%3E tokens,%0A      Table%3CInteger, Integer, Integer%3E tokensByPosition) %7B%0A    this.program = program;%0A    this.tokens = tokens;%0A    this.tokensByPosition = tokensByPosition;%0A  %7D%0A  %0A  private static Table%3CInteger, Integer, Integer%3E indexByPosition(List%3CToken%3E tokens) %7B%0A    Table%3CInteger, Integer, Integer%3E table = HashBasedTable.create();%0A    int index = 0;%0A    for (Token token : tokens) %7B%0A      table.put(token.getLine(), token.getCharPositionInLine(), index++);%0A    %7D%0A    return table;%0A  %7D%0A  %0A  private static List%3CToken%3E getTokens(Reader source) throws IOException %7B%0A    List%3CToken%3E tokens = Lists.newArrayList();%0A    MatlabLexer lexer = new MatlabLexer(new ANTLRReaderStream(source));%0A    Token token = lexer.nextToken();%0A    while (token != Token.EOF_TOKEN) %7B%0A      tokens.add(token);%0A      token = lexer.nextToken();%0A    %7D%0A    return tokens;%0A  %7D%0A%7D%0A

Add LayoutPreservingTransformer.