hschumann2/TempleOS-Source-Code
0838
1 2CLexFile *LexFilePush(CCmpCtrl *cc)3{//#include file push.4 CLexFile *res=CAlloc(sizeof(CLexFile));5 if (res->next=cc->lex_include_stk)6 res->depth=res->next->depth+1;7 else8 res->depth=-1; //Include depth starts with -1.9 return cc->lex_include_stk=res;10}11 12CLexFile *LexFilePop(CCmpCtrl *cc)13{//#include file pop.14 CLexFile *tmpf;15 if (tmpf=cc->lex_include_stk) {16 if ((cc->lex_include_stk=tmpf->next) || !(cc->flags & CCF_DONT_FREE_BUF)) {17 if (tmpf->flags & LFSF_DOC) {18 if (tmpf->doc)19 DocDel(tmpf->doc);20 } else21 Free(tmpf->buf);;22 }23 Free(tmpf->full_name);24 Free(tmpf);25 }26 return cc->lex_include_stk;27}28 29CCmpCtrl *CmpCtrlNew(U8 *buf=NULL,I64 flags=0,U8 *filename=NULL)30{//MAlloc and Init CCmpCtrl.31//Frees buf in CmpCtrlDel unless CCF_DONT_FREE_BUF flag is set.32 //FileName is for error reporting. If files are #included,33 //new names are used. See Psalmody CmpCtrlNew.34 CCmpCtrl *cc=CAlloc(sizeof(CCmpCtrl));35 CLexFile *tmpf;36 QueInit(cc);37 cc->flags=flags;38 cc->opts=1<<OPTf_WARN_UNUSED_VAR|1<<OPTf_WARN_HEADER_MISMATCH;39 cc->htc.hash_mask=HTG_TYPE_MASK-HTT_IMPORT_SYS_SYM;40 cc->htc.define_hash_table=cc->htc.hash_table_lst=41 cc->htc.glbl_hash_table=cc->htc.local_hash_table=Fs->hash_table;42 if (flags&CCF_KEEP_AT_SIGN)43 cc->char_bmp_alpha_numeric=char_bmp_alpha_numeric_no_at;44 else45 cc->char_bmp_alpha_numeric=char_bmp_alpha_numeric;46 tmpf=LexFilePush(cc);47 QueInit(&cc->next_stream_blk);48 if (filename)49 tmpf->full_name=FileNameAbs(filename);50 else51 tmpf->full_name=StrNew(blkdev.tmp_filename);52 if (flags & CCF_PMT)53 buf=CAlloc(8);54 tmpf->buf=tmpf->buf_ptr=tmpf->line_start=cc->cur_buf_ptr=buf;55 tmpf->line_num=1;56 return cc;57}58 59U0 CmpCtrlDel(CCmpCtrl *cc)60{//Free CCmpCtrl.61 while (LexFilePop(cc));62 LinkedLstDel(cc->lex_prs_stk);63 LinkedLstDel(cc->htc.next);64 Free(cc->ps);65 Free(cc->cur_str);66 Free(cc->cur_help_idx);67 Free(cc->dollar_buf);68 Free(cc);69}70 71I64 CmpCtrlSize(CCmpCtrl *cc)72{//Mem size of CCmpCtrl and its members.73 CLexFile *tmpf=cc->lex_include_stk;74 I64 res=0;75 while (tmpf) {76 if (tmpf->next || !(cc->flags & CCF_DONT_FREE_BUF)) {77 if (tmpf->flags & LFSF_DOC) {78 if (tmpf->doc)79 res+=DocSize(tmpf->doc);80 } else81 res+=MSize2(tmpf->buf);82 }83 res+=MSize2(tmpf->full_name);84 res+=MSize2(tmpf);85 tmpf=tmpf->next;86 }87 res+=MSize2(cc->cur_str);88 res+=MSize2(cc);89 return res;90}91 92U32 lex_zeros=0;93 94Bool LexDollar(CCmpCtrl *cc,CDoc *doc,CDocEntry *doc_e)95{96 U8 *st;97 if (cc->flags&CCF_IN_QUOTES) {98 Free(cc->dollar_buf);99 st=Doc2PlainText(doc,doc_e);100 cc->dollar_buf=MStrPrint("$%$Q$",st);101 cc->dollar_cnt=2;102 Free(st);103 return TRUE;104 } else105 return FALSE;106}107 108I64 LexGetChar(CCmpCtrl *cc)109{//Get one char from stream. Allow put-back one.110 U8 *ptr,*src;111 CLexFile *tmpf;112 CDoc *doc;113 CDocEntry *doc_e;114 if (!Btr(&cc->flags,CCf_USE_LAST_U16)) {115lgc_start1:116 if (!(src=cc->cur_buf_ptr++)) {117 cc->cur_buf_ptr=NULL;118 goto lgc_here;119 }120 switch [cc->last_U16=*src++] {121 case 0:122lgc_here:123 tmpf=cc->lex_include_stk;124 if (tmpf->flags & LFSF_DOC) {125 doc=tmpf->doc;126 doc_e=tmpf->cur_entry;127 doc_e=doc_e->next;128lgc_start2:129 if (doc_e!=doc) {130 tmpf->cur_entry=doc_e;131 switch [doc_e->type_u8] {132 case DOCT_TEXT:133 if (doc_e->de_flags & ~(DOCEF_TAG|DOCEF_DEFINE|DOCEF_TAG_CB|134 DOCG_BL_IV_UL|DOCEF_WORD_WRAP|DOCEF_HIGHLIGHT|135 DOCEF_SKIP|DOCEF_FILTER_SKIP) &&136 LexDollar(cc,doc,doc_e) && *(src=cc->dollar_buf)) {137 tmpf->line_num=doc_e->y+1;138 tmpf->buf_ptr=cc->cur_buf_ptr=src;139 } else if (*(src=doc_e->tag))140 tmpf->buf_ptr=cc->cur_buf_ptr=src;141 else {142 doc_e=doc_e->next;143 goto lgc_start2;144 }145 break;146 case DOCT_NEW_LINE:147 tmpf->buf_ptr=cc->cur_buf_ptr=&lex_zeros;148 tmpf->line_start=doc_e->next;149 tmpf->line_num=doc_e->y+2;//+1 because NEW_LINE is on prev line150//+1 because doc y starts at zero151 cmp.compiled_lines++;152 cc->last_U16='\n';153 goto lgc_done;154 case DOCT_TAB:155 tmpf->buf_ptr=cc->cur_buf_ptr=&lex_zeros;156 tmpf->line_num=doc_e->y+1;157 cc->last_U16='\t';158 goto lgc_done;159 case DOCT_INS_BIN:160 tmpf->buf_ptr=cc->cur_buf_ptr=&lex_zeros;161 tmpf->line_num=doc_e->y+1;162 Free(cc->cur_str);163 cc->cur_str=NULL;164 cc->cur_str_len=0;165 if (doc_e->bin_data) {166 ptr=MAlloc(doc_e->bin_data->size);167 if (doc_e->bin_data->data)168 MemCpy(ptr,doc_e->bin_data->data,doc_e->bin_data->size);169 cc->cur_str=ptr;170 cc->cur_str_len=doc_e->bin_data->size;171 }172 cc->last_U16=TK_INS_BIN;173 goto lgc_done;174 case DOCT_INS_BIN_SIZE:175 tmpf->buf_ptr=cc->cur_buf_ptr=&lex_zeros;176 if (doc_e->bin_data)177 cc->cur_i64=doc_e->bin_data->size;178 else179 cc->cur_i64=0;180 tmpf->line_num=doc_e->y+1;181 cc->last_U16=TK_INS_BIN_SIZE;182 goto lgc_done;183 case DOCT_SHIFTED_Y:184 if (LexDollar(cc,doc,doc_e) && *(src=cc->dollar_buf)) {185 tmpf->line_num=doc_e->y+1;186 tmpf->buf_ptr=cc->cur_buf_ptr=src;187 } else {188 tmpf->buf_ptr=cc->cur_buf_ptr=&lex_zeros;189 tmpf->line_num=doc_e->y+1;190 if (doc_e->attr<0)191 cc->last_U16=TK_SUPERSCRIPT;192 else if (doc_e->attr>0)193 cc->last_U16=TK_SUBSCRIPT;194 else195 cc->last_U16=TK_NORMALSCRIPT;196 goto lgc_done;197 }198 break;199 case DOCT_MARKER:200 case DOCT_CURSOR:201 doc_e=doc_e->next;202 goto lgc_start2;203 case 0xFF: //nobound switch204 default:205 if (LexDollar(cc,doc,doc_e) && *(src=cc->dollar_buf)) {206 tmpf->line_num=doc_e->y+1;207 tmpf->buf_ptr=cc->cur_buf_ptr=src;208 } else {209 doc_e=doc_e->next;210 goto lgc_start2;211 }212 }213 }214 if (doc_e!=doc)215 goto lgc_start1;216 tmpf->cur_entry=doc->head.last; //When take next, will still be end.217 }218 tmpf=cc->lex_include_stk;219 if (tmpf->next) {220 tmpf=LexFilePop(cc);221 cc->cur_buf_ptr=tmpf->buf_ptr;222 cc->flags&=~CCF_USE_LAST_U16;223 if (!(cc->last_U16=tmpf->last_U16))224 goto lgc_start1;225 } else {226 if (cc->flags & CCF_PMT) {227 Free(tmpf->buf);228 ptr=CmdLinePmt;229 if (StrCmp(ptr,"\n") && !cc->pmt_line++ && !StrCmp(ptr,"?\n") &&230 cc->flags & CCF_QUESTION_HELP) {231 Free(ptr);232 ptr=StrNew("Help;;\n");233 }234 tmpf->buf=tmpf->buf_ptr=tmpf->line_start=cc->cur_buf_ptr=ptr;235 goto lgc_start1;236 } else {237 if (src)238 cc->cur_buf_ptr=src-1;239 cc->last_U16=TK_EOF;240 }241 }242 break;243 case CH_CURSOR:244 goto lgc_start1;245 case '\n':246 tmpf=cc->lex_include_stk;247 if (!(tmpf->flags & LFSF_DOC)) {248 tmpf->line_num++;249 cmp.compiled_lines++;250 tmpf->line_start=src;251 }252 break;253 case 0xFF: //nobound switch254 }255lgc_done:256 if (cc->last_U16==CH_SHIFT_SPACE)257 cc->last_U16=CH_SPACE;258 if (cc->opts & OPTF_ECHO &&259 cc->last_U16<256 && Bt(char_bmp_printable,cc->last_U16))260 '' cc->last_U16;261 }262 return cc->last_U16;263}264 265U0 LexSkipEol(CCmpCtrl *cc)266{//LexGetChar to NULL until end-of-line.267 I64 ch;268 do ch=LexGetChar(cc);269 while (Bt(char_bmp_non_eol,ch));270}271 272U8 *LexFirstRem(CCmpCtrl *cc,U8 *marker,I64 _len=NULL)273{//LexGetChar() chars making str until marker.274 U8 *res,*ptr;275 CQueVectU8 *tmpv=QueVectU8New;276 I64 i,len=0;277 while (TRUE) {278 i=LexGetChar(cc);279 if (!i||StrOcc(marker,i))280 break;281 QueVectU8Put(tmpv,len++,i);282 }283 if (i)284 Bts(&cc->flags,CCf_USE_LAST_U16);285 res=ptr=MAlloc(len+1);286 for (i=0;i<len;i++)287 *ptr++=QueVectU8Get(tmpv,i);288 *ptr=0;289 QueVectU8Del(tmpv);290 if (_len) *_len=len;291 return res;292}293 294U0 LexIncludeStr(CCmpCtrl *cc,U8 *abs_filename,U8 *src,Bool actual_file)295{296 LexBackupLastChar(cc);297 CLexFile *tmpf=LexFilePush(cc);298 if (actual_file)299 tmpf->full_name=StrNew(abs_filename);300 else301 tmpf->full_name=StrNew(blkdev.tmp_filename);302 tmpf->line_num=1;303 tmpf->buf=tmpf->buf_ptr=tmpf->line_start=cc->cur_buf_ptr=src;304}305 306CDoc *LexDocRead(U8 *abs_filename,I64 flags)307{308 CDoc *doc=DocNew(abs_filename);309 U8 *src;310 I64 size=0;311 doc->flags|=flags;312 src=FileRead(abs_filename,&size);313 if (!src || !size) {314 Free(src);315 src=CAlloc(1);316 size=0;317 }318 DocLoad(doc,src,size);319 Free(src);320 return doc;321}322 323I64 cmp_type_flags_src_code[(DOCT_TYPES_NUM+63)/64]={324 1<<DOCT_TEXT|1<<DOCT_TAB|1<<DOCT_INS_BIN|1<<DOCT_INS_BIN_SIZE};325 326U0 LexAttachDoc(CCmpCtrl *cc,CLexFile *tmpf=NULL,327 CDoc *doc=NULL,U8 *abs_filename=NULL,CDocEntry *doc_e=NULL,I64 col=0)328{//Start lexing doc. Give either doc or abs_filename.329 if (!doc)330 doc=LexDocRead(abs_filename,DOCF_DBL_DOLLARS);331 if (!tmpf) {332 LexBackupLastChar(cc);333 tmpf=LexFilePush(cc);334 }335 if (!doc_e)336 doc_e=doc->head.next;337 tmpf->full_name=StrNew(doc->filename.name);338 tmpf->doc=doc;339 while (doc_e!=doc) {340 if (Bt(cmp_type_flags_src_code,doc_e->type_u8))341 break;342 doc_e=doc_e->next;343 col=doc_e->min_col;344 }345 if (doc_e!=doc) {346 col=ClampI64(col,doc_e->min_col,doc_e->max_col);347 tmpf->line_start=doc_e;348 tmpf->buf=NULL;349 tmpf->line_num=doc_e->y+1;350 if (doc_e->type_u8==DOCT_TEXT) {351 tmpf->cur_entry=doc_e;352 tmpf->buf_ptr=doc_e->tag;353 } else {354 tmpf->cur_entry=doc_e->last; //TODO: might be problem at begin of file355 tmpf->buf_ptr=&lex_zeros;356 }357 tmpf->flags=LFSF_DOC;358 } else {//TODO: DocDel(doc)?359 col=0;360 tmpf->buf=tmpf->buf_ptr=tmpf->line_start=CAlloc(1);361 tmpf->line_num=1;362 tmpf->flags=0;363 }364 cc->cur_buf_ptr=tmpf->buf_ptr+col;365 tmpf->last_U16=0;366}367 368I64 LexInStr(CCmpCtrl *cc,U8 *buf,I64 size,Bool *done)369{370 I64 i=0,j,k,ch;371 *done=TRUE;372 while (i<size-1) {373 ch=LexGetChar(cc);374 if (!ch || ch=='"') {375 buf[i++]=0;376 return i;377 } else if (ch=='\\') {378 switch (ch=LexGetChar(cc)) {379 case '0':380 buf[i++]=0;381 break;382 case '\'':383 buf[i++]='\'';384 break;385 case '\`':386 buf[i++]='\`';387 break;388 case '\\':389 buf[i++]='\\';390 break;391 case '"':392 buf[i++]='"';393 break;394 case 'd':395 buf[i++]='$';396 break;397 case 'n':398 buf[i++]='\n';399 break;400 case 'r':401 buf[i++]='\r';402 break;403 case 't':404 buf[i++]='\t';405 break;406 case 'x':407 case 'X':408 j=0;409 for (k=0;k<2;k++) {410 ch=ToUpper(LexGetChar(cc));411 if (Bt(char_bmp_hex_numeric,ch)) {412 if (ch<='9')413 j=j<<4+ch-'0';414 else415 j=j<<4+ch-'A'+10;416 } else {417 cc->flags|=CCF_USE_LAST_U16;418 break;419 }420 }421 buf[i++]=j;422 break;423 default:424 cc->flags|=CCF_USE_LAST_U16;425 buf[i++]='\\';426 }427 } else if (ch=='$') {428 buf[i++]='$';429 if (cc->dollar_cnt)430 cc->dollar_cnt--;431 else if (LexGetChar(cc)!='$') {432 cc->dollar_cnt=1;433 cc->flags|=CCF_USE_LAST_U16;434 }435 } else436 buf[i++]=ch;437 }438 *done=FALSE;439 return i;440}441 442I64 Lex(CCmpCtrl *cc)443{//Fetch next token.444 I64 i,j,k,l,ch;445 CHash *tmph;446 Bool str_done,neg_e;447 U8 *fbuf,*buf2,*buf3,buf[STR_LEN];448 cc->last_line_num=cc->lex_include_stk->line_num;449 while (TRUE) {450lex_cont:451 switch [ch=LexGetChar(cc)] {452 case 0:453 return cc->token=TK_EOF;454 case TK_SUPERSCRIPT:455 ch='>';456 goto lex_ident;457 case TK_SUBSCRIPT:458 ch='<';459 goto lex_ident;460 case TK_NORMALSCRIPT:461 ch='=';462 goto lex_ident;463 case '@':464 if (cc->flags&CCF_KEEP_AT_SIGN) {465 cc->token=ch;466 goto lex_end;467 }468 case 'A'...'Z':469 case 'a'...'z':470 case '_':471 case 128...255:472lex_ident:473 i=0;474 buf[i++]=ch;475 while (TRUE) {476 if (i>=STR_LEN)477 LexExcept(cc,"Ident limited to STR_LEN chars at ");478 else if (!(ch=LexGetChar(cc)))479 break;480 else if (Bt(cc->char_bmp_alpha_numeric,ch))481 buf[i++]=ch;482 else if (ch==TK_SUPERSCRIPT)483 buf[i++]='>';484 else if (ch==TK_SUBSCRIPT)485 buf[i++]='<';486 else if (ch==TK_NORMALSCRIPT)487 buf[i++]='=';488 else {489 cc->flags|=CCF_USE_LAST_U16;490 break;491 }492 }493 buf[i++]=0;494 tmph=NULL;495 if (cc->htc.local_var_lst)496 cc->local_var_entry=MemberFind(buf,cc->htc.local_var_lst);497 else498 cc->local_var_entry=NULL;499 if (!cc->local_var_entry && cc->htc.hash_table_lst)500 tmph=HashFind(buf,cc->htc.hash_table_lst,cc->htc.hash_mask);501 if (tmph)502 j=tmph->type;503 else504 j=0;505 if (j & HTT_DEFINE_STR && !(cc->flags & CCF_NO_DEFINES)) {506 LexIncludeStr(cc,507 tmph->str,StrNew(tmph(CHashDefineStr *)->data),FALSE);508 cc->lex_include_stk->flags|=LFSF_DEFINE;509 } else {510 cc->hash_entry=tmph;511 Free(cc->cur_str);512 cc->cur_str=StrNew(buf);513 cc->cur_str_len=i;514 cc->token=TK_IDENT;515 goto lex_end;516 }517 break;518 case '0'...'9':519 i=ch-'0';520 ch=ToUpper(LexGetChar(cc));521 if (ch=='X') {522 while (TRUE) {523 ch=ToUpper(LexGetChar(cc));524 if (Bt(char_bmp_hex_numeric,ch)) {525 if (ch<='9')526 i=i<<4+ch-'0';527 else528 i=i<<4+ch-'A'+10;529 } else {530 cc->cur_i64=i;531 cc->flags|=CCF_USE_LAST_U16;532 cc->token=TK_I64;533 goto lex_end;534 }535 }536 } else if (ch=='B') {537 while (TRUE) {538 ch=LexGetChar(cc);539 if (ch=='0')540 i=i<<1;541 else if (ch=='1')542 i=i<<1+1;543 else {544 cc->cur_i64=i;545 cc->flags|=CCF_USE_LAST_U16;546 cc->token=TK_I64;547 goto lex_end;548 }549 }550 }551 while (TRUE) {552 if (Bt(char_bmp_dec_numeric,ch))553 i=i*10+ch-'0';554 else {555 if (ch=='.' || ch=='e' || ch=='E') break;556lex_is_int:557 cc->cur_i64=i;558 cc->flags|=CCF_USE_LAST_U16;559 cc->token=TK_I64;560 goto lex_end;561 }562 ch=LexGetChar(cc);563 }564 if (ch=='.') {565 ch=LexGetChar(cc);566 if (ch=='.') {567 cc->flags|=CCF_LAST_WAS_DOT;568 goto lex_is_int;569 }570 }571lex_float_start:572 k=0;573 while (TRUE) {574 if (Bt(char_bmp_dec_numeric,ch)) {575 i=i*10+ch-'0';576 k++;577 } else {578 if (ch=='e' || ch=='E')579 break;580 cc->cur_f64=i*Pow10I64(-k);581 cc->flags|=CCF_USE_LAST_U16;582 cc->token=TK_F64;583 goto lex_end;584 }585 ch=LexGetChar(cc);586 }587 ch=LexGetChar(cc);588 neg_e=FALSE;589 if (ch=='-') {590 neg_e=TRUE;591 ch=LexGetChar(cc);592 }593 j=0;594 while (TRUE) {595 if (Bt(char_bmp_dec_numeric,ch))596 j=j*10+ch-'0';597 else {598 if (neg_e)599 cc->cur_f64=i*Pow10I64(-j-k);600 else601 cc->cur_f64=i*Pow10I64(j-k);602 cc->flags|=CCF_USE_LAST_U16;603 cc->token=TK_F64;604 goto lex_end;605 }606 ch=LexGetChar(cc);607 }608 break;609 case '"':610 cc->flags|=CCF_IN_QUOTES;611 buf2=NULL;612 i=0;613 do {614 j=LexInStr(cc,buf,STR_LEN,&str_done);615 buf3=MAlloc(i+j);616 if (buf2) {617 MemCpy(buf3,buf2,i);618 Free(buf2);619 buf2=buf3;620 MemCpy(buf2+i,buf,j);621 } else {622 buf2=buf3;623 MemCpy(buf2,buf,j);624 }625 i+=j;626 } while (!str_done);627 Free(cc->cur_str);628 cc->cur_str=MAlloc(i);629 MemCpy(cc->cur_str,buf2,i);630 Free(buf2);631 cc->cur_str_len=i;632 cc->flags&=~CCF_IN_QUOTES;633 cc->token=TK_STR;634 goto lex_end;635 case '\'':636 if (cc->flags&CCF_NO_CHAR_CONST)637 break;638 k=0;639 for (j=0;j<8;j++) {640 if (!(ch=LexGetChar(cc)) || ch=='\'')641 break;642 if (ch=='\\') {643 switch (ch=LexGetChar(cc)) {644 case '0': k.u8[j]=0; break;645 case '\'': k.u8[j]='\''; break;646 case '\`': k.u8[j]='\`'; break;647 case '"': k.u8[j]='"'; break;648 case '\\': k.u8[j]='\\'; break;649 case 'd': k.u8[j]='$'; break;650 case 'n': k.u8[j]='\n'; break;651 case 'r': k.u8[j]='\r'; break;652 case 't': k.u8[j]='\t'; break;653 case 'x':654 case 'X':655 i=0;656 for (l=0;l<2;l++) {657 ch=ToUpper(LexGetChar(cc));658 if (Bt(char_bmp_hex_numeric,ch)) {659 if (ch<='9')660 i=i<<4+ch-'0';661 else662 i=i<<4+ch-'A'+10;663 } else {664 cc->flags|=CCF_USE_LAST_U16;665 break;666 }667 }668 k.u8[j]=i;669 break;670 default:671 k.u8[j]='\\';672 cc->flags|=CCF_USE_LAST_U16;673 }674 } else if (ch=='$') {675 ch=LexGetChar(cc);676 k.u8[j]='$';677 if (ch!='$')678 cc->flags|=CCF_USE_LAST_U16;679 } else680 k.u8[j]=ch;681 }682 if (ch!='\'' && (ch=LexGetChar(cc)) && ch!='\'')683 LexExcept(cc,"Char const limited to 8 chars at ");684 cc->cur_i64=k;685 cc->token=TK_CHAR_CONST;686 goto lex_end;687 case '#':688 if (cc->flags&CCF_KEEP_SIGN_NUM) {689 cc->token=ch;690 goto lex_end;691 }692 if (Lex(cc)!=TK_IDENT) //skip '#'693 goto lex_end;694 if (!(tmph=cc->hash_entry))695 goto lex_end;696 if (!(tmph->type & HTT_KEYWORD))697 goto lex_end;698 switch (i=tmph(CHashGeneric *)->user_data0) {699 case KW_INCLUDE:700 if (Lex(cc)!=TK_STR)701 goto lex_end;702 fbuf=ExtDft(cc->cur_str,"HC.Z");703 buf2=FileNameAbs(fbuf);704 Free(fbuf);705 if (Bt(&sys_run_level,RLf_DOC))706 LexAttachDoc(cc,,,buf2);707 else708 LexIncludeStr(cc,buf2,FileRead(buf2),TRUE);709 Free(buf2);710 break;711 case KW_DEFINE:712 cc->flags|=CCF_NO_DEFINES;713 if (Lex(cc)==TK_IDENT) {714 tmph=CAlloc(sizeof(CHashDefineStr));715 tmph->str=cc->cur_str;716 cc->cur_str=0;717 tmph->type=HTT_DEFINE_STR;718 HashSrcFileSet(cc,tmph);719 720 do ch=LexGetChar(cc); //skip space between define name and start721 while (Bt(char_bmp_non_eol_white_space,ch));722 723 i=j=0;724 buf2=NULL;725 if (ch) {726 do {727 if (ch=='\\') {728 if (ch=LexGetChar(cc)) {729 if (ch!='\r' && ch!='\n') {730 buf[j++]='\\';731 buf[j++]=ch;732 } else if (ch=='\r' && LexGetChar(cc)!='\n')733 cc->flags|=CCF_USE_LAST_U16;734 } else {735 buf[j++]='\\';736 break;737 }738 } else if (ch!='\n')739 buf[j++]=ch;740 else741 break;742 while (ch=LexGetChar(cc)) {743 if (ch=='/') {744 ch=LexGetChar(cc);745 if (ch=='/') {746 do ch=LexGetChar(cc);747 while (Bt(char_bmp_non_eol,ch));748 break;749 } else {750 buf[j++]='/';751 cc->flags|=CCF_USE_LAST_U16;752 }753 } else if (ch=='\\')754 break;755 else if (Bt(char_bmp_non_eol,ch))756 buf[j++]=ch;757 else758 break;759 if (j>=STR_LEN-4) {//Spot for ['\'][ch],[ch],[0]760 buf[j++]=0;761 buf3=MAlloc(i+j);762 if (buf2) {763 MemCpy(buf3,buf2,i);764 Free(buf2);765 buf2=buf3;766 MemCpy(buf2+i,buf,j);767 } else {768 buf2=buf3;769 MemCpy(buf2,buf,j);770 }771 i+=j-1;772 j=0;773 }774 }775 } while (ch=='\\');776 }777 buf[j++]=0;778 buf3=MAlloc(i+j);779 if (buf2) {780 MemCpy(buf3,buf2,i);781 Free(buf2);782 buf2=buf3;783 MemCpy(buf2+i,buf,j);784 } else {785 buf2=buf3;786 MemCpy(buf2,buf,j);787 }788 tmph(CHashDefineStr *)->data=buf2;789 tmph(CHashDefineStr *)->cnt=-1;790 HashAdd(tmph,cc->htc.define_hash_table);791 }792 cc->flags&=~CCF_NO_DEFINES;793 break;794 case KW_ELSE:795 if (cc->flags & CCF_IN_IF) {796 cc->token=TK_ELSE;797 goto lex_end;798 }799lex_else:800 j=1;801 do {802 if (ch=LexGetChar(cc)) {803 if (ch=='#') {804 if (!Lex(cc))805 goto lex_end;806 i=PrsKeyWord(cc);807 if (i==KW_IF || i==KW_IFDEF || i==KW_IFNDEF ||808 i==KW_IFAOT || i==KW_IFJIT)809 j++;810 else if (i==KW_ENDIF)811 j--;812 }813 } else {814 cc->token=TK_EOF;815 goto lex_end;816 }817 } while (j);818 break;819 820 case KW_IF:821 if (cc->flags & CCF_IN_IF) {822 cc->token=TK_IF;823 goto lex_end;824 }825lex_if:826 cc->flags|=CCF_IN_IF;827 if (!Lex(cc)) {828 cc->flags&=~CCF_IN_IF;829 goto lex_end;830 }831 if (LexExpression(cc)) {832 cc->flags&=~CCF_IN_IF;833 switch (cc->token) {834 case TK_IF: goto lex_if;835 case TK_IFDEF: goto lex_ifdef;836 case TK_IFNDEF: goto lex_ifndef;837 case TK_IFAOT: goto lex_ifaot;838 case TK_IFJIT: goto lex_ifjit;839 case TK_ELSE: goto lex_else;840 case TK_ENDIF: goto lex_cont;841 default: goto lex_end;842 }843 } else {844 cc->flags&=~CCF_IN_IF;845 if (cc->token!=TK_ENDIF && cc->token!=TK_ELSE) {846 if (cc->token==TK_IF || cc->token==TK_IFDEF ||847 cc->token==TK_IFNDEF || cc->token==TK_IFAOT ||848 cc->token==TK_IFJIT)849 j=2;850 else851 j=1;852 do {853 if (ch=LexGetChar(cc)) {854 if (ch=='#') {855 if (!Lex(cc))856 goto lex_end;857 i=PrsKeyWord(cc);858 if (i==KW_IF || i==KW_IFDEF || i==KW_IFNDEF ||859 i==KW_IFAOT || i==KW_IFJIT)860 j++;861 else if (i==KW_ENDIF)862 j--;863 else if (i==KW_ELSE && j==1)864 break;865 }866 } else {867 cc->token=TK_EOF;868 goto lex_end;869 }870 } while (j);871 }872 }873 break;874 case KW_IFDEF:875 if (cc->flags & CCF_IN_IF) {876 cc->token=TK_IFDEF;877 goto lex_end;878 }879lex_ifdef:880 cc->flags|=CCF_NO_DEFINES;881 if (!Lex(cc)) {882 cc->flags&=~CCF_NO_DEFINES;883 goto lex_end;884 }885 cc->flags&=~CCF_NO_DEFINES;886 if (cc->token!=TK_IDENT)887 goto lex_end;888 if (cc->hash_entry)889 goto lex_cont;890 j=1;891 do {892 if (ch=LexGetChar(cc)) {893 if (ch=='#') {894 if (!Lex(cc))895 goto lex_end;896 i=PrsKeyWord(cc);897 if (i==KW_IF || i==KW_IFDEF || i==KW_IFNDEF ||898 i==KW_IFAOT || i==KW_IFJIT)899 j++;900 else if (i==KW_ENDIF)901 j--;902 else if (i==KW_ELSE && j==1)903 break;904 }905 } else {906 cc->token=TK_EOF;907 goto lex_end;908 }909 } while (j);910 break;911 case KW_IFNDEF:912 if (cc->flags & CCF_IN_IF) {913 cc->token=TK_IFNDEF;914 goto lex_end;915 }916lex_ifndef:917 cc->flags|=CCF_NO_DEFINES;918 if (!Lex(cc)) {919 cc->flags&=~CCF_NO_DEFINES;920 goto lex_end;921 }922 cc->flags&=~CCF_NO_DEFINES;923 if (cc->token!=TK_IDENT)924 goto lex_end;925 if (!cc->hash_entry)926 goto lex_cont;927 j=1;928 do {929 if (ch=LexGetChar(cc)) {930 if (ch=='#') {931 if (!Lex(cc))932 goto lex_end;933 i=PrsKeyWord(cc);934 if (i==KW_IF || i==KW_IFDEF || i==KW_IFNDEF ||935 i==KW_IFAOT || i==KW_IFJIT)936 j++;937 else if (i==KW_ENDIF)938 j--;939 else if (i==KW_ELSE && j==1)940 break;941 }942 } else {943 cc->token=TK_EOF;944 goto lex_end;945 }946 } while (j);947 break;948 case KW_IFAOT:949 if (cc->flags & CCF_IN_IF) {950 cc->token=TK_IFAOT;951 goto lex_end;952 }953lex_ifaot:954 if (cc->flags & CCF_AOT_COMPILE)955 goto lex_cont;956 j=1;957 do {958 if (ch=LexGetChar(cc)) {959 if (ch=='#') {960 if (!Lex(cc))961 goto lex_end;962 i=PrsKeyWord(cc);963 if (i==KW_IF || i==KW_IFDEF || i==KW_IFNDEF ||964 i==KW_IFAOT || i==KW_IFJIT)965 j++;966 else if (i==KW_ENDIF)967 j--;968 else if (i==KW_ELSE && j==1)969 break;970 }971 } else {972 cc->token=TK_EOF;973 goto lex_end;974 }975 } while (j);976 break;977 case KW_IFJIT:978 if (cc->flags & CCF_IN_IF) {979 cc->token=TK_IFAOT;980 goto lex_end;981 }982lex_ifjit:983 if (!(cc->flags & CCF_AOT_COMPILE))984 goto lex_cont;985 j=1;986 do {987 if (ch=LexGetChar(cc)) {988 if (ch=='#') {989 if (!Lex(cc))990 goto lex_end;991 i=PrsKeyWord(cc);992 if (i==KW_IF || i==KW_IFDEF || i==KW_IFNDEF ||993 i==KW_IFAOT || i==KW_IFJIT)994 j++;995 else if (i==KW_ENDIF)996 j--;997 else if (i==KW_ELSE && j==1)998 break;999 }1000 } else {1001 cc->token=TK_EOF;1002 goto lex_end;1003 }1004 } while (j);1005 break;1006 case KW_ENDIF:1007 if (cc->flags & CCF_IN_IF) {1008 cc->token=TK_ENDIF;1009 goto lex_end;1010 }1011 break;1012 case KW_ASSERT:1013 if (!Lex(cc))1014 goto lex_end;1015 if (!LexExpression(cc))1016 LexWarn(cc,"Assert Failed ");1017 goto lex_end;1018 case KW_EXE:1019 if (!Lex(cc))1020 goto lex_end;1021 PrsStreamBlk(cc);1022 goto lex_end;1023 case KW_HELP_INDEX:1024 if (Lex(cc)!=TK_STR)1025 goto lex_end;1026 Free(cc->cur_help_idx);1027 cc->cur_help_idx=LexExtStr(cc,,FALSE);1028 break;1029 case KW_HELP_FILE:1030 if (Lex(cc)!=TK_STR)1031 goto lex_end;1032 tmph=CAlloc(sizeof(CHashSrcSym));1033 fbuf=ExtDft(cc->cur_str,"DD.Z");1034 tmph->str=FileNameAbs(fbuf);1035 Free(fbuf);1036 tmph->type=HTT_HELP_FILE|HTF_PUBLIC;1037 HashSrcFileSet(cc,tmph);1038 HashAdd(tmph,cc->htc.glbl_hash_table);1039 break;1040 }1041 break;1042 case '\n':1043 if (!(cc->flags&CCF_KEEP_NEW_LINES))1044 break; //else fall through1045 case TK_INS_BIN:1046 case TK_INS_BIN_SIZE:1047 cc->token=ch;1048 goto lex_end;1049 case '.':1050 if (cc->flags&CCF_KEEP_DOT) {1051 cc->token=ch;1052 goto lex_end;1053 }1054 if (cc->flags&CCF_LAST_WAS_DOT) {1055 cc->flags&=~CCF_LAST_WAS_DOT;1056 goto lex_dot_dot;1057 }1058 ch=LexGetChar(cc);1059 if ('0'<=ch<='9') {1060 i=0;1061 goto lex_float_start;1062 } else if (ch=='.') {1063lex_dot_dot:1064 cc->token=TK_DOT_DOT;1065 if (LexGetChar(cc)=='.')1066 cc->token=TK_ELLIPSIS;1067 else1068 cc->flags|=CCF_USE_LAST_U16;1069 goto lex_end;1070 }1071 cc->flags|=CCF_USE_LAST_U16;1072 cc->token='.';1073 goto lex_end;1074 case '!':1075 case '$'...'&':1076 case '('...'-':1077 case '/':1078 case ':'...'?':1079 case '[':1080 case ']'...'^':1081 case '{'...'~':1082 case '`':1083 if (!(i=cmp.dual_U16_tokens1[ch])) {1084 if (ch=='$') {1085 ch=LexGetChar(cc);1086 if (ch=='$') {1087 cc->token='$';1088 goto lex_end;1089 } else if (ch) {1090 do ch=LexGetChar(cc);1091 while (ch && ch!='$');1092 if (!ch) {1093 cc->token=TK_EOF;1094 goto lex_end;1095 } else1096 goto lex_cont;1097 } else {1098 cc->flags|=CCF_USE_LAST_U16;1099 cc->token='$';1100 goto lex_end;1101 }1102 } else {1103 cc->token=ch;1104 goto lex_end;1105 }1106 } else {1107 j=LexGetChar(cc);1108 if (i.u16[0]==j) {1109 i>>=16;1110 if (!i) {// "/*"1111 j=1;1112 do {1113 if (!(ch=LexGetChar(cc)))1114 return cc->token=TK_EOF;1115lex_check_comment:1116 if (ch=='*') {1117 if (!(ch=LexGetChar(cc)))1118 return cc->token=TK_EOF;1119 if (ch=='/')1120 j--;1121 else1122 goto lex_check_comment;1123 } else if (ch=='/') {1124 if (!(ch=LexGetChar(cc)))1125 return cc->token=TK_EOF;1126 if (ch=='*')1127 j++;1128 else1129 goto lex_check_comment;1130 }1131 } while (j);1132 goto lex_cont;1133 } else {1134 cc->token=i;1135 goto lex_end;1136 }1137 }1138 if (i=cmp.dual_U16_tokens2[ch]) {1139 if (i.u16[0]==j) {1140 i>>=16;1141 if (!i) {// "//"1142 LexSkipEol(cc);1143 if (cc->flags&CCF_KEEP_NEW_LINES) {1144 cc->token='\n';1145 goto lex_end;1146 } else1147 goto lex_cont;1148 } else {1149 if (i==TK_SHL || i==TK_SHR) {1150 j=LexGetChar(cc);1151 if (j=='=') {1152 if (i==TK_SHL)1153 i=TK_SHL_EQU;1154 else1155 i=TK_SHR_EQU;1156 } else1157 cc->flags|=CCF_USE_LAST_U16;1158 }1159 cc->token=i;1160 goto lex_end;1161 }1162 }1163 if (i=cmp.dual_U16_tokens3[ch]) {1164 if (i.u16[0]==j) {1165 cc->token=i.u16[1];1166 goto lex_end;1167 }1168 }1169 }1170 cc->flags|=CCF_USE_LAST_U16;1171 cc->token=ch;1172 goto lex_end;1173 }1174 case TK_TKS_NUM:1175 break;1176 }1177 }1178lex_end:1179 LexGetChar(cc); //Do this so WAS_NEW_LINE is right1180 cc->flags|=CCF_USE_LAST_U16;1181 return cc->token;1182}1183 