<reponame>sloboegen98/BasePlag<gh_stars>0
from antlr4.FileStream import FileStream
from antlr4.CommonTokenStream import CommonTokenStream

import java.JavaLexer as jl
import java.JavaParser as jp

from unuseful_cleaner import UnusefulCleaner
from useful_finder import UsefulFinder


def detect_language(filename):
    if '.java' in filename:
        return 'java'
    elif '.cpp' in filename or '.cc' in filename:
        return 'cpp'
    else:
        raise RuntimeError(
            f'Probably the language of {filename} is not supported')


class CommonParser:
    def __init__(self, filename):
        self.filename = filename
        self.language = detect_language(filename)

    def parse(self):
        if self.language == 'java':
            return self._parse_java()
        elif self.language == 'cpp':
            return self._parse_cpp()

    def _parse_java(self):
        input = FileStream(self.filename, encoding='utf-8')
        lexer = jl.JavaLexer(input)
        stream = CommonTokenStream(lexer)
        parser = jp.JavaParser(stream)
        return parser.compilationUnit()

    def _parse_cpp(self):
        raise NotImplementedError()


class TemplateCodeRemover:
    @staticmethod
    def clean_file(cur_path, new_path, quiet=True, log_msg=''):
        if not quiet:
            print(log_msg)

        tcr = TemplateCodeRemover(cur_path)
        cleaned_text = tcr._clean_file()

        with open(new_path, 'w') as new_file:
            new_file.write(cleaned_text)

    def __init__(self, filename):
        self.start_rule = CommonParser(filename).parse()
        self.language = detect_language(filename)

    def _clean_file(self):
        useful_finder = UsefulFinder(self.start_rule, self.language)
        useful_elements = useful_finder.find_useful_elements()
        unuseful_cleaner = UnusefulCleaner(
            self.start_rule, useful_elements, self.language)
        return unuseful_cleaner.clean_file()

from antlr4.FileStream import FileStream
from antlr4.CommonTokenStream import CommonTokenStream

import java.JavaLexer as jl
import java.JavaParser as jp

from unuseful_cleaner import UnusefulCleaner
from useful_finder import UsefulFinder


def detect_language(filename):
    if '.java' in filename:
        return 'java'
    elif '.cpp' in filename or '.cc' in filename:
        return 'cpp'
    else:
        raise RuntimeError(
            f'Probably the language of {filename} is not supported')


class CommonParser:
    def __init__(self, filename):
        self.filename = filename
        self.language = detect_language(filename)

    def parse(self):
        if self.language == 'java':
            return self._parse_java()
        elif self.language == 'cpp':
            return self._parse_cpp()

    def _parse_java(self):
        input = FileStream(self.filename, encoding='utf-8')
        lexer = jl.JavaLexer(input)
        stream = CommonTokenStream(lexer)
        parser = jp.JavaParser(stream)
        return parser.compilationUnit()

    def _parse_cpp(self):
        raise NotImplementedError()


class TemplateCodeRemover:
    @staticmethod
    def clean_file(cur_path, new_path, quiet=True, log_msg=''):
        if not quiet:
            print(log_msg)

        tcr = TemplateCodeRemover(cur_path)
        cleaned_text = tcr._clean_file()

        with open(new_path, 'w') as new_file:
            new_file.write(cleaned_text)

    def __init__(self, filename):
        self.start_rule = CommonParser(filename).parse()
        self.language = detect_language(filename)

    def _clean_file(self):
        useful_finder = UsefulFinder(self.start_rule, self.language)
        useful_elements = useful_finder.find_useful_elements()
        unuseful_cleaner = UnusefulCleaner(
            self.start_rule, useful_elements, self.language)
        return unuseful_cleaner.clean_file()