]> git.saurik.com Git - wxWidgets.git/blob - utils/HelpGen/src/srcparser.h
glibc's vswprintf doesn't nul terminate on truncation.
[wxWidgets.git] / utils / HelpGen / src / srcparser.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: No names yet.
3 // Purpose: To provide a simple _framework_
4 // for series of source code parsers with
5 // compatible interfaces
6 // Author: Aleksandras Gluchovas
7 // Modified by: AG on 28/12/98
8 // Created: 22/09/98
9 // RCS-ID: $Id$
10 // Copyright: (c) Aleskandars Gluchovas
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
13
14 #ifndef __SRCPARSER_G__
15 #define __SRCPARSER_G__
16
17 #if defined( wxUSE_TEMPLATE_STL )
18 #include <vector>
19
20 #ifdef WIN32
21 #include <bstring.h>
22 #else
23
24 #include <strclass.h>
25 #include <string.h>
26
27 #endif
28
29 #else
30 #include "wx/string.h"
31 #include "wxstlvec.h"
32
33 #endif
34
35 #include "markup.h" // markup tags used in spOperator::GetFullName()
36
37 // these methods are used for debugging only and disappear in the release build
38 #ifdef __WXDEBUG__
39 #define DECLARE_DUMP virtual void DumpThis(const wxString& indent) const;
40 #else
41 #define DECLARE_DUMP
42 #endif
43
44 // context class list in "inside-out" order :
45
46 class spContext;
47
48 class spParameter;
49 class spAttribute;
50 class spOperation;
51 class spEnumeration;
52 class spTypeDef;
53 class spPreprocessorLine;
54 class spClass;
55 class spNameSpace;
56 class spFile;
57
58 // source context visibilities
59 enum SRC_VISIBLITY_TYPES
60 {
61 SP_VIS_PUBLIC,
62 SP_VIS_PROTECTED,
63 SP_VIS_PRIVATE
64 };
65
66 // class types
67 enum SP_CLASS_TYPES
68 {
69 SP_CLTYPE_INVALID,
70 SP_CLTYPE_CLASS,
71 SP_CLTYPE_TEMPLATE_CLASS,
72 SP_CLTYPE_STRUCTURE,
73 SP_CLTYPE_UNION,
74 SP_CLTYPE_INTERFACE
75 };
76
77 // inheritance types
78 enum SP_INHERITANCE_TYPES
79 {
80 SP_INHERIT_VIRTUAL,
81 SP_INHERIT_PUBLIC,
82 SP_INHERIT_PRIVATE
83 };
84
85 // proprocessor definitions types (specific to C++ code)
86
87 enum SP_PREP_DEFINITION_TYPES
88 {
89 SP_PREP_DEF_DEFINE_SYMBOL,
90 SP_PREP_DEF_REDEFINE_SYMBOL,
91 SP_PREP_DEF_INCLUDE_FILE,
92 SP_PREP_DEF_OTHER
93 };
94
95 // common context types
96
97 #define SP_CTX_UNKNOWN 0x000
98 #define SP_CTX_FILE 0x001
99 #define SP_CTX_NAMESPACE 0x002
100 #define SP_CTX_CLASS 0x004
101 #define SP_CTX_TYPEDEF 0x008
102 #define SP_CTX_PREPROCESSOR 0x010
103 #define SP_CTX_ENUMERATION 0x020
104 #define SP_CTX_ATTRIBUTE 0x040
105 #define SP_CTX_OPERATION 0x080
106 #define SP_CTX_PARAMETER 0x100
107
108 // other (custom) context codes may be defined elsewere, however they should
109 // not clash with above codes for common type and also should not
110 // exceed 16-bits of in value
111
112 // masks all context types (up to 16 custom context can be defined)
113
114 #define SP_CTX_ANY 0xFFFF
115
116 class spComment;
117
118
119
120 #if defined( wxUSE_TEMPLATE_STL )
121
122 // context members
123 typedef vector<spContext*> MMemberListT;
124 // comments list
125 typedef vector<spComment*> MCommentListT;
126 // list of parameters
127 typedef vector<spParameter*> MParamListT;
128 // wxString list
129 typedef vector<wxString> StrListT;
130
131 #else
132
133 typedef spContext* spContextPtrT;
134 typedef spComment* spCommentPtrT;
135 typedef spParameter* spParameterPtrT;
136 typedef WXSTL_VECTOR_SHALLOW_COPY(spContextPtrT) MMemberListT;
137 typedef WXSTL_VECTOR_SHALLOW_COPY(spCommentPtrT) MCommentListT;
138 typedef WXSTL_VECTOR_SHALLOW_COPY(spParameterPtrT) MParamListT;
139 typedef WXSTL_VECTOR_SHALLOW_COPY(wxString) StrListT;
140
141 #endif
142 // base class for all visitors of source code contents
143
144 class spVisitor
145 {
146 protected:
147 bool mSiblingSkipped;
148 bool mChildSkipped;
149 int mContextMask;
150
151 spContext* mpCurCxt;
152
153 public:
154 // methods invoked by context
155
156 // method invoked from user's controling code
157 // to visit all nodes staring at the given context.
158 // Content is sorted if requrired, see comments
159 // spClass on sorting the class members
160
161 void VisitAll( spContext& atContext,
162 bool sortContent = true
163 );
164
165 // methods invoked by visitor
166
167 // goes to the next context in the outter scope
168 // NOTE:: should not be invoked more than once while
169 // visiting certain context
170
171 void SkipSiblings();
172
173 // prevents going down into the contexts contained by
174 // the current context
175 // NOTE:: the same as above
176
177 void SkipChildren();
178
179 // can be called only in from visiting procedure
180 void RemoveCurrentContext();
181
182 // method enables fast filtered traversal
183 // of source content, e.g. collecting only classes,
184 // or only global functions
185
186 // arg. context - can contain combination of contexts concatinated
187 // with bitwise OR, e.g. SP_CTX_CLASS | SP_CTX_NAMESPACE
188 //
189 // method can be invoked from the user's controling as well as
190 // from within the visting procedure
191
192 void SetFilter( int contextMask );
193
194 // methods should be implemneted by specific visitor:
195
196 // NOTE:: Do not confuse visiting with parsing, first
197 // the source is parsed, and than can be visited
198 // multiple times by variouse visitors (there can
199 // be more the one visitor visiting content at a time)
200
201 virtual void VisitFile( spFile& WXUNUSED(fl) ) {}
202
203 virtual void VisitNameSpace( spNameSpace& WXUNUSED(ns) ) {}
204
205 virtual void VisitClass( spClass& WXUNUSED(cl) ) {}
206
207 virtual void VisitEnumeration( spEnumeration& WXUNUSED(en) ) {}
208
209 virtual void VisitTypeDef( spTypeDef& WXUNUSED(td) ) {}
210
211 virtual void VisitPreprocessorLine( spPreprocessorLine& WXUNUSED(pd) ) {}
212
213 virtual void VisitAttribute( spAttribute& WXUNUSED(attr) ) {}
214
215 virtual void VisitOperation( spOperation& WXUNUSED(op) ) {}
216
217 virtual void VisitParameter( spParameter& WXUNUSED(param) ) {}
218
219 virtual void VisitCustomContext( spContext& WXUNUSED(ctx) ) {}
220 };
221
222 // stores one section of comments,
223 // multiple sections can be put to geather
224 // and attached to some context
225
226 class spComment
227 {
228 public:
229 wxString m_Text;
230 bool mIsMultiline; // multiline comments ar those with /**/'s
231
232 // true, if these was an empty empty
233 // line above single line comment
234
235 bool mStartsPar;
236
237 public:
238
239 bool IsMultiline() const;
240 bool StartsParagraph() const;
241
242 wxString& GetText();
243
244 // contstant version of GetText()
245 wxString GetText() const;
246 };
247
248 // abstract base class for common (to most languages) code
249 // contexts (constructs), e.g file, namespace, class, operation,
250 // etc
251
252 class spContext
253 {
254 protected:
255 // "linked" list of comments belonging to this context
256 MCommentListT mComments;
257
258 // NULL, if this is file context
259 MMemberListT mMembers;
260
261 // NULL, if this is top-most context
262 spContext* m_pParent;
263
264 // points to context object, where the this context
265 // was originally declared, meaning that this object
266 // is redeclaration (or if in the case of operation
267 // this context object most probably referres to the
268 // implemnetation in .cpp file for example)
269
270 // is NULL, if this object referres to the first occurence
271 // of the context
272
273 spContext* mpFirstOccurence;
274
275 // used, to avoid excessive sorting of context's agreggates
276 bool mAlreadySorted;
277
278 public:
279
280 // source line number, (-1) if unknown
281 int mSrcLineNo;
282
283 // offset of context in the source file, (-1) if unknown
284 int mSrcOffset;
285
286 // lentgh of the context in characters, (-1) if unknown
287 int mContextLength;
288
289 // source line number, in which this cotext ends, (-1) if unknown
290 int mLastScrLineNo;
291
292 // fields are valid, if the may contain other contexts nested inside
293 int mHeaderLength;
294 int mFooterLength;
295
296 // zero-based index of the first character of
297 // this context in the source line, (-1) if unknown
298 int mFirstCharPos;
299
300 // zero-based index of the first character of
301 // this context in the last source line of this context, (-1) if unknown
302 int mLastCharPos;
303
304 // see SRC_VISIBLITY_TYPES enumeration
305 int mVisibility;
306
307 // true, if context does not really exist in the source
308 // but was created by external tools (e.g. forward engineering)
309
310 bool mIsVirtualContext;
311 bool mVirtualContextHasChildren;
312
313 // body of the context in case (mIsVirtual == true)
314 wxString mVirtualContextBody;
315 wxString mVittualContextFooter;
316
317 // e.g. can be used by documentation generator to store
318 // reference to section object
319 void* mpUserData;
320
321 public:
322 // universal identifier of the context (e.g. class name)
323 wxString m_Name;
324
325 public:
326 // default constructor
327 spContext();
328
329 // automatically destorys all aggregated contexts
330 // (thus, it's enought to call destructor of root-context)
331 virtual ~spContext();
332
333 // see mUererData member;
334 void* GetUserData() { return mpUserData; }
335
336 // sets untyped pointer to user data
337 void SetUserData( void* pUserData )
338 { mpUserData = pUserData; }
339
340 // searches the whole context tree for the cotnexts
341 // which match given masks, pust results into lst array
342 void GetContextList( MMemberListT& lst, int contextMask );
343
344 // used by default visitor's implementation
345 bool IsSorted();
346
347 /*** forward/reverse ingineering fecilities ***/
348
349 bool PositionIsKnown();
350
351 bool IsVirtualContext();
352
353 bool VitualContextHasChildren();
354
355 void SetVirtualContextBody( const wxString& body,
356 bool hasChildren = false,
357 const wxString& footer = wxEmptyString );
358
359 wxString GetVirtualContextBody();
360 wxString GetFooterOfVirtualContextBody();
361
362 // can be overriden by top-level context classes
363 // to find-out ot the source-fragment of this
364 // context using it's position information
365 virtual wxString GetBody( spContext* pCtx = NULL );
366
367 virtual wxString GetHeader( spContext* pCtx = NULL );
368
369 // true, if there is at least one entry
370 // in the comment list of this context
371 bool HasComments();
372 MCommentListT& GetCommentList() { return mComments; }
373 const MCommentListT& GetCommentList() const { return mComments; }
374
375 // should be overriden, if the context supports sorting
376 // of it's members
377 virtual void SortMembers() {}
378
379 // returns identifier of this context
380 inline wxString& GetName() { return m_Name; }
381
382 // returns -1, if souce line # is unknow
383 inline int GetSourceLineNo() { return mSrcLineNo; }
384
385 // see comments on mpFirstOccurence member variable
386 bool IsFirstOccurence();
387 spContext* GetFirstOccurence();
388
389 // returns not-NULL value if this context
390 // is aggregated by another cotnext
391 spContext* GetOutterContext();
392
393 // perhaps more intuitive alias for `GetOutterContext()'
394 inline spContext* GetParent() { return m_pParent; }
395
396 bool HasOutterContext();
397
398 // add one aggregate (or child) into this context
399 void AddMember ( spContext* pMember );
400 MMemberListT& GetMembers();
401
402 // append comment to the comment list decribing
403 // this context
404 void AddComment( spComment* pComment );
405
406 // returns NULL, if the context with the given
407 // name and type is not contained by this context
408 // and it's children. Children's children are not
409 // searched recursivelly if searchSubMembers is false
410
411 spContext* FindContext( const wxString& identifier,
412 int contextType = SP_CTX_ANY,
413 bool searchSubMembers = true
414 );
415
416 // removes this context from it's parent
417 // (NOTE:: context should have an outter cotnext
418 // to when this method is called, otherwise removal
419 // will result assertion failure)
420 void RemoveThisContext();
421
422 // returns true, if this object is aggregated in the file
423 bool IsInFile();
424
425 // true, if outter context is a namespace
426 bool IsInNameSpace();
427
428 // true, if outter context is a class
429 bool IsInClass();
430
431 // true, if outter cotext is an operation (true for "spParameter"s)
432 bool IsInOperation();
433
434 // true if the context is public
435 bool IsPublic() const { return mVisibility == SP_VIS_PUBLIC; }
436
437 // NOTE:: method returns not the type of this object
438 // but the file/namespace/class/operation or file in which this
439 // attribute is contained. First, check for the type of
440 // context using the above method.
441
442 // Requiering container which does not exist, will result
443 // in assertion failure
444
445 spClass& GetClass();
446 spFile& GetFile();
447 spNameSpace& GetNameSpace();
448 spOperation& GetOperation();
449
450 // each new context should override this method
451 // to return it's specific type
452 virtual int GetContextType() const { return SP_CTX_UNKNOWN; }
453
454 // perhaps more intuitive short-cut
455 inline int GetType() { return GetContextType(); }
456
457 // cast this context to the desired type - returns NULL if type is wrong
458 spAttribute *CastToAttribute()
459 {
460 return GetContextType() == SP_CTX_ATTRIBUTE ? (spAttribute *)this
461 : NULL;
462 }
463
464 // derived classes override this to invoke VisitXXX method
465 // which corresponds to the class of specific context,
466 // - this is what the "Visitor" pattern told us ^)
467
468 // if method is not overriden, then it's probably user-defined
469 // custom context
470
471 virtual void AcceptVisitor( spVisitor& visitor )
472
473 { visitor.VisitCustomContext( *this ); };
474
475 // called by visitors, to remove given subcontext
476 // of this context object
477 void RemoveChild( spContext* pChild );
478
479 void RemoveChildren();
480
481 spContext* GetEnclosingContext( int mask = SP_CTX_ANY );
482
483 #ifdef __WXDEBUG__
484 virtual void Dump(const wxString& indent) const;
485 #endif // __WXDEBUG__
486
487 DECLARE_DUMP
488 };
489
490 // stores information about single argument of operation
491
492 class spParameter : public spContext
493 {
494 public:
495 // type of argument (parameter)
496 wxString m_Type;
497
498 // "stringified" initial value
499 wxString m_InitVal;
500
501 public:
502 virtual int GetContextType() const { return SP_CTX_PARAMETER; }
503
504 virtual void AcceptVisitor( spVisitor& visitor )
505 { visitor.VisitParameter( *this ); }
506
507 DECLARE_DUMP
508 };
509
510
511 // stores information about member(or global) variable
512
513 class spAttribute : public spContext
514 {
515 public:
516 // type of the attribute
517 wxString m_Type;
518
519 // it's initial value
520 wxString m_InitVal;
521
522 // constantness
523 bool mIsConstant;
524 public:
525
526 virtual int GetContextType() const { return SP_CTX_ATTRIBUTE; }
527
528 virtual void AcceptVisitor( spVisitor& visitor )
529 { visitor.VisitAttribute( *this ); }
530
531 DECLARE_DUMP
532 };
533
534 class spOperation : public spContext
535 {
536 public:
537 // type of return value
538 wxString m_RetType;
539
540 // argument list
541 //MParamListT mParams;
542
543 // true, if operation does not modify
544 // the content of the object
545 bool mIsConstant;
546
547 // flag, specific to C++
548 bool mIsVirtual;
549
550 // true, if definition follows the declaration immediatelly
551 bool mHasDefinition;
552
553 // scope if any (e.g. MyClass::MyFunction(), scope stirng is "MyClass" )
554 // usually found along with implementation of the method, which is now skipped
555
556 wxString mScope;
557
558 public:
559 spOperation();
560
561 // returns full declaration of the operations
562 // (ret val., identifier, arg. list),
563
564 // arguments are marked up with italic,
565 // default values marked up with bold-italic,
566 // all the rest is marked as bold
567
568 // NOTE:: this method may be overriden by class
569 // specific to concrete parser, to provide
570 // language-dependent reperesnetation of
571 // operation and it's argumetn list
572 //
573 // the default implementation outputs name in
574 // C++/Java syntax
575
576 virtual wxString GetFullName(MarkupTagsT tags);
577
578 virtual int GetContextType() const { return SP_CTX_OPERATION; }
579
580 virtual void AcceptVisitor( spVisitor& visitor )
581 { visitor.VisitOperation( *this ); }
582
583 DECLARE_DUMP
584 };
585
586 // stores infromation about preprocessor directive
587
588 class spPreprocessorLine : public spContext
589 {
590
591 public:
592
593 // prepocessor statement including '#' and
594 // attached multiple lines with '\' character
595 wxString m_Line;
596
597 int mDefType; // see SP_PREP_DEFINITION_TYPES enumeration
598
599 public:
600
601 virtual int GetContextType() const { return SP_CTX_PREPROCESSOR; }
602
603 virtual int GetStatementType() const { return mDefType; }
604
605 wxString CPP_GetIncludedFileNeme() const;
606
607 virtual void AcceptVisitor( spVisitor& visitor )
608 { visitor.VisitPreprocessorLine( *this ); }
609
610 DECLARE_DUMP
611 };
612
613 // stores information about the class
614
615 class spClass : public spContext
616 {
617 public:
618 // list of superclasses/interfaces
619 StrListT m_SuperClassNames;
620
621 // see SP_CLASS_TYPES enumeration
622 int mClassSubType;
623
624 // see SP_INHERITANCE_TYPES enumeration
625 int mInheritanceType;
626
627 // valid if mClassSubType is SP_CLTYPE_TEMPLATE_CLASS
628 wxString mTemplateTypes;
629
630 // true, if it's and interface of abstract base class
631 bool mIsAbstract;
632
633 public:
634 // sorts class members in the following order:
635 //
636 // (by "privacy level" - first private, than protected, public)
637 //
638 // within above set
639 //
640 // (by member type - attributes first, than methods, nested classes)
641 //
642 // within above set
643 //
644 // (by identifier of the member)
645
646 virtual void SortMembers();
647
648 virtual int GetContextType() const { return SP_CTX_CLASS; }
649
650 virtual void AcceptVisitor( spVisitor& visitor )
651 { visitor.VisitClass( *this ); }
652
653 DECLARE_DUMP
654 };
655
656 // stores information about enum statement
657
658 class spEnumeration : public spContext
659 {
660 public:
661 wxString m_EnumContent; // full-text content of enumeration
662
663 public:
664 virtual int GetContextType() const { return SP_CTX_ENUMERATION; }
665
666 virtual void AcceptVisitor( spVisitor& visitor )
667 { visitor.VisitEnumeration( *this ); }
668
669 DECLARE_DUMP
670 };
671
672 class spTypeDef : public spContext
673 {
674 public:
675 // the original type which is redefined
676 // by this type definition
677 wxString m_OriginalType;
678
679 public:
680 virtual int GetContextType() const { return SP_CTX_TYPEDEF; }
681
682 virtual void AcceptVisitor( spVisitor& visitor )
683 { visitor.VisitTypeDef( *this ); }
684
685 DECLARE_DUMP
686 };
687
688 // NOTE:: files context may be put to other
689 // file context, resulting in a collection
690 // of parsed file contexts, with a virtual "superfile"
691
692 class spFile : public spContext
693 {
694 public:
695 // since file name cannot be determined from
696 // source code, filling in this field is optional
697 wxString m_FileName;
698
699 public:
700 virtual int GetContextType() const { return SP_CTX_FILE; }
701
702 virtual void AcceptVisitor( spVisitor& visitor )
703 { visitor.VisitFile( *this ); }
704
705 DECLARE_DUMP
706 };
707
708 //TODO:: comments.
709
710 class SourceParserPlugin
711 {
712 public:
713 virtual bool CanUnderstandContext( char* cur, char* end, spContext* pOuttterCtx ) = 0;
714 virtual void ParseContext( char* start, char*& cur, char* end, spContext* pOuttterCtx ) = 0;
715 };
716
717 // abstract interface for source parsers
718 // which can output parsing results in the
719 // form of context-tree, where each node
720 // should be derivative of spContext, (see
721 // above classes)
722
723 class SourceParserBase
724 {
725 private:
726 // auto-resizing file buffer, created in ParseFile()
727 // to reuse large heap block for multiple parsings
728
729 char* mpFileBuf;
730 int mFileBufSz;
731
732 protected:
733 SourceParserPlugin* mpPlugin;
734
735 protected:
736 // value is set in the derived parser classes
737 int mParserStatus;
738
739 public:
740 SourceParserBase();
741 virtual ~SourceParserBase();
742
743 // loads entier source file(as text) into memory,
744 // and passes it's contents to ParseAll() method,
745 // memory occupied by source text is released after
746 // parsing is done
747 //
748 // (NOTE:: this is the default implementation),
749
750 virtual spFile* ParseFile( const char* fname );
751
752 // should returns the root-node of the created context tree
753 // (user is responsible for releasing it from the heep)
754 // "end" should point to the (last character + 1) of the
755 // source text area
756
757 virtual spFile* Parse( char* start, char* end ) = 0;
758
759 // returns parser "status word" (specific to concrete parser)
760 int GetParserStatus() { return mParserStatus; }
761
762 void SetPlugin( SourceParserPlugin* pPlugin );
763 };
764
765 #endif