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
) ) {}
221 virtual ~spVisitor() { }
224 // stores one section of comments,
225 // multiple sections can be put to geather
226 // and attached to some context
232 bool mIsMultiline
; // multiline comments ar those with /**/'s
234 // true, if these was an empty empty
235 // line above single line comment
241 bool IsMultiline() const;
242 bool StartsParagraph() const;
246 // contstant version of GetText()
247 wxString
GetText() const;
250 // abstract base class for common (to most languages) code
251 // contexts (constructs), e.g file, namespace, class, operation,
257 // "linked" list of comments belonging to this context
258 MCommentListT mComments
;
260 // NULL, if this is file context
261 MMemberListT mMembers
;
263 // NULL, if this is top-most context
264 spContext
* m_pParent
;
266 // points to context object, where the this context
267 // was originally declared, meaning that this object
268 // is redeclaration (or if in the case of operation
269 // this context object most probably referres to the
270 // implemnetation in .cpp file for example)
272 // is NULL, if this object referres to the first occurence
275 spContext
* mpFirstOccurence
;
277 // used, to avoid excessive sorting of context's agreggates
282 // source line number, (-1) if unknown
285 // offset of context in the source file, (-1) if unknown
288 // lentgh of the context in characters, (-1) if unknown
291 // source line number, in which this cotext ends, (-1) if unknown
294 // fields are valid, if the may contain other contexts nested inside
298 // zero-based index of the first character of
299 // this context in the source line, (-1) if unknown
302 // zero-based index of the first character of
303 // this context in the last source line of this context, (-1) if unknown
306 // see SRC_VISIBLITY_TYPES enumeration
309 // true, if context does not really exist in the source
310 // but was created by external tools (e.g. forward engineering)
312 bool mIsVirtualContext
;
313 bool mVirtualContextHasChildren
;
315 // body of the context in case (mIsVirtual == true)
316 wxString mVirtualContextBody
;
317 wxString mVittualContextFooter
;
319 // e.g. can be used by documentation generator to store
320 // reference to section object
324 // universal identifier of the context (e.g. class name)
328 // default constructor
331 // automatically destorys all aggregated contexts
332 // (thus, it's enought to call destructor of root-context)
333 virtual ~spContext();
335 // see mUererData member;
336 void* GetUserData() { return mpUserData
; }
338 // sets untyped pointer to user data
339 void SetUserData( void* pUserData
)
340 { mpUserData
= pUserData
; }
342 // searches the whole context tree for the cotnexts
343 // which match given masks, pust results into lst array
344 void GetContextList( MMemberListT
& lst
, int contextMask
);
346 // used by default visitor's implementation
349 /*** forward/reverse ingineering fecilities ***/
351 bool PositionIsKnown();
353 bool IsVirtualContext();
355 bool VitualContextHasChildren();
357 void SetVirtualContextBody( const wxString
& body
,
358 bool hasChildren
= false,
359 const wxString
& footer
= wxEmptyString
);
361 wxString
GetVirtualContextBody();
362 wxString
GetFooterOfVirtualContextBody();
364 // can be overriden by top-level context classes
365 // to find-out ot the source-fragment of this
366 // context using it's position information
367 virtual wxString
GetBody( spContext
* pCtx
= NULL
);
369 virtual wxString
GetHeader( spContext
* pCtx
= NULL
);
371 // true, if there is at least one entry
372 // in the comment list of this context
374 MCommentListT
& GetCommentList() { return mComments
; }
375 const MCommentListT
& GetCommentList() const { return mComments
; }
377 // should be overriden, if the context supports sorting
379 virtual void SortMembers() {}
381 // returns identifier of this context
382 inline wxString
& GetName() { return m_Name
; }
384 // returns -1, if souce line # is unknow
385 inline int GetSourceLineNo() { return mSrcLineNo
; }
387 // see comments on mpFirstOccurence member variable
388 bool IsFirstOccurence();
389 spContext
* GetFirstOccurence();
391 // returns not-NULL value if this context
392 // is aggregated by another cotnext
393 spContext
* GetOutterContext();
395 // perhaps more intuitive alias for `GetOutterContext()'
396 inline spContext
* GetParent() { return m_pParent
; }
398 bool HasOutterContext();
400 // add one aggregate (or child) into this context
401 void AddMember ( spContext
* pMember
);
402 MMemberListT
& GetMembers();
404 // append comment to the comment list decribing
406 void AddComment( spComment
* pComment
);
408 // returns NULL, if the context with the given
409 // name and type is not contained by this context
410 // and it's children. Children's children are not
411 // searched recursivelly if searchSubMembers is false
413 spContext
* FindContext( const wxString
& identifier
,
414 int contextType
= SP_CTX_ANY
,
415 bool searchSubMembers
= true
418 // removes this context from it's parent
419 // (NOTE:: context should have an outter cotnext
420 // to when this method is called, otherwise removal
421 // will result assertion failure)
422 void RemoveThisContext();
424 // returns true, if this object is aggregated in the file
427 // true, if outter context is a namespace
428 bool IsInNameSpace();
430 // true, if outter context is a class
433 // true, if outter cotext is an operation (true for "spParameter"s)
434 bool IsInOperation();
436 // true if the context is public
437 bool IsPublic() const { return mVisibility
== SP_VIS_PUBLIC
; }
439 // NOTE:: method returns not the type of this object
440 // but the file/namespace/class/operation or file in which this
441 // attribute is contained. First, check for the type of
442 // context using the above method.
444 // Requiering container which does not exist, will result
445 // in assertion failure
449 spNameSpace
& GetNameSpace();
450 spOperation
& GetOperation();
452 // each new context should override this method
453 // to return it's specific type
454 virtual int GetContextType() const { return SP_CTX_UNKNOWN
; }
456 // perhaps more intuitive short-cut
457 inline int GetType() { return GetContextType(); }
459 // cast this context to the desired type - returns NULL if type is wrong
460 spAttribute
*CastToAttribute()
462 return GetContextType() == SP_CTX_ATTRIBUTE
? (spAttribute
*)this
466 // derived classes override this to invoke VisitXXX method
467 // which corresponds to the class of specific context,
468 // - this is what the "Visitor" pattern told us ^)
470 // if method is not overriden, then it's probably user-defined
473 virtual void AcceptVisitor( spVisitor
& visitor
)
475 { visitor
.VisitCustomContext( *this ); };
477 // called by visitors, to remove given subcontext
478 // of this context object
479 void RemoveChild( spContext
* pChild
);
481 void RemoveChildren();
483 spContext
* GetEnclosingContext( int mask
= SP_CTX_ANY
);
486 virtual void Dump(const wxString
& indent
) const;
487 #endif // __WXDEBUG__
492 // stores information about single argument of operation
494 class spParameter
: public spContext
497 // type of argument (parameter)
500 // "stringified" initial value
504 virtual int GetContextType() const { return SP_CTX_PARAMETER
; }
506 virtual void AcceptVisitor( spVisitor
& visitor
)
507 { visitor
.VisitParameter( *this ); }
513 // stores information about member(or global) variable
515 class spAttribute
: public spContext
518 // type of the attribute
521 // it's initial value
528 virtual int GetContextType() const { return SP_CTX_ATTRIBUTE
; }
530 virtual void AcceptVisitor( spVisitor
& visitor
)
531 { visitor
.VisitAttribute( *this ); }
536 class spOperation
: public spContext
539 // type of return value
543 //MParamListT mParams;
545 // true, if operation does not modify
546 // the content of the object
549 // flag, specific to C++
552 // true, if definition follows the declaration immediatelly
555 // scope if any (e.g. MyClass::MyFunction(), scope stirng is "MyClass" )
556 // usually found along with implementation of the method, which is now skipped
563 // returns full declaration of the operations
564 // (ret val., identifier, arg. list),
566 // arguments are marked up with italic,
567 // default values marked up with bold-italic,
568 // all the rest is marked as bold
570 // NOTE:: this method may be overriden by class
571 // specific to concrete parser, to provide
572 // language-dependent reperesnetation of
573 // operation and it's argumetn list
575 // the default implementation outputs name in
578 virtual wxString
GetFullName(MarkupTagsT tags
);
580 virtual int GetContextType() const { return SP_CTX_OPERATION
; }
582 virtual void AcceptVisitor( spVisitor
& visitor
)
583 { visitor
.VisitOperation( *this ); }
588 // stores infromation about preprocessor directive
590 class spPreprocessorLine
: public spContext
595 // prepocessor statement including '#' and
596 // attached multiple lines with '\' character
599 int mDefType
; // see SP_PREP_DEFINITION_TYPES enumeration
603 virtual int GetContextType() const { return SP_CTX_PREPROCESSOR
; }
605 virtual int GetStatementType() const { return mDefType
; }
607 wxString
CPP_GetIncludedFileNeme() const;
609 virtual void AcceptVisitor( spVisitor
& visitor
)
610 { visitor
.VisitPreprocessorLine( *this ); }
615 // stores information about the class
617 class spClass
: public spContext
620 // list of superclasses/interfaces
621 StrListT m_SuperClassNames
;
623 // see SP_CLASS_TYPES enumeration
626 // see SP_INHERITANCE_TYPES enumeration
627 int mInheritanceType
;
629 // valid if mClassSubType is SP_CLTYPE_TEMPLATE_CLASS
630 wxString mTemplateTypes
;
632 // true, if it's and interface of abstract base class
636 // sorts class members in the following order:
638 // (by "privacy level" - first private, than protected, public)
642 // (by member type - attributes first, than methods, nested classes)
646 // (by identifier of the member)
648 virtual void SortMembers();
650 virtual int GetContextType() const { return SP_CTX_CLASS
; }
652 virtual void AcceptVisitor( spVisitor
& visitor
)
653 { visitor
.VisitClass( *this ); }
658 // stores information about enum statement
660 class spEnumeration
: public spContext
663 wxString m_EnumContent
; // full-text content of enumeration
666 virtual int GetContextType() const { return SP_CTX_ENUMERATION
; }
668 virtual void AcceptVisitor( spVisitor
& visitor
)
669 { visitor
.VisitEnumeration( *this ); }
674 class spTypeDef
: public spContext
677 // the original type which is redefined
678 // by this type definition
679 wxString m_OriginalType
;
682 virtual int GetContextType() const { return SP_CTX_TYPEDEF
; }
684 virtual void AcceptVisitor( spVisitor
& visitor
)
685 { visitor
.VisitTypeDef( *this ); }
690 // NOTE:: files context may be put to other
691 // file context, resulting in a collection
692 // of parsed file contexts, with a virtual "superfile"
694 class spFile
: public spContext
697 // since file name cannot be determined from
698 // source code, filling in this field is optional
702 virtual int GetContextType() const { return SP_CTX_FILE
; }
704 virtual void AcceptVisitor( spVisitor
& visitor
)
705 { visitor
.VisitFile( *this ); }
712 class SourceParserPlugin
715 virtual bool CanUnderstandContext( char* cur
, char* end
, spContext
* pOuttterCtx
) = 0;
716 virtual void ParseContext( char* start
, char*& cur
, char* end
, spContext
* pOuttterCtx
) = 0;
718 virtual ~SourceParserPlugin() { }
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
727 class SourceParserBase
730 // auto-resizing file buffer, created in ParseFile()
731 // to reuse large heap block for multiple parsings
737 SourceParserPlugin
* mpPlugin
;
740 // value is set in the derived parser classes
745 virtual ~SourceParserBase();
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
752 // (NOTE:: this is the default implementation),
754 virtual spFile
* ParseFile( const char* fname
);
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
761 virtual spFile
* Parse( char* start
, char* end
) = 0;
763 // returns parser "status word" (specific to concrete parser)
764 int GetParserStatus() { return mParserStatus
; }
766 void SetPlugin( SourceParserPlugin
* pPlugin
);