1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Contrib. demo
4 // Author: Aleksandras Gluchovas
8 // Copyright: (c) Aleskandars Gluchovas
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
25 #include "srcparser.h"
27 /***** Implementation for class spVisitor *****/
29 void spVisitor::VisitAll( spContext
& atContext
,
33 mSiblingSkipped
= false;
34 mChildSkipped
= false;
35 mContextMask
= SP_CTX_ANY
; // FIXME:: should be an arg.
37 if ( sortContent
&& !atContext
.IsSorted() )
39 atContext
.SortMembers();
41 mpCurCxt
= &atContext
; // FIXME:: this is dirty, restoring it each time
43 if ( atContext
.GetContextType() & mContextMask
)
45 atContext
.AcceptVisitor( *this );
47 MMemberListT
& members
= atContext
.GetMembers();
49 for( size_t i
= 0; i
!= members
.size(); ++i
)
51 if ( mSiblingSkipped
)
57 size_t prevSz
= members
.size();
59 // visit members of the context recursivelly
60 VisitAll( *members
[i
], sortContent
);
62 if ( members
.size() != prevSz
)
64 --i
; // current member was removed!
71 void spVisitor::RemoveCurrentContext()
73 if ( mpCurCxt
->GetParent() )
75 mpCurCxt
->GetParent()->RemoveChild( mpCurCxt
);
78 void spVisitor::SkipSiblings()
80 mSiblingSkipped
= true;
83 void spVisitor::SkipChildren()
88 void spVisitor::SetFilter( int contextMask
)
90 mContextMask
= contextMask
;
93 /***** Implementation for class spComment *****/
95 bool spComment::IsMultiline() const
100 bool spComment::StartsParagraph() const
105 wxString
& spComment::GetText()
110 wxString
spComment::GetText() const
115 /***** Implementation for class spContext *****/
117 spContext::spContext()
119 : m_pParent ( NULL
),
120 mpFirstOccurence( NULL
),
121 mAlreadySorted ( false ),
134 mVisibility( SP_VIS_PRIVATE
),
136 mIsVirtualContext ( false ),
137 mVirtualContextHasChildren( false ),
142 void spContext::RemoveChildren()
144 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
148 mMembers
.erase( mMembers
.begin(), mMembers
.end() );
151 spContext::~spContext()
155 for( size_t i
= 0; i
!= mComments
.size(); ++i
)
160 bool spContext::IsSorted()
162 return mAlreadySorted
;
165 void spContext::GetContextList( MMemberListT
& lst
, int contextMask
)
167 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
169 spContext
& member
= *mMembers
[i
];
171 if ( member
.GetContextType() & contextMask
)
173 lst
.push_back( &member
);
175 // collect required contexts recursively
176 member
.GetContextList( lst
, contextMask
);
180 bool spContext::HasComments()
182 return ( mComments
.size() != 0 );
185 void spContext::RemoveChild( spContext
* pChild
)
187 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
189 if ( mMembers
[i
] == pChild
)
191 mMembers
.erase( &mMembers
[i
] );
197 // the given child should exist on the parent's list
201 spContext
* spContext::GetEnclosingContext( int mask
)
203 spContext
* cur
= this->GetParent();
205 while ( cur
&& !(cur
->GetContextType() & mask
) )
207 cur
= cur
->GetParent();
212 bool spContext::PositionIsKnown()
214 return ( mSrcOffset
!= (-1) && mContextLength
!= (-1) );
217 bool spContext::IsVirtualContext()
219 return mIsVirtualContext
;
222 bool spContext::VitualContextHasChildren()
224 return mVirtualContextHasChildren
;
227 wxString
spContext::GetVirtualContextBody()
229 wxASSERT( mIsVirtualContext
);
231 return mVirtualContextBody
;
234 wxString
spContext::GetFooterOfVirtualContextBody()
236 wxASSERT( mIsVirtualContext
);
238 return mVittualContextFooter
;
242 void spContext::SetVirtualContextBody( const wxString
& body
,
244 const wxString
& footer
)
246 mVirtualContextHasChildren
= hasChildren
;
248 mVirtualContextBody
= body
;
249 mVittualContextFooter
= footer
;
251 // atuomaticllay becomes virtual context
253 mIsVirtualContext
= true;
256 wxString
spContext::GetBody( spContext
* pCtx
)
258 if ( ( pCtx
== NULL
|| pCtx
== this ) && mIsVirtualContext
)
259 return mVirtualContextBody
;
262 return GetParent()->GetBody( ( pCtx
!= NULL
) ? pCtx
: this );
264 return wxEmptyString
; // source-fragment cannot be found
267 wxString
spContext::GetHeader( spContext
* pCtx
)
270 return GetParent()->GetHeader( ( pCtx
!= NULL
) ? pCtx
: this );
272 return wxEmptyString
; // source-fragment cannot be found
275 bool spContext::IsFirstOccurence()
277 return ( mpFirstOccurence
!= 0 );
280 spContext
* spContext::GetFirstOccurence()
282 // this object should not itself be
283 // the first occurence of the context
284 wxASSERT( mpFirstOccurence
!= 0 );
286 return mpFirstOccurence
;
289 void spContext::AddMember( spContext
* pMember
)
291 mMembers
.push_back( pMember
);
293 pMember
->m_pParent
= this;
296 void spContext::AddComment( spComment
* pComment
)
298 mComments
.push_back( pComment
);
301 MMemberListT
& spContext::GetMembers()
306 spContext
* spContext::FindContext( const wxString
& identifier
,
308 bool searchSubMembers
311 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
313 spContext
& member
= *mMembers
[i
];
315 if ( member
.GetName() == identifier
&&
316 ( contextType
& member
.GetContextType() )
321 if ( searchSubMembers
)
324 member
.FindContext( identifier
, contextType
, 1 );
326 if ( result
) return result
;
333 void spContext::RemoveThisContext()
336 m_pParent
->RemoveChild( this );
338 // context should have a parent
339 wxFAIL_MSG("Context should have a parent");
342 spContext
* spContext::GetOutterContext()
347 bool spContext::HasOutterContext()
349 return ( m_pParent
!= 0 );
352 bool spContext::IsInFile()
354 return ( GetOutterContext()->GetContextType() == SP_CTX_FILE
);
357 bool spContext::IsInNameSpace()
359 return ( GetOutterContext()->GetContextType() == SP_CTX_NAMESPACE
);
362 bool spContext::IsInClass()
364 return ( GetOutterContext()->GetContextType() == SP_CTX_CLASS
);
367 bool spContext::IsInOperation()
369 return ( GetOutterContext()->GetContextType() == SP_CTX_OPERATION
);
372 spClass
& spContext::GetClass()
374 wxASSERT( GetOutterContext()->GetType() == SP_CTX_CLASS
);
375 return *((spClass
*)m_pParent
);
378 spFile
& spContext::GetFile()
380 wxASSERT( GetOutterContext()->GetType() == SP_CTX_FILE
);
381 return *((spFile
*)m_pParent
);
384 spNameSpace
& spContext::GetNameSpace()
386 wxASSERT( GetOutterContext()->GetType() == SP_CTX_NAMESPACE
);
387 return *((spNameSpace
*)m_pParent
);
390 spOperation
& spContext::GetOperation()
392 wxASSERT( GetOutterContext()->GetType() == SP_CTX_OPERATION
);
393 return *((spOperation
*)m_pParent
);
396 /***** Implementation for class spClass *****/
398 void spClass::SortMembers()
403 /***** Implementation for class spOperation *****/
405 spOperation::spOperation()
407 : mHasDefinition( false )
411 mHasDefinition
= false;
414 wxString
spOperation::GetFullName(MarkupTagsT tags
)
416 wxString txt
= tags
[TAG_BOLD
].start
+ m_RetType
;
420 txt
+= tags
[TAG_BOLD
].end
;
422 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
425 wxASSERT( mMembers
[i
]->GetContextType() == SP_CTX_PARAMETER
);
427 spParameter
& param
= *((spParameter
*)mMembers
[i
]);
432 txt
+= tags
[TAG_BOLD
].start
;
436 txt
+= tags
[TAG_BOLD
].end
;
437 txt
+= tags
[TAG_ITALIC
].start
;
442 if ( !param
.m_InitVal
.empty() )
445 txt
+= tags
[TAG_BOLD
].start
;
447 txt
+= param
.m_InitVal
;
449 txt
+= tags
[TAG_BOLD
].end
;
452 txt
+= tags
[TAG_ITALIC
].end
;;
455 txt
+= tags
[TAG_BOLD
].start
;
457 txt
+= tags
[TAG_BOLD
].end
;
459 // TBD:: constantness of method
464 /***** Implemenentation for class spPreprocessorLine *****/
466 wxString
spPreprocessorLine::CPP_GetIncludedFileNeme() const
468 wxASSERT( GetStatementType() == SP_PREP_DEF_INCLUDE_FILE
);
472 while( i
< m_Line
.length() && m_Line
[i
] != _T('"') && m_Line
[i
] != _T('<') )
480 while( i
< m_Line
.length() && m_Line
[i
] != _T('"') && m_Line
[i
] != _T('>') )
484 if ( start
< m_Line
.length() )
487 fname
.append( m_Line
, start
, ( i
- start
) );
492 return wxEmptyString
; // syntax error probably
497 /***** Implemenentation for class SourceParserBase *****/
499 SourceParserBase::SourceParserBase()
507 SourceParserBase::~SourceParserBase()
509 if ( mpFileBuf
) free( mpFileBuf
);
511 if ( mpPlugin
) delete mpPlugin
;
514 spFile
* SourceParserBase::ParseFile( const char* fname
)
516 // FIXME:: the below should not be fixed!
518 const size_t MAX_BUF_SIZE
= 1024*256;
520 if ( !mpFileBuf
) mpFileBuf
= (char*)malloc( MAX_BUF_SIZE
);
522 mFileBufSz
= MAX_BUF_SIZE
;
524 FILE* fp
= fopen( fname
, "rt" );
526 if ( !fp
) return NULL
;
528 int sz
= fread( mpFileBuf
, 1, mFileBufSz
, fp
);
530 return Parse( mpFileBuf
, mpFileBuf
+ sz
);
533 void SourceParserBase::SetPlugin( SourceParserPlugin
* pPlugin
)
535 if ( mpPlugin
) delete mpPlugin
;
540 // ===========================================================================
542 // ===========================================================================
546 void spContext::Dump(const wxString
& indent
) const
550 // increase it for the children
551 wxString indentChild
= indent
+ " ";
553 for ( MMemberListT::const_iterator i
= mMembers
.begin();
556 (*i
)->Dump(indentChild
);
560 void spContext::DumpThis(const wxString
& WXUNUSED(indent
)) const
562 wxFAIL_MSG("abstract base class can't be found in parser tree!");
565 void spParameter::DumpThis(const wxString
& indent
) const
567 wxLogDebug("%sparam named '%s' of type '%s'",
568 indent
.c_str(), m_Name
.c_str(), m_Type
.c_str());
571 void spAttribute::DumpThis(const wxString
& indent
) const
573 wxLogDebug("%svariable named '%s' of type '%s'",
574 indent
.c_str(), m_Name
.c_str(), m_Type
.c_str());
577 void spOperation::DumpThis(const wxString
& indent
) const
580 if ( !mScope
.empty() ) {
581 switch ( mVisibility
) {
583 protection
= "public";
586 case SP_VIS_PROTECTED
:
587 protection
= "protected";
591 protection
= "private";
595 wxFAIL_MSG("unknown protection type");
599 protection
= "global";
602 wxString constStr
,virtualStr
;
603 if(mIsConstant
) constStr
= _T("const ");
604 if(mIsVirtual
) virtualStr
= _T("virtual ");
606 wxLogDebug("%s%s%s%s function named '%s::%s' of type '%s'",
611 mScope
.c_str(), m_Name
.c_str(), m_RetType
.c_str());
614 void spPreprocessorLine::DumpThis(const wxString
& indent
) const
617 switch ( mDefType
) {
618 case SP_PREP_DEF_DEFINE_SYMBOL
:
622 case SP_PREP_DEF_REDEFINE_SYMBOL
:
626 case SP_PREP_DEF_INCLUDE_FILE
:
627 kind
.Printf("include (%s)", CPP_GetIncludedFileNeme().c_str());
630 case SP_PREP_DEF_OTHER
:
636 wxLogDebug("%spreprocessor statement: %s",
637 indent
.c_str(), kind
.c_str());
640 void spClass::DumpThis(const wxString
& indent
) const
643 for ( StrListT::const_iterator i
= m_SuperClassNames
.begin();
644 i
!= m_SuperClassNames
.end();
655 switch ( mClassSubType
) {
656 case SP_CLTYPE_CLASS
:
660 case SP_CLTYPE_TEMPLATE_CLASS
:
661 kind
= "template class";
664 case SP_CLTYPE_STRUCTURE
:
668 case SP_CLTYPE_UNION
:
672 case SP_CLTYPE_INTERFACE
:
677 wxFAIL_MSG("unknown class subtype");
680 wxLogDebug("%s%s named '%s' (base classes: %s)",
681 indent
.c_str(), kind
.c_str(),
682 m_Name
.c_str(), base
.c_str());
685 void spEnumeration::DumpThis(const wxString
& indent
) const
687 wxLogDebug("%senum named '%s'",
688 indent
.c_str(), m_Name
.c_str());
691 void spTypeDef::DumpThis(const wxString
& indent
) const
693 wxLogDebug("%stypedef %s = %s",
694 indent
.c_str(), m_Name
.c_str(), m_OriginalType
.c_str());
697 void spFile::DumpThis(const wxString
& indent
) const
699 wxLogDebug("%sfile '%s'",
700 indent
.c_str(), m_FileName
.c_str());
703 #endif // __WXDEBUG__