summaryrefslogtreecommitdiff
path: root/symbol_analyzer/extractor.py
blob: 0e8694c2df516bfe4efbe8c342e00e3c904bf391 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def parse_file(file):
    f = open(file)
    contents = f.read()
    f.close()

    significant_token = ""
    depth = 0
    tokens = []
    swallen_tokens = [0, 0]
    token_buffer = ""
    define_striping = False
    for c in contents:
        if c == " " or c == "{" or c == "}" or \
            c == ";" or c == ":" or c == "[" or c == "]" or \
            c == "(" or c == ")" or c == "," or c == "\n" or c == "*":

            if significant_token == "":
                if c == "{" or c == "}":
                    depth += 1 if c == "{" else -1
            elif significant_token == "#define":
                if tokens[-1] == ")":
                    define_striping = True
                    depth = 1
                if swallen_tokens[-2] != "\\" and swallen_tokens[-1] == "\n":
                    significant_token = ""
                    define_striping = False
                    depth = 0
            else:
                depth = 0
                if swallen_tokens[-1] == ";":
                    significant_token = ""

            if depth == 0:
                if len(token_buffer) > 0:
                    if token_buffer in ["#define", "enum", "struct"]:
                        significant_token = token_buffer
                    tokens.append(token_buffer)
                if c != ' ' and len(c) > 0:
                    tokens.append(c)
            else:
                if len(token_buffer) > 0:
                    if token_buffer in ["#define", "enum", "struct"]:
                        significant_token = token_buffer
                    swallen_tokens.append(token_buffer)
                if c != ' ' and len(c) > 0:
                    swallen_tokens.append(c)

            token_buffer = ""
            continue

        token_buffer += c

    if depth == 0 and len(token_buffer) > 0:
        tokens.append(token_buffer)

    return tokens, swallen_tokens