static void CallParser(ColorizeParams *params,CallbackParam *data) { ColorizeParams c_params; c_params.size=sizeof(ColorizeParams); c_params.eid=params->eid; c_params.startline=data->row; c_params.startcolumn=data->col; c_params.endline=params->endline; c_params.topline=(params->topline>c_params.startline)?params->topline:c_params.startline; c_params.data_size=params->data_size; c_params.data=params->data; c_params.LocalHeap=params->LocalHeap; c_params.callback=c_callback; c_params.param=data; if(data->cache->diff==4) { c_params.callback=NULL; c_params.param=NULL; } data->ok=0; data->topline=params->topline; Info.pCallParser(_T("c/c++"),&c_params); if(data->ok) { params->startline=data->row; params->startcolumn=data->col; if(data->cache->diff==1) data->cache->lex_state=PARSER_CLEAR; else data->cache->lex_state=PARSER_RULES; data->cache->c_state=INVALID_C_STATE; params->topline=(params->topline>params->startline)?params->topline:params->startline; } }
static int WINAPI c_callback(int from,int row,int col,void *param) { const TCHAR *line; int linelen; line=Info.pGetLine(row,&linelen); switch(((CallbackParam *)param)->cache->diff) { case 1: case 2: if(from==1) { if((!_tcsncmp(line+col,_T("%}"),2))&&(!col)) { if(((CallbackParam *)param)->topline<=row) Info.pAddColor(row,col,2,colors+HC_KEYWORD,EPriorityNormal); ((CallbackParam *)param)->ok=1; ((CallbackParam *)param)->row=row; ((CallbackParam *)param)->col=col+2; return true; } } break; case 3: if(from==0) { if(!_tcsncmp(line+col,_T("{"),1)) (((CallbackParam *)param)->cache->recurse)++; if(!_tcsncmp(line+col,_T("}"),1)) { (((CallbackParam *)param)->cache->recurse)--; if(!(((CallbackParam *)param)->cache->recurse)) { ((CallbackParam *)param)->ok=1; ((CallbackParam *)param)->row=row; ((CallbackParam *)param)->col=col+1; return true; } } } break; } return false; }
void WINAPI Colorize(intptr_t index,struct ColorizeParams *params) { if((index>=rules_count)||(index<0)) return; if(!rules[index].contexts_count) return; int context_start; const char *line; intptr_t linelen; int startcol; int lColorize=0; int state_data=0; int *state=&state_data; int state_size=sizeof(state_data); if(params->data_size>=sizeof(state_data)) { state=(int *)(params->data); state_size=params->data_size; } for(int lno=params->startline;lno<params->endline;lno++) { startcol=(lno==params->startline)?params->startcolumn:0; if(((lno%Info.cachestr)==0)&&(!startcol)) if(!Info.pAddState(params->eid,lno/Info.cachestr,state_size,(unsigned char *)state)) return; context_start=0; if(lno==params->topline) lColorize=1; const wchar_t* lineW=Info.pGetLine(params->eid,lno,&linelen); line=(char*)malloc(linelen); if(!line) return; WideCharToMultiByte(CP_OEMCP,0,lineW,linelen,(char*)line,linelen,NULL,NULL); int pos=startcol,pos_next; while(pos<=linelen) { pos_next=0; for(int i=0;i<rules[index].contexts[state[0]].keywords_count;i++) { pos_next=syntax_strcmp(line,linelen,pos,rules[index].contexts[state[0]].keywords[i].keyword,rules[index].contexts[state[0]].keywords[i].whole_chars_left,rules[index].contexts[state[0]].keywords[i].whole_chars_right,rules[index].contexts[state[0]].keywords[i].line_start,rules[index].contexts[state[0]].keywords[i].recursive); if(pos_next) { if(rules[index].contexts[state[0]].keywords[i].exclusive) pos_next=0; else { if(lColorize) Info.pAddColor(params->eid,lno,context_start,pos-context_start,&rules[index].contexts[state[0]].color,EPriorityNormal); if(lColorize) Info.pAddColor(params->eid,lno,pos,pos_next-pos,&rules[index].contexts[state[0]].keywords[i].color,EPriorityNormal); pos=pos_next-1; context_start=pos_next; } break; } } if(!pos_next) { if(state[0]) { pos_next=syntax_strcmp(line,linelen,pos,rules[index].contexts[state[0]].right,rules[index].contexts[state[0]].whole_chars_left,rules[index].contexts[state[0]].whole_chars_right,rules[index].contexts[state[0]].line_start_right,0); if(pos_next) { int end_temp=pos_next,start_temp=pos_next; if(rules[index].contexts[state[0]].exclusive) { end_temp=pos; if(rules[index].contexts[state[0]].exclusive_right>=0) { if(lColorize) Info.pAddColor(params->eid,lno,pos,pos_next-pos,&rules[index].contexts[0].keywords[rules[index].contexts[state[0]].exclusive_right].color,EPriorityNormal); } else start_temp=pos; } if(lColorize) Info.pAddColor(params->eid,lno,context_start,end_temp-context_start,&rules[index].contexts[state[0]].color,EPriorityNormal); context_start=start_temp; state[0]=0; pos=pos_next-1; } } else { for(int i=1;i<rules[index].contexts_count;i++) { pos_next=syntax_strcmp(line,linelen,pos,rules[index].contexts[i].left,rules[index].contexts[i].whole_chars_left,rules[index].contexts[i].whole_chars_right,rules[index].contexts[i].line_start_left,0); if(pos_next) { int end_temp=pos,start_temp=pos; if(rules[index].contexts[i].exclusive) { if(rules[index].contexts[i].exclusive_left>=0) { if(lColorize) Info.pAddColor(params->eid,lno,pos,pos_next-pos,&rules[index].contexts[0].keywords[rules[index].contexts[i].exclusive_left].color,EPriorityNormal); } else end_temp=pos_next; start_temp=pos_next; } if(lColorize) Info.pAddColor(params->eid,lno,context_start,end_temp-context_start,&rules[index].contexts[state[0]].color,EPriorityNormal); context_start=start_temp; state[0]=i; pos=pos_next-1; break; } } } } if(params->callback) if(params->callback(0,lno,pos,params->param)) { free((void*)line); return; } pos++; } if(lColorize) Info.pAddColor(params->eid,lno,context_start,linelen-context_start,&rules[index].contexts[state[0]].color,EPriorityNormal); free((void*)line); line=NULL; } }
void WINAPI Colorize(int index,struct ColorizeParams *params) { (void)index; int commentstart,stringstart; const TCHAR *line; int linelen,startcol; int lColorize=0; CallbackParam callback_data; CacheParam state_data={INVALID_C_STATE,PARSER_CLEAR,0,0}; CacheParam *state=&state_data; int state_size=sizeof(state_data); if(params->data_size>=sizeof(state_data)) { state=(CacheParam *)(params->data); state_size=params->data_size; } else { params->data=(unsigned char *)state; params->data_size=state_size; } callback_data.cache=state; if(state[0].c_state>INVALID_C_STATE) { callback_data.row=params->startline; callback_data.col=params->startcolumn; CallParser(params,&callback_data); if(!callback_data.ok) goto colorize_exit; } colorize_start: for(int lno=params->startline;lno<params->endline;lno++) { startcol=(lno==params->startline)?params->startcolumn:0; if(((lno%Info.cachestr)==0)&&(!startcol)) if(!Info.pAddState(params->eid,lno/Info.cachestr,state_size,(unsigned char *)state)) goto colorize_exit; commentstart=0; if(lno==params->topline) lColorize=1; if(lColorize&&(!startcol)) Info.pDelColor(lno); line=Info.pGetLine(lno,&linelen); for(int i=startcol;i<linelen;i++) { switch(state[0].lex_state&0xffff) { case PARSER_CLEAR: case PARSER_RULES: if(!_tcsncmp(line+i,_T("/*"),2)) { state[0].lex_state=PARSER_COMMENT|(state[0].lex_state<<16); commentstart=i; i++; } else if(*(line+i)=='\'') { stringstart=i; i++; while(i<linelen) { if(*(line+i)=='\\') { i++; } else if(*(line+i)=='\'') { if(lColorize) Info.pAddColor(lno,stringstart,i + 1 - stringstart,colors+HC_STRING,EPriorityNormal); break; } i++; } } else if(*(line+i)=='\"') { state[0].lex_state=PARSER_STRING|(state[0].lex_state<<16); commentstart=i; } else if (!_tcsncmp(line+i,_T("//"),2)) { if(lColorize) Info.pAddColor(lno,i,linelen-i,colors+HC_COMMENT,EPriorityNormal); i=linelen; } else if(*(line+i)=='[') { state[0].lex_state=PARSER_SET|(state[0].lex_state<<16); commentstart=i; } else { if(params->callback) if(params->callback(0,lno,i,params->param)) goto colorize_exit; if((!_tcsncmp(line+i,_T("%%"),2))&&(!i)) { if(lColorize) Info.pAddColor(lno,i,2,colors+HC_KEYWORD,EPriorityNormal); if(state[0].lex_state==PARSER_CLEAR) { state[0].lex_state=PARSER_RULES; } else { callback_data.row=lno; callback_data.col=i+2; state[0].diff=4; state[0].c_state=PARSER_CLEAR; CallParser(params,&callback_data); goto colorize_exit; } } else { if((!_tcsncmp(line+i,_T("%{"),2))&&(!i)) { if(lColorize) Info.pAddColor(lno,i,2,colors+HC_KEYWORD,EPriorityNormal); callback_data.row=lno; callback_data.col=i+2; state[0].diff=1+state[0].lex_state; state[0].c_state=PARSER_CLEAR; CallParser(params,&callback_data); if(!callback_data.ok) goto colorize_exit; goto colorize_start; } if(state[0].lex_state==PARSER_RULES) { if(!_tcsncmp(line+i,_T("{"),1)) { callback_data.row=lno; callback_data.col=i; state[0].diff=3; state[0].c_state=PARSER_CLEAR; CallParser(params,&callback_data); if(!callback_data.ok) goto colorize_exit; goto colorize_start; } } } } break; case PARSER_COMMENT: if(!_tcsncmp(line+i,_T("*/"),2)) { i++; state[0].lex_state=state[0].lex_state>>16; if(lColorize) Info.pAddColor(lno,commentstart,i+1-commentstart,colors+HC_COMMENT,EPriorityNormal); } break; case PARSER_STRING: if(*(line+i)=='\"') { state[0].lex_state=state[0].lex_state>>16; if(lColorize) Info.pAddColor(lno,commentstart,i+1-commentstart,colors+HC_STRING,EPriorityNormal); } else if(*(line+i)=='\\') i++; break; case PARSER_SET: if(*(line+i)==']') { state[0].lex_state=state[0].lex_state>>16; if(lColorize) Info.pAddColor(lno,commentstart,i+1-commentstart,colors+HC_SET,EPriorityNormal); }