class CommentStringParser():
    def __init__(self,verbose=True):
        self.verbose = verbose
        self.special_cases = [['"', '"', 'strings'],
                              ["'", "'", 'strings'],
                              ["//", "\n", 'comments'],
                              ["/*", "*/", 'comments']]

    def _find_next(self,codigo, i, c):
        n = len(c)
        while i < len(codigo) and codigo[i:i + n] != c:
            i += 1
        return i + n

    def _clean_string(self,s):
        return s.replace('function','').replace('contract','').replace('{','').replace('}','')

    def _clean_code(self,codigo):
        n = len(codigo)
        i = 0
        codigo_final = ""
        while i < n - 1:
            addone = True
            for cin, cout, tp in self.special_cases:
                if codigo[i:i + len(cin)] == cin:
                    addone = False
                    desde = i
                    i += len(cin)
                    hasta = self._find_next(codigo, i, cout)
                    i = hasta
                    codigo_final += self._clean_string(codigo[desde:hasta])
            if addone:
                codigo_final += codigo[i]
                i += 1

        if self.verbose and len(codigo_final) != codigo:
            print("Las palabras, function y contract, y los caracteres {} estan reservados. Se borraran de comentarios y strings")
        return codigo_final

    def extract_keywords(self,codigo):
        return self._clean_code(codigo)