###################################################################### # (C) 2019 Hirohisa Aman # # copyright_designation_eraser.py # version 1.02 # # A simple copyright designation filter for a Java source file. # This script reads a Java source file from the starndard input, # and prints the contents other than the copyright designation. # # [useage] # python copyright_designation_eraser.py < input.java > output.java # ###################################################################### import sys import re class CopyrightDesignation: def __init__( self ): self.copyright_pattern = r'(\(c\)|copyright|licensed)' self.found = False def is_copyright_designation( self, comment ): if self.found: return False comment = comment.lower() head = re.search(r'[a-zA-Z\(].*\n', comment) if head: match = re.search(self.copyright_pattern, head.group()) if match: self.found = True return True return False # status # 0: initial # 1: entering a comment? (after '/') # 10: line comment (//) # 11: traditional comment (/*) # 12: exiting from a traditional comment? (after '*' in a traditional comment) # 2: string literal (after '"') # 20: escape character in a string literal (after '\') # 3: character literal (after "'") # 30: escape character in a character literal (after '\') checker = CopyrightDesignation() status = 0 comment = '' for line in sys.stdin: for ch in line: if status == 0: if ch == '/': status = 1 elif ch == '"': status = 2 print(ch, end='') elif ch == '\'': status = 3 print(ch, end='') else: print(ch, end='') elif status == 1: if ch == '/': status = 10 comment = '' elif ch == '*': status = 11 comment = '' else: status = 0 print('/', end='') print(ch, end='') elif status == 10: if ch == '\n': status = 0 if ( not checker.is_copyright_designation(comment) ): print('//', end='') print(comment) else: comment = comment + ch elif status == 11: if ch == '*': status = 12 else: comment = comment + ch elif status == 12: if ch == '/': status = 0 if ( not checker.is_copyright_designation(comment) ): print('/*', end='') print(comment, end='') print('*/', end='') elif ch != '*': status = 11 comment = comment + '*' comment = comment + ch else: comment = comment + '*' elif status == 2: if ch == '"': status = 0 print(ch, end='') elif ch == '\\': status = 20 print(ch, end='') else: print(ch, end='') elif status == 20: status = 2 print(ch, end='') elif status == 3: if ch == '\'': status = 0 print(ch, end='') elif ch == '\\': status = 30 print(ch, end='') else: print(ch, end='') elif status == 30: status = 3 print(ch, end='')