Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- def _find_next_words(dataset, count):
- idx = 0
- for i in range(count):
- idx = dataset.find(' ', idx) + 1
- return dataset[:idx].split()
- def _find_fully_closed_bracket_idx(dataset):
- if '(' != dataset[0]:
- return 0
- closed_bracket_idx = 0
- bracket_count = 0
- for c in dataset:
- if '(' == c:
- bracket_count += 1
- elif ')' == c:
- bracket_count -= 1
- if 0 == bracket_count:
- break
- closed_bracket_idx += 1
- return closed_bracket_idx
- def _package(dataset):
- identifiers = _find_next_words(dataset, 4)
- instantiation = 'new' == identifiers[3]
- rename = 'renames' == identifiers[2]
- statement = ''
- end_idx = 0
- if instantiation:
- open_bracket_idx = dataset.find('(')
- end_bracket_idx = _find_fully_closed_bracket_idx(dataset[open_bracket_idx:]) + open_bracket_idx
- end_idx = end_bracket_idx + dataset[end_bracket_idx:].find(';')
- statement = dataset[:end_idx]
- elif rename:
- end_idx = dataset.find(';')
- statement = dataset[:end_idx]
- else:
- end_idx = dataset.find(' is ') + len(' is') - 1 #remove one as default op is to remove trailing ';', but there is no semicolon here
- statement = dataset[:end_idx + 1]
- return (end_idx, statement)
- def _type(dataset):
- record_idx = dataset.find(' record ')
- eos_idx = dataset.find(';')
- end_idx = 0
- statement = ''
- #not a record type
- if -1 == record_idx or eos_idx < record_idx:
- end_idx = dataset.find(';')
- statement = dataset[:end_idx]
- else:
- end_record_idx = dataset.find('end record;')
- end_idx = end_record_idx + len('end record;') - 1
- statement = dataset[:end_idx]
- return (end_idx, statement)
- def _function(dataset):
- return_idx = dataset.find(' return ')
- end_idx = return_idx + dataset[return_idx:].find(';')
- statement = dataset[:end_idx]
- return (end_idx, statement)
- def _procedure(dataset):
- bracket_idx = dataset.find('(')
- eos_idx = dataset.find(';')
- end_idx = 0
- statement = ''
- #procedure without arguments
- if -1 == bracket_idx or eos_idx < bracket_idx:
- end_idx = eos_idx
- statement = dataset[:end_idx]
- else:
- open_bracket_idx = dataset.find('(')
- end_bracket_idx = _find_fully_closed_bracket_idx(dataset[open_bracket_idx:]) + open_bracket_idx
- end_idx = end_bracket_idx + dataset[end_bracket_idx:].find(';')
- statement = dataset[:end_idx]
- return (end_idx, statement)
- def _private(dataset):
- end_idx = dataset.find(' ') - 1
- statement = 'private'
- return (end_idx, statement)
- HANDLE_WITH_CARE = {
- 'package' : _package,
- 'type' : _type,
- 'function' : _function,
- 'procedure' : _procedure,
- 'private' : _private }
- EOL_INDEX_OFFSET = 2
- def _normalise(content):
- statements = []
- idx = 0
- while(idx < len(content)):
- dataset = content[idx:]
- instruction = dataset[:dataset.find(' ')]
- statement = ''
- end_idx = 0
- if instruction in HANDLE_WITH_CARE:
- end_idx, statement = HANDLE_WITH_CARE[instruction](dataset)
- else:
- end_idx = dataset.find(';')
- statement = dataset[:end_idx]
- statements.append(statement)
- idx += end_idx + EOL_INDEX_OFFSET
- return statements
- def _get_content(file):
- content = ''
- with open(file, 'r') as f:
- for line in f:
- tmp = line.strip()
- if tmp == '' or tmp.startswith('--'):
- continue
- if '--' in tmp:
- tmp = tmp[:tmp.find('--')].strip(' ')
- tmp = ' '.join(tmp.split()) + ' '
- content += tmp
- return content
- def get_statements(file):
- return _normalise(_get_content(file))
- if __name__=='__main__':
- s = get_statements(sys.argv[1])
- print('\n'.join(s))
Advertisement
Add Comment
Please, Sign In to add comment