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"
23 #if defined( wxUSE_TEMPLATE_STL )
32 #include "sourcepainter.h"
34 const int MAX_KEYWORD_LEN
= 16;
38 char keyWord
[MAX_KEYWORD_LEN
];
42 // source fragment ranks :
48 // multil-language keywords map
50 static KeywordT __gKeyWords
[] =
122 ////////////////////////////////////////////////////
156 ////////////////////////////////////////////////////
158 { "dynamic_cast", 1 },
161 //////// some hacks for VB /////////
170 /////// data types ///////
251 { "throw", 2 }, // C++
252 { "throws", 1 }, // Java
270 //////////// meta-information //////////////
301 { "implementation", 2 },
302 { "Implementation", 2 },
303 { "IMPLEMENTATION", 2 },
360 { "[ag_slammer]",2 },
361 { "Aleksandras", 2 },
378 inline bool operator()( char* x
, char* y
) const
379 { return ( strcmp( x
,y
) < 0 );
383 #if defined( wxUSE_TEMPLATE_STL )
385 typedef map
< char*, char*, less_c_str
> KeywordMapT
;
389 typedef char* CharPtrT
;
390 typedef WXSTL_MAP( CharPtrT
, CharPtrT
,less_c_str
) KeywordMapT
;
394 static KeywordMapT __gMultiLangMap
;
395 static int __gMapReady
= 0;
397 void check_keyword_map( int keywordMapNr
)
403 // "make sure" the address of the first member of non-polimorphic class
404 // coinsides with the address of the instance
409 if ( (char*)& dummy != &dummy.keyWord[0] )
413 int size
= sizeof(__gKeyWords
) / sizeof( KeywordT
);
415 for( int i
= 0; i
!= size
; ++i
)
417 __gMultiLangMap
.insert(
418 KeywordMapT::value_type( (char*)&__gKeyWords
[i
],
419 (char*)&__gKeyWords
[i
]
425 int get_rank( char* start
, char* end
)
427 // FIXME:: what if end is no longer leagal adress?
430 *end
= '\0'; // put temporary terminator
432 KeywordMapT::iterator i
;
434 if ( (i
= __gMultiLangMap
.find( start
) ) != __gMultiLangMap
.end() )
436 KeywordT
* pKey
= (KeywordT
*)(*i
).second
;
449 static inline void store_range( SPBlockListT
& results
, int rank
, int range_len
)
451 if ( !range_len
) return;
453 results
.push_back ( ( rank
<< 16 ) | ( range_len
) );
457 #define STORE_RANGE store_range( results, cur_rank, cur_range_len );\
458 cur_rank = cur_range_len = 0;
460 #define NEXT_CHAR cur_range_len++; \
464 static inline int is_alpha( char ch
)
466 return ( (( ch
>= '_' ) && ( ch
<= 'z' )) ||
467 (( ch
>= 'A' ) && ( ch
<= 'Z' ))
472 // Ziema atEjo netikEtai
474 static void heighlight_syntax( char* str
, int strLen
,
475 SPBlockListT
& results
, bool& isComment
)
477 bool isMultiline
= 0;
479 char* end
= str
+ strLen
;
481 int cur_rank
= ( isComment
== 1 ) ? RANK_GREEN
: RANK_BLACK
;
482 int cur_range_len
= 0;
486 int has_next
= ( cur
+1 != end
);
491 if ( has_next
&& *(cur
+1) == '/' )
493 // turn off multiline comment mode
513 cur_rank = RANK_GREEN;
514 cur_range_len = end - cur;
525 if ( *(cur
+1) == '/' )
530 while ( eol
< end
&& *eol
!= 10 )
533 cur_rank
= RANK_GREEN
;
534 cur_range_len
= eol
- cur
;
541 if ( *(cur
+1) == '*' )
544 cur_rank
= RANK_GREEN
;
556 if ( ( is_alpha( *cur
) || *(cur
) == '#' )
560 if ( is_alpha( *(cur
+1) ) )
565 while ( cur
!= end
&& is_alpha(*cur
) ) ++cur
;
569 if ( (wordRank
= get_rank( start
, cur
)) > 0 )
573 store_range( results
, wordRank
, int(cur
-start
) );
574 cur_rank
= cur_range_len
= 0;
578 cur_range_len
+= ( cur
-start
);
588 if ( cur_range_len
> 0 ) STORE_RANGE
;
591 /***** Implementation for class SourcePainter ******/
593 SourcePainter::SourcePainter( bool assembleResultString
)
594 : mCollectResultsOn( assembleResultString
),
595 mIsInComment( FALSE
),
596 mCommentIsMultiline( FALSE
)
598 check_keyword_map(0);
601 void SourcePainter::ProcessSource( char* src
, int srcLen
)
603 // TBD:: multilne state...
605 heighlight_syntax( src
, srcLen
, mBlocks
, mIsInComment
);
607 if ( mCollectResultsOn
)
609 mResultStr
+= string( src
, srcLen
);
612 void SourcePainter::SetState( bool isInComment
,
613 bool commentIsMultiline
)
615 mIsInComment
= isInComment
;
616 mCommentIsMultiline
= commentIsMultiline
;
619 void SourcePainter::Init(bool assembleResultString
)
622 mCommentIsMultiline
= 0;
623 mCollectResultsOn
= assembleResultString
;
627 mBlocks
.erase( mBlocks
.begin(), mBlocks
.end() );
630 static int rank_tags_map
[] =
638 void SourcePainter::GetResultString(string
& result
, MarkupTagsT tags
)
640 // this method works, only if results of processing
642 // ASSERT( mCollectResultsOn );
647 for( size_t i
= 0; i
!= mBlocks
.size(); ++i
)
649 int desc
= mBlocks
[i
];
651 int len
= desc
& 0xFFFF;
652 int rank
= (desc
>> 16) & 0xFFFF;
654 result
+= tags
[ rank_tags_map
[rank
] ].start
;
656 for( int n
= 0; n
!= len
; ++n
)
658 result
+= mResultStr
[pos
+n
];
662 result
+= tags
[ rank_tags_map
[rank
] ].end
;
666 SPBlockListT
& SourcePainter::GetBlocks()
671 bool SourcePainter::IsKeyword( char* word
, int wordLen
)
673 check_keyword_map(0);
675 int rank
= get_rank( word
, word
+ wordLen
);
677 return ( rank
== RANK_BLUE
|| rank
== RANK_RED
);