1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Contrib. demo
4 // Author: Aleksandras Gluchovas
8 // Copyright: (c) Aleskandars Gluchovas
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "srcparser.h"
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
31 #include "srcparser.h"
33 /***** Implementation for class spVisitor *****/
35 void spVisitor::VisitAll( spContext
& atContext
,
39 mSiblingSkipped
= FALSE
;
40 mChildSkipped
= FALSE
;
41 mContextMask
= SP_CTX_ANY
; // FIXME:: should be an arg.
43 if ( sortContent
&& !atContext
.IsSorted() )
45 atContext
.SortMembers();
47 mpCurCxt
= &atContext
; // FIXME:: this is dirty, restoring it each time
49 if ( atContext
.GetContextType() & mContextMask
)
51 atContext
.AcceptVisitor( *this );
53 MMemberListT
& members
= atContext
.GetMembers();
55 for( size_t i
= 0; i
!= members
.size(); ++i
)
57 if ( mSiblingSkipped
)
63 size_t prevSz
= members
.size();
65 // visit members of the context recursivelly
66 VisitAll( *members
[i
], sortContent
);
68 if ( members
.size() != prevSz
)
70 --i
; // current member was removed!
77 void spVisitor::RemoveCurrentContext()
79 if ( mpCurCxt
->GetParent() )
81 mpCurCxt
->GetParent()->RemoveChild( mpCurCxt
);
84 void spVisitor::SkipSiblings()
86 mSiblingSkipped
= TRUE
;
89 void spVisitor::SkipChildren()
94 void spVisitor::SetFilter( int contextMask
)
96 mContextMask
= contextMask
;
99 /***** Implementation for class spComment *****/
101 bool spComment::IsMultiline() const
106 bool spComment::StartsParagraph() const
111 string
& spComment::GetText()
116 string
spComment::GetText() const
121 /***** Implementation for class spContext *****/
123 spContext::spContext()
126 mpFirstOccurence( NULL
),
127 mAlreadySorted ( FALSE
),
140 mVisibility( SP_VIS_PRIVATE
),
142 mIsVirtualContext ( FALSE
),
143 mVirtualContextHasChildren( FALSE
),
148 void spContext::RemoveChildren()
150 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
154 mMembers
.erase( mMembers
.begin(), mMembers
.end() );
157 spContext::~spContext()
161 for( size_t i
= 0; i
!= mComments
.size(); ++i
)
166 bool spContext::IsSorted()
168 return mAlreadySorted
;
171 void spContext::GetContextList( MMemberListT
& lst
, int contextMask
)
173 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
175 spContext
& member
= *mMembers
[i
];
177 if ( member
.GetContextType() & contextMask
)
179 lst
.push_back( &member
);
181 // collect required contexts recursively
182 member
.GetContextList( lst
, contextMask
);
186 bool spContext::HasComments()
188 return ( mComments
.size() != 0 );
191 void spContext::RemoveChild( spContext
* pChild
)
193 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
195 if ( mMembers
[i
] == pChild
)
197 mMembers
.erase( &mMembers
[i
] );
203 // the given child should exist on the parent's list
207 spContext
* spContext::GetEnclosingContext( int mask
)
209 spContext
* cur
= this->GetParent();
211 while ( cur
&& !(cur
->GetContextType() & mask
) )
213 cur
= cur
->GetParent();
218 bool spContext::PositionIsKnown()
220 return ( mSrcOffset
!= (-1) && mContextLength
!= (-1) );
223 bool spContext::IsVirtualContext()
225 return mIsVirtualContext
;
228 bool spContext::VitualContextHasChildren()
230 return mVirtualContextHasChildren
;
233 string
spContext::GetVirtualContextBody()
235 wxASSERT( mIsVirtualContext
);
237 return mVirtualContextBody
;
240 string
spContext::GetFooterOfVirtualContextBody()
242 wxASSERT( mIsVirtualContext
);
244 return mVittualContextFooter
;
248 void spContext::SetVirtualContextBody( const string
& body
,
250 const string
& footer
)
252 mVirtualContextHasChildren
= hasChildren
;
254 mVirtualContextBody
= body
;
255 mVittualContextFooter
= footer
;
257 // atuomaticllay becomes virtual context
259 mIsVirtualContext
= TRUE
;
262 string
spContext::GetBody( spContext
* pCtx
)
264 if ( ( pCtx
== NULL
|| pCtx
== this ) && mIsVirtualContext
)
266 return mVirtualContextBody
;
270 return GetParent()->GetBody( ( pCtx
!= NULL
) ? pCtx
: this );
272 return ""; // source-fragment cannot be found
275 string
spContext::GetHeader( spContext
* pCtx
)
279 return GetParent()->GetHeader( ( pCtx
!= NULL
) ? pCtx
: this );
281 return ""; // source-fragment cannot be found
284 bool spContext::IsFirstOccurence()
286 return ( mpFirstOccurence
!= 0 );
289 spContext
* spContext::GetFirstOccurence()
291 // this object should not itself be
292 // the first occurence of the context
293 wxASSERT( mpFirstOccurence
!= 0 );
295 return mpFirstOccurence
;
298 void spContext::AddMember( spContext
* pMember
)
300 mMembers
.push_back( pMember
);
302 pMember
->mpParent
= this;
305 void spContext::AddComment( spComment
* pComment
)
307 mComments
.push_back( pComment
);
310 MMemberListT
& spContext::GetMembers()
315 spContext
* spContext::FindContext( const string
& identifier
,
317 bool searchSubMembers
320 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
322 spContext
& member
= *mMembers
[i
];
324 if ( member
.GetName() == identifier
&&
325 ( contextType
& member
.GetContextType() )
330 if ( searchSubMembers
)
333 member
.FindContext( identifier
, contextType
, 1 );
335 if ( result
) return result
;
342 void spContext::RemoveThisContext()
345 mpParent
->RemoveChild( this );
347 // context should have a parent
351 spContext
* spContext::GetOutterContext()
356 bool spContext::HasOutterContext()
358 return ( mpParent
!= 0 );
361 bool spContext::IsInFile()
363 return ( GetOutterContext()->GetContextType() == SP_CTX_FILE
);
366 bool spContext::IsInNameSpace()
368 return ( GetOutterContext()->GetContextType() == SP_CTX_NAMESPACE
);
371 bool spContext::IsInClass()
373 return ( GetOutterContext()->GetContextType() == SP_CTX_CLASS
);
376 bool spContext::IsInOperation()
378 return ( GetOutterContext()->GetContextType() == SP_CTX_OPERATION
);
381 spClass
& spContext::GetClass()
383 wxASSERT( GetOutterContext()->GetType() == SP_CTX_CLASS
);
384 return *((spClass
*)mpParent
);
387 spFile
& spContext::GetFile()
389 wxASSERT( GetOutterContext()->GetType() == SP_CTX_FILE
);
390 return *((spFile
*)mpParent
);
393 spNameSpace
& spContext::GetNameSpace()
395 wxASSERT( GetOutterContext()->GetType() == SP_CTX_NAMESPACE
);
396 return *((spNameSpace
*)mpParent
);
399 spOperation
& spContext::GetOperation()
401 wxASSERT( GetOutterContext()->GetType() == SP_CTX_OPERATION
);
402 return *((spOperation
*)mpParent
);
405 /***** Implementation for class spClass *****/
407 void spClass::SortMembers()
412 /***** Implementation for class spOperation *****/
414 spOperation::spOperation()
416 : mHasDefinition( FALSE
)
420 mHasDefinition
= false;
423 string
spOperation::GetFullName(MarkupTagsT tags
)
425 string txt
= tags
[TAG_BOLD
].start
+ mRetType
;
429 txt
+= tags
[TAG_BOLD
].end
;
431 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
434 wxASSERT( mMembers
[i
]->GetContextType() == SP_CTX_PARAMETER
);
436 spParameter
& param
= *((spParameter
*)mMembers
[i
]);
441 txt
+= tags
[TAG_BOLD
].start
;
445 txt
+= tags
[TAG_BOLD
].end
;
446 txt
+= tags
[TAG_ITALIC
].start
;
451 if ( param
.mInitVal
!= "" )
454 txt
+= tags
[TAG_BOLD
].start
;
456 txt
+= param
.mInitVal
;
458 txt
+= tags
[TAG_BOLD
].end
;
461 txt
+= tags
[TAG_ITALIC
].end
;;
464 txt
+= tags
[TAG_BOLD
].start
;
466 txt
+= tags
[TAG_BOLD
].end
;
468 // TBD:: constantness of method
473 /***** Implemenentation for class spPreprocessorLine *****/
475 string
spPreprocessorLine::CPP_GetIncludedFileNeme() const
477 wxASSERT( GetStatementType() == SP_PREP_DEF_INCLUDE_FILE
);
481 while( i
< mLine
.length() && mLine
[i
] != '"' && mLine
[i
] != '<' )
489 while( i
< mLine
.length() && mLine
[i
] != '"' && mLine
[i
] != '>' )
493 if ( start
< mLine
.length() )
496 fname
.append( mLine
, start
, ( i
- start
) );
501 return ""; // syntax error probably
506 /***** Implemenentation for class SourceParserBase *****/
508 SourceParserBase::SourceParserBase()
516 SourceParserBase::~SourceParserBase()
518 if ( mpFileBuf
) free( mpFileBuf
);
520 if ( mpPlugin
) delete mpPlugin
;
523 spFile
* SourceParserBase::ParseFile( const char* fname
)
525 // FIXME:: the below should not be fixed!
527 const size_t MAX_BUF_SIZE
= 1024*256;
529 if ( !mpFileBuf
) mpFileBuf
= (char*)malloc( MAX_BUF_SIZE
);
531 mFileBufSz
= MAX_BUF_SIZE
;
533 FILE* fp
= fopen( fname
, "rt" );
535 if ( (int)fp
== -1 || !fp
) return NULL
;
537 int sz
= fread( mpFileBuf
, 1, mFileBufSz
, fp
);
539 return Parse( mpFileBuf
, mpFileBuf
+ sz
);
542 void SourceParserBase::SetPlugin( SourceParserPlugin
* pPlugin
)
544 if ( mpPlugin
) delete mpPlugin
;
549 // ===========================================================================
551 // ===========================================================================
555 void spContext::Dump(const wxString
& indent
) const
559 // increase it for the children
560 wxString indentChild
= indent
+ " ";
562 for ( MMemberListT::const_iterator i
= mMembers
.begin();
565 (*i
)->Dump(indentChild
);
569 void spContext::DumpThis(const wxString
& indent
) const
571 wxFAIL_MSG("abstract base class can't be found in parser tree!");
574 void spParameter::DumpThis(const wxString
& indent
) const
576 wxLogDebug("%sparam named '%s' of type '%s'",
577 indent
.c_str(), mName
.c_str(), mType
.c_str());
580 void spAttribute::DumpThis(const wxString
& indent
) const
582 wxLogDebug("%svariable named '%s' of type '%s'",
583 indent
.c_str(), mName
.c_str(), mType
.c_str());
586 void spOperation::DumpThis(const wxString
& indent
) const
590 switch ( mVisibility
) {
592 protection
= "public";
595 case SP_VIS_PROTECTED
:
596 protection
= "protected";
600 protection
= "private";
604 wxFAIL_MSG("unknown protection type");
608 protection
= "global";
611 wxLogDebug("%s%s%s%s function named '%s::%s' of type '%s'",
613 mIsConstant
? "const " : "",
614 mIsVirtual
? "virtual " : "",
616 mScope
.c_str(), mName
.c_str(), mRetType
.c_str());
619 void spPreprocessorLine::DumpThis(const wxString
& indent
) const
622 switch ( mDefType
) {
623 case SP_PREP_DEF_DEFINE_SYMBOL
:
627 case SP_PREP_DEF_REDEFINE_SYMBOL
:
631 case SP_PREP_DEF_INCLUDE_FILE
:
632 kind
.Printf("include (%s)", CPP_GetIncludedFileNeme().c_str());
635 case SP_PREP_DEF_OTHER
:
641 wxLogDebug("%spreprocessor statement: %s",
642 indent
.c_str(), kind
.c_str());
645 void spClass::DumpThis(const wxString
& indent
) const
648 for ( StrListT::const_iterator i
= mSuperClassNames
.begin();
649 i
!= mSuperClassNames
.end();
660 switch ( mClassSubType
) {
661 case SP_CLTYPE_CLASS
:
665 case SP_CLTYPE_TEMPLATE_CLASS
:
666 kind
= "template class";
669 case SP_CLTYPE_STRUCTURE
:
673 case SP_CLTYPE_UNION
:
677 case SP_CLTYPE_INTERFACE
:
682 wxFAIL_MSG("unknown class subtype");
685 wxLogDebug("%s%s named '%s' (base classes: %s)",
686 indent
.c_str(), kind
.c_str(),
687 mName
.c_str(), base
.c_str());
690 void spEnumeration::DumpThis(const wxString
& indent
) const
692 wxLogDebug("%senum named '%s'",
693 indent
.c_str(), mName
.c_str());
696 void spTypeDef::DumpThis(const wxString
& indent
) const
698 wxLogDebug("%stypedef %s = %s",
699 indent
.c_str(), mName
.c_str(), mOriginalType
.c_str());
702 void spFile::DumpThis(const wxString
& indent
) const
704 wxLogDebug("%sfile '%s'",
705 indent
.c_str(), mFileName
.c_str());
708 #endif // __WXDEBUG__