package matlab;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import natlab.CompilationProblem;
import natlab.Parse;

/**
 * A utility for translating from Matlab source to Natlab source.
 * 
 * Exit status:
 *   0 - normal return: successful parse or list of errors
 *   1 - incorrect usage
 *   2 - I/O issue
 *   3 - unexpected parse issue
 */
public class Translator {
  public static void main(String[] args) {
    if (args.length != 1) {
      System.err.println("Usage: java matlab.Translator {basename}");
      System.exit(1);
    }
    List<CompilationProblem> errors = new ArrayList<CompilationProblem>();
    String basename = args[0];
    Reader natlab = Parse.translateFile(basename + ".m", errors);
    if (natlab == null) {
      System.out.println(errors);
      System.exit(0);
    }
    if (errors.isEmpty()) {
      try {
        new FileWriter(basename + ".n").write(Parse.readerToString(natlab));
      } catch (IOException e) {
        e.printStackTrace();
        System.exit(2);
      }
    }
  }
}

@@ -25,26 +25,17 @@
 java.io.
-FileWriter
+*
 ;%0Aimport
@@ -44,152 +44,240 @@
 ava.
-io.IOException;%0Aimport java.io.Reader;%0Aimport java.util.ArrayList;%0Aimport java.util.Lis
+util.ArrayList;%0Aimport java.util.List;%0A%0Aimport matlab.FunctionEndScanner.NoChangeResult;%0Aimport matlab.FunctionEndScanner.ProblemResul
 t;%0A
-%0A
 import 
-n
+m
 atlab.
-CompilationProblem;%0Aimport natlab.Parse
+FunctionEndScanner.TranslationResult;%0A%0Aimport org.antlr.runtime.ANTLRReaderStream
 ;%0A%0A/
@@ -530,16 +530,18 @@
 lator %7B%0A
+  
   public
@@ -571,24 +571,28 @@
 g%5B%5D args) %7B%0A
+    
     if (args
@@ -609,24 +609,30 @@
  1) %7B%0A      
+ 
+     
 System.err.p
@@ -681,32 +681,38 @@
 ename%7D%22);%0A      
+ 
+     
 System.exit(1);%0A
@@ -719,208 +719,630 @@
     
-%7D%0A    List%3CCompilationProblem%3E errors = new ArrayList%3CCompilationProblem%3E();%0A    String basename = args%5B0%5D;%0A    Reader natlab = Parse.translateFile(basename + %22.m%22, errors);%0A    if (natlab == null) %7B%0A
+    %7D%0A        String basename = args%5B0%5D;%0A        try %7B%0A            BufferedReader in = new BufferedReader(new FileReader(basename + %22.m%22));%0A%0A            FunctionEndScanner prescanner = new FunctionEndScanner(in);%0A            FunctionEndScanner.Result result = prescanner.translate();%0A            in.close();%0A%0A            if(result instanceof NoChangeResult) %7B%0A                in = new BufferedReader(new FileReader(basename + %22.m%22)); //just re-open original file%0A            %7D else if(result instanceof ProblemResult) %7B%0A                for(TranslationProblem prob : ((ProblemResult) result).getProblems()) %7B%0A              
     
@@ -1354,11 +1354,11 @@
 tem.
-out
+err
 .pri
@@ -1366,162 +1366,1067 @@
 tln(
-errors);%0A      System.exit(0);%0A    %7D%0A    if (errors.isEmpty()) %7B%0A
+prob);%0A                %7D%0A                System.exit(0); //terminate early since extraction parser can't work without balanced 'end's%0A            %7D else if(result instanceof TranslationResult) %7B%0A                TranslationResult transResult = (TranslationResult) result;%0A                in = new BufferedReader(new StringReader(transResult.getText()));%0A            %7D%0A%0A            OffsetTracker offsetTracker = new OffsetTracker(new TextPosition(1, 1));%0A            List%3CTranslationProblem%3E problems = new ArrayList%3CTranslationProblem%3E();%0A      
       
+S
 tr
-y %7B%0A        new FileWriter(basename + %22.n%22).write(Parse.readerToString(natlab)
+ing destText = MatlabParser.translate(new ANTLRReaderStream(in), 1, 1, offsetTracker, problems);%0A            %0A            PrintWriter out = new PrintWriter(new FileWriter(basename + %22.n%22));%0A            if(problems.isEmpty()) %7B%0A                out.print(destText);%0A            %7D else %7B%0A                for(TranslationProblem prob : problems) %7B%0A                    System.err.println(%22~%22 + prob);%0A                %7D%0A            %7D%0A            out.close();%0A            in.close();%0A            System.exit(0
 );%0A
+  
     
@@ -2434,17 +2434,16 @@
  %7D catch
- 
 (IOExcep
@@ -2460,16 +2460,20 @@
         
+  
+  
 e.printS
@@ -2481,24 +2481,28 @@
 ackTrace();%0A
+    
         Syst
@@ -2521,18 +2521,16 @@
     
-  %7D%0A
     %7D%0A
+  
   %7D%0A

Revert 2362; can't refer to natlab package from matlab package