package runner;

import gerador.*;
import infrastructure.CompilationResultWriter;
import infrastructure.ErrorListeners.*;
import infrastructure.MessageBag;
import java.io.FileInputStream;
import laparser.*;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;

/**
 * Runs the LaParser on a inputFile and outputs the result in an outputFile.
 *
 * This is the main class of the LaParser project. Therefore, the method
 * Runner.main(String[] args) will be called by the evaluator.
 *
 * @author Lucas
 */
public class Runner {

    /**
     * Runs the LaParser on a file that contains a LA source-code.
     *
     * @param inputFile name of the LA source-code file
     * @param outputFile name of the analysis result file
     * @throws Exception
     */
    public void start(String inputFile, String outputFile) throws Exception {

        ANTLRInputStream in        = new ANTLRInputStream(new FileInputStream(inputFile));
        ANTLRInputStream inGerador = new ANTLRInputStream(new FileInputStream(inputFile));
        MessageBag lexicalAndSintaticBag = new MessageBag();
        MessageBag semanticBag = new MessageBag();

        LaLexer lexer   = new LaLexer(in);
        LaParser parser = new LaParser(new CommonTokenStream(lexer));
        LaGeradorLexer  lexerGerador  = new LaGeradorLexer(inGerador);
        LaGeradorParser parserGerador = new LaGeradorParser(new CommonTokenStream(lexerGerador));

        parser.removeErrorListeners();
        lexer .removeErrorListeners();
        parser.removeErrorListeners();
        lexer .removeErrorListeners();

        LexicalErrorListener  lexical  = new LexicalErrorListener(lexicalAndSintaticBag);
        SyntaticErrorListener syntatic = new SyntaticErrorListener(lexicalAndSintaticBag);
        SemanticErrorListener.DefineMessageBag(semanticBag);

        parser.addErrorListener(syntatic);
        lexer .addErrorListener(lexical);

        parser.programa();
        parserGerador.programa();

        CompilationResultWriter writer = new CompilationResultWriter(outputFile);

        // put the first lexic/sintatic error in the writer's buffer
        if (lexicalAndSintaticBag.all().size() > 0) {
            writer
                .put(lexicalAndSintaticBag.first())
                .put("Fim da compilacao")
                .close();
        }
        
        // or, case there none lexic/sintatic error, put all semantic errors
        // in the writer's buffer
        else if (semanticBag.all().size() > 0) {
            for (String message : semanticBag.all()) {
                writer.put(message);
            }
            writer.put("Fim da compilacao").close();
        } else {
            writer.put(Gerador.getText());
            writer.close();
        }
    }

    /**
     * Executes Runner.start() method with the arguments given.
     *
     * @param args array that contains the names of the input and bag files
     * @throws java.lang.Exception
     */
    public static void main(String[] args) throws Exception {
        new Runner().start(args[0], args[1]);
    }
}

@@ -17,16 +17,195 @@
 %0Aimport 
+infrastructure.errorlisteners.SemanticErrorListener;%0Aimport infrastructure.errorlisteners.SyntaticErrorListener;%0Aimport infrastructure.errorlisteners.LexicalErrorListener;%0Aimport 
 gerador.
@@ -256,48 +256,8 @@
 iter
-;%0Aimport infrastructure.ErrorListeners.*
 ;%0Aim
@@ -276,16 +276,27 @@
 ructure.
+messagebag.
 MessageB
@@ -1047,23 +1047,16 @@
 ream in 
-       
 = new AN
@@ -1107,98 +1107,8 @@
 ));%0A
-        ANTLRInputStream inGerador = new ANTLRInputStream(new FileInputStream(inputFile));
 %0A   
@@ -1123,34 +1123,22 @@
 sageBag 
-lexicalAndSintatic
+ first
 Bag = ne
@@ -1174,22 +1174,20 @@
 geBag se
-mantic
+cond
 Bag = ne
@@ -1320,231 +1320,22 @@
 ));%0A
-        LaGeradorLexer  lexerGerador  = new LaGeradorLexer(inGerador);%0A        LaGeradorParser parserGerador = new LaGeradorParser(new CommonTokenStream(lexerGerador));%0A%0A        parser.removeErrorListeners();%0A        lexer 
+%0A        lexer
 .rem
@@ -1397,47 +1397,8 @@
 s();
-%0A        lexer .removeErrorListeners();
 %0A%0A  
@@ -1429,16 +1429,17 @@
 er  
+ 
 lexical 
  = n
@@ -1434,23 +1434,23 @@
 lexical 
- 
 = new 
+ 
 LexicalE
@@ -1462,34 +1462,21 @@
 istener(
-lexicalAndSintatic
+first
 Bag);%0A  
@@ -1540,34 +1540,21 @@
 istener(
-lexicalAndSintatic
+first
 Bag);%0A  
@@ -1600,22 +1600,20 @@
 geBag(se
-mantic
+cond
 Bag);%0A%0A 
@@ -1696,17 +1696,43 @@
 xical);%0A
+        
 %0A
+        try %7B%0A    
         
@@ -1762,33 +1762,195 @@
     
-parserGerador.programa();
+%7D catch (Exception e) %7B%0A            // In this point, it's impossible to say if the input program%0A            // is corect or not. The program should execute under a try catch. %0A        %7D
 %0A%0A  
@@ -2111,34 +2111,21 @@
     if (
-lexicalAndSintatic
+first
 Bag.all(
@@ -2184,26 +2184,13 @@
 put(
-lexicalAndSintatic
+first
 Bag.
@@ -2415,22 +2415,20 @@
 e if (se
-mantic
+cond
 Bag.all(
@@ -2479,22 +2479,20 @@
 age : se
-mantic
+cond
 Bag.all(
@@ -2561,24 +2561,41 @@
       writer
+%0A                
 .put(%22Fim da
@@ -2607,16 +2607,33 @@
 ilacao%22)
+%0A                
 .close()
@@ -2647,15 +2647,437 @@
    %7D
- else %7B
+%0A        %0A        // Reached this point. The code does not have any errors.%0A        // Translate it into C.%0A        else %7B%0A            LaGeradorLexer lexerGerador = new LaGeradorLexer(in);%0A            LaGeradorParser parserGerador = new LaGeradorParser(new CommonTokenStream(lexerGerador));%0A            lexerGerador.removeErrorListeners();%0A            parserGerador.removeErrorListeners();%0A%0A            parserGerador.programa();%0A
 %0A   

Refactor Runner.java