summaryrefslogtreecommitdiff
path: root/symbol_analyzer/parser.py
blob: 551857e0f62a967d442fbc53bb896fb43db81184 (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
57
58
59
60
61
62
63
64
65
def parse_definition_tokens(tokens, symbols):
    typedeffed = False
    simple_typedefs = []
    structs = []
    enums = []
    functions = []

    i = 0
    while i < len(tokens):
        # skip comments
        if tokens[i] == "//":
            while tokens[i] != "\n":
                i += 1
            i += 1
            continue
        if tokens[i] == "typedef":
            if tokens[i + 1] in symbols:
                simple_typedefs.append(tokens[i + 2])
                i += 2
        if tokens[i] == "struct":
            i += 1
            defs = []
            depth = 0
            while True:
                if tokens[i] == "//":
                    while tokens[i] != "\n":
                        i += 1
                    i += 1
                    continue
                if tokens[i] == ";":
                    if depth == 0:
                        break
                if tokens[i] == "{":
                    depth += 1
                if tokens[i] == "}":
                    depth -= 1
                defs.append(tokens[i])
                i += 1
            structs.append(defs)
        if tokens[i] == "enum":
            i += 1
            defs = []
            depth = 0
            while True:
                if tokens[i] == "//":
                    while tokens[i] != "\n":
                        i += 1
                    i += 1
                    continue
                if tokens[i] == ";":
                    if depth == 0:
                        break
                if tokens[i] == "{":
                    depth += 1
                if tokens[i] == "}":
                    depth -= 1
                defs.append(tokens[i])
                i += 1
            enums.append(defs)
        i += 1

        if tokens[i] == "(":
            functions.append(tokens[i - 1])

    return structs, enums, functions, simple_typedefs