Files
Shrine/Adam/DolDoc/DocHighlight.HC
T
2018-09-08 21:47:50 +02:00

156 lines
3.9 KiB
HolyC

#help_index "DolDoc/Misc"
U32 *DocHighlight(CDocEntry *doc_e,U8 *src,I64 len,I64 _temp_u32_attr)
{//Be aware of $LK,"::/Demo/ToHtmlToTXTDemo/ToHtml.HC"$.
U32 *res=MAlloc((len+1)*sizeof(U32)),*dst=res;
U8 *ptr;
CDocSettings *s=&doc_e->settings;
I64 ch,ch1,last_ch,temp_u32_attr,mask_temp_u32_attr=_temp_u32_attr&0xFFFFF000,
comment_depth,brace_depth=s->brace_depth,paren_depth=s->paren_depth;
CHash *temph;
switch [s->state] {
case DOCSS_NORMAL:
hl_normal:
while (len) {
while (len && !Bt(chars_bmp_alpha_numeric,*src)) {
temp_u32_attr=_temp_u32_attr;
ch1=*src++;
switch (ch1) {
case '/':
if (len>=2) {
if (*src=='/') {
temp_u32_attr=DOC_COLOR_COMMENT<<8|mask_temp_u32_attr;
*dst++=ch1+temp_u32_attr;
*dst++=*src++ +temp_u32_attr;
len-=2;
goto hl_cpp_comment;
} else if (*src=='*') {
temp_u32_attr=DOC_COLOR_COMMENT<<8|mask_temp_u32_attr;
*dst++=ch1+temp_u32_attr;
*dst++=*src++ +temp_u32_attr;
len-=2;
comment_depth=1;
goto hl_comment;
}
}
break;
case '\'':
temp_u32_attr=DOC_COLOR_QUOTE<<8|mask_temp_u32_attr;
*dst++=ch1+temp_u32_attr;
len--;
goto hl_single_quote;
case '\"':
temp_u32_attr=DOC_COLOR_QUOTE<<8|mask_temp_u32_attr;
*dst++=ch1+temp_u32_attr;
len--;
goto hl_dbl_quote;
case '(':
if (paren_depth++&1)
temp_u32_attr=DOC_COLOR_ALT_TEXT<<8|mask_temp_u32_attr;
break;
case ')':
if (--paren_depth&1)
temp_u32_attr=DOC_COLOR_ALT_TEXT<<8|mask_temp_u32_attr;
break;
case '{':
if (brace_depth++&1)
temp_u32_attr=DOC_COLOR_ALT_TEXT<<8|mask_temp_u32_attr;
break;
case '}':
if (--brace_depth&1)
temp_u32_attr=DOC_COLOR_ALT_TEXT<<8|mask_temp_u32_attr;
break;
}
*dst++=ch1+temp_u32_attr;
if (!--len) goto hl_normal_done;
}
ptr=src;
while (len && Bt(chars_bmp_alpha_numeric,*src)) {
src++;
len--;
}
ch=*src;
*src=0;
if (temph=HashFind(ptr,cmp.asm_hash,
HTT_KEYWORD|HTT_ASM_KEYWORD|HTT_OPCODE|HTT_REG)) {
if (temph->type&(HTT_KEYWORD|HTT_ASM_KEYWORD|HTT_OPCODE))
temp_u32_attr=DOC_COLOR_KEYWORD<<8|mask_temp_u32_attr;
else
temp_u32_attr=DOC_COLOR_KEYWORD2<<8|mask_temp_u32_attr;
} else
temp_u32_attr=_temp_u32_attr;
while (ch1=*ptr++)
*dst++=ch1+temp_u32_attr;
*src=ch;
}
hl_normal_done:
s->state=DOCSS_NORMAL;
s->comment_depth=0;
break;
case DOCSS_SINGLE_QUOTE:
temp_u32_attr=DOC_COLOR_QUOTE<<8|mask_temp_u32_attr;
hl_single_quote:
last_ch=0;
while (len--) {
ch1=*src++;
*dst++=ch1+temp_u32_attr;
if (last_ch!='\\' && ch1=='\'')
goto hl_normal;
if (last_ch=='\\' && ch1=='\\')
last_ch=0;
else
last_ch=ch1;
}
s->state=DOCSS_SINGLE_QUOTE;
s->comment_depth=0;
break;
case DOCSS_DBL_QUOTE:
temp_u32_attr=DOC_COLOR_QUOTE<<8|mask_temp_u32_attr;
hl_dbl_quote:
last_ch=0;
while (len--) {
ch1=*src++;
*dst++=ch1+temp_u32_attr;
if (last_ch!='\\' && ch1=='\"')
goto hl_normal;
if (last_ch=='\\' && ch1=='\\')
last_ch=0;
else
last_ch=ch1;
}
s->state=DOCSS_DBL_QUOTE;
s->comment_depth=0;
break;
case DOCSS_COMMENT:
temp_u32_attr=DOC_COLOR_COMMENT<<8|mask_temp_u32_attr;
comment_depth=s->comment_depth;
hl_comment:
last_ch=0;
while (len--) {
ch1=*src++;
*dst++=ch1+temp_u32_attr;
if (last_ch=='*' && ch1=='/') {
if (!--comment_depth)
goto hl_normal;
} else if (last_ch=='/' && ch1=='*')
comment_depth++;
last_ch=ch1;
}
s->state=DOCSS_COMMENT;
s->comment_depth=comment_depth;
break;
case DOCSS_CPP_Z_COMMENT:
temp_u32_attr=DOC_COLOR_COMMENT<<8|mask_temp_u32_attr;
hl_cpp_comment:
while (len--)
*dst++=*src++ +temp_u32_attr;
s->state=DOCSS_CPP_Z_COMMENT;
s->comment_depth=0;
break;
}
s->paren_depth=paren_depth;
s->brace_depth=brace_depth;
*dst=0;
return res;
}