package matlab;

import java.io.*;
import java.util.List;

import org.antlr.runtime.ANTLRReaderStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.Token;

/**
 * A utility for producing the output file corresponding to a given input file.
 * Note that the output should be checked manually before using it as a test.
 */
public class ScannerTestTool {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: java matlab.ScannerTestTool {basename}");
            System.exit(1);
        }
        String basename = args[0];
        try {
            BufferedReader in = new BufferedReader(new FileReader(basename + ".in"));
            MatlabLexer lexer = new MatlabLexer(new ANTLRReaderStream(in));
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            PrintWriter out = new PrintWriter(new FileWriter(basename + ".out"));
            List<TranslationProblem> problems = lexer.getProblems();
            if(problems.isEmpty()) {
                for(Token tok : (List<Token>) tokens.getTokens()) {
                    printToken(out, tok);
                }
            } else {
                for(TranslationProblem prob : problems) {
                    out.print('~');
                    out.print(' ');
                    out.print(prob.getLine());
                    out.print(' ');
                    out.println(prob.getColumn());
                }
            }
            out.close();
            in.close();
            System.exit(0);
        } catch(IOException e) {
            e.printStackTrace();
            System.exit(2);
        }
    }

    public static void printToken(PrintWriter out, Token tok) throws IOException {
        out.print(MatlabParser.tokenNames[tok.getType()]);
        out.print(' ');
        int startLine = tok.getLine();
        int startCol = tok.getCharPositionInLine() + 1;
        TextPosition lastPos = getLastPosition(tok.getText());
        if(lastPos.getLine() == 1) {
            out.print(startLine);
            out.print(' ');
            out.print(startCol);
            out.print(' ');
            out.print(lastPos.getColumn());
        } else {
            out.print(startLine);
            out.print(' ');
            out.print(startCol);
            out.print(' ');
            out.print(startLine + lastPos.getLine() - 1);
            out.print(' ');
            out.print(lastPos.getColumn());
        }
        out.print(' ');
        out.print('=');
        out.print(stringifyValue(tok.getText()));
        out.println();
    }

    public static String stringifyValue(Object value) {
        if(value == null) {
            return null;
        }
        return value.toString().replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
    }
    
    public static TextPosition getLastPosition(String text) throws IOException {
        TrivialScanner scanner = new TrivialScanner(new StringReader(text));
        TextPosition lastPos = null;
        while(true) {
            TextPosition temp = scanner.nextPos();
            if(temp == null) {
                break;
            }
            lastPos = temp;
        }
        return lastPos;
    }
}

@@ -881,16 +881,72 @@
 lexer);%0A
+            List%3CToken%3E tokenList = tokens.getTokens();%0A
         
@@ -1031,21 +1031,57 @@
         
-List%3C
+if(lexer.hasProblem()) %7B%0A                
 Translat
@@ -1094,18 +1094,13 @@
 blem
-%3E
  prob
-lems
  = l
@@ -1117,16 +1117,23 @@
 oblems()
+.get(0)
 ;%0A      
@@ -1142,96 +1142,185 @@
     
-if(problems.isEmpty()) %7B%0A                for(Token tok : (List%3CToken%3E) tokens.getTokens(
+    for(Token tok : tokenList) %7B%0A                    if(tok.getLine() %3E prob.getLine() %7C%7C (tok.getLine() == prob.getLine() && tok.getCharPositionInLine() + 1 %3E= prob.getColumn()
 )) %7B
@@ -1344,27 +1344,25 @@
     
+  
-printToken(out, tok
+  out.print('~'
 );%0A 
@@ -1372,34 +1372,32 @@
             
-%7D%0A
         
     %7D else %7B
@@ -1380,36 +1380,39 @@
             
-    %7D else %7B
+out.print(' ');
 %0A           
@@ -1420,50 +1420,47 @@
     
-for(TranslationProblem prob : problems) %7B%0A
+        out.print(prob.getLine());%0A    
     
@@ -1478,33 +1478,33 @@
      out.print('
-~
+ 
 ');%0A            
@@ -1503,32 +1503,36 @@
                 
+ 
+   
 out.print(' ');%0A
@@ -1516,36 +1516,51 @@
        out.print
-(' '
+ln(prob.getColumn()
 );%0A             
@@ -1570,70 +1570,166 @@
     
-out.print(prob.getLine());%0A                    out.print(' ');
+    break;%0A                    %7D%0A                    printToken(out, tok);%0A                %7D%0A            %7D else %7B%0A                for(Token tok : tokenList) %7B
 %0A   
@@ -1749,36 +1749,27 @@
     
-out.
 print
-ln(prob.getColumn()
+Token(out, tok
 );%0A 
@@ -3133,20 +3133,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub

Print errors in the correct spot.