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"
33 // FOR NOW:: quick n' dirty:
35 #define string wxString
39 #include "markup.h" // markup tags used in spOperator::GetFullName()
41 // these methods are used for debugging only and disappear in the release build
43 #define DECLARE_DUMP virtual void DumpThis(const wxString& indent) const;
48 // context class list in "inside-out" order :
57 class spPreprocessorLine
;
62 // source context visibilities
63 enum SRC_VISIBLITY_TYPES
75 SP_CLTYPE_TEMPLATE_CLASS
,
82 enum SP_INHERITANCE_TYPES
89 // proprocessor definitions types (specific to C++ code)
91 enum SP_PREP_DEFINITION_TYPES
93 SP_PREP_DEF_DEFINE_SYMBOL
,
94 SP_PREP_DEF_REDEFINE_SYMBOL
,
95 SP_PREP_DEF_INCLUDE_FILE
,
99 // common context types
101 #define SP_CTX_UNKNOWN 0x000
102 #define SP_CTX_FILE 0x001
103 #define SP_CTX_NAMESPACE 0x002
104 #define SP_CTX_CLASS 0x004
105 #define SP_CTX_TYPEDEF 0x008
106 #define SP_CTX_PREPROCESSOR 0x010
107 #define SP_CTX_ENUMERATION 0x020
108 #define SP_CTX_ATTRIBUTE 0x040
109 #define SP_CTX_OPERATION 0x080
110 #define SP_CTX_PARAMETER 0x100
112 // other (custom) context codes may be defined elsewere, however they should
113 // not clash with above codes for common type and also should not
114 // exceed 16-bits of in value
116 // masks all context types (up to 16 custom context can be defined)
118 #define SP_CTX_ANY 0xFFFF
124 #if defined( wxUSE_TEMPLATE_STL )
127 typedef vector
<spContext
*> MMemberListT
;
129 typedef vector
<spComment
*> MCommentListT
;
130 // list of parameters
131 typedef vector
<spParameter
*> MParamListT
;
133 typedef vector
<string
> StrListT
;
137 typedef spContext
* spContextPtrT
;
138 typedef spComment
* spCommentPtrT
;
139 typedef spParameter
* spParameterPtrT
;
140 typedef WXSTL_VECTOR_SHALLOW_COPY(spContextPtrT
) MMemberListT
;
141 typedef WXSTL_VECTOR_SHALLOW_COPY(spCommentPtrT
) MCommentListT
;
142 typedef WXSTL_VECTOR_SHALLOW_COPY(spParameterPtrT
) MParamListT
;
143 typedef WXSTL_VECTOR_SHALLOW_COPY(string
) StrListT
;
146 // base class for all visitors of source code contents
151 bool mSiblingSkipped
;
158 // methods invoked by context
160 // method invoked from user's controling code
161 // to visit all nodes staring at the given context.
162 // Content is sorted if requrired, see comments
163 // spClass on sorting the class members
165 void VisitAll( spContext
& atContext
,
166 bool sortContent
= TRUE
169 // methods invoked by visitor
171 // goes to the next context in the outter scope
172 // NOTE:: should not be invoked more than once while
173 // visiting certain context
177 // prevents going down into the contexts contained by
178 // the current context
179 // NOTE:: the same as above
183 // can be called only in from visiting procedure
184 void RemoveCurrentContext();
186 // method enables fast filtered traversal
187 // of source content, e.g. collecting only classes,
188 // or only global functions
190 // arg. context - can contain combination of contexts concatinated
191 // with bitwise OR, e.g. SP_CTX_CLASS | SP_CTX_NAMESPACE
193 // method can be invoked from the user's controling as well as
194 // from within the visting procedure
196 void SetFilter( int contextMask
);
198 // methods should be implemneted by specific visitor:
200 // NOTE:: Do not confuse visiting with parsing, first
201 // the source is parsed, and than can be visited
202 // multiple times by variouse visitors (there can
203 // be more the one visitor visiting content at a time)
205 virtual void VisitFile( spFile
& fl
) {}
207 virtual void VisitNameSpace( spNameSpace
& ns
) {}
209 virtual void VisitClass( spClass
& cl
) {}
211 virtual void VisitEnumeration( spEnumeration
& en
) {}
213 virtual void VisitTypeDef( spTypeDef
& td
) {}
215 virtual void VisitPreprocessorLine( spPreprocessorLine
& pd
) {}
217 virtual void VisitAttribute( spAttribute
& attr
) {}
219 virtual void VisitOperation( spOperation
& op
) {}
221 virtual void VisitParameter( spParameter
& param
) {}
223 virtual void VisitCustomContext( spContext
& ctx
) {}
226 // stores one section of comments,
227 // multiple sections can be put to geather
228 // and attached to some context
234 bool mIsMultiline
; // multiline comments ar those with /**/'s
236 // TRUE, if these was an empty empty
237 // line above single line comment
243 bool IsMultiline() const;
244 bool StartsParagraph() const;
248 // contstant version of GetText()
249 string
GetText() const;
252 // abstract base class for common (to most languages) code
253 // contexts (constructs), e.g file, namespace, class, operation,
259 // "linked" list of comments belonging to this context
260 MCommentListT mComments
;
262 // NULL, if this is file context
263 MMemberListT mMembers
;
265 // NULL, if this is top-most context
268 // points to context object, where the this context
269 // was originally declared, meaning that this object
270 // is redeclaration (or if in the case of operation
271 // this context object most probably referres to the
272 // implemnetation in .cpp file for example)
274 // is NULL, if this object referres to the first occurence
277 spContext
* mpFirstOccurence
;
279 // used, to avoid excessive sorting of context's agreggates
284 // source line number, (-1) if unknown
287 // offset of context in the source file, (-1) if unknown
290 // lentgh of the context in characters, (-1) if unknown
293 // source line number, in which this cotext ends, (-1) if unknown
296 // fields are valid, if the may contain other contexts nested inside
300 // zero-based index of the first character of
301 // this context in the source line, (-1) if unknown
304 // zero-based index of the first character of
305 // this context in the last source line of this context, (-1) if unknown
308 // see SRC_VISIBLITY_TYPES enumeration
311 // TRUE, if context does not really exist in the source
312 // but was created by external tools (e.g. forward engineering)
314 bool mIsVirtualContext
;
315 bool mVirtualContextHasChildren
;
317 // body of the context in case (mIsVirtual == TRUE)
318 string mVirtualContextBody
;
319 string mVittualContextFooter
;
321 // e.g. can be used by documentation generator to store
322 // reference to section object
326 // universal identifier of the context (e.g. class name)
330 // default constructor
333 // automatically destorys all aggregated contexts
334 // (thus, it's enought to call destructor of root-context)
335 virtual ~spContext();
337 // see mUererData member;
338 void* GetUserData() { return mpUserData
; }
340 // sets untyped pointer to user data
341 void SetUserData( void* pUserData
)
342 { mpUserData
= pUserData
; }
344 // searches the whole context tree for the cotnexts
345 // which match given masks, pust results into lst array
346 void GetContextList( MMemberListT
& lst
, int contextMask
);
348 // used by default visitor's implementation
351 /*** forward/reverse ingineering fecilities ***/
353 bool PositionIsKnown();
355 bool IsVirtualContext();
357 bool VitualContextHasChildren();
359 void SetVirtualContextBody( const string
& body
,
360 bool hasChildren
= FALSE
,
361 const string
& footer
= "" );
363 string
GetVirtualContextBody();
364 string
GetFooterOfVirtualContextBody();
366 // can be overriden by top-level context classes
367 // to find-out ot the source-fragment of this
368 // context using it's position information
369 virtual string
GetBody( spContext
* pCtx
= NULL
);
371 virtual string
GetHeader( spContext
* pCtx
= NULL
);
373 // TRUE, if there is at least one entry
374 // in the comment list of this context
376 MCommentListT
& GetCommentList() { return mComments
; }
377 const MCommentListT
& GetCommentList() const { return mComments
; }
379 // should be overriden, if the context supports sorting
381 virtual void SortMembers() {}
383 // returns identifier of this context
384 inline string
& GetName() { return mName
; }
386 // returns -1, if souce line # is unknow
387 inline int GetSourceLineNo() { return mSrcLineNo
; }
389 // see comments on mpFirstOccurence member variable
390 bool IsFirstOccurence();
391 spContext
* GetFirstOccurence();
393 // returns not-NULL value if this context
394 // is aggregated by another cotnext
395 spContext
* GetOutterContext();
397 // perhaps more intuitive alias for `GetOutterContext()'
398 inline spContext
* GetParent() { return mpParent
; }
400 bool HasOutterContext();
402 // add one aggregate (or child) into this context
403 void AddMember ( spContext
* pMember
);
404 MMemberListT
& GetMembers();
406 // append comment to the comment list decribing
408 void AddComment( spComment
* pComment
);
410 // returns NULL, if the context with the given
411 // name and type is not contained by this context
412 // and it's children. Children's children are not
413 // searched recursivelly if searchSubMembers is FALSE
415 spContext
* FindContext( const string
& identifier
,
416 int contextType
= SP_CTX_ANY
,
417 bool searchSubMembers
= TRUE
420 // removes this context from it's parent
421 // (NOTE:: context should have an outter cotnext
422 // to when this method is called, otherwise removal
423 // will result assertion failure)
424 void RemoveThisContext();
426 // returns TRUE, if this object is aggregated in the file
429 // TRUE, if outter context is a namespace
430 bool IsInNameSpace();
432 // TRUE, if outter context is a class
435 // TRUE, if outter cotext is an operation (TRUE for "spParameter"s)
436 bool IsInOperation();
438 // TRUE if the context is public
439 bool IsPublic() const { return mVisibility
== SP_VIS_PUBLIC
; }
441 // NOTE:: method returns not the type of this object
442 // but the file/namespace/class/operation or file in which this
443 // attribute is contained. First, check for the type of
444 // context using the above method.
446 // Requiering container which does not exist, will result
447 // in assertion failure
451 spNameSpace
& GetNameSpace();
452 spOperation
& GetOperation();
454 // each new context should override this method
455 // to return it's specific type
456 virtual int GetContextType() const { return SP_CTX_UNKNOWN
; }
458 // perhaps more intuitive short-cut
459 inline int GetType() { return GetContextType(); }
461 // cast this context to the desired type - returns NULL if type is wrong
462 spAttribute
*CastToAttribute()
464 return GetContextType() == SP_CTX_ATTRIBUTE
? (spAttribute
*)this
468 // derived classes override this to invoke VisitXXX method
469 // which corresponds to the class of specific context,
470 // - this is what the "Visitor" pattern told us ^)
472 // if method is not overriden, then it's probably user-defined
475 virtual void AcceptVisitor( spVisitor
& visitor
)
477 { visitor
.VisitCustomContext( *this ); };
479 // called by visitors, to remove given subcontext
480 // of this context object
481 void RemoveChild( spContext
* pChild
);
483 void RemoveChildren();
485 spContext
* GetEnclosingContext( int mask
= SP_CTX_ANY
);
488 virtual void Dump(const wxString
& indent
) const;
489 #endif // __WXDEBUG__
494 // stores information about single argument of operation
496 class spParameter
: public spContext
499 // type of argument (parameter)
502 // "stringified" initial value
506 virtual int GetContextType() const { return SP_CTX_PARAMETER
; }
508 virtual void AcceptVisitor( spVisitor
& visitor
)
509 { visitor
.VisitParameter( *this ); }
515 // stores information about member(or global) variable
517 class spAttribute
: public spContext
520 // type of the attribute
523 // it's initial value
530 virtual int GetContextType() const { return SP_CTX_ATTRIBUTE
; }
532 virtual void AcceptVisitor( spVisitor
& visitor
)
533 { visitor
.VisitAttribute( *this ); }
538 class spOperation
: public spContext
541 // type of return value
545 //MParamListT mParams;
547 // TRUE, if operation does not modify
548 // the content of the object
551 // flag, specific to C++
554 // TRUE, if definition follows the declaration immediatelly
557 // scope if any (e.g. MyClass::MyFunction(), scope stirng is "MyClass" )
558 // usually found along with implementation of the method, which is now skipped
565 // returns full declaration of the operations
566 // (ret val., identifier, arg. list),
568 // arguments are marked up with italic,
569 // default values marked up with bold-italic,
570 // all the rest is marked as bold
572 // NOTE:: this method may be overriden by class
573 // specific to concrete parser, to provide
574 // language-dependent reperesnetation of
575 // operation and it's argumetn list
577 // the default implementation outputs name in
580 virtual string
GetFullName(MarkupTagsT tags
);
582 virtual int GetContextType() const { return SP_CTX_OPERATION
; }
584 virtual void AcceptVisitor( spVisitor
& visitor
)
585 { visitor
.VisitOperation( *this ); }
590 // stores infromation about preprocessor directive
592 class spPreprocessorLine
: public spContext
597 // prepocessor statement including '#' and
598 // attached multiple lines with '\' character
601 int mDefType
; // see SP_PREP_DEFINITION_TYPES enumeration
605 virtual int GetContextType() const { return SP_CTX_PREPROCESSOR
; }
607 virtual int GetStatementType() const { return mDefType
; }
609 string
CPP_GetIncludedFileNeme() const;
611 virtual void AcceptVisitor( spVisitor
& visitor
)
612 { visitor
.VisitPreprocessorLine( *this ); }
617 // stores information about the class
619 class spClass
: public spContext
622 // list of superclasses/interfaces
623 StrListT mSuperClassNames
;
625 // see SP_CLASS_TYPES enumeration
628 // see SP_INHERITANCE_TYPES enumeration
629 int mInheritanceType
;
631 // valid if mClassSubType is SP_CLTYPE_TEMPLATE_CLASS
632 string mTemplateTypes
;
634 // TRUE, if it's and interface of abstract base class
638 // sorts class members in the following order:
640 // (by "privacy level" - first private, than protected, public)
644 // (by member type - attributes first, than methods, nested classes)
648 // (by identifier of the member)
650 virtual void SortMembers();
652 virtual int GetContextType() const { return SP_CTX_CLASS
; }
654 virtual void AcceptVisitor( spVisitor
& visitor
)
655 { visitor
.VisitClass( *this ); }
660 // stores information about enum statement
662 class spEnumeration
: public spContext
665 string mEnumContent
; // full-text content of enumeration
668 virtual int GetContextType() const { return SP_CTX_ENUMERATION
; }
670 virtual void AcceptVisitor( spVisitor
& visitor
)
671 { visitor
.VisitEnumeration( *this ); }
676 class spTypeDef
: public spContext
679 // the original type which is redefined
680 // by this type definition
681 string mOriginalType
;
684 virtual int GetContextType() const { return SP_CTX_TYPEDEF
; }
686 virtual void AcceptVisitor( spVisitor
& visitor
)
687 { visitor
.VisitTypeDef( *this ); }
692 // NOTE:: files context may be put to other
693 // file context, resulting in a collection
694 // of parsed file contexts, with a virtual "superfile"
696 class spFile
: public spContext
699 // since file name cannot be determined from
700 // source code, filling in this field is optional
704 virtual int GetContextType() const { return SP_CTX_FILE
; }
706 virtual void AcceptVisitor( spVisitor
& visitor
)
707 { visitor
.VisitFile( *this ); }
714 class SourceParserPlugin
717 virtual bool CanUnderstandContext( char* cur
, char* end
, spContext
* pOuttterCtx
) = 0;
718 virtual void ParseContext( char* start
, char*& cur
, char* end
, spContext
* pOuttterCtx
) = 0;
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
);