]>
Commit | Line | Data |
---|---|---|
cecfc5e7 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: No names yet. | |
3 | // Purpose: Contrib. demo | |
4 | // Author: Aleksandras Gluchovas | |
5 | // Modified by: | |
6 | // Created: 22/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Aleskandars Gluchovas | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "srcparser.h" | |
14 | #pragma interface | |
15 | #endif | |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/wx.h" | |
26 | #endif | |
27 | ||
28 | #include <malloc.h> | |
29 | #include <stdio.h> | |
30 | ||
31 | #include "srcparser.h" | |
32 | ||
33 | /***** Implementation for class spVisitor *****/ | |
34 | ||
35 | void spVisitor::VisitAll( spContext& atContext, | |
36 | bool sortContent | |
37 | ) | |
38 | { | |
39 | mSiblingSkipped = FALSE; | |
40 | mChildSkipped = FALSE; | |
41 | mContextMask = SP_CTX_ANY; // FIXME:: should be an arg. | |
42 | ||
43 | if ( sortContent && !atContext.IsSorted() ) | |
44 | ||
45 | atContext.SortMembers(); | |
46 | ||
47 | mpCurCxt = &atContext; // FIXME:: this is dirty, restoring it each time | |
48 | ||
49 | if ( atContext.GetContextType() & mContextMask ) | |
50 | ||
51 | atContext.AcceptVisitor( *this ); | |
52 | ||
53 | MMemberListT& members = atContext.GetMembers(); | |
54 | ||
55 | for( size_t i = 0; i != members.size(); ++i ) | |
56 | { | |
57 | if ( mSiblingSkipped ) | |
58 | ||
59 | return; | |
60 | ||
61 | if ( !mChildSkipped ) | |
62 | { | |
63 | size_t prevSz = members.size(); | |
64 | ||
65 | // visit members of the context recursivelly | |
66 | VisitAll( *members[i], sortContent ); | |
67 | ||
68 | if ( members.size() != prevSz ) | |
69 | ||
70 | --i; // current member was removed! | |
71 | ||
72 | mChildSkipped = 0; | |
73 | } | |
74 | } | |
75 | } | |
76 | ||
77 | void spVisitor::RemoveCurrentContext() | |
78 | { | |
79 | if ( mpCurCxt->GetParent() ) | |
80 | ||
81 | mpCurCxt->GetParent()->RemoveChild( mpCurCxt ); | |
82 | } | |
83 | ||
84 | void spVisitor::SkipSiblings() | |
85 | { | |
86 | mSiblingSkipped = TRUE; | |
87 | } | |
88 | ||
89 | void spVisitor::SkipChildren() | |
90 | { | |
91 | mChildSkipped = TRUE; | |
92 | } | |
93 | ||
94 | void spVisitor::SetFilter( int contextMask ) | |
95 | { | |
96 | mContextMask = contextMask; | |
97 | } | |
98 | ||
99 | /***** Implementation for class spComment *****/ | |
100 | ||
101 | bool spComment::IsMultiline() const | |
102 | { | |
103 | return mIsMultiline; | |
104 | } | |
105 | ||
106 | bool spComment::StartsParagraph() const | |
107 | { | |
108 | return mStartsPar; | |
109 | } | |
110 | ||
111 | string& spComment::GetText() | |
112 | { | |
113 | return mText; | |
114 | } | |
115 | ||
116 | string spComment::GetText() const | |
117 | { | |
118 | return mText; | |
119 | } | |
120 | ||
121 | /***** Implementation for class spContext *****/ | |
122 | ||
123 | spContext::spContext() | |
124 | ||
125 | : mpParent ( NULL ), | |
126 | mpFirstOccurence( NULL ), | |
127 | mAlreadySorted ( FALSE ), | |
128 | ||
129 | mSrcLineNo (-1), | |
130 | mSrcOffset (-1), | |
131 | mContextLength(-1), | |
132 | mLastScrLineNo(-1), | |
133 | ||
134 | mHeaderLength (-1), | |
135 | mFooterLength (-1), | |
136 | ||
137 | mFirstCharPos (-1), | |
138 | mLastCharPos (-1), | |
139 | ||
140 | mVisibility( SP_VIS_PRIVATE ), | |
141 | ||
142 | mIsVirtualContext ( FALSE ), | |
143 | mVirtualContextHasChildren( FALSE ), | |
144 | ||
145 | mpUserData( NULL ) | |
146 | {} | |
147 | ||
148 | void spContext::RemoveChildren() | |
149 | { | |
150 | for( size_t i = 0; i != mMembers.size(); ++i ) | |
151 | ||
152 | delete mMembers[i]; | |
153 | ||
154 | mMembers.erase( mMembers.begin(), mMembers.end() ); | |
155 | } | |
156 | ||
157 | spContext::~spContext() | |
158 | { | |
159 | RemoveChildren(); | |
160 | ||
161 | for( size_t i = 0; i != mComments.size(); ++i ) | |
162 | ||
163 | delete mComments[i]; | |
164 | } | |
165 | ||
166 | bool spContext::IsSorted() | |
167 | { | |
168 | return mAlreadySorted; | |
169 | } | |
170 | ||
171 | void spContext::GetContextList( MMemberListT& lst, int contextMask ) | |
172 | { | |
173 | for( size_t i = 0; i != mMembers.size(); ++i ) | |
174 | { | |
175 | spContext& member = *mMembers[i]; | |
176 | ||
177 | if ( member.GetContextType() & contextMask ) | |
178 | ||
179 | lst.push_back( &member ); | |
180 | ||
181 | // collect required contexts recursively | |
182 | member.GetContextList( lst, contextMask ); | |
183 | } | |
184 | } | |
185 | ||
186 | bool spContext::HasComments() | |
187 | { | |
188 | return ( mComments.size() != 0 ); | |
189 | } | |
190 | ||
191 | void spContext::RemoveChild( spContext* pChild ) | |
192 | { | |
193 | for( size_t i = 0; i != mMembers.size(); ++i ) | |
194 | ||
195 | if ( mMembers[i] == pChild ) | |
196 | { | |
197 | mMembers.erase( &mMembers[i] ); | |
198 | ||
199 | delete pChild; | |
200 | return; | |
201 | } | |
202 | ||
203 | // the given child should exist on the parent's list | |
204 | wxASSERT( 0 ); | |
205 | } | |
206 | ||
207 | spContext* spContext::GetEnclosingContext( int mask ) | |
208 | { | |
209 | spContext* cur = this->GetParent(); | |
210 | ||
211 | while ( cur && !(cur->GetContextType() & mask) ) | |
212 | ||
213 | cur = cur->GetParent(); | |
214 | ||
215 | return cur; | |
216 | } | |
217 | ||
218 | bool spContext::PositionIsKnown() | |
219 | { | |
220 | return ( mSrcOffset != (-1) && mContextLength != (-1) ); | |
221 | } | |
222 | ||
223 | bool spContext::IsVirtualContext() | |
224 | { | |
225 | return mIsVirtualContext; | |
226 | } | |
227 | ||
228 | bool spContext::VitualContextHasChildren() | |
229 | { | |
230 | return mVirtualContextHasChildren; | |
231 | } | |
232 | ||
233 | string spContext::GetVirtualContextBody() | |
234 | { | |
235 | wxASSERT( mIsVirtualContext ); | |
236 | ||
237 | return mVirtualContextBody; | |
238 | } | |
239 | ||
240 | string spContext::GetFooterOfVirtualContextBody() | |
241 | { | |
242 | wxASSERT( mIsVirtualContext ); | |
243 | ||
244 | return mVittualContextFooter; | |
245 | } | |
246 | ||
247 | ||
248 | void spContext::SetVirtualContextBody( const string& body, | |
249 | bool hasChildren, | |
250 | const string& footer ) | |
251 | { | |
252 | mVirtualContextHasChildren = hasChildren; | |
253 | ||
254 | mVirtualContextBody = body; | |
255 | mVittualContextFooter = footer; | |
256 | ||
257 | // atuomaticllay becomes virtual context | |
258 | ||
259 | mIsVirtualContext = TRUE; | |
260 | } | |
261 | ||
262 | string spContext::GetBody( spContext* pCtx ) | |
263 | { | |
264 | if ( ( pCtx == NULL || pCtx == this ) && mIsVirtualContext ) | |
265 | ||
266 | return mVirtualContextBody; | |
267 | ||
268 | if ( GetParent() ) | |
269 | ||
270 | return GetParent()->GetBody( ( pCtx != NULL ) ? pCtx : this ); | |
271 | else | |
272 | return ""; // source-fragment cannot be found | |
273 | } | |
274 | ||
275 | string spContext::GetHeader( spContext* pCtx ) | |
276 | { | |
277 | if ( GetParent() ) | |
278 | ||
279 | return GetParent()->GetHeader( ( pCtx != NULL ) ? pCtx : this ); | |
280 | else | |
281 | return ""; // source-fragment cannot be found | |
282 | } | |
283 | ||
284 | bool spContext::IsFirstOccurence() | |
285 | { | |
286 | return ( mpFirstOccurence != 0 ); | |
287 | } | |
288 | ||
289 | spContext* spContext::GetFirstOccurence() | |
290 | { | |
291 | // this object should not itself be | |
292 | // the first occurence of the context | |
293 | wxASSERT( mpFirstOccurence != 0 ); | |
294 | ||
295 | return mpFirstOccurence; | |
296 | } | |
297 | ||
298 | void spContext::AddMember( spContext* pMember ) | |
299 | { | |
300 | mMembers.push_back( pMember ); | |
301 | ||
302 | pMember->mpParent = this; | |
303 | } | |
304 | ||
305 | void spContext::AddComment( spComment* pComment ) | |
306 | { | |
307 | mComments.push_back( pComment ); | |
308 | } | |
309 | ||
310 | MMemberListT& spContext::GetMembers() | |
311 | { | |
312 | return mMembers; | |
313 | } | |
314 | ||
315 | spContext* spContext::FindContext( const string& identifier, | |
316 | int contextType, | |
317 | bool searchSubMembers | |
318 | ) | |
319 | { | |
320 | for( size_t i = 0; i != mMembers.size(); ++i ) | |
321 | { | |
322 | spContext& member = *mMembers[i]; | |
323 | ||
324 | if ( member.GetName() == identifier && | |
325 | ( contextType & member.GetContextType() ) | |
326 | ) | |
327 | ||
328 | return &member; | |
329 | ||
330 | if ( searchSubMembers ) | |
331 | { | |
332 | spContext* result = | |
333 | member.FindContext( identifier, contextType, 1 ); | |
334 | ||
335 | if ( result ) return result; | |
336 | } | |
337 | } | |
338 | ||
339 | return 0; | |
340 | } | |
341 | ||
342 | void spContext::RemoveThisContext() | |
343 | { | |
344 | if ( mpParent ) | |
345 | mpParent->RemoveChild( this ); | |
346 | else | |
347 | // context should have a parent | |
348 | wxASSERT(0); | |
349 | } | |
350 | ||
351 | spContext* spContext::GetOutterContext() | |
352 | { | |
353 | return mpParent; | |
354 | } | |
355 | ||
356 | bool spContext::HasOutterContext() | |
357 | { | |
358 | return ( mpParent != 0 ); | |
359 | } | |
360 | ||
361 | bool spContext::IsInFile() | |
362 | { | |
363 | return ( GetOutterContext()->GetContextType() == SP_CTX_FILE ); | |
364 | } | |
365 | ||
366 | bool spContext::IsInNameSpace() | |
367 | { | |
368 | return ( GetOutterContext()->GetContextType() == SP_CTX_NAMESPACE ); | |
369 | } | |
370 | ||
371 | bool spContext::IsInClass() | |
372 | { | |
373 | return ( GetOutterContext()->GetContextType() == SP_CTX_CLASS ); | |
374 | } | |
375 | ||
376 | bool spContext::IsInOperation() | |
377 | { | |
378 | return ( GetOutterContext()->GetContextType() == SP_CTX_OPERATION ); | |
379 | } | |
380 | ||
381 | spClass& spContext::GetClass() | |
382 | { | |
383 | wxASSERT( GetOutterContext()->GetType() == SP_CTX_CLASS ); | |
384 | return *((spClass*)mpParent ); | |
385 | } | |
386 | ||
387 | spFile& spContext::GetFile() | |
388 | { | |
389 | wxASSERT( GetOutterContext()->GetType() == SP_CTX_FILE ); | |
390 | return *((spFile*)mpParent ); | |
391 | } | |
392 | ||
393 | spNameSpace& spContext::GetNameSpace() | |
394 | { | |
395 | wxASSERT( GetOutterContext()->GetType() == SP_CTX_NAMESPACE ); | |
396 | return *((spNameSpace*)mpParent ); | |
397 | } | |
398 | ||
399 | spOperation& spContext::GetOperation() | |
400 | { | |
401 | wxASSERT( GetOutterContext()->GetType() == SP_CTX_OPERATION ); | |
402 | return *((spOperation*)mpParent ); | |
403 | } | |
404 | ||
405 | /***** Implementation for class spClass *****/ | |
406 | ||
407 | void spClass::SortMembers() | |
408 | { | |
409 | // TBD:: | |
410 | } | |
411 | ||
412 | /***** Implementation for class spOperation *****/ | |
413 | ||
414 | spOperation::spOperation() | |
415 | ||
416 | : mHasDefinition( FALSE ) | |
417 | {} | |
418 | ||
419 | string spOperation::GetFullName(MarkupTagsT tags) | |
420 | { | |
421 | string txt = tags[TAG_BOLD].start + mRetType; | |
422 | txt += " "; | |
423 | txt += mName; | |
424 | txt += "( "; | |
425 | txt += tags[TAG_BOLD].end; | |
426 | ||
427 | for( size_t i = 0; i != mMembers.size(); ++i ) | |
428 | { | |
429 | // DBG:: | |
430 | wxASSERT( mMembers[i]->GetContextType() == SP_CTX_PARAMETER ); | |
431 | ||
432 | spParameter& param = *((spParameter*)mMembers[i]); | |
433 | ||
434 | if ( i != 0 ) | |
435 | txt += ", "; | |
436 | ||
437 | txt += tags[TAG_BOLD].start; | |
438 | ||
439 | txt += param.mType; | |
440 | ||
441 | txt += tags[TAG_BOLD].end; | |
442 | txt += tags[TAG_ITALIC].start; | |
443 | ||
444 | txt += " "; | |
445 | txt += param.mName; | |
446 | ||
447 | if ( param.mInitVal != "" ) | |
448 | { | |
449 | txt += " = "; | |
450 | txt += tags[TAG_BOLD].start; | |
451 | ||
452 | txt += param.mInitVal; | |
453 | ||
454 | txt += tags[TAG_BOLD].end; | |
455 | } | |
456 | ||
457 | txt += tags[TAG_ITALIC].end;; | |
458 | } | |
459 | ||
460 | txt += tags[TAG_BOLD].start; | |
461 | txt += " )"; | |
462 | txt += tags[TAG_BOLD].end; | |
463 | ||
464 | // TBD:: constantness of method | |
465 | ||
466 | return txt; | |
467 | } | |
468 | ||
469 | /***** Implemenentation for class spPreprocessorLine *****/ | |
470 | ||
471 | string spPreprocessorLine::CPP_GetIncludedFileNeme() | |
472 | { | |
473 | wxASSERT( GetStatementType() == SP_PREP_DEF_INCLUDE_FILE ); | |
474 | ||
475 | size_t i = 0; | |
476 | ||
477 | while( i < mLine.length() && mLine[i] != '"' && mLine[i] != '<' ) | |
478 | ||
479 | ++i; | |
480 | ||
481 | ++i; | |
482 | ||
483 | size_t start = i; | |
484 | ||
485 | while( i < mLine.length() && mLine[i] != '"' && mLine[i] != '>' ) | |
486 | ||
487 | ++i; | |
488 | ||
489 | if ( start < mLine.length() ) | |
490 | { | |
491 | string fname; | |
492 | fname.append( mLine, start, ( i - start ) ); | |
493 | ||
494 | return fname; | |
495 | } | |
496 | else | |
497 | return ""; // syntax error probably | |
498 | } | |
499 | ||
500 | ||
501 | ||
502 | /***** Implemenentation for class SourceParserBase *****/ | |
503 | ||
504 | SourceParserBase::SourceParserBase() | |
505 | ||
506 | : mpFileBuf( NULL ), | |
507 | mFileBufSz( 0 ), | |
508 | ||
509 | mpPlugin( NULL ) | |
510 | {} | |
511 | ||
512 | SourceParserBase::~SourceParserBase() | |
513 | { | |
514 | if ( mpFileBuf ) free( mpFileBuf ); | |
515 | ||
516 | if ( mpPlugin ) delete mpPlugin; | |
517 | } | |
518 | ||
519 | spFile* SourceParserBase::ParseFile( const char* fname ) | |
520 | { | |
521 | // FIXME:: the below should not be fixed! | |
522 | ||
523 | const size_t MAX_BUF_SIZE = 1024*256; | |
524 | ||
525 | if ( !mpFileBuf ) mpFileBuf = (char*)malloc( MAX_BUF_SIZE ); | |
526 | ||
527 | mFileBufSz = MAX_BUF_SIZE; | |
528 | ||
529 | FILE* fp = fopen( fname, "rt" ); | |
530 | ||
531 | if ( (int)fp == -1 || !fp ) return NULL; | |
532 | ||
533 | int sz = fread( mpFileBuf, 1, mFileBufSz, fp ); | |
534 | ||
535 | return Parse( mpFileBuf, mpFileBuf + sz ); | |
536 | } | |
537 | ||
538 | void SourceParserBase::SetPlugin( SourceParserPlugin* pPlugin ) | |
539 | { | |
540 | if ( mpPlugin ) delete mpPlugin; | |
541 | ||
542 | mpPlugin = pPlugin; | |
543 | } |