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