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 )
32 #include "wx/string.h"
35 // FOR NOW:: quick n' dirty:
37 #define string wxString
41 #include "markup.h" // markup tags used in spOperator::GetFullName()
43 // context class list in "inside-out" order :
52 class spPreprocessorLine
;
57 // source context visibilities
58 enum SRC_VISIBLITY_TYPES
69 SP_CLTYPE_TEMPLATE_CLASS
,
76 enum SP_INHERITANCE_TYPES
83 // proprocessor definitions types (specific to C++ code)
85 enum SP_PREP_DEFINITION_TYPES
87 SP_PREP_DEF_DEFINE_SYMBOL
,
88 SP_PREP_DEF_REDEFINE_SYMBOL
,
89 SP_PREP_DEF_INCLUDE_FILE
,
93 // common context types
95 #define SP_CTX_UNKNOWN 0x000
96 #define SP_CTX_FILE 0x001
97 #define SP_CTX_NAMESPACE 0x002
98 #define SP_CTX_CLASS 0x004
99 #define SP_CTX_TYPEDEF 0x008
100 #define SP_CTX_PREPROCESSOR 0x010
101 #define SP_CTX_ENUMERATION 0x020
102 #define SP_CTX_ATTRIBUTE 0x040
103 #define SP_CTX_OPERATION 0x080
104 #define SP_CTX_PARAMETER 0x100
106 // other (custom) context codes may be defined elsewere, however they should
107 // not clash with above codes for common type and also should not
108 // exceed 16-bits of in value
110 // masks all context types (up to 16 custom context can be defined)
112 #define SP_CTX_ANY 0xFFFF
118 #if defined( wxUSE_TEMPLATE_STL )
121 typedef vector
<spContext
*> MMemberListT
;
123 typedef vector
<spComment
*> MCommentListT
;
124 // list of parameters
125 typedef vector
<spParameter
*> MParamListT
;
127 typedef vector
<string
> StrListT
;
131 typedef spContext
* spContextPtrT
;
132 typedef spComment
* spCommentPtrT
;
133 typedef spParameter
* spParameterPtrT
;
134 typedef WXSTL_VECTOR_SHALLOW_COPY(spContextPtrT
) MMemberListT
;
135 typedef WXSTL_VECTOR_SHALLOW_COPY(spCommentPtrT
) MCommentListT
;
136 typedef WXSTL_VECTOR_SHALLOW_COPY(spParameterPtrT
) MParamListT
;
137 typedef WXSTL_VECTOR_SHALLOW_COPY(string
) StrListT
;
140 // base class for all visitors of source code contents
145 bool mSiblingSkipped
;
152 // methods invoked by context
154 // method invoked from user's controling code
155 // to visit all nodes staring at the given context.
156 // Content is sorted if requrired, see comments
157 // spClass on sorting the class members
159 void VisitAll( spContext
& atContext
,
160 bool sortContent
= TRUE
163 // methods invoked by visitor
165 // goes to the next context in the outter scope
166 // NOTE:: should not be invoked more than once while
167 // visiting certain context
171 // prevents going down into the contexts contained by
172 // the current context
173 // NOTE:: the same as above
177 // can be called only in from visiting procedure
178 void RemoveCurrentContext();
180 // method enables fast filtered traversal
181 // of source content, e.g. collecting only classes,
182 // or only global functions
184 // arg. context - can contain combination of contexts concatinated
185 // with bitwise OR, e.g. SP_CTX_CLASS | SP_CTX_NAMESPACE
187 // method can be invoked from the user's controling as well as
188 // from within the visting procedure
190 void SetFilter( int contextMask
);
192 // methods should be implemneted by specific visitor:
194 // NOTE:: Do not confuse visiting with parsing, first
195 // the source is parsed, and than can be visited
196 // multiple times by variouse visitors (there can
197 // be more the one visitor visiting content at a time)
199 virtual void VisitFile( spFile
& fl
) {}
201 virtual void VisitNameSpace( spNameSpace
& ns
) {}
203 virtual void VisitClass( spClass
& cl
) {}
205 virtual void VisitEnumeration( spEnumeration
& en
) {}
207 virtual void VisitTypeDef( spTypeDef
& td
) {}
209 virtual void VisitPreprocessorLine( spPreprocessorLine
& pd
) {}
211 virtual void VisitAttribute( spAttribute
& attr
) {}
213 virtual void VisitOperation( spOperation
& op
) {}
215 virtual void VisitParameter( spParameter
& param
) {}
217 virtual void VisitCustomContext( spContext
& ctx
) {}
220 // stores one section of comments,
221 // multiple sections can be put to geather
222 // and attached to some context
228 bool mIsMultiline
; // multiline comments ar those with /**/'s
230 // TRUE, if these was an empty empty
231 // line above single line comment
237 bool IsMultiline() const;
238 bool StartsParagraph() const;
242 // contstant version of GetText()
243 string
GetText() const;
246 // abstract base class for common (to most languages) code
247 // contexts (constructs), e.g file, namespace, class, operation,
253 // "linked" list of comments belonging to this context
254 MCommentListT mComments
;
256 // NULL, if this is file context
257 MMemberListT mMembers
;
259 // NULL, if this is top-most context
262 // points to context object, where the this context
263 // was originally declared, meaning that this object
264 // is redeclaration (or if in the case of operation
265 // this context object most probably referres to the
266 // implemnetation in .cpp file for example)
268 // is NULL, if this object referres to the first occurence
271 spContext
* mpFirstOccurence
;
273 // used, to avoid excessive sorting of context's agreggates
278 // source line number, (-1) if unknown
281 // offset of context in the source file, (-1) if unknown
284 // lentgh of the context in characters, (-1) if unknown
287 // source line number, in which this cotext ends, (-1) if unknown
290 // fields are valid, if the may contain other contexts nested inside
294 // zero-based index of the first character of
295 // this context in the source line, (-1) if unknown
298 // zero-based index of the first character of
299 // this context in the last source line of this context, (-1) if unknown
302 // see SRC_VISIBLITY_TYPES enumeration
305 // TRUE, if context does not really exist in the source
306 // but was created by external tools (e.g. forward engineering)
308 bool mIsVirtualContext
;
309 bool mVirtualContextHasChildren
;
311 // body of the context in case (mIsVirtual == TRUE)
312 string mVirtualContextBody
;
313 string mVittualContextFooter
;
315 // e.g. can be used by documentation generator to store
316 // reference to section object
320 // universal identifier of the context (e.g. class name)
324 // default constructor
327 // automatically destorys all aggregated contexts
328 // (thus, it's enought to call destructor of root-context)
329 virtual ~spContext();
331 // see mUererData member;
332 void* GetUserData() { return mpUserData
; }
334 // sets untyped pointer to user data
335 void SetUserData( void* pUserData
)
336 { mpUserData
= pUserData
; }
338 // searches the whole context tree for the cotnexts
339 // which match given masks, pust results into lst array
340 void GetContextList( MMemberListT
& lst
, int contextMask
);
342 // used by default visitor's implementation
345 /*** forward/reverse ingineering fecilities ***/
347 bool PositionIsKnown();
349 bool IsVirtualContext();
351 bool VitualContextHasChildren();
353 void SetVirtualContextBody( const string
& body
,
354 bool hasChildren
= FALSE
,
355 const string
& footer
= "" );
357 string
GetVirtualContextBody();
358 string
GetFooterOfVirtualContextBody();
360 // can be overriden by top-level context classes
361 // to find-out ot the source-fragment of this
362 // context using it's position information
363 virtual string
GetBody( spContext
* pCtx
= NULL
);
365 virtual string
GetHeader( spContext
* pCtx
= NULL
);
367 // TRUE, if there is at least one entry
368 // in the comment list of this context
370 inline MCommentListT
& GetCommentList()
371 { return mComments
; }
373 // should be overriden, if the context supports sorting
375 virtual void SortMembers() {}
377 // returns identifier of this context
378 inline string
& GetName() { return mName
; }
380 // returns -1, if souce line # is unknow
381 inline int GetSourceLineNo() { return mSrcLineNo
; }
383 // see comments on mpFirstOccurence member variable
384 bool IsFirstOccurence();
385 spContext
* GetFirstOccurence();
387 // returns not-NULL value if this context
388 // is aggregated by another cotnext
389 spContext
* GetOutterContext();
391 // perhaps more intuitive alias for `GetOutterContext()'
392 inline spContext
* GetParent() { return mpParent
; }
394 bool HasOutterContext();
396 // add one aggregate (or child) into this context
397 void AddMember ( spContext
* pMember
);
398 MMemberListT
& GetMembers();
400 // append comment to the comment list decribing
402 void AddComment( spComment
* pComment
);
404 // returns NULL, if the context with the given
405 // name and type is not contained by this context
406 // and it's children. Children's children are not
407 // searched recursivelly if searchSubMembers is FALSE
409 spContext
* FindContext( const string
& identifier
,
410 int contextType
= SP_CTX_ANY
,
411 bool searchSubMembers
= TRUE
414 // removes this context from it's parent
415 // (NOTE:: context should have an outter cotnext
416 // to when this method is called, otherwise removal
417 // will result assertion failure)
418 void RemoveThisContext();
420 // returns TRUE, if this object is aggregated in the file
423 // TRUE, if outter context is a namespace
424 bool IsInNameSpace();
426 // TRUE, if outter context is a class
429 // TRUE, if outter cotext is an operation (TRUE for "spParameter"s)
430 bool IsInOperation();
432 // TRUE if the context is public
433 bool IsPublic() const { return mVisibility
== SP_VIS_PUBLIC
; }
435 // NOTE:: method returns not the type of this object
436 // but the file/namespace/class/operation or file in which this
437 // attribute is contained. First, check for the type of
438 // context using the above method.
440 // Requiering container which does not exist, will result
441 // in assertion failure
445 spNameSpace
& GetNameSpace();
446 spOperation
& GetOperation();
448 // each new context should override this method
449 // to return it's specific type
450 virtual int GetContextType() { return SP_CTX_UNKNOWN
; }
452 // perhaps more intuitive short-cut
453 inline int GetType() { return GetContextType(); }
455 // derived classes override this to invoke VisitXXX method
456 // which corresponds to the class of specific context,
457 // - this is what the "Visitor" pattern told us ^)
459 // if method is not overriden, then it's probably user-defined
462 virtual void AcceptVisitor( spVisitor
& visitor
)
464 { visitor
.VisitCustomContext( *this ); };
466 // called by visitors, to remove given subcontext
467 // of this context object
468 void RemoveChild( spContext
* pChild
);
470 void RemoveChildren();
472 spContext
* GetEnclosingContext( int mask
= SP_CTX_ANY
);
475 // stores information about single argument of operation
477 class spParameter
: public spContext
480 // type of argument (parameter)
483 // "stringified" initial value
487 virtual int GetContextType() { return SP_CTX_PARAMETER
; }
489 virtual void AcceptVisitor( spVisitor
& visitor
)
490 { visitor
.VisitParameter( *this ); }
494 // stores information about member(or global) variable
496 class spAttribute
: public spContext
499 // type of the attribute
502 // it's initial value
509 virtual int GetContextType() { return SP_CTX_ATTRIBUTE
; }
511 virtual void AcceptVisitor( spVisitor
& visitor
)
512 { visitor
.VisitAttribute( *this ); }
515 class spOperation
: public spContext
518 // type of return value
522 //MParamListT mParams;
524 // TRUE, if operation does not modify
525 // the content of the object
528 // flag, specific to C++
531 // TRUE, if definition follows the declaration immediatelly
534 // scope if any (e.g. MyClass::MyFunction(), scope stirng is "MyClass" )
535 // usually found along with implementation of the method, which is now skipped
542 // returns full declaration of the operations
543 // (ret val., identifier, arg. list),
545 // arguments are marked up with italic,
546 // default values marked up with bold-italic,
547 // all the rest is marked as bold
549 // NOTE:: this method may be overriden by class
550 // specific to concrete parser, to provide
551 // language-dependent reperesnetation of
552 // operation and it's argumetn list
554 // the default implementation outputs name in
557 virtual string
GetFullName(MarkupTagsT tags
);
559 virtual int GetContextType() { return SP_CTX_OPERATION
; }
561 virtual void AcceptVisitor( spVisitor
& visitor
)
562 { visitor
.VisitOperation( *this ); }
566 // stores infromation about preprocessor directive
568 class spPreprocessorLine
: public spContext
573 // prepocessor statement including '#' and
574 // attached multiple lines with '\' character
577 int mDefType
; // see SP_PREP_DEFINITION_TYPES enumeration
581 virtual int GetContextType() { return SP_CTX_PREPROCESSOR
; }
583 virtual int GetStatementType() { return mDefType
; }
585 string
CPP_GetIncludedFileNeme();
587 virtual void AcceptVisitor( spVisitor
& visitor
)
588 { visitor
.VisitPreprocessorLine( *this ); }
591 // stores information about the class
593 class spClass
: public spContext
596 // list of superclasses/interfaces
597 StrListT mSuperClassNames
;
599 // see SP_CLASS_TYPES enumeration
602 // see SP_INHERITANCE_TYPES enumeration
603 int mInheritanceType
;
605 // valid if mClassSubType is SP_CLTYPE_TEMPLATE_CLASS
606 string mTemplateTypes
;
608 // TRUE, if it's and interface of abstract base class
613 // sorts class members in the following order:
615 // (by "privacy level" - first private, than protected, public)
619 // (by member type - attributes first, than methods, nested classes)
623 // (by identifier of the member)
625 virtual void SortMembers();
627 virtual int GetContextType() { return SP_CTX_CLASS
; }
629 virtual void AcceptVisitor( spVisitor
& visitor
)
630 { visitor
.VisitClass( *this ); }
633 // stores information about enum statement
635 class spEnumeration
: public spContext
638 string mEnumContent
; // full-text content of enumeration
641 virtual int GetContextType() { return SP_CTX_ENUMERATION
; }
643 virtual void AcceptVisitor( spVisitor
& visitor
)
644 { visitor
.VisitEnumeration( *this ); }
647 class spTypeDef
: public spContext
650 // the original type which is redefined
651 // by this type definition
652 string mOriginalType
;
655 virtual int GetContextType() { return SP_CTX_TYPEDEF
; }
657 virtual void AcceptVisitor( spVisitor
& visitor
)
658 { visitor
.VisitTypeDef( *this ); }
661 // NOTE:: files context may be put to other
662 // file context, resulting in a collection
663 // of parsed file contexts, with a virtual "superfile"
665 class spFile
: public spContext
668 // since file name cannot be determined from
669 // source code, filling in this field is optional
673 virtual int GetContextType() { return SP_CTX_FILE
; }
675 virtual void AcceptVisitor( spVisitor
& visitor
)
676 { visitor
.VisitFile( *this ); }
681 class SourceParserPlugin
684 virtual bool CanUnderstandContext( char* cur
, char* end
, spContext
* pOuttterCtx
) = 0;
685 virtual void ParseContext( char* start
, char*& cur
, char* end
, spContext
* pOuttterCtx
) = 0;
688 // abstract interface for source parsers
689 // which can output parsing results in the
690 // form of context-tree, where each node
691 // should be derivative of spContext, (see
694 class SourceParserBase
697 // auto-resizing file buffer, created in ParseFile()
698 // to reuse large heap block for multiple parsings
704 SourceParserPlugin
* mpPlugin
;
707 // value is set in the derived parser classes
712 virtual ~SourceParserBase();
714 // loads entier source file(as text) into memory,
715 // and passes it's contents to ParseAll() method,
716 // memory occupied by source text is released after
719 // (NOTE:: this is the default implementation),
721 virtual spFile
* ParseFile( const char* fname
);
723 // should returns the root-node of the created context tree
724 // (user is responsible for releasing it from the heep)
725 // "end" should point to the (last character + 1) of the
728 virtual spFile
* Parse( char* start
, char* end
) = 0;
730 // returns parser "status word" (specific to concrete parser)
731 int GetParserStatus() { return mParserStatus
; }
733 void SetPlugin( SourceParserPlugin
* pPlugin
);