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"
28 #include "srcparser.h"
30 /***** Implementation for class spVisitor *****/
32 void spVisitor::VisitAll( spContext
& atContext
,
36 mSiblingSkipped
= false;
37 mChildSkipped
= false;
38 mContextMask
= SP_CTX_ANY
; // FIXME:: should be an arg.
40 if ( sortContent
&& !atContext
.IsSorted() )
42 atContext
.SortMembers();
44 mpCurCxt
= &atContext
; // FIXME:: this is dirty, restoring it each time
46 if ( atContext
.GetContextType() & mContextMask
)
48 atContext
.AcceptVisitor( *this );
50 MMemberListT
& members
= atContext
.GetMembers();
52 for( size_t i
= 0; i
!= members
.size(); ++i
)
54 if ( mSiblingSkipped
)
60 size_t prevSz
= members
.size();
62 // visit members of the context recursivelly
63 VisitAll( *members
[i
], sortContent
);
65 if ( members
.size() != prevSz
)
67 --i
; // current member was removed!
74 void spVisitor::RemoveCurrentContext()
76 if ( mpCurCxt
->GetParent() )
78 mpCurCxt
->GetParent()->RemoveChild( mpCurCxt
);
81 void spVisitor::SkipSiblings()
83 mSiblingSkipped
= true;
86 void spVisitor::SkipChildren()
91 void spVisitor::SetFilter( int contextMask
)
93 mContextMask
= contextMask
;
96 /***** Implementation for class spComment *****/
98 bool spComment::IsMultiline() const
103 bool spComment::StartsParagraph() const
108 wxString
& spComment::GetText()
113 wxString
spComment::GetText() const
118 /***** Implementation for class spContext *****/
120 spContext::spContext()
122 : m_pParent ( NULL
),
123 mpFirstOccurence( NULL
),
124 mAlreadySorted ( false ),
137 mVisibility( SP_VIS_PRIVATE
),
139 mIsVirtualContext ( false ),
140 mVirtualContextHasChildren( false ),
145 void spContext::RemoveChildren()
147 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
151 mMembers
.erase( mMembers
.begin(), mMembers
.end() );
154 spContext::~spContext()
158 for( size_t i
= 0; i
!= mComments
.size(); ++i
)
163 bool spContext::IsSorted()
165 return mAlreadySorted
;
168 void spContext::GetContextList( MMemberListT
& lst
, int contextMask
)
170 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
172 spContext
& member
= *mMembers
[i
];
174 if ( member
.GetContextType() & contextMask
)
176 lst
.push_back( &member
);
178 // collect required contexts recursively
179 member
.GetContextList( lst
, contextMask
);
183 bool spContext::HasComments()
185 return ( mComments
.size() != 0 );
188 void spContext::RemoveChild( spContext
* pChild
)
190 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
192 if ( mMembers
[i
] == pChild
)
194 mMembers
.erase( &mMembers
[i
] );
200 // the given child should exist on the parent's list
204 spContext
* spContext::GetEnclosingContext( int mask
)
206 spContext
* cur
= this->GetParent();
208 while ( cur
&& !(cur
->GetContextType() & mask
) )
210 cur
= cur
->GetParent();
215 bool spContext::PositionIsKnown()
217 return ( mSrcOffset
!= (-1) && mContextLength
!= (-1) );
220 bool spContext::IsVirtualContext()
222 return mIsVirtualContext
;
225 bool spContext::VitualContextHasChildren()
227 return mVirtualContextHasChildren
;
230 wxString
spContext::GetVirtualContextBody()
232 wxASSERT( mIsVirtualContext
);
234 return mVirtualContextBody
;
237 wxString
spContext::GetFooterOfVirtualContextBody()
239 wxASSERT( mIsVirtualContext
);
241 return mVittualContextFooter
;
245 void spContext::SetVirtualContextBody( const wxString
& body
,
247 const wxString
& footer
)
249 mVirtualContextHasChildren
= hasChildren
;
251 mVirtualContextBody
= body
;
252 mVittualContextFooter
= footer
;
254 // atuomaticllay becomes virtual context
256 mIsVirtualContext
= true;
259 wxString
spContext::GetBody( spContext
* pCtx
)
261 if ( ( pCtx
== NULL
|| pCtx
== this ) && mIsVirtualContext
)
262 return mVirtualContextBody
;
265 return GetParent()->GetBody( ( pCtx
!= NULL
) ? pCtx
: this );
267 return wxEmptyString
; // source-fragment cannot be found
270 wxString
spContext::GetHeader( spContext
* pCtx
)
273 return GetParent()->GetHeader( ( pCtx
!= NULL
) ? pCtx
: this );
275 return wxEmptyString
; // source-fragment cannot be found
278 bool spContext::IsFirstOccurence()
280 return ( mpFirstOccurence
!= 0 );
283 spContext
* spContext::GetFirstOccurence()
285 // this object should not itself be
286 // the first occurence of the context
287 wxASSERT( mpFirstOccurence
!= 0 );
289 return mpFirstOccurence
;
292 void spContext::AddMember( spContext
* pMember
)
294 mMembers
.push_back( pMember
);
296 pMember
->m_pParent
= this;
299 void spContext::AddComment( spComment
* pComment
)
301 mComments
.push_back( pComment
);
304 MMemberListT
& spContext::GetMembers()
309 spContext
* spContext::FindContext( const wxString
& identifier
,
311 bool searchSubMembers
314 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
316 spContext
& member
= *mMembers
[i
];
318 if ( member
.GetName() == identifier
&&
319 ( contextType
& member
.GetContextType() )
324 if ( searchSubMembers
)
327 member
.FindContext( identifier
, contextType
, 1 );
329 if ( result
) return result
;
336 void spContext::RemoveThisContext()
339 m_pParent
->RemoveChild( this );
341 // context should have a parent
342 wxFAIL_MSG("Context should have a parent");
345 spContext
* spContext::GetOutterContext()
350 bool spContext::HasOutterContext()
352 return ( m_pParent
!= 0 );
355 bool spContext::IsInFile()
357 return ( GetOutterContext()->GetContextType() == SP_CTX_FILE
);
360 bool spContext::IsInNameSpace()
362 return ( GetOutterContext()->GetContextType() == SP_CTX_NAMESPACE
);
365 bool spContext::IsInClass()
367 return ( GetOutterContext()->GetContextType() == SP_CTX_CLASS
);
370 bool spContext::IsInOperation()
372 return ( GetOutterContext()->GetContextType() == SP_CTX_OPERATION
);
375 spClass
& spContext::GetClass()
377 wxASSERT( GetOutterContext()->GetType() == SP_CTX_CLASS
);
378 return *((spClass
*)m_pParent
);
381 spFile
& spContext::GetFile()
383 wxASSERT( GetOutterContext()->GetType() == SP_CTX_FILE
);
384 return *((spFile
*)m_pParent
);
387 spNameSpace
& spContext::GetNameSpace()
389 wxASSERT( GetOutterContext()->GetType() == SP_CTX_NAMESPACE
);
390 return *((spNameSpace
*)m_pParent
);
393 spOperation
& spContext::GetOperation()
395 wxASSERT( GetOutterContext()->GetType() == SP_CTX_OPERATION
);
396 return *((spOperation
*)m_pParent
);
399 /***** Implementation for class spClass *****/
401 void spClass::SortMembers()
406 /***** Implementation for class spOperation *****/
408 spOperation::spOperation()
410 : mHasDefinition( false )
414 mHasDefinition
= false;
417 wxString
spOperation::GetFullName(MarkupTagsT tags
)
419 wxString txt
= tags
[TAG_BOLD
].start
+ m_RetType
;
423 txt
+= tags
[TAG_BOLD
].end
;
425 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
428 wxASSERT( mMembers
[i
]->GetContextType() == SP_CTX_PARAMETER
);
430 spParameter
& param
= *((spParameter
*)mMembers
[i
]);
435 txt
+= tags
[TAG_BOLD
].start
;
439 txt
+= tags
[TAG_BOLD
].end
;
440 txt
+= tags
[TAG_ITALIC
].start
;
445 if ( !param
.m_InitVal
.empty() )
448 txt
+= tags
[TAG_BOLD
].start
;
450 txt
+= param
.m_InitVal
;
452 txt
+= tags
[TAG_BOLD
].end
;
455 txt
+= tags
[TAG_ITALIC
].end
;;
458 txt
+= tags
[TAG_BOLD
].start
;
460 txt
+= tags
[TAG_BOLD
].end
;
462 // TBD:: constantness of method
467 /***** Implemenentation for class spPreprocessorLine *****/
469 wxString
spPreprocessorLine::CPP_GetIncludedFileNeme() const
471 wxASSERT( GetStatementType() == SP_PREP_DEF_INCLUDE_FILE
);
475 while( i
< m_Line
.length() && m_Line
[i
] != _T('"') && m_Line
[i
] != _T('<') )
483 while( i
< m_Line
.length() && m_Line
[i
] != _T('"') && m_Line
[i
] != _T('>') )
487 if ( start
< m_Line
.length() )
490 fname
.append( m_Line
, start
, ( i
- start
) );
495 return wxEmptyString
; // syntax error probably
500 /***** Implemenentation for class SourceParserBase *****/
502 SourceParserBase::SourceParserBase()
510 SourceParserBase::~SourceParserBase()
512 if ( mpFileBuf
) free( mpFileBuf
);
514 if ( mpPlugin
) delete mpPlugin
;
517 spFile
* SourceParserBase::ParseFile( const char* fname
)
519 // FIXME:: the below should not be fixed!
521 const size_t MAX_BUF_SIZE
= 1024*256;
523 if ( !mpFileBuf
) mpFileBuf
= (char*)malloc( MAX_BUF_SIZE
);
525 mFileBufSz
= MAX_BUF_SIZE
;
527 FILE* fp
= fopen( fname
, "rt" );
529 if ( !fp
) return NULL
;
531 int sz
= fread( mpFileBuf
, 1, mFileBufSz
, fp
);
533 return Parse( mpFileBuf
, mpFileBuf
+ sz
);
536 void SourceParserBase::SetPlugin( SourceParserPlugin
* pPlugin
)
538 if ( mpPlugin
) delete mpPlugin
;
543 // ===========================================================================
545 // ===========================================================================
549 void spContext::Dump(const wxString
& indent
) const
553 // increase it for the children
554 wxString indentChild
= indent
+ " ";
556 for ( MMemberListT::const_iterator i
= mMembers
.begin();
559 (*i
)->Dump(indentChild
);
563 void spContext::DumpThis(const wxString
& WXUNUSED(indent
)) const
565 wxFAIL_MSG("abstract base class can't be found in parser tree!");
568 void spParameter::DumpThis(const wxString
& indent
) const
570 wxLogDebug("%sparam named '%s' of type '%s'",
571 indent
.c_str(), m_Name
.c_str(), m_Type
.c_str());
574 void spAttribute::DumpThis(const wxString
& indent
) const
576 wxLogDebug("%svariable named '%s' of type '%s'",
577 indent
.c_str(), m_Name
.c_str(), m_Type
.c_str());
580 void spOperation::DumpThis(const wxString
& indent
) const
583 if ( !mScope
.empty() ) {
584 switch ( mVisibility
) {
586 protection
= "public";
589 case SP_VIS_PROTECTED
:
590 protection
= "protected";
594 protection
= "private";
598 wxFAIL_MSG("unknown protection type");
602 protection
= "global";
605 wxString constStr
,virtualStr
;
606 if(mIsConstant
) constStr
= _T("const ");
607 if(mIsVirtual
) virtualStr
= _T("virtual ");
609 wxLogDebug("%s%s%s%s function named '%s::%s' of type '%s'",
614 mScope
.c_str(), m_Name
.c_str(), m_RetType
.c_str());
617 void spPreprocessorLine::DumpThis(const wxString
& indent
) const
620 switch ( mDefType
) {
621 case SP_PREP_DEF_DEFINE_SYMBOL
:
625 case SP_PREP_DEF_REDEFINE_SYMBOL
:
629 case SP_PREP_DEF_INCLUDE_FILE
:
630 kind
.Printf("include (%s)", CPP_GetIncludedFileNeme().c_str());
633 case SP_PREP_DEF_OTHER
:
639 wxLogDebug("%spreprocessor statement: %s",
640 indent
.c_str(), kind
.c_str());
643 void spClass::DumpThis(const wxString
& indent
) const
646 for ( StrListT::const_iterator i
= m_SuperClassNames
.begin();
647 i
!= m_SuperClassNames
.end();
658 switch ( mClassSubType
) {
659 case SP_CLTYPE_CLASS
:
663 case SP_CLTYPE_TEMPLATE_CLASS
:
664 kind
= "template class";
667 case SP_CLTYPE_STRUCTURE
:
671 case SP_CLTYPE_UNION
:
675 case SP_CLTYPE_INTERFACE
:
680 wxFAIL_MSG("unknown class subtype");
683 wxLogDebug("%s%s named '%s' (base classes: %s)",
684 indent
.c_str(), kind
.c_str(),
685 m_Name
.c_str(), base
.c_str());
688 void spEnumeration::DumpThis(const wxString
& indent
) const
690 wxLogDebug("%senum named '%s'",
691 indent
.c_str(), m_Name
.c_str());
694 void spTypeDef::DumpThis(const wxString
& indent
) const
696 wxLogDebug("%stypedef %s = %s",
697 indent
.c_str(), m_Name
.c_str(), m_OriginalType
.c_str());
700 void spFile::DumpThis(const wxString
& indent
) const
702 wxLogDebug("%sfile '%s'",
703 indent
.c_str(), m_FileName
.c_str());
706 #endif // __WXDEBUG__