atesdal

bad_normaliser

Aug 6th, 2025
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.01 KB | None | 0 0
  1. import sys
  2.  
  3. def _find_next_words(dataset, count):
  4.     idx = 0
  5.     for i in range(count):
  6.         idx = dataset.find(' ', idx) + 1
  7.     return dataset[:idx].split()
  8.  
  9. def _find_fully_closed_bracket_idx(dataset):
  10.     if '(' != dataset[0]:
  11.         return 0
  12.    
  13.     closed_bracket_idx = 0
  14.     bracket_count = 0
  15.     for c in dataset:
  16.         if '(' == c:
  17.             bracket_count += 1
  18.         elif ')' == c:
  19.             bracket_count -= 1
  20.        
  21.         if 0 == bracket_count:
  22.             break
  23.  
  24.         closed_bracket_idx += 1
  25.  
  26.     return closed_bracket_idx
  27.  
  28.  
  29. def _package(dataset):
  30.     identifiers = _find_next_words(dataset, 4)
  31.     instantiation = 'new' == identifiers[3]
  32.     rename = 'renames' == identifiers[2]
  33.  
  34.     statement = ''
  35.     end_idx = 0
  36.  
  37.     if instantiation:
  38.         open_bracket_idx = dataset.find('(')
  39.         end_bracket_idx = _find_fully_closed_bracket_idx(dataset[open_bracket_idx:]) + open_bracket_idx
  40.         end_idx = end_bracket_idx + dataset[end_bracket_idx:].find(';')
  41.         statement = dataset[:end_idx]
  42.     elif rename:
  43.         end_idx = dataset.find(';')
  44.         statement = dataset[:end_idx]
  45.     else:
  46.         end_idx = dataset.find(' is ') + len(' is') - 1 #remove one as default op is to remove trailing ';', but there is no semicolon here
  47.         statement = dataset[:end_idx + 1]
  48.    
  49.     return (end_idx, statement)
  50.  
  51. def _type(dataset):
  52.     record_idx = dataset.find(' record ')
  53.     eos_idx = dataset.find(';')
  54.  
  55.     end_idx = 0
  56.     statement = ''
  57.  
  58.     #not a record type
  59.     if -1 == record_idx or eos_idx < record_idx:
  60.         end_idx = dataset.find(';')
  61.         statement = dataset[:end_idx]
  62.     else:
  63.         end_record_idx = dataset.find('end record;')
  64.         end_idx = end_record_idx + len('end record;') - 1
  65.         statement = dataset[:end_idx]
  66.    
  67.     return (end_idx, statement)
  68.  
  69. def _function(dataset):
  70.     return_idx = dataset.find(' return ')
  71.     end_idx = return_idx + dataset[return_idx:].find(';')
  72.     statement = dataset[:end_idx]
  73.     return (end_idx, statement)
  74.  
  75. def _procedure(dataset):
  76.     bracket_idx = dataset.find('(')
  77.     eos_idx = dataset.find(';')
  78.     end_idx = 0
  79.     statement = ''
  80.  
  81.     #procedure without arguments
  82.     if -1 == bracket_idx or eos_idx < bracket_idx:
  83.         end_idx = eos_idx
  84.         statement = dataset[:end_idx]
  85.     else:
  86.         open_bracket_idx = dataset.find('(')
  87.         end_bracket_idx = _find_fully_closed_bracket_idx(dataset[open_bracket_idx:]) + open_bracket_idx
  88.         end_idx = end_bracket_idx + dataset[end_bracket_idx:].find(';')
  89.         statement = dataset[:end_idx]
  90.        
  91.     return (end_idx, statement)
  92.  
  93. def _private(dataset):
  94.     end_idx = dataset.find(' ') - 1
  95.     statement = 'private'
  96.     return (end_idx, statement)
  97.  
  98. HANDLE_WITH_CARE = {
  99.     'package' : _package,
  100.     'type' : _type,
  101.     'function' : _function,
  102.     'procedure' : _procedure,
  103.     'private' : _private }
  104.  
  105. EOL_INDEX_OFFSET = 2
  106.  
  107. def _normalise(content):
  108.     statements = []
  109.     idx = 0
  110.     while(idx < len(content)):
  111.         dataset = content[idx:]
  112.         instruction = dataset[:dataset.find(' ')]
  113.         statement = ''
  114.         end_idx = 0
  115.  
  116.         if instruction in HANDLE_WITH_CARE:
  117.             end_idx, statement = HANDLE_WITH_CARE[instruction](dataset)
  118.         else:
  119.             end_idx = dataset.find(';')
  120.             statement = dataset[:end_idx]
  121.  
  122.         statements.append(statement)
  123.         idx += end_idx + EOL_INDEX_OFFSET
  124.  
  125.     return statements
  126.  
  127. def _get_content(file):
  128.     content = ''
  129.     with open(file, 'r') as f:
  130.         for line in f:
  131.             tmp = line.strip()
  132.             if tmp == '' or tmp.startswith('--'):
  133.                 continue
  134.             if '--' in tmp:
  135.                 tmp = tmp[:tmp.find('--')].strip(' ')
  136.  
  137.             tmp = ' '.join(tmp.split()) + ' '
  138.             content += tmp
  139.     return content
  140.  
  141. def get_statements(file):
  142.     return _normalise(_get_content(file))
  143.  
  144. if __name__=='__main__':
  145.     s = get_statements(sys.argv[1])
  146.     print('\n'.join(s))
  147.  
Advertisement
Add Comment
Please, Sign In to add comment