<commit_before>package com.makkajai;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Autogenerated by ObjCToCppTranslator (c) 2015 Makkajai
 * @author <NAME>
 * This code is licensed under MIT license (see LICENSE.txt for details)
 */
public class ObjCToCppTranslator extends ObjCBaseVisitor<Void> {

    public static final String BEGINS_WITH_2_UPPER_CASE_LETTERS = "([A-Z]{2})(.*)";
    public static final String BEGINS_WITH_UPPER_CASE_LETTERS = "([A-Z]{1})(.*)";

    private CommonTokenStream tokens;
    private StringBuilder outputBuffer;

    /**
     * Main Method
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        //The input file to parse!
        ANTLRInputStream input = new ANTLRInputStream(
                new FileInputStream("/Users/administrator/playground/projarea/math-monsters-2/makkajai-number-muncher/makkajai-ios/Makkajai/Makkajai/Utils/MakkajaiEnum.m")); // we'll


        //The instance of the translator.
        ObjCToCppTranslator visitor = new ObjCToCppTranslator();

        //Getting the lexer.
        ObjCLexer lexer = new ObjCLexer(input);

        //This is the single most important guy.  Useful in getting the source set of tokens and stuff.
        visitor.tokens = new CommonTokenStream(lexer);

        //Parser obviously.
        ObjCParser parser = new ObjCParser(visitor.tokens);
        ParseTree tree = parser.translation_unit();

        //Output file.
        FileWriter outputFile = new FileWriter("/Users/administrator/playground/projarea/math-monsters-2/makkajai-number-muncher/makkajai-ios/Makkajai/Makkajai/Utils/MakkajaiEnum.cpp");
        visitor.outputBuffer = new StringBuilder(getFileHeader())
                .append(visitor.tokens.getText());

        //This is where the entire file is parsed and appropreat callbacks are made to parse the input file.
        visitor.visit(tree);

        //All done, lets write the output buffer to the output file and get done with it!
        outputFile.write(visitor.translateLiterals(visitor.outputBuffer.toString()));
        outputFile.flush();
        outputFile.close();
    }

    @Override
    public Void visitMethod_type(ObjCParser.Method_typeContext ctx) {
//        System.out.println("Method type:" + ctx.getText());
        return super.visitMethod_type(ctx);
    }

    @Override
    public Void visitDictionary_pair(ObjCParser.Dictionary_pairContext ctx) {
        System.out.println("Dictionary Pair:" + ctx.getText());
        return super.visitDictionary_pair(ctx);
    }

    @Override
    public Void visitArray_expression(ObjCParser.Array_expressionContext ctx) {
        System.out.println("Array expression:" + ctx.getText());
        return super.visitArray_expression(ctx);
    }

    @Override
    public Void visitMessage_expression(ObjCParser.Message_expressionContext ctx) {
        String text = tokens.getText(ctx.getSourceInterval());
        int start = outputBuffer.indexOf(text);
        if(start < 0) {
            return super.visitMessage_expression(ctx);
        }
        int end = start + text.length();

        String receiver = translateReceiver(tokens.getText(ctx.receiver().getSourceInterval()));
        String finalMethod = "(" + receiver + translateInvocationOperator(receiver);
        finalMethod += translateMethodNameAndParameters(ctx, finalMethod);

        outputBuffer.replace(start, end, finalMethod);
        return super.visitMessage_expression(ctx);
    }

    @Override
    public Void visitMethod_definition(ObjCParser.Method_definitionContext ctx) {
//        System.out.println("Method name:" + ctx.getText());
        return super.visitMethod_definition(ctx);
    }

    @Override
    public Void visitMethod_declaration(ObjCParser.Method_declarationContext ctx) {
//        System.out.println("Method name:" + ctx.getText());
        return super.visitMethod_declaration(ctx);
    }

    @Override
    public Void visitInstance_method_declaration(ObjCParser.Instance_method_declarationContext ctx) {
//        System.out.println("Method name:" + ctx.getText());
        return super.visitInstance_method_declaration(ctx);
    }

    @Override
    public Void visitClass_method_declaration(ObjCParser.Class_method_declarationContext ctx) {
//        System.out.println("Method name:" + ctx.getText());
        return super.visitClass_method_declaration(ctx);
    }

    private String translateReceiver(String receiver) {
        Pattern pattern = Pattern.compile(BEGINS_WITH_2_UPPER_CASE_LETTERS);
        Matcher matcher = pattern.matcher(receiver);
        if(matcher.matches()) {
            return matcher.group(2);
        }
        return receiver;
    }

    private String translateInvocationOperator(String receiver) {
        Pattern pattern = Pattern.compile(BEGINS_WITH_UPPER_CASE_LETTERS);
        Matcher matcher = pattern.matcher(receiver);
        if(matcher.matches()) {
            //Looks like we are making a static call.
            return "::";
        }
        return "->";
    }

    private String translateMethodNameAndParameters(ObjCParser.Message_expressionContext ctx, String finalMethod) {
        String finalMethodName = "";
        String finalParameters = "";

        //If this is just a method with no keyword argument we need to get just the selector and use it as the method name.
        if(ctx.message_selector().keyword_argument().size() == 0) {
            finalMethodName = tokens.getText(ctx.message_selector().selector().getSourceInterval());
        }

        //Well this method is made up of multiple parts we will need to upper case the subsequent parts to follow Camel case convention
        //and then pass the parameters as method call.
        for (int i=0; i<ctx.message_selector().keyword_argument().size(); i++) {
            ObjCParser.Keyword_argumentContext keyword_argumentContext = ctx.message_selector().keyword_argument().get(i);
            String sourceMethod = tokens.getText(keyword_argumentContext.selector().getSourceInterval());
            String sourceParameter = tokens.getText(keyword_argumentContext.expression().getSourceInterval());
            if(i > 0) {
                sourceMethod = sourceMethod.substring(0, 1).toUpperCase() + sourceMethod.substring(1);
                finalParameters += ", ";
            }
            finalMethodName += sourceMethod;
            finalParameters += sourceParameter;
        }

        return finalMethodName + "(" + finalParameters + "))";
    }

    private String translateLiterals(String source) {
        //Final set of translations to get rid of the imports and supers and self and nil.
        return source.replaceAll("#import", "#include")
                .replaceAll("super", "base")
                .replaceAll("self", "this")
                .replaceAll("nil", "NULL");
    }<commit_msg>We are now able to parse and convert the method declerations as well! <Deep><commit_after>package com.makkajai;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Autogenerated by ObjCToCppTranslator (c) 2015 Makkajai
 * @author <NAME>
 * This code is licensed under MIT license (see LICENSE.txt for details)
 */
public class ObjCToCppTranslator extends ObjCBaseVisitor<Void> {

    public static final String BEGINS_WITH_2_UPPER_CASE_LETTERS = "([A-Z]{2})(.*)";
    public static final String BEGINS_WITH_UPPER_CASE_LETTERS = "([A-Z]{1})(.*)";

    private CommonTokenStream tokens;
    private StringBuilder outputBuffer;

    /**
     * Main Method
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        //The input file to parse!
        ANTLRInputStream input = new ANTLRInputStream(
                new FileInputStream("/Users/administrator/playground/projarea/math-monsters-2/makkajai-number-muncher/makkajai-ios/Makkajai/Makkajai/Utils/MakkajaiEnum.m")); // we'll


        //The instance of the translator.
        ObjCToCppTranslator visitor = new ObjCToCppTranslator();

        //Getting the lexer.
        ObjCLexer lexer = new ObjCLexer(input);

        //This is the single most important guy.  Useful in getting the source set of tokens and stuff.
        visitor.tokens = new CommonTokenStream(lexer);

        //Parser obviously.
        ObjCParser parser = new ObjCParser(visitor.tokens);
        ParseTree tree = parser.translation_unit();

        //Output file.
        FileWriter outputFile = new FileWriter("/Users/administrator/playground/projarea/math-monsters-2/makkajai-number-muncher/makkajai-ios/Makkajai/Makkajai/Utils/MakkajaiEnum.cpp");
        visitor.outputBuffer = new StringBuilder(getFileHeader())
                .append(visitor.tokens.getText());

        //This is where the entire file is parsed and appropreat callbacks are made to parse the input file.
        visitor.visit(tree);

        //All done, lets write the output buffer to the output file and get done with it!
        outputFile.write(visitor.translateLiterals(visitor.outputBuffer.toString()));
        outputFile.flush();
        outputFile.close();
    }

    @Override
    public Void visitMethod_type(ObjCParser.Method_typeContext ctx) {
//        System.out.println("Method type:" + ctx.getText());
        return super.visitMethod_type(ctx);
    }

    @Override
    public Void visitDictionary_pair(ObjCParser.Dictionary_pairContext ctx) {
        return super.visitDictionary_pair(ctx);
    }

    @Override
    public Void visitArray_expression(ObjCParser.Array_expressionContext ctx) {
        System.out.println("Array expression:" + ctx.getText());
        return super.visitArray_expression(ctx);
    }

    @Override
    public Void visitMessage_expression(ObjCParser.Message_expressionContext ctx) {
        String text = tokens.getText(ctx.getSourceInterval());
        int start = outputBuffer.indexOf(text);
        if(start < 0) {
            return super.visitMessage_expression(ctx);
        }
        int end = start + text.length();

        String receiver = translateReceiver(tokens.getText(ctx.receiver().getSourceInterval()));
        String finalMethod = "(" + receiver + translateInvocationOperator(receiver);
        finalMethod += translateMethodNameAndParameters(ctx.message_selector()) + ")";

        outputBuffer.replace(start, end, finalMethod);
        return super.visitMessage_expression(ctx);
    }

    @Override
    public Void visitInstance_method_definition(ObjCParser.Instance_method_definitionContext ctx) {
        String methodBody = tokens.getText(ctx.getSourceInterval());

        int startMethodBody = outputBuffer.indexOf(methodBody);

        startMethodBody = outputBuffer.lastIndexOf("-", startMethodBody);

        if(startMethodBody < 0) {
            return super.visitInstance_method_definition(ctx);
        }

        translateMethodDefination(ctx.method_definition(), startMethodBody, "");

        return super.visitInstance_method_definition(ctx);
    }

    @Override
    public Void visitClass_method_definition(ObjCParser.Class_method_definitionContext ctx) {
        String methodBody = tokens.getText(ctx.getSourceInterval());

        int startMethodBody = outputBuffer.indexOf(methodBody);

        startMethodBody = outputBuffer.lastIndexOf("+", startMethodBody);

        if(startMethodBody < 0) {
            return super.visitClass_method_definition(ctx);
        }

        translateMethodDefination(ctx.method_definition(), startMethodBody, "static ");

        return super.visitClass_method_definition(ctx);
    }

    @Override
    public Void visitMethod_definition(ObjCParser.Method_definitionContext ctx) {
        return super.visitMethod_definition(ctx);
    }

    @Override
    public Void visitMethod_selector(ObjCParser.Method_selectorContext ctx) {
        return super.visitMethod_selector(ctx);
    }

    @Override
    public Void visitMethod_declaration(ObjCParser.Method_declarationContext ctx) {
        System.out.println("Method name:" + ctx.getText());
        return super.visitMethod_declaration(ctx);
    }

    @Override
    public Void visitInstance_method_declaration(ObjCParser.Instance_method_declarationContext ctx) {
        System.out.println("Method name:" + ctx.getText());
        return super.visitInstance_method_declaration(ctx);
    }

    @Override
    public Void visitClass_method_declaration(ObjCParser.Class_method_declarationContext ctx) {
//        System.out.println("Method name:" + ctx.getText());
        return super.visitClass_method_declaration(ctx);
    }

    private String translateReceiver(String receiver) {
        Pattern pattern = Pattern.compile(BEGINS_WITH_2_UPPER_CASE_LETTERS);
        Matcher matcher = pattern.matcher(receiver);
        if(matcher.matches()) {
            return matcher.group(2);
        }
        return receiver;
    }

    private String translateInvocationOperator(String receiver) {
        Pattern pattern = Pattern.compile(BEGINS_WITH_UPPER_CASE_LETTERS);
        Matcher matcher = pattern.matcher(receiver);
        if(matcher.matches()) {
            //Looks like we are making a static call.
            return "::";
        }
        return "->";
    }

    private String translateMethodNameAndParameters(ObjCParser.Message_selectorContext messageSelectorContext) {
        String finalMethodName = "";
        String finalParameters = "";

        //If this is just a method with no keyword argument we need to get just the selector and use it as the method name.
        if(messageSelectorContext.keyword_argument().size() == 0) {
            finalMethodName = tokens.getText(messageSelectorContext.selector().getSourceInterval());
        }

        //Well this method is made up of multiple parts we will need to upper case the subsequent parts to follow Camel case convention
        //and then pass the parameters as method call.
        for (int i = 0; i< messageSelectorContext.keyword_argument().size(); i++) {
            ObjCParser.Keyword_argumentContext keyword_argumentContext = messageSelectorContext.keyword_argument().get(i);
            String sourceMethod = tokens.getText(keyword_argumentContext.selector().getSourceInterval());
            String sourceParameter = tokens.getText(keyword_argumentContext.expression().getSourceInterval());
            if(i > 0) {
                sourceMethod = sourceMethod.substring(0, 1).toUpperCase() + sourceMethod.substring(1);
                finalParameters += ", ";
            }
            finalMethodName += sourceMethod;
            finalParameters += sourceParameter;
        }

        return finalMethodName + "(" + finalParameters + ")";
    }

    private void translateMethodDefination(ObjCParser.Method_definitionContext methodDefinitionContext, int startMethodBody, String methodPrefix) {

        ObjCParser.Method_typeContext method_typeContext = methodDefinitionContext.method_type();
        ObjCParser.Method_selectorContext method_selectorContext = methodDefinitionContext.method_selector();

        String methodTypeString = tokens.getText(method_typeContext.getSourceInterval());
        int startMethodType = outputBuffer.indexOf(methodTypeString, startMethodBody);
        int endMethodType = startMethodType + methodTypeString.length();

        methodTypeString = methodPrefix + methodTypeString.replaceAll("\\(", "").replaceAll("\\)", " ");
        outputBuffer.replace(startMethodBody, endMethodType, methodTypeString);

        String finalMethodNameParameterTypeAndNames = translateMethodNameParameterTypeAndNames(method_selectorContext);

        String methodSelectorString = tokens.getText(method_selectorContext.getSourceInterval());
        int startMethodSelector = outputBuffer.indexOf(methodSelectorString);
        int endMethodSelector = startMethodSelector + methodSelectorString.length();
        outputBuffer.replace(startMethodSelector, endMethodSelector, finalMethodNameParameterTypeAndNames);
    }

    private String translateMethodNameParameterTypeAndNames(ObjCParser.Method_selectorContext methodSelectorContext) {
        String finalMethodName = "";
        String finalParameters = "";

        //If this is just a method with no keyword argument we need to get just the selector and use it as the method name.
        if(methodSelectorContext.keyword_declarator().size() == 0) {
            finalMethodName = tokens.getText(methodSelectorContext.selector().getSourceInterval());
        }

        //Well this method is made up of multiple parts we will need to upper case the subsequent parts to follow Camel case convention
        //and then pass the parameters as method call.
        for (int i = 0; i< methodSelectorContext.keyword_declarator().size(); i++) {
            ObjCParser.Keyword_declaratorContext keyword_declaratorContext = methodSelectorContext.keyword_declarator().get(i);
            String sourceMethod = tokens.getText(keyword_declaratorContext.selector().getSourceInterval());
            String sourceType = tokens.getText(keyword_declaratorContext.method_type(0).getSourceInterval());
            String sourceParameter = tokens.getText(keyword_declaratorContext.IDENTIFIER().getSourceInterval());
            if(i > 0) {
                sourceMethod = sourceMethod.substring(0, 1).toUpperCase() + sourceMethod.substring(1);
                finalParameters += ", ";
            }
            finalMethodName += sourceMethod;
            finalParameters += sourceType.replaceAll("\\(", "").replaceAll("\\)", "") + " " + sourceParameter;
        }

        return finalMethodName + "(" + finalParameters + ")";
    }

    private String translateLiterals(String source) {
        //Final set of translations to get rid of the imports and supers and self and nil.
        return source.replaceAll("#import", "#include")
                .replaceAll("super", "base")
                .replaceAll("self", "this")
                .replaceAll("nil", "NULL");
    }
{'fraction_non_alphanumeric': 0.09751361846718085, 'fraction_numerical': 0.002750660697912734, 'mean_word_length': 3.203581954205396, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 13, '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': '964701', 'n_tokens_mistral': 4971, 'n_tokens_neox': 4797, 'n_words': 1104}