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