Subversion Repositories Kolibri OS

Rev

Rev 9367 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. # Copyright Magomed Kostoev
  2. # Published under MIT license
  3.  
  4. block_beginner2finisher = {
  5.     "(": ")",
  6.     "{": "}",
  7.     "\"": "\"",
  8. }
  9.  
  10. def get_to(src, ptr, c, backwards = False):
  11.     while src[ptr] != c:
  12.         if not backwards:
  13.             ptr += 1
  14.         else:
  15.             ptr -= 1
  16.     return ptr
  17.  
  18. # Assuming we are at beginning of some block (string,
  19. # parenthesed expression, {}-like thing), get to end
  20. # of the block (also handles the blocks openned in the
  21. # way to the block ending character. So it's not just
  22. # stupid searching for block closing character.
  23. def get_to_block_finisher(src, ptr):
  24.     assert(src[ptr] in block_beginner2finisher)
  25.  
  26.     block_beginner = src[ptr]
  27.     ptr += 1
  28.     while src[ptr] != block_beginner2finisher[block_beginner]:
  29.         # If any block starts here - get to its end and then continue
  30.         if src[ptr] in block_beginner2finisher:
  31.             ptr = get_to_block_finisher(src, ptr)
  32.         ptr += 1
  33.     return ptr
  34.  
  35. def get_strnig(src, ptr):
  36.     # Strings starts with "\""
  37.     assert(src[ptr] == "\"")
  38.  
  39.     result = ""
  40.     # Skip first "\"" of the string
  41.     ptr += 1
  42.     while src[ptr] != "\"":
  43.         result += src[ptr]
  44.         ptr += 1
  45.     return result
  46.  
  47. def parse_rule_output(src, ptr):
  48.     # Get straight to the first argument
  49.     ptr += len("tup.rule")
  50.     # Get to parenthese
  51.     ptr = get_to(src, ptr, "(")
  52.     # Get to the closing parenthese
  53.     ptr = get_to_block_finisher(src, ptr)
  54.     # We are at closing parenthese of argument list
  55.     # And the last argument is always output file
  56.     # Let's get to closing "\"" of the output file name
  57.     ptr = get_to(src, ptr, "\"", backwards = True)
  58.     # Get into the string
  59.     ptr -= 1
  60.     # Then get to the beginning of the string
  61.     ptr = get_to(src, ptr, "\"", backwards = True)
  62.     # Now we can read the string
  63.     return get_strnig(src, ptr)
  64.  
  65. def parse_tupfile_outputs(file_name):
  66.     outputs = []
  67.     with open(file_name) as f:
  68.         tupfile = f.read()
  69.     rule_begin_index = tupfile.find("tup.rule(")
  70.     while (rule_begin_index != -1):
  71.         outputs.append(parse_rule_output(tupfile, rule_begin_index))
  72.         # Find the next tup.rule call
  73.         rule_begin_index = tupfile.find("tup.rule(", rule_begin_index + len("tup.rule("))
  74.     return outputs
  75.