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"
35 #include "markup.h" // markup tags used in spOperator::GetFullName()
37 // these methods are used for debugging only and disappear in the release build
39 #define DECLARE_DUMP virtual void DumpThis(const wxString& indent) const;
44 // context class list in "inside-out" order :
53 class spPreprocessorLine
;
58 // source context visibilities
59 enum SRC_VISIBLITY_TYPES
71 SP_CLTYPE_TEMPLATE_CLASS
,
78 enum SP_INHERITANCE_TYPES
85 // proprocessor definitions types (specific to C++ code)
87 enum SP_PREP_DEFINITION_TYPES
89 SP_PREP_DEF_DEFINE_SYMBOL
,
90 SP_PREP_DEF_REDEFINE_SYMBOL
,
91 SP_PREP_DEF_INCLUDE_FILE
,
95 // common context types
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
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
112 // masks all context types (up to 16 custom context can be defined)
114 #define SP_CTX_ANY 0xFFFF
120 #if defined( wxUSE_TEMPLATE_STL )
123 typedef vector
<spContext
*> MMemberListT
;
125 typedef vector
<spComment
*> MCommentListT
;
126 // list of parameters
127 typedef vector
<spParameter
*> MParamListT
;
129 typedef vector
<wxString
> StrListT
;
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
;
142 // base class for all visitors of source code contents
147 bool mSiblingSkipped
;
154 // methods invoked by context
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
161 void VisitAll( spContext
& atContext
,
162 bool sortContent
= true
165 // methods invoked by visitor
167 // goes to the next context in the outter scope
168 // NOTE:: should not be invoked more than once while
169 // visiting certain context
173 // prevents going down into the contexts contained by
174 // the current context
175 // NOTE:: the same as above
179 // can be called only in from visiting procedure
180 void RemoveCurrentContext();
182 // method enables fast filtered traversal
183 // of source content, e.g. collecting only classes,
184 // or only global functions
186 // arg. context - can contain combination of contexts concatinated
187 // with bitwise OR, e.g. SP_CTX_CLASS | SP_CTX_NAMESPACE
189 // method can be invoked from the user's controling as well as
190 // from within the visting procedure
192 void SetFilter( int contextMask
);
194 // methods should be implemneted by specific visitor:
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)
201 virtual void VisitFile( spFile
& WXUNUSED(fl
) ) {}
203 virtual void VisitNameSpace( spNameSpace
& WXUNUSED(ns
) ) {}
205 virtual void VisitClass( spClass
& WXUNUSED(cl
) ) {}
207 virtual void VisitEnumeration( spEnumeration
& WXUNUSED(en
) ) {}
209 virtual void VisitTypeDef( spTypeDef
& WXUNUSED(td
) ) {}
211 virtual void VisitPreprocessorLine( spPreprocessorLine
& WXUNUSED(pd
) ) {}
213 virtual void VisitAttribute( spAttribute
& WXUNUSED(attr
) ) {}
215 virtual void VisitOperation( spOperation
& WXUNUSED(op
) ) {}
217 virtual void VisitParameter( spParameter
& WXUNUSED(param
) ) {}
219 virtual void VisitCustomContext( spContext
& WXUNUSED(ctx
) ) {}
222 // stores one section of comments,
223 // multiple sections can be put to geather
224 // and attached to some context
230 bool mIsMultiline
; // multiline comments ar those with /**/'s
232 // true, if these was an empty empty
233 // line above single line comment
239 bool IsMultiline() const;
240 bool StartsParagraph() const;
244 // contstant version of GetText()
245 wxString
GetText() const;
248 // abstract base class for common (to most languages) code
249 // contexts (constructs), e.g file, namespace, class, operation,
255 // "linked" list of comments belonging to this context
256 MCommentListT mComments
;
258 // NULL, if this is file context
259 MMemberListT mMembers
;
261 // NULL, if this is top-most context
262 spContext
* m_pParent
;
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)
270 // is NULL, if this object referres to the first occurence
273 spContext
* mpFirstOccurence
;
275 // used, to avoid excessive sorting of context's agreggates
280 // source line number, (-1) if unknown
283 // offset of context in the source file, (-1) if unknown
286 // lentgh of the context in characters, (-1) if unknown
289 // source line number, in which this cotext ends, (-1) if unknown
292 // fields are valid, if the may contain other contexts nested inside
296 // zero-based index of the first character of
297 // this context in the source line, (-1) if unknown
300 // zero-based index of the first character of
301 // this context in the last source line of this context, (-1) if unknown
304 // see SRC_VISIBLITY_TYPES enumeration
307 // true, if context does not really exist in the source
308 // but was created by external tools (e.g. forward engineering)
310 bool mIsVirtualContext
;
311 bool mVirtualContextHasChildren
;
313 // body of the context in case (mIsVirtual == true)
314 wxString mVirtualContextBody
;
315 wxString mVittualContextFooter
;
317 // e.g. can be used by documentation generator to store
318 // reference to section object
322 // universal identifier of the context (e.g. class name)
326 // default constructor
329 // automatically destorys all aggregated contexts
330 // (thus, it's enought to call destructor of root-context)
331 virtual ~spContext();
333 // see mUererData member;
334 void* GetUserData() { return mpUserData
; }
336 // sets untyped pointer to user data
337 void SetUserData( void* pUserData
)
338 { mpUserData
= pUserData
; }
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
);
344 // used by default visitor's implementation
347 /*** forward/reverse ingineering fecilities ***/
349 bool PositionIsKnown();
351 bool IsVirtualContext();
353 bool VitualContextHasChildren();
355 void SetVirtualContextBody( const wxString
& body
,
356 bool hasChildren
= false,
357 const wxString
& footer
= wxEmptyString
);
359 wxString
GetVirtualContextBody();
360 wxString
GetFooterOfVirtualContextBody();
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
);
367 virtual wxString
GetHeader( spContext
* pCtx
= NULL
);
369 // true, if there is at least one entry
370 // in the comment list of this context
372 MCommentListT
& GetCommentList() { return mComments
; }
373 const MCommentListT
& GetCommentList() const { return mComments
; }
375 // should be overriden, if the context supports sorting
377 virtual void SortMembers() {}
379 // returns identifier of this context
380 inline wxString
& GetName() { return m_Name
; }
382 // returns -1, if souce line # is unknow
383 inline int GetSourceLineNo() { return mSrcLineNo
; }
385 // see comments on mpFirstOccurence member variable
386 bool IsFirstOccurence();
387 spContext
* GetFirstOccurence();
389 // returns not-NULL value if this context
390 // is aggregated by another cotnext
391 spContext
* GetOutterContext();
393 // perhaps more intuitive alias for `GetOutterContext()'
394 inline spContext
* GetParent() { return m_pParent
; }
396 bool HasOutterContext();
398 // add one aggregate (or child) into this context
399 void AddMember ( spContext
* pMember
);
400 MMemberListT
& GetMembers();
402 // append comment to the comment list decribing
404 void AddComment( spComment
* pComment
);
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
411 spContext
* FindContext( const wxString
& identifier
,
412 int contextType
= SP_CTX_ANY
,
413 bool searchSubMembers
= true
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();
422 // returns true, if this object is aggregated in the file
425 // true, if outter context is a namespace
426 bool IsInNameSpace();
428 // true, if outter context is a class
431 // true, if outter cotext is an operation (true for "spParameter"s)
432 bool IsInOperation();
434 // true if the context is public
435 bool IsPublic() const { return mVisibility
== SP_VIS_PUBLIC
; }
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.
442 // Requiering container which does not exist, will result
443 // in assertion failure
447 spNameSpace
& GetNameSpace();
448 spOperation
& GetOperation();
450 // each new context should override this method
451 // to return it's specific type
452 virtual int GetContextType() const { return SP_CTX_UNKNOWN
; }
454 // perhaps more intuitive short-cut
455 inline int GetType() { return GetContextType(); }
457 // cast this context to the desired type - returns NULL if type is wrong
458 spAttribute
*CastToAttribute()
460 return GetContextType() == SP_CTX_ATTRIBUTE
? (spAttribute
*)this
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 ^)
468 // if method is not overriden, then it's probably user-defined
471 virtual void AcceptVisitor( spVisitor
& visitor
)
473 { visitor
.VisitCustomContext( *this ); };
475 // called by visitors, to remove given subcontext
476 // of this context object
477 void RemoveChild( spContext
* pChild
);
479 void RemoveChildren();
481 spContext
* GetEnclosingContext( int mask
= SP_CTX_ANY
);
484 virtual void Dump(const wxString
& indent
) const;
485 #endif // __WXDEBUG__
490 // stores information about single argument of operation
492 class spParameter
: public spContext
495 // type of argument (parameter)
498 // "stringified" initial value
502 virtual int GetContextType() const { return SP_CTX_PARAMETER
; }
504 virtual void AcceptVisitor( spVisitor
& visitor
)
505 { visitor
.VisitParameter( *this ); }
511 // stores information about member(or global) variable
513 class spAttribute
: public spContext
516 // type of the attribute
519 // it's initial value
526 virtual int GetContextType() const { return SP_CTX_ATTRIBUTE
; }
528 virtual void AcceptVisitor( spVisitor
& visitor
)
529 { visitor
.VisitAttribute( *this ); }
534 class spOperation
: public spContext
537 // type of return value
541 //MParamListT mParams;
543 // true, if operation does not modify
544 // the content of the object
547 // flag, specific to C++
550 // true, if definition follows the declaration immediatelly
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
561 // returns full declaration of the operations
562 // (ret val., identifier, arg. list),
564 // arguments are marked up with italic,
565 // default values marked up with bold-italic,
566 // all the rest is marked as bold
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
573 // the default implementation outputs name in
576 virtual wxString
GetFullName(MarkupTagsT tags
);
578 virtual int GetContextType() const { return SP_CTX_OPERATION
; }
580 virtual void AcceptVisitor( spVisitor
& visitor
)
581 { visitor
.VisitOperation( *this ); }
586 // stores infromation about preprocessor directive
588 class spPreprocessorLine
: public spContext
593 // prepocessor statement including '#' and
594 // attached multiple lines with '\' character
597 int mDefType
; // see SP_PREP_DEFINITION_TYPES enumeration
601 virtual int GetContextType() const { return SP_CTX_PREPROCESSOR
; }
603 virtual int GetStatementType() const { return mDefType
; }
605 wxString
CPP_GetIncludedFileNeme() const;
607 virtual void AcceptVisitor( spVisitor
& visitor
)
608 { visitor
.VisitPreprocessorLine( *this ); }
613 // stores information about the class
615 class spClass
: public spContext
618 // list of superclasses/interfaces
619 StrListT m_SuperClassNames
;
621 // see SP_CLASS_TYPES enumeration
624 // see SP_INHERITANCE_TYPES enumeration
625 int mInheritanceType
;
627 // valid if mClassSubType is SP_CLTYPE_TEMPLATE_CLASS
628 wxString mTemplateTypes
;
630 // true, if it's and interface of abstract base class
634 // sorts class members in the following order:
636 // (by "privacy level" - first private, than protected, public)
640 // (by member type - attributes first, than methods, nested classes)
644 // (by identifier of the member)
646 virtual void SortMembers();
648 virtual int GetContextType() const { return SP_CTX_CLASS
; }
650 virtual void AcceptVisitor( spVisitor
& visitor
)
651 { visitor
.VisitClass( *this ); }
656 // stores information about enum statement
658 class spEnumeration
: public spContext
661 wxString m_EnumContent
; // full-text content of enumeration
664 virtual int GetContextType() const { return SP_CTX_ENUMERATION
; }
666 virtual void AcceptVisitor( spVisitor
& visitor
)
667 { visitor
.VisitEnumeration( *this ); }
672 class spTypeDef
: public spContext
675 // the original type which is redefined
676 // by this type definition
677 wxString m_OriginalType
;
680 virtual int GetContextType() const { return SP_CTX_TYPEDEF
; }
682 virtual void AcceptVisitor( spVisitor
& visitor
)
683 { visitor
.VisitTypeDef( *this ); }
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"
692 class spFile
: public spContext
695 // since file name cannot be determined from
696 // source code, filling in this field is optional
700 virtual int GetContextType() const { return SP_CTX_FILE
; }
702 virtual void AcceptVisitor( spVisitor
& visitor
)
703 { visitor
.VisitFile( *this ); }
710 class SourceParserPlugin
713 virtual bool CanUnderstandContext( char* cur
, char* end
, spContext
* pOuttterCtx
) = 0;
714 virtual void ParseContext( char* start
, char*& cur
, char* end
, spContext
* pOuttterCtx
) = 0;
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
723 class SourceParserBase
726 // auto-resizing file buffer, created in ParseFile()
727 // to reuse large heap block for multiple parsings
733 SourceParserPlugin
* mpPlugin
;
736 // value is set in the derived parser classes
741 virtual ~SourceParserBase();
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
748 // (NOTE:: this is the default implementation),
750 virtual spFile
* ParseFile( const char* fname
);
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
757 virtual spFile
* Parse( char* start
, char* end
) = 0;
759 // returns parser "status word" (specific to concrete parser)
760 int GetParserStatus() { return mParserStatus
; }
762 void SetPlugin( SourceParserPlugin
* pPlugin
);