1 /////////////////////////////////////////////////////////////////////////////
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
10 // Copyright: (c) Aleskandars Gluchovas
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
14 #ifndef __SRCPARSER_G__
15 #define __SRCPARSER_G__
17 #if defined( wxUSE_TEMPLATE_STL )
30 #include "wx/string.h"
36 // FOR NOW:: quick n' dirty:
37 #define string wxString
42 #include "markup.h" // markup tags used in spOperator::GetFullName()
44 // these methods are used for debugging only and disappear in the release build
46 #define DECLARE_DUMP virtual void DumpThis(const wxString& indent) const;
51 // context class list in "inside-out" order :
60 class spPreprocessorLine
;
65 // source context visibilities
66 enum SRC_VISIBLITY_TYPES
78 SP_CLTYPE_TEMPLATE_CLASS
,
85 enum SP_INHERITANCE_TYPES
92 // proprocessor definitions types (specific to C++ code)
94 enum SP_PREP_DEFINITION_TYPES
96 SP_PREP_DEF_DEFINE_SYMBOL
,
97 SP_PREP_DEF_REDEFINE_SYMBOL
,
98 SP_PREP_DEF_INCLUDE_FILE
,
102 // common context types
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
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
119 // masks all context types (up to 16 custom context can be defined)
121 #define SP_CTX_ANY 0xFFFF
127 #if defined( wxUSE_TEMPLATE_STL )
130 typedef vector
<spContext
*> MMemberListT
;
132 typedef vector
<spComment
*> MCommentListT
;
133 // list of parameters
134 typedef vector
<spParameter
*> MParamListT
;
136 typedef vector
<wxString
> StrListT
;
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(wxString
) StrListT
;
149 // base class for all visitors of source code contents
154 bool mSiblingSkipped
;
161 // methods invoked by context
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
168 void VisitAll( spContext
& atContext
,
169 bool sortContent
= true
172 // methods invoked by visitor
174 // goes to the next context in the outter scope
175 // NOTE:: should not be invoked more than once while
176 // visiting certain context
180 // prevents going down into the contexts contained by
181 // the current context
182 // NOTE:: the same as above
186 // can be called only in from visiting procedure
187 void RemoveCurrentContext();
189 // method enables fast filtered traversal
190 // of source content, e.g. collecting only classes,
191 // or only global functions
193 // arg. context - can contain combination of contexts concatinated
194 // with bitwise OR, e.g. SP_CTX_CLASS | SP_CTX_NAMESPACE
196 // method can be invoked from the user's controling as well as
197 // from within the visting procedure
199 void SetFilter( int contextMask
);
201 // methods should be implemneted by specific visitor:
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)
208 virtual void VisitFile( spFile
& WXUNUSED(fl
) ) {}
210 virtual void VisitNameSpace( spNameSpace
& WXUNUSED(ns
) ) {}
212 virtual void VisitClass( spClass
& WXUNUSED(cl
) ) {}
214 virtual void VisitEnumeration( spEnumeration
& WXUNUSED(en
) ) {}
216 virtual void VisitTypeDef( spTypeDef
& WXUNUSED(td
) ) {}
218 virtual void VisitPreprocessorLine( spPreprocessorLine
& WXUNUSED(pd
) ) {}
220 virtual void VisitAttribute( spAttribute
& WXUNUSED(attr
) ) {}
222 virtual void VisitOperation( spOperation
& WXUNUSED(op
) ) {}
224 virtual void VisitParameter( spParameter
& WXUNUSED(param
) ) {}
226 virtual void VisitCustomContext( spContext
& WXUNUSED(ctx
) ) {}
229 // stores one section of comments,
230 // multiple sections can be put to geather
231 // and attached to some context
237 bool mIsMultiline
; // multiline comments ar those with /**/'s
239 // true, if these was an empty empty
240 // line above single line comment
246 bool IsMultiline() const;
247 bool StartsParagraph() const;
251 // contstant version of GetText()
252 wxString
GetText() const;
255 // abstract base class for common (to most languages) code
256 // contexts (constructs), e.g file, namespace, class, operation,
262 // "linked" list of comments belonging to this context
263 MCommentListT mComments
;
265 // NULL, if this is file context
266 MMemberListT mMembers
;
268 // NULL, if this is top-most context
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)
277 // is NULL, if this object referres to the first occurence
280 spContext
* mpFirstOccurence
;
282 // used, to avoid excessive sorting of context's agreggates
287 // source line number, (-1) if unknown
290 // offset of context in the source file, (-1) if unknown
293 // lentgh of the context in characters, (-1) if unknown
296 // source line number, in which this cotext ends, (-1) if unknown
299 // fields are valid, if the may contain other contexts nested inside
303 // zero-based index of the first character of
304 // this context in the source line, (-1) if unknown
307 // zero-based index of the first character of
308 // this context in the last source line of this context, (-1) if unknown
311 // see SRC_VISIBLITY_TYPES enumeration
314 // true, if context does not really exist in the source
315 // but was created by external tools (e.g. forward engineering)
317 bool mIsVirtualContext
;
318 bool mVirtualContextHasChildren
;
320 // body of the context in case (mIsVirtual == true)
321 string mVirtualContextBody
;
322 string mVittualContextFooter
;
324 // e.g. can be used by documentation generator to store
325 // reference to section object
329 // universal identifier of the context (e.g. class name)
333 // default constructor
336 // automatically destorys all aggregated contexts
337 // (thus, it's enought to call destructor of root-context)
338 virtual ~spContext();
340 // see mUererData member;
341 void* GetUserData() { return mpUserData
; }
343 // sets untyped pointer to user data
344 void SetUserData( void* pUserData
)
345 { mpUserData
= pUserData
; }
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
);
351 // used by default visitor's implementation
354 /*** forward/reverse ingineering fecilities ***/
356 bool PositionIsKnown();
358 bool IsVirtualContext();
360 bool VitualContextHasChildren();
362 void SetVirtualContextBody( const string
& body
,
363 bool hasChildren
= false,
364 const string
& footer
= wxEmptyString
);
366 string
GetVirtualContextBody();
367 string
GetFooterOfVirtualContextBody();
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 wxString
GetBody( spContext
* pCtx
= NULL
);
374 virtual wxString
GetHeader( spContext
* pCtx
= NULL
);
376 // true, if there is at least one entry
377 // in the comment list of this context
379 MCommentListT
& GetCommentList() { return mComments
; }
380 const MCommentListT
& GetCommentList() const { return mComments
; }
382 // should be overriden, if the context supports sorting
384 virtual void SortMembers() {}
386 // returns identifier of this context
387 inline wxString
& GetName() { return m_Name
; }
389 // returns -1, if souce line # is unknow
390 inline int GetSourceLineNo() { return mSrcLineNo
; }
392 // see comments on mpFirstOccurence member variable
393 bool IsFirstOccurence();
394 spContext
* GetFirstOccurence();
396 // returns not-NULL value if this context
397 // is aggregated by another cotnext
398 spContext
* GetOutterContext();
400 // perhaps more intuitive alias for `GetOutterContext()'
401 inline spContext
* GetParent() { return mpParent
; }
403 bool HasOutterContext();
405 // add one aggregate (or child) into this context
406 void AddMember ( spContext
* pMember
);
407 MMemberListT
& GetMembers();
409 // append comment to the comment list decribing
411 void AddComment( spComment
* pComment
);
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
418 spContext
* FindContext( const string
& identifier
,
419 int contextType
= SP_CTX_ANY
,
420 bool searchSubMembers
= true
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();
429 // returns true, if this object is aggregated in the file
432 // true, if outter context is a namespace
433 bool IsInNameSpace();
435 // true, if outter context is a class
438 // true, if outter cotext is an operation (true for "spParameter"s)
439 bool IsInOperation();
441 // true if the context is public
442 bool IsPublic() const { return mVisibility
== SP_VIS_PUBLIC
; }
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.
449 // Requiering container which does not exist, will result
450 // in assertion failure
454 spNameSpace
& GetNameSpace();
455 spOperation
& GetOperation();
457 // each new context should override this method
458 // to return it's specific type
459 virtual int GetContextType() const { return SP_CTX_UNKNOWN
; }
461 // perhaps more intuitive short-cut
462 inline int GetType() { return GetContextType(); }
464 // cast this context to the desired type - returns NULL if type is wrong
465 spAttribute
*CastToAttribute()
467 return GetContextType() == SP_CTX_ATTRIBUTE
? (spAttribute
*)this
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 ^)
475 // if method is not overriden, then it's probably user-defined
478 virtual void AcceptVisitor( spVisitor
& visitor
)
480 { visitor
.VisitCustomContext( *this ); };
482 // called by visitors, to remove given subcontext
483 // of this context object
484 void RemoveChild( spContext
* pChild
);
486 void RemoveChildren();
488 spContext
* GetEnclosingContext( int mask
= SP_CTX_ANY
);
491 virtual void Dump(const wxString
& indent
) const;
492 #endif // __WXDEBUG__
497 // stores information about single argument of operation
499 class spParameter
: public spContext
502 // type of argument (parameter)
505 // "stringified" initial value
509 virtual int GetContextType() const { return SP_CTX_PARAMETER
; }
511 virtual void AcceptVisitor( spVisitor
& visitor
)
512 { visitor
.VisitParameter( *this ); }
518 // stores information about member(or global) variable
520 class spAttribute
: public spContext
523 // type of the attribute
526 // it's initial value
533 virtual int GetContextType() const { return SP_CTX_ATTRIBUTE
; }
535 virtual void AcceptVisitor( spVisitor
& visitor
)
536 { visitor
.VisitAttribute( *this ); }
541 class spOperation
: public spContext
544 // type of return value
548 //MParamListT mParams;
550 // true, if operation does not modify
551 // the content of the object
554 // flag, specific to C++
557 // true, if definition follows the declaration immediatelly
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
568 // returns full declaration of the operations
569 // (ret val., identifier, arg. list),
571 // arguments are marked up with italic,
572 // default values marked up with bold-italic,
573 // all the rest is marked as bold
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
580 // the default implementation outputs name in
583 virtual wxString
GetFullName(MarkupTagsT tags
);
585 virtual int GetContextType() const { return SP_CTX_OPERATION
; }
587 virtual void AcceptVisitor( spVisitor
& visitor
)
588 { visitor
.VisitOperation( *this ); }
593 // stores infromation about preprocessor directive
595 class spPreprocessorLine
: public spContext
600 // prepocessor statement including '#' and
601 // attached multiple lines with '\' character
604 int mDefType
; // see SP_PREP_DEFINITION_TYPES enumeration
608 virtual int GetContextType() const { return SP_CTX_PREPROCESSOR
; }
610 virtual int GetStatementType() const { return mDefType
; }
612 wxString
CPP_GetIncludedFileNeme() const;
614 virtual void AcceptVisitor( spVisitor
& visitor
)
615 { visitor
.VisitPreprocessorLine( *this ); }
620 // stores information about the class
622 class spClass
: public spContext
625 // list of superclasses/interfaces
626 StrListT m_SuperClassNames
;
628 // see SP_CLASS_TYPES enumeration
631 // see SP_INHERITANCE_TYPES enumeration
632 int mInheritanceType
;
634 // valid if mClassSubType is SP_CLTYPE_TEMPLATE_CLASS
635 string mTemplateTypes
;
637 // true, if it's and interface of abstract base class
641 // sorts class members in the following order:
643 // (by "privacy level" - first private, than protected, public)
647 // (by member type - attributes first, than methods, nested classes)
651 // (by identifier of the member)
653 virtual void SortMembers();
655 virtual int GetContextType() const { return SP_CTX_CLASS
; }
657 virtual void AcceptVisitor( spVisitor
& visitor
)
658 { visitor
.VisitClass( *this ); }
663 // stores information about enum statement
665 class spEnumeration
: public spContext
668 wxString m_EnumContent
; // full-text content of enumeration
671 virtual int GetContextType() const { return SP_CTX_ENUMERATION
; }
673 virtual void AcceptVisitor( spVisitor
& visitor
)
674 { visitor
.VisitEnumeration( *this ); }
679 class spTypeDef
: public spContext
682 // the original type which is redefined
683 // by this type definition
684 wxString m_OriginalType
;
687 virtual int GetContextType() const { return SP_CTX_TYPEDEF
; }
689 virtual void AcceptVisitor( spVisitor
& visitor
)
690 { visitor
.VisitTypeDef( *this ); }
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"
699 class spFile
: public spContext
702 // since file name cannot be determined from
703 // source code, filling in this field is optional
707 virtual int GetContextType() const { return SP_CTX_FILE
; }
709 virtual void AcceptVisitor( spVisitor
& visitor
)
710 { visitor
.VisitFile( *this ); }
717 class SourceParserPlugin
720 virtual bool CanUnderstandContext( char* cur
, char* end
, spContext
* pOuttterCtx
) = 0;
721 virtual void ParseContext( char* start
, char*& cur
, char* end
, spContext
* pOuttterCtx
) = 0;
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
730 class SourceParserBase
733 // auto-resizing file buffer, created in ParseFile()
734 // to reuse large heap block for multiple parsings
740 SourceParserPlugin
* mpPlugin
;
743 // value is set in the derived parser classes
748 virtual ~SourceParserBase();
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
755 // (NOTE:: this is the default implementation),
757 virtual spFile
* ParseFile( const char* fname
);
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
764 virtual spFile
* Parse( char* start
, char* end
) = 0;
766 // returns parser "status word" (specific to concrete parser)
767 int GetParserStatus() { return mParserStatus
; }
769 void SetPlugin( SourceParserPlugin
* pPlugin
);