summaryrefslogtreecommitdiff
path: root/symbol_analyzer/parser.py
blob: 977a6340ac2caf79878463471b020612581fadd4 (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
66
67
68
69
70
71
72
73
74
75
76
def parse_definition_tokens(tokens, symbols):
    typedeffed = False
    simple_typedefs = []
    structs = []
    enums = []
    functions = []
    imports = []
    usage = []

    i = 0
    while i < len(tokens):
        # skip comments
        if tokens[i] == "//":
            while tokens[i] != "\n" and i < len(tokens):
                i += 1
            i += 1
            continue
        elif tokens[i] == "#include":
            imports.append(tokens[i + 1])
            i += 2
        elif tokens[i] == "typedef":
            if tokens[i + 1] in symbols:
                simple_typedefs.append(tokens[i + 2])
                i += 2
        elif tokens[i] == "struct":
            i += 1
            defs = []
            depth = 0
            while i < len(tokens):
                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)
        elif 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)
        elif tokens[i] == "(":
            functions.append(tokens[i - 1])
            while tokens[i] != ")":
                i += 1
            i += 1
        else:
            usage.append(tokens[i])

        i += 1

    print(structs, enums, functions, simple_typedefs, imports)
    return structs, enums, functions, simple_typedefs, imports, usage