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
)
419 string
spOperation::GetFullName(MarkupTagsT tags
)
421 string txt
= tags
[TAG_BOLD
].start
+ mRetType
;
425 txt
+= tags
[TAG_BOLD
].end
;
427 for( size_t i
= 0; i
!= mMembers
.size(); ++i
)
430 wxASSERT( mMembers
[i
]->GetContextType() == SP_CTX_PARAMETER
);
432 spParameter
& param
= *((spParameter
*)mMembers
[i
]);
437 txt
+= tags
[TAG_BOLD
].start
;
441 txt
+= tags
[TAG_BOLD
].end
;
442 txt
+= tags
[TAG_ITALIC
].start
;
447 if ( param
.mInitVal
!= "" )
450 txt
+= tags
[TAG_BOLD
].start
;
452 txt
+= param
.mInitVal
;
454 txt
+= tags
[TAG_BOLD
].end
;
457 txt
+= tags
[TAG_ITALIC
].end
;;
460 txt
+= tags
[TAG_BOLD
].start
;
462 txt
+= tags
[TAG_BOLD
].end
;
464 // TBD:: constantness of method
469 /***** Implemenentation for class spPreprocessorLine *****/
471 string
spPreprocessorLine::CPP_GetIncludedFileNeme()
473 wxASSERT( GetStatementType() == SP_PREP_DEF_INCLUDE_FILE
);
477 while( i
< mLine
.length() && mLine
[i
] != '"' && mLine
[i
] != '<' )
485 while( i
< mLine
.length() && mLine
[i
] != '"' && mLine
[i
] != '>' )
489 if ( start
< mLine
.length() )
492 fname
.append( mLine
, start
, ( i
- start
) );
497 return ""; // syntax error probably
502 /***** Implemenentation for class SourceParserBase *****/
504 SourceParserBase::SourceParserBase()
512 SourceParserBase::~SourceParserBase()
514 if ( mpFileBuf
) free( mpFileBuf
);
516 if ( mpPlugin
) delete mpPlugin
;
519 spFile
* SourceParserBase::ParseFile( const char* fname
)
521 // FIXME:: the below should not be fixed!
523 const size_t MAX_BUF_SIZE
= 1024*256;
525 if ( !mpFileBuf
) mpFileBuf
= (char*)malloc( MAX_BUF_SIZE
);
527 mFileBufSz
= MAX_BUF_SIZE
;
529 FILE* fp
= fopen( fname
, "rt" );
531 if ( (int)fp
== -1 || !fp
) return NULL
;
533 int sz
= fread( mpFileBuf
, 1, mFileBufSz
, fp
);
535 return Parse( mpFileBuf
, mpFileBuf
+ sz
);
538 void SourceParserBase::SetPlugin( SourceParserPlugin
* pPlugin
)
540 if ( mpPlugin
) delete mpPlugin
;