<commit_before>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]);
    }
}<commit_msg>Refactor Runner.java<commit_after>package runner;

import infrastructure.errorlisteners.SemanticErrorListener;
import infrastructure.errorlisteners.SyntaticErrorListener;
import infrastructure.errorlisteners.LexicalErrorListener;
import gerador.*;
import infrastructure.CompilationResultWriter;
import infrastructure.messagebag.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));

        MessageBag  firstBag = new MessageBag();
        MessageBag secondBag = new MessageBag();

        LaLexer lexer   = new LaLexer(in);
        LaParser parser = new LaParser(new CommonTokenStream(lexer));

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

        LexicalErrorListener   lexical = new  LexicalErrorListener(firstBag);
        SyntaticErrorListener syntatic = new SyntaticErrorListener(firstBag);
        SemanticErrorListener.DefineMessageBag(secondBag);

        parser.addErrorListener(syntatic);
        lexer .addErrorListener(lexical);
        
        try {
            parser.programa();
        } catch (Exception e) {
            // In this point, it's impossible to say if the input program
            // is corect or not. The program should execute under a try catch. 
        }

        CompilationResultWriter writer = new CompilationResultWriter(outputFile);

        // put the first lexic/sintatic error in the writer's buffer
        if (firstBag.all().size() > 0) {
            writer
                .put(firstBag.first())
                .put("Fim da compilacao")
                .close();
        }
        
        // or, case there none lexic/sintatic error, put all semantic errors
        // in the writer's buffer
        else if (secondBag.all().size() > 0) {
            for (String message : secondBag.all()) {
                writer.put(message);
            }
            writer
                .put("Fim da compilacao")
                .close();
        }
        
        // Reached this point. The code does not have any errors.
        // Translate it into C.
        else {
            LaGeradorLexer lexerGerador = new LaGeradorLexer(in);
            LaGeradorParser parserGerador = new LaGeradorParser(new CommonTokenStream(lexerGerador));
            lexerGerador.removeErrorListeners();
            parserGerador.removeErrorListeners();

            parserGerador.programa();

            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]);
    }
}
{'fraction_non_alphanumeric': 0.08758465011286681, 'fraction_numerical': 0.0018058690744920992, 'mean_word_length': 2.5426439232409384, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4475357', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1748, 'n_words': 505}