def split_coq_file_contents(contents): """Splits the contents of a coq file into multiple statements. This is done by finding one or three periods followed by whitespace. This is a dumb algorithm, but it seems to be (nearly) the one that ProofGeneral and CoqIDE use. We additionally merge lines inside of quotations.""" return list(merge_quotations(re.split('(?<=[^\.]\.\.\.)\s|(?<=[^\.]\.)\s', strip_comments(contents))))
def move_from_proof(filename, **kwargs): if kwargs['verbose']: kwargs['log']('Processing %s...' % filename) try: with open(filename, 'r') as f: contents = f.read() except IOError as e: if kwargs['verbose']: kwargs['log']('Failed to process %s' % filename) if kwargs['verbose'] >= 2: kwargs['log'](repr(e)) return ret = [] cur_statements = [] cur_statement = [] deferred_copied_statements = [] deferred_statements = [] orig_space_count = 0 cur_diff_space_count = 0 if ''.join(split_coq_file_contents_with_comments(contents)) != contents: kwargs['log']('WARNING: Could not split %s' % filename) return for i in split_coq_file_contents_with_comments(contents): is_definition_full = (ALL_DEFINITIONS_REG.match(i) is not None and (':=' in strip_parens(strip_comments(i)) or ONELINE_DEFINITIONS_REG.match(i))) is_definition_start = (ALL_DEFINITIONS_REG.match(i) is not None and ':=' not in strip_parens(strip_comments(i)) and not ONELINE_DEFINITIONS_REG.match(i)) #print((is_definition_start, ONELINE_DEFINITIONS_REG.match(i), ALL_DEFINITIONS_REG.match(i), i)) is_definition_end = ALL_ENDINGS.match(i) is not None #if 'not_reachable_iff' in i: # print((ALL_DEFINITIONS_REG.match(i), strip_parens(strip_comments(i)), ONELINE_DEFINITIONS_REG.match(i))) if not is_definition_start and not cur_statements and not cur_statement: if kwargs['verbose'] >= 3: kwargs['log'](repr(i)) ret.append(i) elif is_definition_start: if kwargs['verbose'] >= 2: kwargs['log']('Starting definition (%d): %s' % (len(deferred_statements), repr(i))) if not cur_statement and not deferred_statements: orig_space_count = len(get_leading_space(i)) if cur_statement: deferred_statements.append( (cur_diff_space_count, cur_statement)) cur_diff_space_count = max( 0, len(get_leading_space(i)) - orig_space_count) cur_statement = [remove_leading_space(i, cur_diff_space_count)] elif (SAFE_REG.match(i) or not i.strip()) and cur_statement: if kwargs['verbose'] >= 3: kwargs['log'](repr(i)) cur_statement.append(remove_leading_space(i, cur_diff_space_count)) elif is_definition_end and cur_statement: if kwargs['verbose'] >= 2: kwargs['log']('Ending definition: ' + repr(i)) cur_statement.append(remove_leading_space(i, cur_diff_space_count)) cur_statements.append(''.join(cur_statement)) #print(''.join(preminimize_lifted_statements(cur_statements))) if deferred_statements: cur_diff_space_count, cur_statement = deferred_statements.pop() deferred_copied_statements = list( preminimize_lifted_statements(deferred_copied_statements)) cur_statement.extend( deferred_copied_statements) # todo: fix indentation else: #print('extending') #print(''.join(minimize_lifted_statements(cur_statements))) ret.extend(list(minimize_lifted_statements(cur_statements))) cur_statement = [] cur_statements = [] deferred_copied_statements = [] elif MOVE_UP_REG.match(i) or is_definition_full: if kwargs['verbose'] >= 2: kwargs['log']('Lifting: ' + repr(i)) cur_statements.append(set_leading_space(i, orig_space_count)) elif COPY_UP_REG.match(i) and cur_statement: if kwargs['verbose'] >= 2: kwargs['log']('Lift-copying: ' + repr(i)) cur_statement.append(remove_leading_space(i, cur_diff_space_count)) cur_statements.append(set_leading_space(i, orig_space_count)) deferred_copied_statements.append( remove_leading_space(i, cur_diff_space_count)) else: raw_input('WARNING: Unrecognized: %s' % repr(i)) #print(cur_diff_space_count) #print(remove_leading_space(i, cur_diff_space_count)) if cur_statements or deferred_statements or cur_statement: raw_input('WARNING: extra statements: %s' % repr( (cur_statements, cur_statement, deferred_statements))) cur_statements.append(''.join(cur_statement)) for i in deferred_statements: cur_statements.append(''.join(i)) ret.extend(list(minimize_lifted_statements(cur_statements))) ret = ''.join(ret) if ret == contents: return if kwargs['inplace']: do_backup = kwargs['suffix'] is not None and len(kwargs['suffix']) > 0 write_to_file(filename, ret, do_backup=do_backup, backup_ext=kwargs['suffix']) else: print(ret)
if kwargs['verbose']: kwargs['log']('Failed to process %s' % filename) if kwargs['verbose'] >= 2: kwargs['log'](repr(e)) return ret = [] cur_statements = [] cur_statement = [] deferred_copied_statements = [] deferred_statements = [] orig_space_count = 0 cur_diff_space_count = 0 if ''.join(split_coq_file_contents_with_comments(contents)) != contents: kwargs['log']('WARNING: Could not split %s' % filename) return for i in split_coq_file_contents_with_comments(contents): is_definition_full = (ALL_DEFINITIONS_REG.match(i) is not None and (':=' in strip_parens(strip_comments(i)) or ONELINE_DEFINITIONS_REG.match(i))) is_definition_start = (ALL_DEFINITIONS_REG.match(i) is not None and ':=' not in strip_parens(strip_comments(i)) and not ONELINE_DEFINITIONS_REG.match(i)) #print((is_definition_start, ONELINE_DEFINITIONS_REG.match(i), ALL_DEFINITIONS_REG.match(i), i)) is_definition_end = ALL_ENDINGS.match(i) is not None #if 'not_reachable_iff' in i: # print((ALL_DEFINITIONS_REG.match(i), strip_parens(strip_comments(i)), ONELINE_DEFINITIONS_REG.match(i))) if not is_definition_start and not cur_statements and not cur_statement: if kwargs['verbose'] >= 3: kwargs['log'](repr(i)) ret.append(i) elif is_definition_start: if kwargs['verbose'] >= 2: kwargs['log']('Starting definition (%d): %s' % (len(deferred_statements), repr(i))) if not cur_statement and not deferred_statements: orig_space_count = len(get_leading_space(i))
if kwargs['verbose']: kwargs['log']('Failed to process %s' % filename) if kwargs['verbose'] >= 2: kwargs['log'](repr(e)) return ret = [] cur_statements = [] cur_statement = [] deferred_copied_statements = [] deferred_statements = [] orig_space_count = 0 cur_diff_space_count = 0 if ''.join(split_coq_file_contents_with_comments(contents)) != contents: kwargs['log']('WARNING: Could not split %s' % filename) return for i in split_coq_file_contents_with_comments(contents): is_definition_full = (ALL_DEFINITIONS_REG.match(i) is not None and (':=' in strip_parens(strip_comments(i)) or ONELINE_DEFINITIONS_REG.match(i))) is_definition_start = (ALL_DEFINITIONS_REG.match(i) is not None and ':=' not in strip_parens(strip_comments(i)) and not ONELINE_DEFINITIONS_REG.match(i)) #print((is_definition_start, ONELINE_DEFINITIONS_REG.match(i), ALL_DEFINITIONS_REG.match(i), i)) is_definition_end = ALL_ENDINGS.match(i) is not None #if 'not_reachable_iff' in i: # print((ALL_DEFINITIONS_REG.match(i), strip_parens(strip_comments(i)), ONELINE_DEFINITIONS_REG.match(i))) if not is_definition_start and not cur_statements and not cur_statement: if kwargs['verbose'] >= 3: kwargs['log'](repr(i)) ret.append(i) elif is_definition_start: if kwargs['verbose'] >= 2: kwargs['log']('Starting definition (%d): %s' % (len(deferred_statements), repr(i)))