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 // context class list in "inside-out" order :
50 class spPreprocessorLine
;
55 // source context visibilities
56 enum SRC_VISIBLITY_TYPES
67 SP_CLTYPE_TEMPLATE_CLASS
,
74 enum SP_INHERITANCE_TYPES
81 // proprocessor definitions types (specific to C++ code)
83 enum SP_PREP_DEFINITION_TYPES
85 SP_PREP_DEF_DEFINE_SYMBOL
,
86 SP_PREP_DEF_REDEFINE_SYMBOL
,
87 SP_PREP_DEF_INCLUDE_FILE
,
91 // common context types
93 #define SP_CTX_UNKNOWN 0x000
94 #define SP_CTX_FILE 0x001
95 #define SP_CTX_NAMESPACE 0x002
96 #define SP_CTX_CLASS 0x004
97 #define SP_CTX_TYPEDEF 0x008
98 #define SP_CTX_PREPROCESSOR 0x010
99 #define SP_CTX_ENUMERATION 0x020
100 #define SP_CTX_ATTRIBUTE 0x040
101 #define SP_CTX_OPERATION 0x080
102 #define SP_CTX_PARAMETER 0x100
104 // other (custom) context codes may be defined elsewere, however they should
105 // not clash with above codes for common type and also should not
106 // exceed 16-bits of in value
108 // masks all context types (up to 16 custom context can be defined)
110 #define SP_CTX_ANY 0xFFFF
116 #if defined( wxUSE_TEMPLATE_STL )
119 typedef vector
<spContext
*> MMemberListT
;
121 typedef vector
<spComment
*> MCommentListT
;
122 // list of parameters
123 typedef vector
<spParameter
*> MParamListT
;
125 typedef vector
<string
> StrListT
;
129 typedef spContext
* spContextPtrT
;
130 typedef spComment
* spCommentPtrT
;
131 typedef spParameter
* spParameterPtrT
;
132 typedef WXSTL_VECTOR_SHALLOW_COPY(spContextPtrT
) MMemberListT
;
133 typedef WXSTL_VECTOR_SHALLOW_COPY(spCommentPtrT
) MCommentListT
;
134 typedef WXSTL_VECTOR_SHALLOW_COPY(spParameterPtrT
) MParamListT
;
135 typedef WXSTL_VECTOR_SHALLOW_COPY(string
) StrListT
;
138 // base class for all visitors of source code contents
143 bool mSiblingSkipped
;
150 // methods invoked by context
152 // method invoked from user's controling code
153 // to visit all nodes staring at the given context.
154 // Content is sorted if requrired, see comments
155 // spClass on sorting the class members
157 void VisitAll( spContext
& atContext
,
158 bool sortContent
= TRUE
161 // methods invoked by visitor
163 // goes to the next context in the outter scope
164 // NOTE:: should not be invoked more than once while
165 // visiting certain context
169 // prevents going down into the contexts contained by
170 // the current context
171 // NOTE:: the same as above
175 // can be called only in from visiting procedure
176 void RemoveCurrentContext();
178 // method enables fast filtered traversal
179 // of source content, e.g. collecting only classes,
180 // or only global functions
182 // arg. context - can contain combination of contexts concatinated
183 // with bitwise OR, e.g. SP_CTX_CLASS | SP_CTX_NAMESPACE
185 // method can be invoked from the user's controling as well as
186 // from within the visting procedure
188 void SetFilter( int contextMask
);
190 // methods should be implemneted by specific visitor:
192 // NOTE:: Do not confuse visiting with parsing, first
193 // the source is parsed, and than can be visited
194 // multiple times by variouse visitors (there can
195 // be more the one visitor visiting content at a time)
197 virtual void VisitFile( spFile
& fl
) {}
199 virtual void VisitNameSpace( spNameSpace
& ns
) {}
201 virtual void VisitClass( spClass
& cl
) {}
203 virtual void VisitEnumeration( spEnumeration
& en
) {}
205 virtual void VisitTypeDef( spTypeDef
& td
) {}
207 virtual void VisitPreprocessorLine( spPreprocessorLine
& pd
) {}
209 virtual void VisitAttribute( spAttribute
& attr
) {}
211 virtual void VisitOperation( spOperation
& op
) {}
213 virtual void VisitParameter( spParameter
& param
) {}
215 virtual void VisitCustomContext( spContext
& ctx
) {}
218 // stores one section of comments,
219 // multiple sections can be put to geather
220 // and attached to some context
226 bool mIsMultiline
; // multiline comments ar those with /**/'s
228 // TRUE, if these was an empty empty
229 // line above single line comment
235 bool IsMultiline() const;
236 bool StartsParagraph() const;
240 // contstant version of GetText()
241 string
GetText() const;
244 // abstract base class for common (to most languages) code
245 // contexts (constructs), e.g file, namespace, class, operation,
251 // "linked" list of comments belonging to this context
252 MCommentListT mComments
;
254 // NULL, if this is file context
255 MMemberListT mMembers
;
257 // NULL, if this is top-most context
260 // points to context object, where the this context
261 // was originally declared, meaning that this object
262 // is redeclaration (or if in the case of operation
263 // this context object most probably referres to the
264 // implemnetation in .cpp file for example)
266 // is NULL, if this object referres to the first occurence
269 spContext
* mpFirstOccurence
;
271 // used, to avoid excessive sorting of context's agreggates
276 // source line number, (-1) if unknown
279 // offset of context in the source file, (-1) if unknown
282 // lentgh of the context in characters, (-1) if unknown
285 // source line number, in which this cotext ends, (-1) if unknown
288 // fields are valid, if the may contain other contexts nested inside
292 // zero-based index of the first character of
293 // this context in the source line, (-1) if unknown
296 // zero-based index of the first character of
297 // this context in the last source line of this context, (-1) if unknown
300 // see SRC_VISIBLITY_TYPES enumeration
303 // TRUE, if context does not really exist in the source
304 // but was created by external tools (e.g. forward engineering)
306 bool mIsVirtualContext
;
307 bool mVirtualContextHasChildren
;
309 // body of the context in case (mIsVirtual == TRUE)
310 string mVirtualContextBody
;
311 string mVittualContextFooter
;
313 // e.g. can be used by documentation generator to store
314 // reference to section object
318 // universal identifier of the context (e.g. class name)
322 // default constructor
325 // automatically destorys all aggregated contexts
326 // (thus, it's enought to call destructor of root-context)
327 virtual ~spContext();
329 // see mUererData member;
330 void* GetUserData() { return mpUserData
; }
332 // sets untyped pointer to user data
333 void SetUserData( void* pUserData
)
334 { mpUserData
= pUserData
; }
336 // searches the whole context tree for the cotnexts
337 // which match given masks, pust results into lst array
338 void GetContextList( MMemberListT
& lst
, int contextMask
);
340 // used by default visitor's implementation
343 /*** forward/reverse ingineering fecilities ***/
345 bool PositionIsKnown();
347 bool IsVirtualContext();
349 bool VitualContextHasChildren();
351 void SetVirtualContextBody( const string
& body
,
352 bool hasChildren
= FALSE
,
353 const string
& footer
= "" );
355 string
GetVirtualContextBody();
356 string
GetFooterOfVirtualContextBody();
358 // can be overriden by top-level context classes
359 // to find-out ot the source-fragment of this
360 // context using it's position information
361 virtual string
GetBody( spContext
* pCtx
= NULL
);
363 virtual string
GetHeader( spContext
* pCtx
= NULL
);
365 // TRUE, if there is at least one entry
366 // in the comment list of this context
368 MCommentListT
& GetCommentList() { return mComments
; }
369 const MCommentListT
& GetCommentList() const { return mComments
; }
371 // should be overriden, if the context supports sorting
373 virtual void SortMembers() {}
375 // returns identifier of this context
376 inline string
& GetName() { return mName
; }
378 // returns -1, if souce line # is unknow
379 inline int GetSourceLineNo() { return mSrcLineNo
; }
381 // see comments on mpFirstOccurence member variable
382 bool IsFirstOccurence();
383 spContext
* GetFirstOccurence();
385 // returns not-NULL value if this context
386 // is aggregated by another cotnext
387 spContext
* GetOutterContext();
389 // perhaps more intuitive alias for `GetOutterContext()'
390 inline spContext
* GetParent() { return mpParent
; }
392 bool HasOutterContext();
394 // add one aggregate (or child) into this context
395 void AddMember ( spContext
* pMember
);
396 MMemberListT
& GetMembers();
398 // append comment to the comment list decribing
400 void AddComment( spComment
* pComment
);
402 // returns NULL, if the context with the given
403 // name and type is not contained by this context
404 // and it's children. Children's children are not
405 // searched recursivelly if searchSubMembers is FALSE
407 spContext
* FindContext( const string
& identifier
,
408 int contextType
= SP_CTX_ANY
,
409 bool searchSubMembers
= TRUE
412 // removes this context from it's parent
413 // (NOTE:: context should have an outter cotnext
414 // to when this method is called, otherwise removal
415 // will result assertion failure)
416 void RemoveThisContext();
418 // returns TRUE, if this object is aggregated in the file
421 // TRUE, if outter context is a namespace
422 bool IsInNameSpace();
424 // TRUE, if outter context is a class
427 // TRUE, if outter cotext is an operation (TRUE for "spParameter"s)
428 bool IsInOperation();
430 // TRUE if the context is public
431 bool IsPublic() const { return mVisibility
== SP_VIS_PUBLIC
; }
433 // NOTE:: method returns not the type of this object
434 // but the file/namespace/class/operation or file in which this
435 // attribute is contained. First, check for the type of
436 // context using the above method.
438 // Requiering container which does not exist, will result
439 // in assertion failure
443 spNameSpace
& GetNameSpace();
444 spOperation
& GetOperation();
446 // each new context should override this method
447 // to return it's specific type
448 virtual int GetContextType() { return SP_CTX_UNKNOWN
; }
450 // perhaps more intuitive short-cut
451 inline int GetType() { return GetContextType(); }
453 // derived classes override this to invoke VisitXXX method
454 // which corresponds to the class of specific context,
455 // - this is what the "Visitor" pattern told us ^)
457 // if method is not overriden, then it's probably user-defined
460 virtual void AcceptVisitor( spVisitor
& visitor
)
462 { visitor
.VisitCustomContext( *this ); };
464 // called by visitors, to remove given subcontext
465 // of this context object
466 void RemoveChild( spContext
* pChild
);
468 void RemoveChildren();
470 spContext
* GetEnclosingContext( int mask
= SP_CTX_ANY
);
473 // stores information about single argument of operation
475 class spParameter
: public spContext
478 // type of argument (parameter)
481 // "stringified" initial value
485 virtual int GetContextType() { return SP_CTX_PARAMETER
; }
487 virtual void AcceptVisitor( spVisitor
& visitor
)
488 { visitor
.VisitParameter( *this ); }
492 // stores information about member(or global) variable
494 class spAttribute
: public spContext
497 // type of the attribute
500 // it's initial value
507 virtual int GetContextType() { return SP_CTX_ATTRIBUTE
; }
509 virtual void AcceptVisitor( spVisitor
& visitor
)
510 { visitor
.VisitAttribute( *this ); }
513 class spOperation
: public spContext
516 // type of return value
520 //MParamListT mParams;
522 // TRUE, if operation does not modify
523 // the content of the object
526 // flag, specific to C++
529 // TRUE, if definition follows the declaration immediatelly
532 // scope if any (e.g. MyClass::MyFunction(), scope stirng is "MyClass" )
533 // usually found along with implementation of the method, which is now skipped
540 // returns full declaration of the operations
541 // (ret val., identifier, arg. list),
543 // arguments are marked up with italic,
544 // default values marked up with bold-italic,
545 // all the rest is marked as bold
547 // NOTE:: this method may be overriden by class
548 // specific to concrete parser, to provide
549 // language-dependent reperesnetation of
550 // operation and it's argumetn list
552 // the default implementation outputs name in
555 virtual string
GetFullName(MarkupTagsT tags
);
557 virtual int GetContextType() { return SP_CTX_OPERATION
; }
559 virtual void AcceptVisitor( spVisitor
& visitor
)
560 { visitor
.VisitOperation( *this ); }
564 // stores infromation about preprocessor directive
566 class spPreprocessorLine
: public spContext
571 // prepocessor statement including '#' and
572 // attached multiple lines with '\' character
575 int mDefType
; // see SP_PREP_DEFINITION_TYPES enumeration
579 virtual int GetContextType() { return SP_CTX_PREPROCESSOR
; }
581 virtual int GetStatementType() { return mDefType
; }
583 string
CPP_GetIncludedFileNeme();
585 virtual void AcceptVisitor( spVisitor
& visitor
)
586 { visitor
.VisitPreprocessorLine( *this ); }
589 // stores information about the class
591 class spClass
: public spContext
594 // list of superclasses/interfaces
595 StrListT mSuperClassNames
;
597 // see SP_CLASS_TYPES enumeration
600 // see SP_INHERITANCE_TYPES enumeration
601 int mInheritanceType
;
603 // valid if mClassSubType is SP_CLTYPE_TEMPLATE_CLASS
604 string mTemplateTypes
;
606 // TRUE, if it's and interface of abstract base class
611 // sorts class members in the following order:
613 // (by "privacy level" - first private, than protected, public)
617 // (by member type - attributes first, than methods, nested classes)
621 // (by identifier of the member)
623 virtual void SortMembers();
625 virtual int GetContextType() { return SP_CTX_CLASS
; }
627 virtual void AcceptVisitor( spVisitor
& visitor
)
628 { visitor
.VisitClass( *this ); }
631 // stores information about enum statement
633 class spEnumeration
: public spContext
636 string mEnumContent
; // full-text content of enumeration
639 virtual int GetContextType() { return SP_CTX_ENUMERATION
; }
641 virtual void AcceptVisitor( spVisitor
& visitor
)
642 { visitor
.VisitEnumeration( *this ); }
645 class spTypeDef
: public spContext
648 // the original type which is redefined
649 // by this type definition
650 string mOriginalType
;
653 virtual int GetContextType() { return SP_CTX_TYPEDEF
; }
655 virtual void AcceptVisitor( spVisitor
& visitor
)
656 { visitor
.VisitTypeDef( *this ); }
659 // NOTE:: files context may be put to other
660 // file context, resulting in a collection
661 // of parsed file contexts, with a virtual "superfile"
663 class spFile
: public spContext
666 // since file name cannot be determined from
667 // source code, filling in this field is optional
671 virtual int GetContextType() { return SP_CTX_FILE
; }
673 virtual void AcceptVisitor( spVisitor
& visitor
)
674 { visitor
.VisitFile( *this ); }
679 class SourceParserPlugin
682 virtual bool CanUnderstandContext( char* cur
, char* end
, spContext
* pOuttterCtx
) = 0;
683 virtual void ParseContext( char* start
, char*& cur
, char* end
, spContext
* pOuttterCtx
) = 0;
686 // abstract interface for source parsers
687 // which can output parsing results in the
688 // form of context-tree, where each node
689 // should be derivative of spContext, (see
692 class SourceParserBase
695 // auto-resizing file buffer, created in ParseFile()
696 // to reuse large heap block for multiple parsings
702 SourceParserPlugin
* mpPlugin
;
705 // value is set in the derived parser classes
710 virtual ~SourceParserBase();
712 // loads entier source file(as text) into memory,
713 // and passes it's contents to ParseAll() method,
714 // memory occupied by source text is released after
717 // (NOTE:: this is the default implementation),
719 virtual spFile
* ParseFile( const char* fname
);
721 // should returns the root-node of the created context tree
722 // (user is responsible for releasing it from the heep)
723 // "end" should point to the (last character + 1) of the
726 virtual spFile
* Parse( char* start
, char* end
) = 0;
728 // returns parser "status word" (specific to concrete parser)
729 int GetParserStatus() { return mParserStatus
; }
731 void SetPlugin( SourceParserPlugin
* pPlugin
);