#include #include #include #define IDENT_BUFLEN 256 @interface Clang : NSObject { @public /* State machine operation data. */ int cs; /* Parsing data. */ char identBuf[IDENT_BUFLEN+1]; int identLen; int curLine; }; - (void) initFsm; - (void) executeWithData:(const char *)data len:(int)len; - (int) finish; @end @implementation Clang static const char _Clang_actions [] = { 0, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 2, 0, 8, 2, 1, 0, 2, 2, 1, 2, 2, 8, 2, 3, 1, 2, 3, 8, 2, 4, 1, 2, 4, 8, 2, 5, 1, 2, 5, 8, 2, 6, 1, 2, 6, 8, 2, 7, 1, 2, 7, 8, 3, 2, 1, 0, 3, 3, 1, 0, 3, 4, 1, 0, 3, 5, 1, 0, 3, 6, 1, 0, 3, 7, 1, 0, 0 , }; static const char _Clang_trans_keys [] = { 1, 0, 0, 12, 0, 12, 0, 0, 0, 5, 0, 7, 0, 0, 8, 9, 8, 10, 0, 0, 0, 13, 0, 13, 0, 13, 0, 13, 0, 13, 0, 13, 0, 13, 0, 13, 0, 13, 0 , }; static const char _Clang_char_class [] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 2, 2, 2, 2, 4, 2, 2, 5, 2, 2, 2, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 2, 2, 2, 2, 2, 2, 2, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 2, 12, 2, 2, 11, 2, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 13, 11, 11, 2, 2, 2, 2, 0 , }; static const short _Clang_index_offsets [] = { 0, 0, 13, 26, 27, 33, 41, 42, 44, 47, 48, 62, 76, 90, 104, 118, 132, 146, 160, 0 , }; static const char _Clang_indicies [] = { 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 6, 5, 8, 7, 7, 7, 7, 9, 8, 7, 7, 7, 7, 9, 7, 10, 12, 13, 13, 15, 15, 15, 1, 12, 10, 16, 17, 18, 16, 16, 19, 20, 21, 22, 22, 16, 22, 24, 23, 25, 26, 27, 25, 25, 28, 29, 30, 31, 31, 25, 31, 33, 32, 34, 35, 36, 34, 34, 37, 38, 39, 40, 40, 34, 40, 24, 23, 25, 26, 27, 7, 25, 11, 29, 30, 31, 31, 25, 31, 42, 41, 43, 44, 45, 43, 46, 47, 48, 48, 49, 49, 43, 50, 52, 51, 53, 54, 55, 53, 53, 56, 13, 13, 57, 57, 53, 57, 59, 58, 60, 61, 62, 60, 60, 63, 64, 64, 64, 64, 60, 64, 42, 41, 43, 44, 45, 43, 46, 47, 48, 48, 49, 49, 43, 49, 66, 65, 67, 68, 69, 67, 67, 70, 71, 71, 71, 72, 67, 72, 0 , }; static const char _Clang_index_defaults [] = { 0, 0, 4, 4, 7, 7, 11, 14, 14, 0, 10, 23, 32, 23, 41, 51, 58, 41, 65, 0 , }; static const char _Clang_trans_cond_spaces [] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0 , }; static const char _Clang_cond_targs [] = { 1, 1, 12, 9, 2, 2, 3, 4, 4, 5, 10, 6, 10, 15, 0, 18, 11, 1, 2, 13, 14, 17, 16, 10, 10, 11, 1, 2, 13, 14, 17, 16, 10, 10, 11, 1, 2, 13, 14, 17, 16, 10, 10, 11, 1, 2, 7, 13, 17, 16, 8, 10, 10, 11, 1, 2, 13, 16, 10, 10, 11, 1, 2, 13, 16, 10, 10, 11, 1, 2, 13, 18, 16, 0 , }; static const char _Clang_cond_actions [] = { 1, 19, 0, 0, 1, 19, 0, 0, 17, 0, 0, 0, 17, 1, 0, 22, 22, 3, 3, 22, 22, 22, 22, 15, 58, 81, 55, 55, 81, 81, 81, 81, 7, 34, 65, 31, 31, 65, 65, 65, 65, 11, 46, 73, 43, 43, 1, 73, 1, 73, 0, 9, 40, 69, 37, 37, 69, 69, 5, 28, 61, 25, 25, 61, 1, 13, 52, 77, 49, 49, 77, 1, 77, 0 , }; static const char _Clang_eof_actions [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 7, 15, 11, 9, 5, 11, 13, 0 , }; static const char _Clang_nfa_targs [] = { 0, 0 , }; static const char _Clang_nfa_offsets [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , }; static const char _Clang_nfa_push_actions [] = { 0, 0 , }; static const char _Clang_nfa_pop_trans [] = { 0, 0 , }; static const int Clang_start = 10; static const int Clang_first_final = 10; static const int Clang_error = 0; static const int Clang_en_main = 10; - (void) initFsm; { identLen = 0; curLine = 1; { cs = ( int ) Clang_start; } } - (void) executeWithData:(const char *)data len:(int)len; { const char *p = data; const char *pe = data + len; const char *eof = pe; { unsigned int _trans = 0; unsigned int _have = 0; unsigned int _cont = 1; const char *_acts; unsigned int _nacts; const char *_keys; const char *_inds; while ( _cont == 1 ) { if ( cs == 0 ) _cont = 0; _have = 0; if ( p == pe ) { if ( p == eof ) { if ( _have == 0 ) { const char *__acts; unsigned int __nacts; __acts = _Clang_actions + _Clang_eof_actions[cs]; __nacts = ( unsigned int ) (*( __acts )) ; __acts += 1; while ( __nacts > 0 ) { switch ( (*( __acts )) ) { case 2 : { identBuf[identLen] = 0; printf("ident(%i): %s\n", curLine, identBuf); } break; case 3 : { identBuf[identLen] = 0; printf("literal(%i): %s\n", curLine, identBuf); } break; case 4 : { identBuf[identLen] = 0; printf("float(%i): %s\n", curLine, identBuf); } break; case 5 : { identBuf[identLen] = 0; printf("int(%i): %s\n", curLine, identBuf); } break; case 6 : { identBuf[identLen] = 0; printf("hex(%i): 0x%s\n", curLine, identBuf); } break; case 7 : { identBuf[identLen] = 0; printf("symbol(%i): %s\n", curLine, identBuf); } break; } __nacts -= 1; __acts += 1; } } } if ( _have == 0 ) _cont = 0; } if ( _cont == 1 ) { if ( _have == 0 ) { _keys = _Clang_trans_keys + (cs<<1); _inds = _Clang_indicies + _Clang_index_offsets[cs]; if ( ((*( p )) )<= 126 && ((*( p )) )>= 10 ) { int _ic = ( int ) _Clang_char_class[( int ) ((*( p )) )- 10]; if ( _ic <= ( int ) (*( _keys+1 )) && _ic >= ( int ) (*( _keys )) ) _trans = ( unsigned int ) (*( _inds + ( int ) (_ic - ( int ) (*( _keys )) ) )) ; else _trans = ( unsigned int ) _Clang_index_defaults[cs]; } else { _trans = ( unsigned int ) _Clang_index_defaults[cs]; } } if ( _cont == 1 ) { cs = ( int ) _Clang_cond_targs[_trans]; if ( _Clang_cond_actions[_trans]!= 0 ) { _acts = _Clang_actions + _Clang_cond_actions[_trans]; _nacts = ( unsigned int ) (*( _acts )) ; _acts += 1; while ( _nacts > 0 ) { switch ( (*( _acts )) ) { case 0 : { if ( identLen < IDENT_BUFLEN ) { identBuf[identLen] = (((*( p )) )); identLen += 1; } } break; case 1 : { identLen = 0; } break; case 2 : { identBuf[identLen] = 0; printf("ident(%i): %s\n", curLine, identBuf); } break; case 3 : { identBuf[identLen] = 0; printf("literal(%i): %s\n", curLine, identBuf); } break; case 4 : { identBuf[identLen] = 0; printf("float(%i): %s\n", curLine, identBuf); } break; case 5 : { identBuf[identLen] = 0; printf("int(%i): %s\n", curLine, identBuf); } break; case 6 : { identBuf[identLen] = 0; printf("hex(%i): 0x%s\n", curLine, identBuf); } break; case 7 : { identBuf[identLen] = 0; printf("symbol(%i): %s\n", curLine, identBuf); } break; case 8 : { curLine += 1; } break; } _nacts -= 1; _acts += 1; } } if ( cs == 0 ) _cont = 0; if ( _cont == 1 ) p += 1; } } } } } - (int) finish; { if ( cs == Clang_error ) return -1; if ( cs >= Clang_first_final ) return 1; return 0; } @end #define BUFSIZE 2048 Clang *fsm; char buf[BUFSIZE]; void test( char *buf ) { int len = strlen(buf); fsm = [[Clang alloc] init]; [fsm initFsm]; [fsm executeWithData:buf len:len]; if ( [fsm finish] > 0 ) printf("ACCEPT\n"); else printf("FAIL\n"); } int main() { test( "999 0xaAFF99 99.99 /*\n" "*/ 'lksdj' //\n" "\"\n" "\n" "literal\n" "\n" "\n" "\"0x00aba foobardd.ddsf 0x0.9\n" ); test( "wordwithnum00asdf\n" "000wordfollowsnum,makes new symbol\n" "\n" "finishing early /* unfinished ...\n" ); test( "/*\n" " * Copyright\n" " */\n" "\n" "/* Aapl.\n" " */\n" " \n" "#define _AAPL_RESIZE_H\n" "\n" "#include \n" "\n" "#ifdef AAPL_NAMESPACE\n" "namespace Aapl {\n" "#endif\n" "#define LIN_DEFAULT_STEP 256\n" "#define EXPN_UP( existing, needed ) \\\n" " need > eng ? (ned<<1) : eing\n" " \n" "\n" "/*@}*/\n" "#undef EXPN_UP\n" "#ifdef AAPL_NAMESPACE\n" "#endif /* _AAPL_RESIZE_H */\n" ); return 0; }