1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Contrib. demo
4 // Author: Aleksandras Gluchovas
8 // Copyright: (c) Aleskandars Gluchovas
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "acell.h"
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
30 #if defined( wxUSE_TEMPLATE_STL )
41 /***** Implementation for class SJParser *****/
43 // statics used by inline'ed C helper-functions
44 static char* _gSrcStart
= 0;
45 static char* _gSrcEnd
= 0;
46 static char* _gLastSuppresedComment
= 0;
47 static int _gLineNo
= 0;
49 // FOR NOW:: comments queue is static
50 #define MAX_CQ_ENTRIES 128
51 static char* _gCommentsQueue
[MAX_CQ_ENTRIES
];
52 static int _gCQSize
= 0;
54 /***** keyword map related structures *****/
58 inline bool operator()( char* x
, char* y
) const
59 { return ( strcmp( x
,y
) < 0 );
63 //WXSTL_MAP(CharPtrT,CharPtrT, LESS_THEN_FUNCTOR(CharPtrT));
65 #if defined( wxUSE_TEMPLATE_STL )
67 typedef map
< char*, char*, less_c_str
> KeywordMapT
;
71 typedef char* CharPtrT
;
72 typedef WXSTL_MAP( CharPtrT
, CharPtrT
,less_c_str
) KeywordMapT
;
76 static KeywordMapT __gMultiLangMap
;
77 static int __gMapReady
= 0;
79 static char* __gKeyWords
[] =
115 static void check_keyword_map()
121 // "make sure" the address of the first member of non-polimorphic class
122 // coinsides with the address of the instance
124 char** keyword
= __gKeyWords
;
126 while ( (*keyword
) != 0 )
128 __gMultiLangMap
.insert(
129 KeywordMapT::value_type( *keyword
, *keyword
)
137 /***** helper functions *****/
139 static inline void skip_to_eol( char*& cur
)
141 while( *(cur
) != 10 && *cur
!= 13 && cur
< _gSrcEnd
) ++cur
;
144 static inline void skip_eol( char*& cur
)
155 static inline bool skip_to_next_comment_in_the_line( char*& cur
)
159 while( cur
< _gSrcEnd
&&
165 if ( cur
== _gSrcEnd
) return FALSE
;
169 if ( (*(cur
+1) == '*') ||
170 (*(cur
+1) == '/') ) return TRUE
;
183 inline static void store_line_no( int& toVar
)
188 inline static void restore_line_no( int storedLineNo
)
190 _gLineNo
= storedLineNo
;
193 inline static int get_line_no()
198 static void skip_to_prev_line( char*& cur
)
200 while( cur
>= _gSrcStart
&&
205 // NOTE:: '\n' is 13,10 for DOS
206 // '\n' is 10 for UNIX
208 // NOTE1: '\n' symbol is not used here,
209 // to provide possibility of loading
219 if ( *cur
== 13 ) --cur
;
221 while( cur
>= _gSrcStart
&&
226 ++cur
; // move to the first character in the line
229 static inline void skip_comments( char*& cur
)
231 ++cur
; // skip '/' token
233 if ( *cur
!= '/' && *cur
!= '*' ) return;
235 // first, store position of the comment into the queue
236 // (which further will be attached to the next context
239 if ( cur
-1 != _gLastSuppresedComment
)
241 if ( _gCQSize
== MAX_CQ_ENTRIES
)
243 size_t i
= MAX_CQ_ENTRIES
-1;
247 _gCommentsQueue
[i
-1] = _gCommentsQueue
[i
];
254 _gCommentsQueue
[_gCQSize
++] = cur
-1;
257 // if signle-line comment, skip it now
267 // check for multiline comment (handle nested multiline comments!)
275 // TBD:: check eof cond.
277 // detect and remove vertical columns of '*''s
279 while ( *cur
!= '/' && cur
< _gSrcEnd
)
285 if ( *(cur
+1) != '/' )
295 case 13 : line_len
= 0; break;
296 case 10 : { line_len
= 0; ++_gLineNo
; } break;
298 default : ++line_len
;
304 if ( cur
>= _gSrcEnd
) return;
308 if ( *(cur
-2) == '*' )
326 static inline void clear_commets_queue()
331 static inline void skip_quoted_string( char*& cur
)
333 ++cur
; // skip first quote '"'
335 // check if quote wasn't prefixed
336 if ( *(cur
-2) == '\\' )
341 while ( *cur
!= '"' && cur
< _gSrcEnd
)
343 if ( *cur
== 10 ) ++_gLineNo
;
347 if ( cur
>= _gSrcEnd
) return;
349 ++cur
; // skip the last quote
351 // check if it wasn't prefixed
353 if ( *(cur
-2) != '\\' )
359 // skips subsequent white space and comments
360 // (return false if the end of source code reached)
362 static inline bool get_next_token( char*& cur
)
364 for( ; cur
< _gSrcEnd
; ++cur
)
372 case 10 : { ++_gLineNo
;continue; }
374 case '/' : skip_comments( cur
);
384 if ( cur
>= _gSrcEnd
)
391 static inline void skip_preprocessor_dir( char*& cur
)
397 if ( *(cur
-1) != '\\' )
400 if ( cur
< _gSrcEnd
)
408 static void skip_token( char*& cur
)
412 skip_quoted_string( cur
);
429 ++cur
; // leading character is always skipped
431 for( ; cur
< _gSrcEnd
; ++cur
)
444 // FIXME:: QUICK-HACK:: to treat scope resolution
445 // tokens are a part of the string - e.g. SomeSpace::SubName would
448 case ':' : if ( *(cur
+1) == ':' )
467 static inline size_t get_token_len( char* tok
)
473 return size_t( tok
- start
);
476 // returns true, if given tokens are equel
478 static inline bool cmp_tokens( char* tok1
, char* tok2
)
480 // NOTE:: the case one token includes
481 // other in it's entirely is not handled
483 size_t len
= get_token_len( tok1
);
485 // assuming that tokens are non-zero length
489 if ( *(tok1
++) != *(tok2
++) )
499 static inline bool cmp_tokens_fast( char* tok1
, char* tok2
, size_t len
)
503 if ( *(tok1
++) != *(tok2
++) )
511 static inline void skip_tempalate_statement( char*& cur
)
515 // go one level deeper
516 while( *cur
!= '<' && cur
< _gSrcEnd
)
518 if (*cur
== 10 ) ++_gLineNo
;
522 // FIXME:: template should be checked statement for
523 // comments inside of it
532 ++cur
; // skip '<' or '>' token
537 while( *cur
!= '<' && *cur
!= '>' && cur
< _gSrcEnd
)
539 if (*cur
== 10 ) ++_gLineNo
;
546 static inline void skip_statement( char*& cur
)
548 for( ; cur
< _gSrcEnd
; ++cur
)
552 case ';' : ++cur
; // skip statement-terminator token
555 case '"' : skip_quoted_string(cur
);
559 case 10 : ++_gLineNo
;
562 case '/' : skip_comments( cur
);
569 // "reversed" versions of skip_token() and get_next_token()
571 static inline void skip_token_back( char*& cur
)
573 // FIXME:: now, when moving backwards, neither strings nor
574 // comment blocks are checked
576 --cur
; // skip to the trailing character
585 for( ; cur
< _gSrcEnd
; --cur
)
602 ++cur
; // get to the leading character of the token
605 static inline void skip_next_token_back( char*& cur
)
607 --cur
; // skip leading character of the current token
618 for( ; cur
< _gSrcEnd
; --cur
)
635 ++cur
; // position after the trailing charcter of the prev token
638 static string
get_token_str( char* cur
)
640 return string( cur
, get_token_len( cur
) );
643 // skips token or whole expression which may have
644 // nested expressions between '(' ')' brackets.
646 // Upon return, the cursor points to the terminating bracket ')',
648 // Return value is the size of the block
650 static size_t skip_block( char*& cur
)
652 size_t level
= 0; // nesting level
656 // NOTE:: assumed that block not necessarely starts
657 // with bracket rightaway
668 char* savedPos
= cur
;
670 store_line_no( tmpLnNo
);
672 get_next_token( cur
);
674 if ( cur
>= _gSrcEnd
) return 0;
686 restore_line_no( tmpLnNo
);
688 return size_t(cur
-start
);
697 // QUICK-HACK::to easily handle function prototypes ,
698 // it works, besause theoretically there should
699 // be no cast-expressions in non-implementation
700 // scope (e.g. "time( (long*)(ptr+1) )" should not
701 // appear in the declarations, thus it is most likelly
702 // for the ")(" fragment to be within a function
703 // prototype in the declarations scope
711 else return size_t(cur
-start
);
719 restore_line_no( tmpLnNo
);
721 return size_t(cur
-start
);
728 // returns 0, if end of source reached
729 static inline bool skip_imp_block( char*& cur
)
731 while( *cur
!= '{' && cur
< _gSrcEnd
)
734 if ( !get_next_token( cur
) ) return FALSE
;
737 while( *cur
!= '}' && cur
< _gSrcEnd
)
740 if ( !get_next_token( cur
) ) return FALSE
;
748 static bool is_class_token( char*& cur
)
750 // FIXME:: the below mess should be cleaned in it's entirely
753 if ( *(cur
+1) == 'n' )
755 return cmp_tokens_fast( cur
, "interface", 9 );
758 if ( *(cur
+1) == 'l' )
760 return cmp_tokens_fast( cur
, "class", 5 );
763 if ( *(cur
+1) == 't' )
765 return cmp_tokens_fast( cur
, "struct", 6 );
768 if ( *(cur
+1) == 'n' )
770 return cmp_tokens_fast( cur
, "union", 5 );
775 inline static bool is_forward_decl( char* cur
)
781 case ':' : return FALSE
;
782 case '{' : return FALSE
;
783 case '(' : return FALSE
;
785 case ';' : return TRUE
;
792 } while (cur
< _gSrcEnd
); // prevent running out of bounds
797 inline static bool is_function( char* cur
, bool& isAMacro
)
802 store_line_no( tmpLnNo
);
804 // NOTE:: comments and quoted strings are not checked here
806 // first,check for "single-line hanginging macros" like:
814 get_next_token( cur
);
819 restore_line_no( tmpLnNo
);
824 // it's not a macro, go to the begining of arg. list
828 // if bracket found, it's a function or a begining
832 restore_line_no( tmpLnNo
);
836 // end of statement found without any brackets in it
837 // - it cannot be a function
841 restore_line_no( tmpLnNo
);
847 } while( cur
< _gSrcEnd
);
850 restore_line_no( tmpLnNo
);
855 // upon return the cursor is positioned after the
856 // terminating curly brace
858 static inline void skip_scope_block( char*& cur
)
862 for( ; cur
< _gSrcEnd
; ++cur
)
866 case '/' : skip_comments( cur
);
869 case '"' : skip_quoted_string( cur
);
879 ++cur
; // skip final closing curly brace
883 case 10 : ++_gLineNo
; continue;
889 // moves tokens like '*' '**', '***', '&' from the name
892 static void arrange_indirection_tokens_between( string
& type
,
895 // TBD:: FIXME:: return value of operators !
897 while ( identifier
[0] == '*' ||
901 type
+= identifier
[0];
902 identifier
.erase(0,1);
904 if ( !identifier
.length() ) return;
909 // the only function where multi-lang keyword map is accessed
911 static bool is_keyword( char* cur
)
913 size_t len
= get_token_len( cur
);
915 // put a terminating zero after the given token
916 char tmp
= *(cur
+ len
);
919 KeywordMapT::iterator i
;
921 i
= __gMultiLangMap
.find( cur
);
923 // restore original character suppresed by terminating zero
926 return ( i
!= __gMultiLangMap
.end() );
929 static inline void get_string_between( char* start
, char* end
,
939 static char* set_comment_text( string
& text
, char* start
)
943 // to avoid poluting the queue with this comment
944 _gLastSuppresedComment
= start
;
946 skip_comments( end
);
948 if ( *(end
-1) == '/' )
953 // skip multiple leading '/''s or '*''s
954 while( *start
== '/' && start
< end
) ++start
;
955 while( *start
== '*' && start
< end
) ++start
;
957 get_string_between( start
, end
, &text
);
962 /***** Implementation for class CJSourceParser *****/
964 CJSourceParser::CJSourceParser( bool collectCommnets
, bool collectMacros
)
968 mCommentsOn( collectCommnets
),
969 mMacrosOn ( collectMacros
)
974 spFile
* CJSourceParser::Parse( char* start
, char* end
)
976 // set up state variables
977 mCurVis
= SP_VIS_PRIVATE
;
979 spFile
* pTopCtx
= new spFile();
991 _gSrcEnd
= mpEnd
; // let all the C-functions "smell" the end of file
996 clear_commets_queue();
1002 if ( !get_next_token( cur
) )
1003 // end of source reached
1006 if ( memcmp( cur
, "ScriptSection( const string&",
1007 strlen( "ScriptSection( const string&" )
1019 AddMacroNode( cur
);
1050 if ( is_keyword( cur
) )
1052 // parses, token, if token identifies
1053 // the container context (e.g. class/namespace)
1054 // the corresponding context object is created
1055 // and set as current context
1057 ParseKeyword( cur
);
1061 if ( *cur
>= '0' && *cur
<= '9' )
1069 if ( mCurCtxType
!= SP_CTX_CLASS
)
1071 // FOR NOW:: disable the below assertion
1073 // DBG:: unexpected closing-bracket found
1076 skip_token( cur
); // just skip it
1080 if ( mpCurCtx
->GetType() == SP_CTX_CLASS
)
1082 int curOfs
= ( (cur
+1) - _gSrcStart
);
1084 mpCurCtx
->mContextLength
= ( curOfs
- mpCurCtx
->mSrcOffset
);
1089 // terminate operation/class/namespace context
1090 // TBD:: check if it's really this type of context
1092 wxASSERT( mpCurCtx
);
1093 mpCurCtx
= mpCurCtx
->GetOutterContext();
1094 wxASSERT( mpCurCtx
);
1096 if ( mNestingLevel
== 0 )
1099 mCurCtxType
= SP_CTX_FILE
;
1101 // not-nested class delclaration finished,
1102 // rest template flag in any case
1112 if ( is_function( cur
, isAMacro
) )
1120 char* savedPos
= cur
;
1123 store_line_no( tmpLnNo
);
1127 if ( !ParseNameAndRetVal( cur
, isAMacro
) )
1132 SkipFunction( cur
);
1137 if ( !ParseArguments( cur
) )
1139 // failure while parsing arguments,
1140 // remove enclosing operation context
1142 spContext
* pFailed
= mpCurCtx
;
1143 mpCurCtx
= mpCurCtx
->GetOutterContext();
1144 mpCurCtx
->RemoveChild( pFailed
);
1151 // otherwise, successfully close operation context:
1153 clear_commets_queue();
1155 SkipFunctionBody( cur
);
1157 mpCurCtx
= mpCurCtx
->GetOutterContext();
1160 wxASSERT( mpCurCtx
);
1164 else // otherwise it's declaration of a variable;
1166 // now, the cursor point to the end of statement (';' token)
1168 if ( mCurCtxType
!= SP_CTX_CLASS
)
1170 // non-class members are ignored
1172 skip_token( cur
); // skip the end of statement
1176 ParseMemberVar( cur
);
1182 void CJSourceParser::AttachComments( spContext
& ctx
, char* cur
)
1184 if ( !mCommentsOn
) return;
1186 MCommentListT
& lst
= ctx
.GetCommentList();
1188 char* prevComEnd
= 0;
1191 store_line_no( tmpLnNo
);
1193 // attach comments which were found before the given context
1195 for( int i
= 0; i
!= _gCQSize
; ++i
)
1197 spComment
* pComment
= new spComment();
1198 lst
.push_back( pComment
);
1200 // find the end of comment
1201 char* start
= _gCommentsQueue
[i
];
1203 pComment
->mIsMultiline
= ( *(start
+1) == '*' );
1205 // first comment in the queue and multiline
1206 // comments are always treated as a begining
1207 // of the new paragraph in the comment text
1211 pComment
->mStartsPar
= TRUE
;
1213 if ( pComment
->mIsMultiline
)
1215 pComment
->mStartsPar
= TRUE
;
1218 // find out wheather there is a new-line
1219 // between to adjecent comments
1222 char* prevLine
= start
;
1223 skip_to_prev_line(prevLine
);
1225 if ( prevLine
>= prevComEnd
)
1227 pComment
->mStartsPar
= TRUE
;
1229 pComment
->mStartsPar
= FALSE
;
1232 prevComEnd
= set_comment_text( pComment
->mText
, start
);
1236 // attach comments which are at the end of the line
1237 // of the given context (if any)
1239 if ( skip_to_next_comment_in_the_line( cur
) )
1241 spComment
* pComment
= new spComment();
1242 lst
.push_back( pComment
);
1244 set_comment_text( pComment
->mText
, cur
);
1246 pComment
->mStartsPar
= 1;
1247 pComment
->mIsMultiline
= ( *(cur
+1) == '*' );
1249 // mark this comment, so that it would not
1250 // get in the comments list of the next context
1251 _gLastSuppresedComment
= cur
;
1254 restore_line_no( tmpLnNo
);
1256 clear_commets_queue();
1259 void CJSourceParser::AddMacroNode( char*& cur
)
1263 int lineNo
= get_line_no();
1265 skip_preprocessor_dir( cur
);
1268 store_line_no( tmpLnNo
);
1270 if ( !mMacrosOn
) return;
1272 spPreprocessorLine
* pPL
= new spPreprocessorLine();
1273 pPL
->mSrcLineNo
= lineNo
;
1275 AttachComments( *pPL
, cur
);
1277 get_string_between( start
, cur
, &pPL
->mLine
);
1279 ++start
; // skip '#'
1280 get_next_token( start
);
1282 pPL
->mDefType
= SP_PREP_DEF_OTHER
;
1284 // if we found a definition or redefinition,
1285 // determine the type exactly and assign
1286 // a name to the context
1288 if ( *start
== 'd' )
1290 if ( cmp_tokens_fast( start
, "define", 6 ) )
1292 char* tok
= start
+6;
1294 get_next_token( tok
);
1296 pPL
->mName
= get_token_str( tok
);
1299 get_next_token( tok
);
1303 pPL
->mDefType
= SP_PREP_DEF_DEFINE_SYMBOL
;
1305 pPL
->mDefType
= SP_PREP_DEF_REDEFINE_SYMBOL
;
1309 if ( *start
== 'i' )
1311 if ( cmp_tokens_fast( start
, "include", 7 ) )
1313 pPL
->mDefType
= SP_PREP_DEF_INCLUDE_FILE
;
1316 mpCurCtx
->AddMember( pPL
);
1318 restore_line_no( tmpLnNo
);
1320 clear_commets_queue();
1323 void CJSourceParser::ParseKeyword( char*& cur
)
1325 // analyze token, which identifies the begining of a new context
1327 if ( CheckVisibilty( cur
) )
1333 if ( is_class_token( cur
) )
1335 if ( is_forward_decl( cur
) )
1337 // forward declarations are ignored;
1342 if ( mNestingLevel
== 0 )
1344 // change context form global class context
1345 mCurCtxType
= SP_CTX_CLASS
;
1350 // add information about new class (name, inheritance, etc)
1351 AddClassNode( cur
);
1353 // the default visiblity for class members is 'private'
1354 mCurVis
= SP_VIS_PRIVATE
;
1359 size_t len
= get_token_len( cur
);
1361 if ( cmp_tokens_fast( cur
, "typedef", len
) )
1364 get_next_token(cur
);
1366 if ( cmp_tokens_fast( cur
, "struct", len
) ||
1367 cmp_tokens_fast( cur
, "union", len
) ||
1368 cmp_tokens_fast( cur
, "class", len
)
1371 if ( mNestingLevel
== 0 )
1373 // change context form global class context
1374 mCurCtxType
= SP_CTX_CLASS
;
1379 // add information about new class (name, inheritance, etc)
1380 AddClassNode( cur
);
1382 // the default visiblity for class members is 'private'
1383 mCurVis
= SP_VIS_PRIVATE
;
1387 // FOR NOW:: typedef struct, etc are also ignored
1388 //skip_scope_block( cur );
1391 if ( cmp_tokens_fast( cur
, "enum", len
) )
1397 AddTypeDefNode( cur
);
1402 if ( cmp_tokens_fast( cur
, "enum", len
) )
1408 if ( cmp_tokens_fast( cur
, "extern", len
) )
1410 // extern's are ignored (both extern "C" and extern vars)
1411 while ( *cur
!= '{' &&
1415 get_next_token( cur
);
1420 if ( cmp_tokens_fast( cur
, "enum", len
) )
1422 // enumeration blocks are ignored
1424 skip_scope_block( cur
);
1426 get_next_token( cur
);
1427 skip_token( cur
); // skip ';' token;
1431 if ( cmp_tokens_fast( cur
, "package", len
) )
1433 // packages are ignored
1434 skip_statement( cur
);
1438 if ( cmp_tokens_fast( cur
, "import", len
) )
1440 // import statements are ignored
1441 skip_statement( cur
);
1445 if ( cmp_tokens_fast( cur
, "virtual", len
) )
1447 // probably the virtual method is in front of us;
1453 if ( cmp_tokens_fast( cur
, "template", len
) )
1456 skip_tempalate_statement( cur
);
1460 if ( cmp_tokens_fast( cur
, "friend", len
) )
1462 skip_statement( cur
);
1466 // ingnore "unsigificant" tokens (i.e. which do not
1467 // affect the current parsing context)
1472 bool CJSourceParser::ParseNameAndRetVal( char*& cur
, bool& isAMacro
)
1476 // FOR NOW:: all functions in the global
1477 // scope are ignored
1479 int lineNo
= get_line_no();
1483 while( *cur
!= '(' )
1486 if ( !get_next_token( cur
) ) return FALSE
;
1489 char* bracketPos
= cur
;
1490 char* savedPos
= cur
+ 1;
1493 store_line_no( tmpLnNo
);
1495 // skip gap between function name and start of paramters list
1496 while ( *(cur
-1) == ' ' )
1499 // check if it's not a macro, and let plugin handle it, if so
1503 skip_token_back( cur
);
1507 if ( mpPlugin
->CanUnderstandContext( tmp
, _gSrcEnd
, mpCurCtx
) )
1511 mpPlugin
->ParseContext( _gSrcStart
, cur
, _gSrcEnd
, mpCurCtx
);
1519 spOperation
* pOp
= new spOperation();
1521 pOp
->mSrcLineNo
= lineNo
;
1522 pOp
->mSrcOffset
= int( start
- _gSrcStart
);
1523 pOp
->mHeaderLength
= int( bracketPos
- start
);
1525 mpCurCtx
->AddMember( pOp
);
1526 pOp
->mVisibility
= mCurVis
;
1528 // add comments about operation
1529 AttachComments( *pOp
, cur
);
1531 // go backwards to method name
1532 skip_token_back( cur
);
1534 pOp
->mName
= get_token_str( cur
);
1536 // go backwards to method return type
1537 skip_next_token_back( cur
);
1541 pOp
->mRetType
= string( start
, size_t( cur
-start
) );
1543 arrange_indirection_tokens_between( pOp
->mRetType
, pOp
->mName
);
1546 restore_line_no( tmpLnNo
);
1548 // now, enter operation context
1554 bool CJSourceParser::ParseArguments( char*& cur
)
1558 // now cursor position is right after the first opening bracket
1559 // of the function declaration
1561 char* blocks
[16]; // used exclusivelly for iterative "lean out"
1562 // of macros and misc. not-obviouse grammar
1563 // (dirty,, but we cannot do it very nice,
1564 // we're not preprocessor-free C/C++ code)
1569 size_t blocksSkipped
= 0;
1571 get_next_token( cur
);
1575 while( *cur
!= ')' && *cur
!= ',' )
1577 blocks
[blocksSkipped
] = cur
;
1584 blockSizes
[blocksSkipped
] = size_t(cur
-prev
);
1589 blockSizes
[blocksSkipped
] = skip_block( cur
);
1591 get_next_token( cur
);
1596 if ( blocksSkipped
== 1 )
1598 // check if the empty arg. list stressed with "void" inside
1599 if ( cmp_tokens_fast( blocks
[0] , "void", 4 ) )
1602 // FIXME:: TBD:: K&R-style function declarations!
1604 // if only one block enclosed, than it's probably
1605 // some macro, there should be at least two blocks,
1606 // one for argument type and another for it's identifier
1610 if ( blocksSkipped
== 0 )
1612 if ( *cur
== 10 ) ++_gLineNo
;
1614 break; // function without paramters
1617 // we should be in the operation context now
1618 spOperation
* pOp
= (spOperation
*)mpCurCtx
;
1620 spParameter
* pPar
= new spParameter();
1622 pOp
->AddMember( pPar
);
1623 // FOR NOW:: line number is not exact if argument list is mutiline
1624 pPar
->mSrcLineNo
= get_line_no();
1626 size_t nameBlock
= blocksSkipped
- 1;
1627 size_t typeBlock
= nameBlock
- 1;
1629 // check if default values present
1630 if ( *blocks
[typeBlock
] == '=' )
1632 // expressions like "int = 5" are ignored,
1633 // since name for paramters is required
1634 if ( blocksSkipped
== 3 )
1645 pPar
->mInitVal
= string( blocks
[nameBlock
], blockSizes
[nameBlock
] );
1647 nameBlock
= nameBlock
- 2; // skip '=' token and default value block
1648 typeBlock
= nameBlock
- 1;
1651 // attach comments about the parameter
1652 AttachComments( *pPar
, blocks
[nameBlock
] );
1654 // retrieve argument name
1655 pPar
->mName
= string( blocks
[nameBlock
], blockSizes
[nameBlock
] );
1657 // retreive argument type
1659 size_t len
= blockSizes
[ typeBlock
];
1660 len
= size_t ( (blocks
[ typeBlock
] + len
) - blocks
[ 0 ] );
1662 pPar
->mType
= string( blocks
[0], len
);
1664 arrange_indirection_tokens_between( pPar
->mType
, pOp
->mName
);
1672 ++cur
; // skip comma
1673 get_next_token(cur
);
1677 // check if it was really a function not a macro,
1678 // if so, than it should be terminated with semicolon ';'
1679 // or opening implemenetaton bracket '{'
1684 store_line_no( tmpLnNo
);
1688 if ( *tok
== '{' || *tok
== ';' )
1690 restore_line_no(tmpLnNo
);
1694 // check for unexpected tokens
1695 if ( *tok
== '=' || *tok
== '0' )
1698 if ( !get_next_token(tok
) ) return FALSE
;
1702 if ( *tok
== '}' ) return FALSE
;
1704 // if initialization list found
1707 restore_line_no(tmpLnNo
);
1711 if ( cmp_tokens_fast( tok
, "const", 5 ) )
1714 if ( !get_next_token(tok
) ) return FALSE
;
1718 if ( CheckVisibilty( tok
) ) return FALSE
;
1720 // if next context found
1721 if ( is_keyword( tok
) ) return FALSE
;
1724 if ( !get_next_token(tok
) ) return FALSE
;
1731 void CJSourceParser::ParseMemberVar( char*& cur
)
1733 MMemberListT
& members
= mpCurCtx
->GetMembers();
1735 bool firstMember
= 1;
1741 // jump to the end of statement
1742 // and start collecting same-type varibles
1743 // back-to-front towards the type identifier
1745 skip_statement( cur
);
1746 char* savedPos
= cur
;
1749 store_line_no( tmpLnNo
);
1751 --cur
; // rewind back to ';'
1755 spAttribute
* pAttr
= new spAttribute();
1756 // FOR NOW:: line not is not exact, if member declaration is multiline
1757 pAttr
->mSrcLineNo
= get_line_no();
1759 mpCurCtx
->AddMember( pAttr
);
1760 pAttr
->mVisibility
= mCurVis
;
1762 pAttr
->mIsConstant
= 0;
1767 first
= members
.size() - 1;;
1770 skip_token_back( cur
);
1772 // attach comments about the attribute
1773 AttachComments( *pAttr
, cur
);
1775 pAttr
->mName
= get_token_str( cur
);
1777 // guessing that this going to be variable type
1778 skip_next_token_back( cur
);
1779 skip_token_back( cur
);
1781 pAttr
->mType
= get_token_str( cur
);
1783 // if comma, than variable list continues
1784 // otherwise the variable type reached - stop
1788 // yes, we've mistaken, it was not a identifier,
1789 // but it's default value
1793 // skip default value and '=' symbol
1794 skip_next_token_back( cur
);
1795 skip_token_back( cur
);
1797 pAttr
->mName
= get_token_str( cur
);
1799 skip_next_token_back( cur
);
1800 skip_token_back( cur
);
1805 type
= get_token_str( cur
);
1811 // set up types for all collected (same-type) attributes;
1812 while ( first
!= members
.size() - 1 )
1814 spAttribute
* pAttr
= (spAttribute
*)members
[first
];
1816 pAttr
->mType
= type
;
1817 pAttr
->mVisibility
= mCurVis
;
1819 arrange_indirection_tokens_between( pAttr
->mType
, pAttr
->mName
);
1825 restore_line_no( tmpLnNo
);
1827 clear_commets_queue();
1832 void CJSourceParser::SkipFunction( char*& cur
)
1834 while ( *cur
!= '(' && cur
< _gSrcEnd
)
1836 if (*cur
== 10 ) ++_gLineNo
;
1840 skip_next_token_back( cur
); // go back and skip function identifier
1841 skip_token_back( cur
); // go back and skip return type
1843 skip_block( cur
); // now, go ahead and skip whole declaration
1845 SkipFunctionBody( cur
);
1849 void CJSourceParser::SkipFunctionBody( char*& cur
)
1851 // FIXME:: check for comments and quoted stirngs here
1853 bool hasDefinition
= FALSE
;
1855 while( *cur
!= '{' && *cur
!= ';' )
1857 if (*cur
== 10 ) ++_gLineNo
;
1867 hasDefinition
= TRUE
;
1869 skip_scope_block( cur
); // skip the whole imp.
1872 if ( mpCurCtx
->GetType() == SP_CTX_OPERATION
)
1874 spOperation
& op
= *((spOperation
*)mpCurCtx
);
1876 int curOfs
= int ( cur
- _gSrcStart
);
1878 op
.mContextLength
= curOfs
- mpCurCtx
->mSrcOffset
;
1880 op
.mHasDefinition
= hasDefinition
;
1882 // separate scope resolution token from the name of operation
1884 for( size_t i
= 0; i
!= op
.mName
.length(); ++i
)
1886 if ( op
.mName
[i
] == ':' && op
.mName
[i
+1] == ':' )
1888 string
unscoped( op
.mName
, i
+2, op
.mName
.length() - ( i
+ 2 ) );
1890 op
.mScope
= string( op
.mName
, 0, i
);
1892 op
.mName
= unscoped
;
1900 bool CJSourceParser::CheckVisibilty( char*& cur
)
1902 size_t len
= get_token_len( cur
);
1904 if ( cmp_tokens_fast( cur
, "public:", len
) )
1906 mCurVis
= SP_VIS_PUBLIC
;
1910 if ( cmp_tokens_fast( cur
, "protected:", len
) )
1912 mCurVis
= SP_VIS_PROTECTED
;
1916 if ( cmp_tokens_fast( cur
, "private:", len
) )
1918 mCurVis
= SP_VIS_PRIVATE
;
1925 void CJSourceParser::AddClassNode( char*& cur
)
1927 char* ctxStart
= cur
;
1929 skip_token( cur
); // skip 'class' keyword
1930 if ( !get_next_token( cur
) ) return;
1936 get_next_token( cur
);
1939 spClass
* pClass
= new spClass();
1942 mpCurCtx
->AddMember( pClass
);
1944 // by default all class members are private
1945 mCurVis
= SP_VIS_PRIVATE
;
1947 // attach comments about the class
1948 AttachComments( *pClass
, cur
);
1950 pClass
->mSrcLineNo
= get_line_no();
1952 pClass
->mSrcOffset
= int( ctxStart
- _gSrcStart
);
1954 char* nameTok
= cur
;
1955 pClass
->mName
= get_token_str( cur
);
1964 if ( !get_next_token( cur
) ) return;
1973 store_line_no( tmpLn
);
1975 skip_next_token_back( tok
);
1976 skip_token_back( tok
);
1978 restore_line_no( tmpLn
);
1980 // class name should precend ':' colon, thus
1981 // the one which was captured before was
1982 // proablty something else (like __dllexport MyClass : ... )
1984 if ( nameTok
!= tok
)
1986 pClass
->mName
= get_token_str( tok
);
1997 size_t len
= get_token_len( cur
);
1999 // skip neglectable C++ modifieres
2000 if ( cmp_tokens_fast( cur
, "public", len
) )
2003 if ( cmp_tokens_fast( cur
, "protected", len
) )
2006 if ( cmp_tokens_fast( cur
, "private", len
) )
2009 if ( cmp_tokens_fast( cur
, "virtual", len
) )
2012 // skip neglectable JAVA modifieres
2014 if ( cmp_tokens_fast( cur
, "extends", len
) )
2020 if ( cmp_tokens_fast( cur
, "implements", len
) )
2026 // all we need to know is superclass or interface
2030 store_line_no( tmpLn
);
2033 get_next_token(tok
);
2035 restore_line_no( tmpLn
);
2037 if ( *tok
!= ':' && *cur
!= ':' )
2039 pClass
->mSuperClassNames
.push_back( string( cur
, len
) );
2046 store_line_no( tmpLn
);
2048 while ( pClass
->mSuperClassNames
.size() )
2050 pClass
->mSuperClassNames
.erase( &pClass
->mSuperClassNames
[0] );
2054 // some non-obviouse token was following "class" keyword -
2055 // we've confused it with class name - thus now we're reverting this mistake
2057 skip_next_token_back( tok
);
2058 skip_token_back( tok
);
2060 pClass
->mName
= get_token_str( tok
);
2062 restore_line_no( tmpLn
);
2066 ++cur
; // skip opening curly brace
2068 pClass
->mHeaderLength
= ( cur
- ctxStart
);
2070 // now, enter the class context
2073 clear_commets_queue();
2076 void CJSourceParser::AddEnumNode( char*& cur
)
2078 // now the cursor is at "enum" keyword
2081 spEnumeration
* pEnum
= new spEnumeration();
2082 mpCurCtx
->AddMember( pEnum
);
2084 pEnum
->mSrcLineNo
= get_line_no();
2087 AttachComments( *pEnum
, cur
);
2090 if ( !get_next_token( cur
) ) return;
2092 // check if enumeration has got it's identifier
2095 pEnum
->mName
= get_token_str( cur
);
2098 if ( !skip_imp_block( cur
) ) return;
2100 get_string_between( start
, cur
, &pEnum
->mEnumContent
);
2102 if ( get_next_token(cur
) )
2104 // check if the identifier if after the {...} block
2107 pEnum
->mName
= get_token_str( cur
);
2110 clear_commets_queue();
2113 void CJSourceParser::AddTypeDefNode( char*& cur
)
2115 // now the cursor at the token next to "typedef" keyword
2117 if ( !get_next_token(cur
) ) return;
2121 spTypeDef
* pTDef
= new spTypeDef();
2122 mpCurCtx
->AddMember( pTDef
);
2124 pTDef
->mSrcLineNo
= get_line_no();
2126 AttachComments( *pTDef
, cur
);
2128 skip_statement( cur
);
2131 store_line_no( tmpLnNo
);
2134 skip_next_token_back( tok
);
2136 char* nameEnd
= tok
;
2138 skip_token_back( tok
);
2140 char* nameStart
= tok
;
2142 skip_next_token_back( tok
);
2144 char* typeEnd
= tok
;
2146 // check if it's function prototype
2147 if ( *nameStart
== ')' )
2149 typeEnd
= nameStart
+1;
2151 // skip argument list
2152 while ( *nameStart
!= '(' ) --nameStart
;
2154 // skip to function type definition
2155 while ( *nameStart
!= ')' ) --nameStart
;
2157 skip_next_token_back( nameStart
);
2159 nameEnd
= nameStart
;
2161 skip_token_back( nameStart
);
2163 if ( *nameStart
== '*' ) ++nameStart
;
2166 get_string_between( start
, typeEnd
, &pTDef
->mOriginalType
);
2168 get_string_between( nameStart
, nameEnd
, &pTDef
->mName
);
2170 clear_commets_queue();
2172 restore_line_no( tmpLnNo
);