-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCppHeaderParser.py
More file actions
2609 lines (2198 loc) · 109 KB
/
CppHeaderParser.py
File metadata and controls
2609 lines (2198 loc) · 109 KB
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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#
# Author: Jashua R. Cloutier (contact via sourceforge username:senexcanis)
#
# Copyright (C) 2010, Jashua R. Cloutier
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# * Neither the name of Jashua R. Cloutier nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#
# The CppHeaderParser.py script is written in Python 2.4 and released to
# the open source community for continuous improvements under the BSD
# 2.0 new license, which can be found at:
#
# http://www.opensource.org/licenses/bsd-license.php
#
"""
CppHeaderParser2.0: April 2011 - August 2011
by HartsAntler
http://pyppet.blogspot.com
Quick Start - User API:
h = CppHeaderParser.CppHeader("someheader.h")
for name in h.classes:
c = h.classes[name]
for method in c['methods']['public']:
print method['name']
print dir(method) # view the rest of the API here.
... TODO document more ...
New Features by Hart:
should be able to parse all c++ files, not just headers
parsing global typedefs with resolution
parsing global structs
fixes nested struct in class changes accessor type
parsing if class is abstract
parsing more info about variables
save ordering of classes, structs, and typedefs
handle forward decl of class in a class
handle mutable, static, and other variable types
handle 1D arrays
handle throw keyword and function prefix __attribute__((__const__))
handle nameless parameters "void method(void);"
handle simple templates, and global functions.
Internal Developer Notes:
1. double name stacks:
. the main stack is self.nameStack, this stack is simpler and easy to get hints from
. the secondary stack is self.stack is the full name stack, required for parsing somethings
. each stack maybe cleared at different points, since they are used to detect different things
. it looks ugly but it works :)
2. Had to make the __repr__ methods simple because some of these dicts are interlinked.
For nice printing, call something.show()
"""
import ply.lex as lex
import os
import sys
import re
import inspect
def lineno():
"""Returns the current line number in our program."""
return inspect.currentframe().f_back.f_lineno
version = __version__ = "1.9.9o"
tokens = [
'NUMBER',
'NAME',
'OPEN_PAREN',
'CLOSE_PAREN',
'OPEN_BRACE',
'CLOSE_BRACE',
'COLON',
'SEMI_COLON',
'COMMA',
'COMMENT_SINGLELINE',
'COMMENT_MULTILINE',
'PRECOMP_MACRO',
'PRECOMP_MACRO_CONT',
'ASTERISK',
'AMPERSTAND',
'EQUALS',
'MINUS',
'PLUS',
'DIVIDE',
'CHAR_LITERAL',
'STRING_LITERAL',
'OPERATOR_DIVIDE_OVERLOAD',
'NEW_LINE',
'OPEN_BRACKET',
'CLOSE_BRACKET',
]
t_OPEN_BRACKET = r'\['
t_CLOSE_BRACKET = r'\]'
#t_ignore = " \t\r[].|!?%@" # (cppheaderparser 1.9x)
#t_ignore = " \t\r[].|!?%@'^\\"
t_ignore = " \t\r.|!?%@'^\\"
t_NUMBER = r'[0-9][0-9XxA-Fa-f]*'
t_NAME = r'[<>A-Za-z_~][A-Za-z0-9_]*'
t_OPERATOR_DIVIDE_OVERLOAD = r'/='
t_OPEN_PAREN = r'\('
t_CLOSE_PAREN = r'\)'
t_OPEN_BRACE = r'{'
t_CLOSE_BRACE = r'}'
t_SEMI_COLON = r';'
t_COLON = r':'
t_COMMA = r','
t_PRECOMP_MACRO = r'\#.*'
t_PRECOMP_MACRO_CONT = r'.*\\\n'
def t_COMMENT_SINGLELINE(t):
r'\/\/.*\n'
global doxygenCommentCache
if t.value.startswith("///") or t.value.startswith("//!"):
if doxygenCommentCache:
doxygenCommentCache += "\n"
if t.value.endswith("\n"):
doxygenCommentCache += t.value[:-1]
else:
doxygenCommentCache += t.value
t_ASTERISK = r'\*'
t_MINUS = r'\-'
t_PLUS = r'\+'
t_DIVIDE = r'/[^/]' # fails to catch "/(" - method operator that overloads divide
t_AMPERSTAND = r'&'
t_EQUALS = r'='
t_CHAR_LITERAL = "'.'"
#found at http://wordaligned.org/articles/string-literals-and-regular-expressions
#TODO: This does not work with the string "bla \" bla"
t_STRING_LITERAL = r'"([^"\\]|\\.)*"'
#Found at http://ostermiller.org/findcomment.html
def t_COMMENT_MULTILINE(t):
r'/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/'
global doxygenCommentCache
if t.value.startswith("/**") or t.value.startswith("/*!"):
#not sure why, but get double new lines
v = t.value.replace("\n\n", "\n")
#strip prefixing whitespace
v = re.sub("\n[\s]+\*", "\n*", v)
doxygenCommentCache += v
def t_NEWLINE(t):
r'\n+'
t.lexer.lineno += len(t.value)
def t_error(v):
print( "Lex error: ", v )
lex.lex()
debug = 0
debug_trace = 0
def trace_print(*arg):
global debug_trace
if debug_trace: print(arg)
supportedAccessSpecifier = [
'public',
'protected',
'private'
]
enumMaintianValueFormat = False
doxygenCommentCache = ""
#################################
def strip_to_pure( s ):
# create stripped raw_type #
p = '* & con
4270
st static mutable volatile'.split()
raw = []
for x in s.split():
if x not in p: raw.append( x )
return ' '.join( raw )
def is_namespace(nameStack):
"""Determines if a namespace is being specified"""
if len(nameStack) == 0:
return False
if nameStack[0] == "namespace":
return True
return False
def is_enum_namestack(nameStack):
"""Determines if a namestack is an enum namestack"""
if len(nameStack) == 0:
return False
if nameStack[0] == "enum":
return True
if len(nameStack) > 1 and nameStack[0] == "typedef" and nameStack[1] == "enum":
return True
return False
class CppParseError(Exception): pass
class _CppClass(dict):
def _parser_helper( self, stack ):
if stack[0] == 'template':
self['template'] = True
if stack[1] == '<': # may not have a template def
hit = 0; raw = []
for a in stack:
if a == '<' or a.startswith('<'): hit += 1; raw.append( a )
elif a == '>' or a.startswith('>'):
hit -= 1; raw.append( a )
if not hit: break
elif hit: raw.append( a )
raw = ' '.join( raw ).replace(' : : ', '::')
raw = raw.replace('< ', '<').replace(' <', '<')
raw = raw.replace('> ', '>').replace(' <', '<')
self['template_raw'] = raw
if ',' in raw: self['complex_template'] = True
prev = None
prev2 = None
print('stack IN', ' '.join(stack))
for i, tok in enumerate(stack): # can not trust the first single ":" or last
if prev and prev2 and tok == ':' and prev != ':' and prev2 != ':':
break
prev = tok
prev2 = prev
a = stack[ : i+1 ]
b = stack[ i+1 : ]
while a[-1] == ':': a.pop()
print( 'HEAD', a )
print('______________')
print( 'TAIL', b )
if ''.join(stack).replace('::','_').count(':') >= 2:
if stack.count('class') == 1:
self['name'] = stack[ stack.index('class') + 1 ]
elif stack.count('struct') == 1:
self['name'] = stack[ stack.index('struct') + 1 ]
else:
self['unsafe_template'] = True
b = []
elif a[0] == 'template' and ('class' in a or 'struct' in a):
if '>' not in a:
self['name'] = a[ a.index('class') + 1 ]
self['unsafe_template'] = True
else:
copy = list( a )
last = len(a) - 1 - a[::-1].index('>')
self['template_typename'] = a[ a.index('>')-1 ]
a = a[ last+1 : ]
if not a:
a = copy[ copy.index('class')+1 : ]
x = ''.join( a )
assert '<' in x and '>' in x
self['name'] = x
self['special_template'] = True
elif 'class' in a:
self['name'] = ''.join( a[1:] )
elif 'struct' in a:
self['name'] = ''.join( a[1:] )
self['struct'] = True
self['struct_template'] = self['template_typename']
elif 'class' in b:
self['name'] = b[ b.index('class') + 1 ]
b = []
elif 'struct' in b:
self['name'] = b[ b.index('struct') + 1 ]
b = []
else:
self['unsafe_template'] = True
assert 0
elif a[0] == 'template' and b[-2] in ('class','struct'):
self['name'] = b[-1]
b = [] # b is invalid
elif a[0] == 'class':
self['name'] = ''.join( a[1:] )
elif 'class' in b:
self['name'] = b[ b.index('class') + 1 ]
b = []
elif 'struct' in b:
self['name'] = b[ b.index('struct') + 1 ]
self['struct'] = True
b = []
else:
assert 0
if b:
p = [ {'access':'public', 'class':''} ]
for x in b:
if x in 'public protected private'.split():
p[-1]['access'] = x
elif x == 'virtual':
p[-1]['virtual'] = True
elif x == ',':
p.append( {'access':'public', 'class':''} )
else:
p[-1]['class'] += x
self['inherits'] = p
else:
self['inherits'] = []
return True
class CppClass( _CppClass ):
"""Takes a name stack and turns it into a class
Contains the following Keys:
self['name'] - Name of the class
self['doxygen'] - Doxygen comments associated with the class if they exist
self['inherits'] - List of Classes that this one inherits where the values
are of the form {"access": Anything in supportedAccessSpecifier
"class": Name of the class
self['methods'] - Dictionary where keys are from supportedAccessSpecifier
and values are a lists of CppMethod's
self['properties'] - Dictionary where keys are from supportedAccessSpecifier
and values are lists of CppVariable's
self['enums'] - Dictionary where keys are from supportedAccessSpecifier and
values are lists of CppEnum's
self['structs'] - Dictionary where keys are from supportedAccessSpecifier and
values are lists of nested Struct's
An example of how this could look is as follows:
#self =
{
'name': ""
'inherits':[]
'methods':
{
'public':[],
'protected':[],
'private':[]
},
'properties':
{
'public':[],
'protected':[],
'private':[]
},
'enums':
{
'public':[],
'protected':[],
'private':[]
}
}
"""
def __repr__( self ): return self['name']
def combine( self, name ): # very often things are typedef'ed in the parent class
r = {}
for p in self['inherits']:
pname = p['class']
if pname in Resolver.CLASSES:
r.update( getattr( Resolver.CLASSES[pname], name ) )
return r
def get_all_methods(self):
r = []
for typ in 'public protected private'.split(): r += self['methods'][typ]
return r
def get_all_method_names( self ):
r = []
for typ in 'public protected private'.split(): r += self.get_method_names(typ) # returns list
return r
def get_all_pure_virtual_methods( self ):
r = {}
for typ in 'public protected private'.split(): r.update(self.get_pure_virtual_methods(typ)) # returns dict
return r
def get_method_names( self, type='public' ): return [ meth['name'] for meth in self['methods'][ type ] ]
def get_pure_virtual_methods( self, type='public' ):
r = {}
for meth in self['methods'][ type ]:
if meth['pure_virtual']: r[ meth['name'] ] = meth
return r
def __init__(self, nameStack):
self['nested_classes'] = []
self['parent'] = None
self['abstract'] = False
self['namespace'] = ""
self._public_enums = {}
self._public_structs = {}
self._public_typedefs = {}
self._public_classes = {}
self._public_forward_declares = []
self._typedefs_info = {}
if (debug): print( "Class: ", nameStack )
if (len(nameStack) < 2):
print( "Error detecting class" )
return
global doxygenCommentCache
if len(doxygenCommentCache):
self["doxygen"] = doxygenCommentCache
doxygenCommentCache = ""
methodAccessSpecificList = {}
propertyAccessSpecificList = {}
enumAccessSpecificList = {}
structAccessSpecificList = {}
typedefAccessSpecificList = {}
forwardAccessSpecificList = {}
for accessSpecifier in supportedAccessSpecifier:
methodAccessSpecificList[accessSpecifier] = []
propertyAccessSpecificList[accessSpecifier] = []
enumAccessSpecificList[accessSpecifier] = []
structAccessSpecificList[accessSpecifier] = []
typedefAccessSpecificList[accessSpecifier] = []
forwardAccessSpecificList[accessSpecifier] = []
self['methods'] = methodAccessSpecificList
self['properties'] = propertyAccessSpecificList
self['enums'] = enumAccessSpecificList
self['structs'] = structAccessSpecificList
self['typedefs'] = typedefAccessSpecificList
self['forward_declares'] = forwardAccessSpecificList
ok = self._parser_helper( nameStack )
if not ok: self['invalid'] = True
def show_all(self):
self.show()
for key in self.keys(): print( ' %s : %s' %(key,self[key]) )
def show(self):
"""Convert class to a string"""
namespace_prefix = ""
if self["namespace"]: namespace_prefix = self["namespace"] + "::"
rtn = "class %s"%(namespace_prefix + self["name"])
if self['abstract']: rtn += ' (abstract)\n'
else: rtn += '\n'
if 'doxygen' in self.keys(): rtn += self["doxygen"] + '\n'
if 'parent' in self.keys() and self['parent']: rtn += 'parent class:' + self['parent'] + '\n'
if "inherits" in self.keys():
rtn += " Inherits: "
for inheritClass in self["inherits"]:
rtn += "%s %s, "%(inheritClass["access"], inheritClass["class"])
rtn += "\n"
rtn += " {\n"
for accessSpecifier in supportedAccessSpecifier:
rtn += " %s\n"%(accessSpecifier)
#Enums
if (len(self["enums"][accessSpecifier])):
rtn += " <Enums>\n"
for enum in self["enums"][accessSpecifier]:
rtn += " %s\n"%(repr(enum))
#Properties
if (len(self["properties"][accessSpecifier])):
rtn += " <Properties>\n"
for property in self["properties"][accessSpecifier]:
rtn += " %s\n"%(repr(property))
#Methods
if (len(self["methods"][accessSpecifier])):
rtn += " <Methods>\n"
for method in self["methods"][accessSpecifier]:
rtn += "\t\t" + method.show() + '\n'
rtn += " }\n"
print( rtn )
class _CppMethod( dict ):
def _params_helper1( self, stack ):
# new July 7th, deal with defaults that init: vec3(0,0,0)
# so that comma split still works later on parsing the parameters.
# also deal with "throw" keyword
if 'throw' in stack: stack = stack[ : stack.index('throw') ]
## remove GCC keyword __attribute__(...) and preserve returns ##
cleaned = []
hit = False; hitOpen = 0; hitClose = 0
for a in stack:
if a == '__attribute__': hit = True
if hit:
if a == '(': hitOpen += 1
elif a == ')': hitClose += 1
if a==')' and hitOpen == hitClose:
hit = False
else:
cleaned.append( a )
stack = cleaned
# also deal with attri
8B44
bute((const)) function prefix #
# TODO this needs to be better #
if len(stack) > 5:
a = ''.join(stack)
if a.startswith('((__const__))'): stack = stack[ 5 : ]
elif a.startswith('__attribute__((__const__))'): stack = stack[ 6 : ]
stack = stack[stack.index('(') + 1: ]
if not stack: return []
if len(stack)>=3 and stack[0]==')' and stack[1]==':': # is this always a constructor?
self['constructor'] = True
return []
stack.reverse(); _end_ = stack.index(')'); stack.reverse()
stack = stack[ : len(stack)-(_end_+1) ]
if '(' not in stack: return stack # safe to return, no defaults that init a class
elif stack.index('(') > stack.index(')'): # deals with: "constructor(int x) : func(x) {}"
return stack[ : stack.index(')') ] # fixed july20
# transforms ['someclass', '(', '0', '0', '0', ')'] into "someclass(0,0,0)'"
r = []; hit=False
for a in stack:
if a == '(': hit=True
elif a == ')': hit=False
if hit or a == ')': r[-1] = r[-1] + a
else: r.append( a )
return r
def _params_helper2( self, params ):
for p in params:
# if param becomes unresolved - function/parent is marked with 'unresolved_parameters'
if 'function' in self: p['function'] = self
else: p['method'] = self
# force full namespace for nested items, or take method name space as our own (bad idea?)
if '::' in p['type']:
ns = p['type'].split('::')[0]
if ns not in Resolver.NAMESPACES and ns in Resolver.CLASSES:
p['type'] = self['namespace'] + p['type']
else: p['namespace'] = self[ 'namespace' ]
class CppMethod( _CppMethod ):
"""Takes a name stack and turns it into a method
Contains the following Keys:
self['returns'] - Return type of the method (ex. "int")
self['name'] - Name of the method (ex. "getSize")
self['doxygen'] - Doxygen comments associated with the method if they exist
self['parameters'] - List of CppVariables
"""
def show(self):
r = ['method name: %s (%s)' %(self['name'],self['debug']) ]
if self['returns']: r.append( 'returns: %s'%self['returns'] )
if self['parameters']: r.append( 'number arguments: %s' %len(self['parameters']))
if self['pure_virtual']: r.append( 'pure virtual: %s'%self['pure_virtual'] )
if self['constructor']: r.append( 'constructor' )
if self['destructor']: r.append( 'destructor' )
return '\n\t\t '.join( r )
def __init__(self, nameStack, curClass=None, methinfo={} ):
if (debug): print( "Method: ", nameStack )
global doxygenCommentCache
if not curClass: self['function'] = True
if len(doxygenCommentCache):
self["doxygen"] = doxygenCommentCache
doxygenCommentCache = ""
if "operator" in nameStack:
self["name"] = "".join(nameStack[nameStack.index('operator'):nameStack.index('(')])
else:
self["name"] = " ".join(nameStack[nameStack.index('(') - 1:nameStack.index('(')])
self.update( methinfo ) # harts hack
paramsStack = self._params_helper1( nameStack )
params = []
#See if there is a doxygen comment for the variable
doxyVarDesc = {}
#TODO: Put this into a class
if self.has_key("doxygen"):
doxyLines = self["doxygen"].split("\n")
lastParamDesc = ""
for doxyLine in doxyLines:
if " @param " in doxyLine or " \param " in doxyLine:
try:
#Strip out the param
doxyLine = doxyLine[doxyLine.find("param ") + 6:]
(var, desc) = doxyLine.split(" ", 1)
doxyVarDesc[var] = desc.strip()
lastParamDesc = var
except: pass
elif " @return " in doxyLine or " \return " in doxyLine:
lastParamDesc = ""
# not handled for now
elif lastParamDesc:
try:
doxyLine = doxyLine.strip()
if " " not in doxyLine:
lastParamDesc = ""
continue
doxyLine = doxyLine[doxyLine.find(" ") + 1:]
doxyVarDesc[lastParamDesc] += " " + doxyLine
except: pass
#Create the variable now
while (len(paramsStack)):
if (',' in paramsStack):
param = CppVariable(paramsStack[0:paramsStack.index(',')], doxyVarDesc=doxyVarDesc)
if len(param.keys()): params.append(param)
paramsStack = paramsStack[paramsStack.index(',') + 1:]
else:
param = CppVariable(paramsStack, doxyVarDesc=doxyVarDesc)
if len(param.keys()): params.append(param)
break
self["parameters"] = params
self._params_helper2( params ) # mods params inplace
class _CppVariable(dict):
def _name_stack_helper( self, stack, fullStack ):
#print( stack ); print(fullStack); print('_'*80)
stack = list(stack)
if stack[-1].isdigit() and '=' not in stack: # TODO refactor me - was: '=' not in stack or
# check for array[n] and deal with funny array syntax: "int myvar:99"
bits = []
while stack and stack[-1].isdigit(): bits.append( stack.pop() )
if bits:
bits.reverse()
self['bitfield'] = int(''.join(bits))
assert stack[-1] == ':'
stack.pop()
## find and strip array def ##
if '[' in stack:
assert stack.count('[') == stack.count(']')
a = ['']; hit = 0; _stack = []
for s in stack:
if s == '[': hit += 1
elif s == ']': hit -= 1; a.append( '' )
elif hit: a[-1] += s
elif not hit: _stack.append( s )
stack = _stack
b = []
for s in a:
if s.isdigit(): b.append( int( s ) )
elif s != '': self['invalid'] = True
if not b: self['pointer'] += 1
else:
self['array'] = b[0]
self['array_dimensions'] = b
if len(b)>1: self['multidimensional'] = True
while stack and not stack[-1]: stack.pop() # can be empty?
return stack
class CppVariable( _CppVariable ):
"""Takes a name stack and turns it into a method
Contains the following Keys:
self['type'] - Type for the variable (ex. "const string &")
self['raw_type'] - Type of variable without pointers or other markup (ex. "string")
self['name'] - Name of the variable (ex. "numItems")
self['namespace'] - Namespace containing the enum
self['desc'] - Description of the variable if part of a method (optional)
self['doxygen'] - Doxygen comments associated with the method if they exist
self['defalt'] - Default value of the variable, this key will only exist if there is a default value
"""
Vars = []
def __init__(self, nameStack, fullStack=None, doxyVarDesc=None): # CppMethod will not pass fullStack for params
self['aliases'] = []; self['parent'] = None; self['typedef'] = None
for key in 'constant reference pointer static typedefs class fundamental unresolved mutable'.split():
self[ key ] = 0
_stack_ = nameStack
nameStack = self._name_stack_helper( nameStack, fullStack )
global doxygenCommentCache
if len(doxygenCommentCache):
self["doxygen"] = doxygenCommentCache
doxygenCommentCache = ""
if (debug): print( "Variable: ", nameStack )
if (len(nameStack) < 2):
if len(nameStack) == 1: self['type'] = nameStack[0]; self['name'] =
CDB4
039;'
else: print(_stack_); assert 0
elif ("=" in nameStack):
self["type"] = " ".join(nameStack[:nameStack.index("=") - 1])
self["name"] = nameStack[nameStack.index("=") - 1]
self['default'] = " ".join(nameStack[nameStack.index("=") + 1:])
self['default'] = self['default'].replace(' <', '<' )
self['default'] = self['default'].replace(' >', '>' )
elif nameStack[-1] in '*&': # rare cases - function param is an unnamed pointer: "void somemethod( SomeObject* )"
self['type'] = ' '.join(nameStack)
self['name'] = ''
else: # common case
self["type"] = " ".join(nameStack[:-1])
self["name"] = nameStack[-1]
self["type"] = self["type"].replace(" :",":")
self["type"] = self["type"].replace(": ",":")
self["type"] = self["type"].replace(" <","<")
self["type"] = self["type"].replace(" >",">")
#Optional doxygen description
if doxyVarDesc and self['name'] in doxyVarDesc:
self['description'] = doxyVarDesc[ self['name'] ]
self['type'] = self['type'].strip()
a = []
for b in self['type'].split():
if b == '__const__': b = 'const'
a.append( b )
if not a:
self['invalid'] = True # void someinvalidfunction( int x, y=INVALID );
print('WARN - bad variable', self )
else:
if a[0] == 'class':
self['explicit_class'] = a[1]
a = a[1:]
elif a[0] == 'struct':
self['explicit_struct'] = a[1]
a = a[1:]
self['type'] = ' '.join( a )
if self['name'].count('<') != self['name'].count('>'): self['invalid'] = True
CppVariable.Vars.append( self ) # save and resolve later
class _CppEnum(dict):
def resolve_enum_values( self, values ):
"""Evaluates the values list of dictionaries passed in and figures out what the enum value
for each enum is editing in place:
Example:
From: [{'name': 'ORANGE'},
{'name': 'RED'},
{'name': 'GREEN', 'value': '8'}]
To: [{'name': 'ORANGE', 'value': 0},
{'name': 'RED', 'value': 1},
{'name': 'GREEN', 'value': 8}]
"""
t = 'int'; i = 0
names = [ v['name'] for v in values ]
for v in values:
if 'value' in v:
a = v['value'].strip()
if a.lower().startswith("0x"):
try:
i = a = int(a , 16)
except:pass
elif a.isdigit():
i = a = int( a )
elif a in names:
for other in values:
if other['name'] == a:
v['value'] = other['value']
break
elif '"' in a or "'" in a: t = 'char*' # only if there are quotes it this a string enum
else:
try:
a = i = ord(a)
except: pass
if not enumMaintianValueFormat: v['value'] = a
else: v['value'] = i
i += 1
return t
class CppEnum(_CppEnum):
"""Takes a name stack and turns it into an Enum
Contains the following Keys:
self['name'] - Name of the enum (ex. "ItemState")
self['namespace'] - Namespace containing the enum
self['values'] - List of values where the values are a dictionary of the
form {"name": name of the key (ex. "PARSING_HEADER"),
"value": Specified value of the enum, this key will only exist
if a value for a given enum value was defined
}
"""
def __init__(self, nameStack):
if len(nameStack) < 4 or "{" not in nameStack or "}" not in nameStack:
#Not enough stuff for an enum
return
global doxygenCommentCache
if len(doxygenCommentCache):
self["doxygen"] = doxygenCommentCache
doxygenCommentCache = ""
valueList = []
#Figure out what values it has
valueStack = nameStack[nameStack.index('{') + 1: nameStack.index('}')]
while len(valueStack):
tmpStack = []
if "," in valueStack:
tmpStack = valueStack[:valueStack.index(",")]
valueStack = valueStack[valueStack.index(",") + 1:]
else:
tmpStack = valueStack
valueStack = []
d = {}
if len(tmpStack) == 1: d["name"] = tmpStack[0]
elif len(tmpStack) >= 3 and tmpStack[1] == "=":
d["name"] = tmpStack[0]; d["value"] = " ".join(tmpStack[2:])
elif len(tmpStack) == 2 and tmpStack[1] == "=":
if (debug): print( "WARN-enum: parser missed value for %s"%tmpStack[0] )
d["name"] = tmpStack[0]
if d: valueList.append( d )
if len(valueList):
self['type'] = self.resolve_enum_values( valueList ) # returns int for standard enum
self["values"] = valueList
else:
print( 'WARN-enum: empty enum', nameStack )
return
#Figure out if it has a name
preBraceStack = nameStack[:nameStack.index("{")]
postBraceStack = nameStack[nameStack.index("}") + 1:]
if (len(preBraceStack) == 2 and "typedef" not in nameStack):
self["name"] = preBraceStack[1]
elif len(postBraceStack) and "typedef" in nameStack:
self["name"] = " ".join(postBraceStack)
else: print( 'WARN-enum: nameless enum', nameStack )
#See if there are instances of this
if "typedef" not in nameStack and len(postBraceStack):
self["instances"] = []
for var in postBraceStack:
if "," in var:
continue
self["instances"].append(var)
self["namespace"] = ""
def is_fundamental(s):
for a in s.split():
if a not in 'size_t wchar_t struct union unsigned signed bool char short int float double long void *':
if a not in C99_NONSTANDARD: return False
return True
def prune_templates( stack ):
x = []; hit = 0
for a in stack:
if a == '<' or a.startswith('<'): hit += 1
elif a == '>' or a.endswith('>'): hit -= 1
elif not hit and a != 'template': x.append( a )
return x
def prune_arrays( stack ):
x = []; hit = 0
for a in stack:
if a == '[' or a.startswith('['): hit += 1
elif a == ']' or a.endswith(']'): hit -= 1
elif not hit: x.append( a )
return x
def is_method_namestack(stack):
clean = prune_templates( stack );# print('CLEAN TEMPLATES',clean)
clean = prune_arrays( clean );# print('CLEAN ARRAYS',clean)
r = False
if 'operator' in stack: r = True # allow all operators
elif not ('(' in stack or '/(' in stack): r = False
elif stack[0]=='mutable': r = False
elif clean and clean[0] in ('class', 'struct'): r = False
elif not ('(' in clean or '/(' in clean): r = False
#elif '__attribute__' in stack: r = False
#elif stack[0] == '__attribute': r = False
elif stack[0] == 'typedef': r = False # TODO deal with typedef function prototypes
elif stack[0] in 'return if else case switch throw +'.split(): print( stack ); assert 0; r = False
elif stack[0] == '}' and stack[1] in 'return if else case switch'.split(): print( stack ); assert 0; r = False
elif '=' in stack:# and stack.index('=') < stack.index('('):
#if 'template' not in stack: r = False
if '=' in clean and clean.index('=') < clean.index('('): r = False
else: r = True
elif '{' in stack and stack.index('{') < stack.index('('): r = False # struct that looks like a method/class
elif '(' in stack and ')' in stack:
if '{' in stack and '}' in stack: r = True
#elif '/' in stack: r = False
#elif '/ ' in stack: r = False
elif stack[-1] == ';': r = True
elif '{' in stack: r = True
x = ''.join(stack)
if x.endswith('(0,0,0);'): r = False
elif x.endswith('(0);'): r = False
elif x.endswith(',0);'): r = False
elif x.endswith(',1);'): r = False
elif x.endswith(',true);'): r = False
elif x.endswith(',false);'): r = False
elif x.endswith(',0xFF);'): r = False
else: r = False
print( 'is method namestack', r, stack ); print('_'*80)
return r
class CppStruct(dict):
Structs = []
def __init__(self, nameStack):
if nameStack[0] == 'template': self['template'] = True
if nameStack.index('struct')+1 < len(nameStack):
self['type'] = nameStack[ nameStack.index('struct') + 1 ]
else: self['type'] = None
self['fields'] = []
self['methods'] = []
self['parent'] = None
self.Structs.append( self )
C99_NONSTANDARD = {
'int8' : 'signed char',
'int16' : 'short int',
'int32' : 'int',
'int64' : 'long int', # this can be: long int (64bit), or long long int (32bit) int64_t
'uint' : 'unsigned int',
'uint8' : 'unsigned char',
'uint16' : 'unsigned short int',
'uint32' : 'unsigned int',
'uint64' : 'unsigned long int', # depends on host bits (uint64_t)
'time_t': 'unsigned long int', # unsafe? http://stackoverflow.com/questions/6418221/getting-type-size-of-time-t-using-ctypes
'uint64_t' : 'unsigned long int',
'int64_t' : 'long int',
}
def standardize_fundamental( s ):
if s in C99_NONSTANDARD: return C99_NONSTANDARD[ s ]
else: return s
class Resolver(object):
C_FUNDAMENTAL = 'size_t unsigned signed bool char wchar short int float double long void'.split()
C_FUNDAMENTAL += 'struct union enum'.split()
NAMESPACES = []
CLASSES = {}
STRUCTS = {}
SubTypedefs = {} # TODO deprecate?
def initextra(self):
self.typedefs = {}
self.typedefs_info = {}
self.typedefs_order = []
self.classes_order = []
self.template_classes = {}
self.template_typedefs = {}
self.structs = Resolver.STRUCTS
self.structs_order = []
self.namespaces = Resolver.NAMESPACES # save all namespaces
self.curStruct = None
self.stack = [] # full name stack, good idea to keep both stacks? (simple stack and full stack)
self._classes_brace_level = {} # class name : level
self._structs_brace_level = {} # struct type : level
self._method_body = None
self._forward_decls = []