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