Rev 9367 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
9367 | Boppan | 1 | # Copyright Magomed Kostoev |
2 | # Published under MIT license |
||
3 | |||
9377 | Boppan | 4 | block_beginner2finisher = { |
5 | "(": ")", |
||
6 | "{": "}", |
||
7 | "\"": "\"", |
||
8 | } |
||
9367 | Boppan | 9 | |
9377 | Boppan | 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 |
||
9367 | Boppan | 17 | |
9377 | Boppan | 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) |
||
9367 | Boppan | 32 | ptr += 1 |
33 | return ptr |
||
34 | |||
9377 | Boppan | 35 | def get_strnig(src, ptr): |
36 | # Strings starts with "\"" |
||
37 | assert(src[ptr] == "\"") |
||
9367 | Boppan | 38 | |
9377 | Boppan | 39 | result = "" |
40 | # Skip first "\"" of the string |
||
9367 | Boppan | 41 | ptr += 1 |
42 | while src[ptr] != "\"": |
||
9377 | Boppan | 43 | result += src[ptr] |
9367 | Boppan | 44 | ptr += 1 |
45 | return result |
||
46 | |||
9377 | Boppan | 47 | def parse_rule_output(src, ptr): |
9367 | Boppan | 48 | # Get straight to the first argument |
9377 | Boppan | 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) |
||
9367 | Boppan | 64 | |
9377 | Boppan | 65 | def parse_tupfile_outputs(file_name): |
66 | outputs = [] |
||
9367 | Boppan | 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): |
||
9377 | Boppan | 71 | outputs.append(parse_rule_output(tupfile, rule_begin_index)) |
9367 | Boppan | 72 | # Find the next tup.rule call |
73 | rule_begin_index = tupfile.find("tup.rule(", rule_begin_index + len("tup.rule(")) |
||
9377 | Boppan | 74 | return outputs |