-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcgu.py
928 lines (820 loc) · 29.4 KB
/
cgu.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
# cgu - code gen utilities for parsing c-like languages for use in code generation tools
# copyright Alex Dixon 2020: https://github.com/polymonster/cgu/blob/master/license
import re
import json
import sys
# make code gen more readable and less fiddly
def in_quotes(string):
return "\"" + string + "\""
# append to string with newline print() style
def src_line(line):
line += "\n"
return line
# like c style unsigned wraparound
def us(val):
if val < 0:
val = sys.maxsize + val
return val
# remove all single and multi line comments
def remove_comments(source):
lines = source.split("\n")
inside_block = False
conditioned = ""
for line in lines:
if inside_block:
ecpos = line.find("*/")
if ecpos != -1:
inside_block = False
line = line[ecpos+2:]
else:
continue
cpos = line.find("//")
mcpos = line.find("/*")
if cpos != -1:
conditioned += line[:cpos] + "\n"
elif mcpos != -1:
conditioned += line[:mcpos] + "\n"
inside_block = True
else:
conditioned += line + "\n"
return conditioned
# generates a nice UI friendly name from, snake_case, camelCase or SCAREY_CASE and strip known prefixes
def display_name(token, title):
prefix = ["m_", "s_", "k_", "g_"]
for p in prefix:
if token.startswith(p):
token = token[len(p):]
break
spaced = ""
for i in range(len(token)):
if i > 0:
if token[i-1].islower() and token[i].isupper():
spaced += " "
spaced += token[i]
spaced = spaced.replace("_", " ")
if title:
spaced = spaced.title()
else:
spaced = spaced.capitalize()
return spaced.strip()
# separate alpha and numeric characters ie. TEXCOORD0 becomes (TEXCOORD, 0)
def separate_alpha_numeric(src):
name = re.sub(r'[0-9]', '', src)
index = re.sub(r'[^0-9]','', src)
if len(index) == 0:
index = 0
index = int(index)
return (name, index)
# finds the end of a body of text enclosed between 2 symbols ie. [], {}, <>
def enclose(open_symbol, close_symbol, source, pos):
pos = source.find(open_symbol, pos)
stack = [open_symbol]
pos += 1
while len(stack) > 0 and pos < len(source):
if source[pos] == open_symbol:
stack.append(open_symbol)
if source[pos] == close_symbol:
stack.pop()
pos += 1
return pos
# returns the interior range of the string contained within open_symbol and close_symbol
def enclose_start_end(open_symbol, close_symbol, source, pos):
pos = source.find(open_symbol, pos)
if pos == -1:
return (-1, -1)
start_pos = pos+1
stack = [open_symbol]
pos += 1
while len(stack) > 0 and pos < len(source):
if source[pos] == open_symbol:
stack.append(open_symbol)
if source[pos] == close_symbol:
stack.pop()
pos += 1
return (start_pos, pos-1)
# parse a string and return the end position in source, taking into account escaped \" quotes
def enclose_string(start, source):
pos = start+1
while True:
pos = source.find("\"", pos)
prev = pos - 1
if prev > 0:
if source[prev] == "\\":
pos = pos + 1
continue
return pos+1
# un-terminated string
print("ERROR: unterminated string")
assert 0
# format source with indents
def format_source(source, indent_size):
formatted = ""
lines = source.splitlines()
indent = 0
indents = ["{"]
unindnets = ["}"]
newline = False
for line in lines:
if newline and len(line) > 0 and line[0] != "}":
formatted += "\n"
newline = False
cur_indent = indent
line = line.strip()
attr = line.find("[[")
if len(line) < 1 or attr != -1:
continue
for c in line:
if c in indents:
indent += 1
elif c in unindnets:
indent -= 1
cur_indent = indent
newline = True
formatted += " " * cur_indent * indent_size
formatted += line
formatted += "\n"
return formatted
# returns the name of a type.. ie struct <name>, enum <name>
def type_name(type_declaration):
# skip past templates (allow multiple template parameters)
if type_declaration.find("<") != -1:
template_end = enclose("<", ">", type_declaration, 0)
return type_declaration[template_end:].strip().split()[0].strip(":")
else:
# assume the type name is the second token
pos = type_declaration.find("{")
name = type_declaration[:pos].strip().split()[1].strip(":")
return name
# get the typename stripping resource_array[21] arrays and return (resource_array, 21)
def type_name_array(type_declaration):
pos = type_declaration.find("[")
if pos != -1:
start, end = enclose_start_end("[", "]", type_declaration, 0)
array_size_str = type_declaration[start:end]
if len(array_size_str) == 0:
array_size = -1
else:
array_size = int(array_size_str)
return (type_declaration[:pos], array_size)
return (type_declaration, None)
# tidy source with consistent spaces, remove tabs and comments to make subsequent operations easier
def sanitize_source(source):
# replace tabs with spaces
source = source.replace("\t", " ")
# replace all spaces with single space
source = re.sub(' +', ' ', source)
# remove comments
source = remove_comments(source)
# remove empty lines and strip whitespace
sanitized = ""
for line in source.splitlines():
line = line.strip()
if len(line) > 0:
sanitized += src_line(line)
return sanitized
# finds token in source code
def find_token(token, source):
delimiters = [
"(", ")", "{", "}", ".", ",", "+", "-", "=", "*", "/",
"&", "|", "~", "\n", "\t", "<", ">", "[", "]", ";", " "
]
fp = source.find(token)
if fp != -1:
left = False
right = False
# check left
if fp > 0:
for d in delimiters:
if source[fp - 1] == d:
left = True
break
else:
left = True
# check right
ep = fp + len(token)
if fp < ep-1:
for d in delimiters:
if source[ep] == d:
right = True
break
else:
right = True
if left and right:
return fp
# try again
tt = find_token(token, source[fp + len(token):])
if tt == -1:
return -1
return fp+len(token) + tt
return -1
# replace all occurrences of token in source code
def replace_token(token, replace, source):
while True:
pos = find_token(token, source)
if pos == -1:
break
else:
source = source[:pos] + replace + source[pos + len(token):]
pass
return source
# find all occurences of token, with their location within source
def find_all_tokens(token, source):
pos = 0
locations = []
while True:
token_pos = find_token(token, source[pos:])
if token_pos != -1:
token_pos += pos
locations.append(token_pos)
pos = token_pos + len(token)
else:
break
return locations
# return true if source[pos] is a whitespace char
def is_whitespace(source, pos):
whitespace = [' ', '\n', '\t']
return source[pos] in whitespace
# finds the previous non whitespace
def find_prev_non_whitespace(source, pos):
pos -= 1
while pos >= 0 and is_whitespace(source, pos):
pos -= 1
if pos >= 0 and pos < len(source):
return source[pos]
return None
# finds the previous non whitespace
def find_next_non_whitespace(source, pos):
pos += 1
while pos < len(source) and is_whitespace(source, pos):
pos += 1
if pos >= 0 and pos < len(source):
return source[pos]
return None
# find all string literals in source
def find_string_literals(source):
pos = 0
strings = []
while True:
pos = source.find("\"", pos)
if pos == -1:
break
end = enclose_string(pos, source)
string = source[pos:end]
strings.append(string)
pos = end+1
return strings
# removes string literals and inserts a place holder, returning the ist of string literals and the conditioned source
def placeholder_string_literals(source):
strings = find_string_literals(source)
index = 0
for s in strings:
source = source.replace(s, '"<placeholder_string_literal_{}>"'.format(index))
index += 1
return strings, source
# replace placeholder literals with the strings
def replace_placeholder_string_literals(strings, source):
index = 0
for s in strings:
source = source.replace('"<placeholder_string_literal_{}>"'.format(index), s)
index += 1
return source
# get all enum member names and values
def get_enum_members(declaration):
start = declaration.find("{")+1
end = enclose("{", "}", declaration, 0)-1
body = declaration[start:end]
members = body.split(",")
conditioned = []
for member in members:
if len(member.strip()) > 0:
conditioned.append(member.strip())
enum_value = 0
enum_members = []
for member in conditioned:
if member.find("=") != -1:
name_value = member.split("=")
enum_members.append({
"name": name_value[0],
"value": name_value[1]
})
else:
enum_members.append({
"name": member,
"value": enum_value
})
enum_value += 1
return enum_members
# get all struct member names, types, defaults and other metadata
def get_struct_members(declaration):
members = []
pos = declaration.find("{")+1
while pos != -1:
semantic_pos = declaration.find(":", pos)
end_pos = declaration.find(";", pos)
if end_pos == -1:
break
skip = end_pos
if us(semantic_pos) < end_pos:
end_pos = semantic_pos
bracket_pos = declaration.find("{", pos)
start_pos = pos
if us(bracket_pos) < end_pos:
end_pos = enclose("{", "}", declaration, start_pos)
statement = declaration[start_pos:end_pos]
member_type = "variable"
if statement.find("(") != -1 and statement.find("=") == -1:
member_type = "function"
attrubutes = None
attr_start = statement.find("[[")
if attr_start != -1:
attr_end = statement.find("]]")
attrubutes = statement[attr_start+2:attr_end]
decl = statement.strip()
type_decl = breakdown_type_decl(decl)
semantic = None
if semantic_pos != -1:
semantic = declaration[semantic_pos:skip].strip().strip(";").strip(":").strip()
members.append({
"member_type": member_type,
"data_type": type_decl["type"].strip(),
"name": type_decl["name"].strip(),
"default": type_decl["default"],
"declaration": decl,
"attributes": attrubutes,
"semantic": semantic
})
pos = skip + 1
return members
def get_members(type_specifier, declaration):
lookup = {
"enum": get_enum_members,
"struct": get_struct_members,
"cbuffer": get_struct_members
}
if type_specifier in lookup:
return lookup[type_specifier](declaration)
return []
# finds the fully qualified scope for a type declaration
def get_type_declaration_scope(source, type_pos):
scope_identifier = [
"namespace",
"struct",
"class"
]
pos = 0
scopes = []
while True:
scope_start, i = find_first(source, scope_identifier, pos)
if scope_start != -1:
scp = source.find(";", scope_start)
pp = source.find("{", scope_start)
if us(pp) > scp:
if scp == -1:
return scopes
pos = scp
continue
scope_end = enclose("{", "}", source, scope_start)
if scope_end > type_pos > scope_start:
scope_name = type_name(source[scope_start:scope_end])
scopes.append({
"type": i,
"name": scope_name
})
pos = source.find("{", scope_start) + 1
else:
pos = scope_end
else:
return scopes
if pos > type_pos:
return scopes
return []
# return list of any typedefs for a particular type
def find_typedefs(fully_qualified_name, source):
pos = 0
typedefs = []
typedef_names = []
while True:
start_pos = find_token("typedef", source[pos:])
if start_pos != -1:
start_pos += pos
end_pos = start_pos + source[start_pos:].find(";")
typedef = source[start_pos:end_pos]
q = find_token(fully_qualified_name, typedef)
if q != -1:
typedefs.append(source[start_pos:end_pos])
name = typedef[q+len(fully_qualified_name):end_pos].strip()
typedef_names.append(name)
pos = end_pos
else:
break
return typedefs, typedef_names
# return list of any typedefs for a particular type
def find_typedef_decls(source):
pos = 0
typedef_decls = []
while True:
start_pos = find_token("typedef", source[pos:])
if start_pos != -1:
start_pos += pos
end_pos = start_pos + source[start_pos:].find(";")
typedef_decls.append(source[start_pos:end_pos])
pos = end_pos
else:
break
return typedef_decls
def find_type_attributes(source, type_pos):
delimiters = [";", "}"]
attr = source[:type_pos].rfind("[[")
first_d = us(-1)
for d in delimiters:
first_d = min(us(source[:type_pos].rfind(d)), first_d)
if first_d == us(-1):
first_d = -1
if attr > first_d:
attr_end = source[attr:].find("]]")
return source[attr+2:attr+attr_end]
return None
# extract the type of the template parameter ie. StructuredBuffer<float4> (template_type = float4)
def get_template_type(type_decl):
start, end = enclose_start_end("<", ">", type_decl, 0)
if start != -1 and end != -1:
return type_decl[start:end]
return None
# finds all type declarations.. ie struct, enum. returning them in dict with name, and code
def find_type_declarations(type_specifier, source):
results = []
names = []
pos = 0
while True:
start_pos = find_token(type_specifier, source[pos:])
if start_pos != -1:
start_pos += pos
# handle forward decl
fp, tok = find_first(source, ["{", ";"], start_pos)
forward = False
members = []
if tok == ";":
declaration = source[start_pos:fp]
forward = True
end_pos = fp
else:
end_pos = enclose("{", "}", source, start_pos)
declaration = source[start_pos:end_pos]
members = get_members(type_specifier, declaration)
scope = get_type_declaration_scope(source, start_pos)
name = type_name(declaration)
name, array_size = type_name_array(name)
qualified_name = ""
for s in scope:
if s["type"] == "namespace":
qualified_name += s["name"] + "::"
qualified_name += name
typedefs, typedef_names = find_typedefs(qualified_name, source)
attributes = find_type_attributes(source, start_pos)
decl = declaration.strip()
results.append({
"type": type_specifier,
"name": name,
"qualified_name": qualified_name,
"declaration": declaration.strip(),
"position": pos,
"members": members,
"scope": scope,
"typedefs": typedefs,
"typedef_names": typedef_names,
"attributes": attributes,
"forward_declaration": forward,
"template_type": get_template_type(decl),
"array_size": array_size
})
pos = end_pos+1
else:
break
for r in results:
names.append(r["name"])
names.append(r["qualified_name"])
for name in r["typedef_names"]:
names.append(name)
return results, names
# find include statements
def find_include_statements(source):
includes = []
for line in source.splitlines():
if line.strip().startswith("#include"):
includes.append(line)
return includes
# find pragma statements
def find_pragma_statements(source):
pragmas = []
for line in source.splitlines():
if line.strip().startswith("#pragma"):
pragmas.append(line)
return pragmas
# finds the next token ignoring white space
def next_token(source, start):
white_space = [" ", "\n"]
pos = start+1
while True:
if source[pos] in white_space:
pos += 1
else:
return source[pos]
if pos >= len(source):
break
return None
# find first string
def find_first(source, tokens, start):
first = sys.maxsize
first_token = ""
for t in tokens:
i = source.find(t, start)
if first > i > -1:
first = i
first_token = t
return first, first_token
# find first token
def find_first_token(source, tokens, start):
first = sys.maxsize
first_token = ""
for t in tokens:
i = find_token(t, source)
if first > i > -1:
first = i
first_token = t
return first, first_token
def arg_list(args):
args = args.replace("\n", " ")
args = re.sub(' +', ' ', args)
pos = 0
a = []
while True:
# find comma separators
cp = args.find(",", pos)
ep = args.find("=", pos)
if cp == -1:
# add final arg
aa = args[pos:].strip()
if len(aa) > 0:
a.append(args[pos:].strip())
break
if -1 < ep < cp:
# handle function with default
end, tok = find_first(args, [",", "{", "("], ep)
if tok == ",":
a.append(args[pos:end].strip())
pos = end+1
continue
elif tok == "(":
end = enclose(tok, ")", args, end)
else:
end = enclose(tok, "}", args, end)
a.append(args[pos:end])
end = args.find(",", end)
if end == -1:
end = len(args)
pos = end+1
else:
# plain arg
a.append(args[pos:cp].strip())
pos = cp+1
return a
# breakdown a type decl ie. StructName variable_name = {}, into data_type: StructName, name: variable_name, default = {}
def breakdown_type_decl(a):
decl = a
# extract default
dp = a.find("=")
default = None
if dp != -1:
default = a[dp + 1:].strip()
decl = a[:dp - 1]
sp = decl.find(":")
# extract semantic
sp = decl.find(":")
semantic = None
if sp != -1:
semantic = decl[sp + 1:].strip()
decl = decl[:sp - 1]
name_pos = decl.rfind(" ")
name_pos = decl.rfind(" ")
return {
"type": decl[:name_pos].strip(),
"name": decl[name_pos:].strip(),
"default": default,
"semantic": semantic
}
# break down arg decl (int a, int b, int c = 0) into contextual info
def breakdown_function_args(args):
args = arg_list(args)
args_context = []
for a in args:
a = a.strip()
if len(a) == "":
continue
if a == "...":
# va list
args_context.append({
"type": "...",
"name": "va_list",
"default": None,
"semantic": None
})
else:
args_context.append(breakdown_type_decl(a))
return args_context
# parse return type of function and split out any template or inline
def parse_return_type(statement):
template = None
inline = None
rt = statement.strip("}")
rt = rt.strip()
tp = rt.find("template")
if tp != -1:
etp = enclose("<", ">", rt, tp)
template = rt[tp:etp]
rt = rt[etp:]
ip = rt.find("inline")
if ip != -1:
ipe = ip+len("inline")
inline = rt[:ipe]
rt = rt[ipe:]
return rt, template, inline
# prepends attributes to soiurce coe of functions, structs etc
def combine_src_attributes(attribs, src):
if len(attribs) == 0:
return src
out = "["
for attrib in attribs:
out += attrib
out += "]\n" + src
return out
# find functions
def find_functions(source):
# look for parenthesis to identiy function decls
functions = []
function_names = []
pos = 0
attributes = []
while True:
statement_end, statement_token = find_first(source, [";", "{", "}"], pos)
if statement_end == -1:
break
statement = source[pos:statement_end].strip()
src_start = pos
pp = statement.find("(")
ep = statement.find("=")
skip = statement_end+1
# check for attributes
if len(statement) > 0:
if statement[0] == "[":
start, end = enclose_start_end("[", "]", statement, 0)
attributes.append(statement[start:end])
pos += start + end + 1
continue
if pp != -1:
next = next_token(statement, pp)
if (ep == -1 or pp < ep) and next != "*":
# this a function decl, so break down into context
body = ""
body_end = skip
if statement_token == "{":
body_end = enclose("{", "}", source, statement_end-1)
body = source[statement_end-1:body_end]
statement_end = body_end+1
skip = body_end
args_end = enclose("(", ")", statement, pp)-1
name_pos = statement[:pp].rfind(" ")
name = statement[name_pos+1:pp].strip()
name_unscoped = name.rfind(":")
qualifier = ""
if name_unscoped != -1:
qualifier = name[:name_unscoped-1]
name = name[name_unscoped+1:]
return_type, template, inline = parse_return_type(statement[:name_pos])
args = breakdown_function_args(statement[pp+1:args_end])
scope = get_type_declaration_scope(source, pos)
functions.append({
"name": name,
"qualifier": qualifier,
"return_type": return_type,
"args": args,
"scope": scope,
"body": body,
"template": template,
"inline": inline,
"source": combine_src_attributes(attributes, source[src_start:body_end].strip()).strip(),
"attributes": list(attributes)
})
attributes.clear()
function_names.append(name)
pos = skip
if pos > len(source):
break
return functions, function_names
# returns prototype with no names, ie (int, int, float), from a function context
def get_funtion_prototype(func):
args = ""
num_args = len(func["args"])
for a in range(0, num_args):
args += func["args"][a]["type"]
if a < num_args - 1:
args += ", "
if num_args == 0:
args = "void"
return "(" + args + ")"
# find the line, column position within source
def position_to_line_column(source, position):
if position < 0 or position > len(source):
raise ValueError("position out of bounds")
# split the string into lines
lines = source.splitlines(keepends=True)
# find the line and column
current_pos = 0
for line_number, line in enumerate(lines, start=1):
line_length = len(line)
if current_pos + line_length > position:
# Found the line
column = position - current_pos + 1 # Convert to 1-based
return line_number, column
current_pos += line_length
# If we exit the loop, something went wrong
raise ValueError("position not found in string")
# main function for scope
def test():
# read source from file
source = open("test.h", "r").read()
# sanitize source to make further ops simpler
source = sanitize_source(source)
print("--------------------------------------------------------------------------------")
print("sanitize source ----------------------------------------------------------------")
print("--------------------------------------------------------------------------------")
print(source)
# find all include statements, fromsanitized source to ignore commented out ones
includes = find_include_statements(source)
print("--------------------------------------------------------------------------------")
print("find includes ------------------------------------------------------------------")
print("--------------------------------------------------------------------------------")
print(includes)
# find string literals within source
print("--------------------------------------------------------------------------------")
print("find strings literals ----------------------------------------------------------")
print("--------------------------------------------------------------------------------")
strings = find_string_literals(source)
print(strings)
# remove string literals to avoid conflicts when parsing
print("--------------------------------------------------------------------------------")
print("placeholder literals -----------------------------------------------------------")
print("--------------------------------------------------------------------------------")
strings, source = placeholder_string_literals(source)
print(format_source(source, 4))
# find single token
print("--------------------------------------------------------------------------------")
print("find token ---------------------------------------------------------------------")
print("--------------------------------------------------------------------------------")
token = "SOME_TOKEN"
token_pos = find_token(token, source)
print("token pos: {}".format(token_pos))
print("token:" + source[token_pos:token_pos+len(token)])
print("--------------------------------------------------------------------------------")
print("find all tokens ----------------------------------------------------------------")
print("--------------------------------------------------------------------------------")
token = "int"
token_locations = find_all_tokens(token, source)
for loc in token_locations:
print("{}: ".format(loc) + source[loc:loc+10] + "...")
# find structs
print("--------------------------------------------------------------------------------")
print("find structs -------------------------------------------------------------------")
print("--------------------------------------------------------------------------------")
structs, struct_names = find_type_declarations("struct", source)
print(struct_names)
print(json.dumps(structs, indent=4))
# find enums
print("--------------------------------------------------------------------------------")
print("find enums ---------------------------------------------------------------------")
print("--------------------------------------------------------------------------------")
enums, enum_names = find_type_declarations("enum", source)
print(enum_names)
print(json.dumps(enums, indent=4))
# find free functions
print("--------------------------------------------------------------------------------")
print("find functions -----------------------------------------------------------------")
print("--------------------------------------------------------------------------------")
functions, function_names = find_functions(source)
print(function_names)
print(json.dumps(functions, indent=4))
# replace placeholder literals
print("--------------------------------------------------------------------------------")
print("replace placeholder literals ---------------------------------------------------")
print("--------------------------------------------------------------------------------")
source = replace_placeholder_string_literals(strings, source)
print(format_source(source, 4))
# display names
print("--------------------------------------------------------------------------------")
print(" display name ------------------------------------------------------------------")
print("--------------------------------------------------------------------------------")
print(display_name("m_snake_case_variable", False))
print(display_name("m_camelCaseVariable", False))
print(display_name("SCAREY_CASE_DEFINE", True))
# entry
if __name__ == "__main__":
test()