source: dev/BasicCompiler_Common/StrOperation.cpp@ 17

Last change on this file since 17 was 15, checked in by dai_9181, 18 years ago

LexicalAnalysisを廃止。

File size: 14.0 KB
Line 
1#include "../BasicCompiler_Common/common.h"
2
3void KillSpaces(char *str1,char *str2){
4 int i,i2,i3,IsStr;
5 for(i=0,i2=0,IsStr=0;;i++,i2++){
6 if((str1[i]==' '||str1[i]=='\t')&&IsStr==0){
7 i3=i+1;
8 while(str1[i3]==' '||str1[i3]=='\t') i3++;
9 if(i!=0){
10 if(i==1){
11 if(IsVariableChar(str1[i-1])&&
12 IsVariableChar(str1[i3])){
13 //識別子間にある省略すべきでない空白
14 str2[i2++]=' ';
15 }
16 }
17 else{
18 if(str1[i-2]!=1&&IsVariableChar(str1[i-1])&&
19 IsVariableChar(str1[i3])){
20 //識別子間にある省略すべきでない空白
21 str2[i2++]=' ';
22 }
23 }
24 }
25 i=i3;
26 }
27 if(str1[i]=='\"') IsStr^=1;
28 str2[i2]=str1[i];
29 if(str1[i]=='\0') break;
30 }
31}
32void KillStringSpaces(char *str){
33 char *s2;
34
35 extern HANDLE hHeap;
36 s2=(char *)HeapAlloc(hHeap,0,lstrlen(str)+1);
37 lstrcpy(s2,str);
38 KillSpaces(s2,str);
39 HeapDefaultFree(s2);
40 return;
41}
42BOOL RemoveStringQuotes(char *str){
43 int i;
44 if(str[0]!='\"') return 0;
45 for(i=0;;i++){
46 str[i]=str[i+1];
47 if(str[i]=='\"') break;
48 }
49 str[i]=0;
50 if(str[i+2]!='\0') return 0;
51 return 1;
52}
53void RemoveStringPare(char *str){
54 int i;
55 i=lstrlen(str);
56 if(str[0]!='('||str[i-1]!=')') return;
57 SlideString(str+1,-1);
58 str[i-2]=0;
59}
60void RemoveStringBracket(char *str){
61 int i;
62 i=lstrlen(str);
63 if(str[0]!='['||str[i-1]!=']') return;
64 SlideString(str+1,-1);
65 str[i-2]=0;
66}
67void SetStringQuotes(char *str){
68 int i;
69 if(str[0]=='\"') return;
70 i=lstrlen(str);
71 SlideString(str,1);
72 str[0]='\"';
73 str[i+1]='\"';
74 str[i+2]=0;
75}
76int FormatString_EscapeSequence(char *buffer){
77 int i2,i3,i4;
78
79 for(i2=0,i3=0;;i2++,i3++){
80 if(IsDBCSLeadByte(buffer[i3])){
81 buffer[i2++]=buffer[i3++];
82 buffer[i2]=buffer[i3];
83 if(buffer[i3]=='\0') break;
84 continue;
85 }
86 if(buffer[i3]=='\\'){
87 switch(buffer[i3+1]){
88 case '\\':
89 buffer[i2]='\\';
90 i3++;
91 break;
92 case '0':
93 buffer[i2]='\0';
94 i3++;
95 break;
96 case 'n':
97 buffer[i2]='\n';
98 i3++;
99 break;
100 case 'q':
101 buffer[i2]='\"';
102 i3++;
103 break;
104 case 'r':
105 buffer[i2]='\r';
106 i3++;
107 break;
108 case 't':
109 buffer[i2]='\t';
110 i3++;
111 break;
112 case 'x':
113 //16進表記
114 char temporary[8];
115 temporary[0]=buffer[i3+2];
116 temporary[1]=buffer[i3+3];
117 temporary[2]=0;
118 CharUpper(temporary);
119
120 i4=temporary[0]-0x30;
121 if(i4<0||9<i4){
122 i4=temporary[0]-0x41+10;
123 if(i4<0xA||0xF<i4){
124 buffer[i2]=buffer[i3];
125 break;
126 }
127 }
128 temporary[0]=i4;
129
130 i4=temporary[1]-0x30;
131 if(i4<0||9<i4){
132 i4=temporary[1]-0x41+10;
133 if(i4<0xA||0xF<i4){
134 buffer[i2]=buffer[i3];
135 break;
136 }
137 }
138 temporary[1]=i4;
139
140 buffer[i2]=temporary[0]*0x10+temporary[1];
141 i3+=3;
142 break;
143 default:
144 buffer[i2]=buffer[i3];
145 break;
146 }
147 }
148 else{
149 buffer[i2]=buffer[i3];
150 if(buffer[i3]=='\0') break;
151 }
152 }
153 return i2;
154}
155void SlideString(char *buffer, int slide){
156 char *temp;
157 temp=(char *)malloc(lstrlen(buffer)+1);
158 lstrcpy(temp,buffer);
159 lstrcpy(buffer+slide,temp);
160 free(temp);
161}
162void SlideBuffer(char *buffer,int length,int slide){
163 void *temp;
164 temp=malloc(length+1);
165 memcpy(temp,buffer,length);
166 memcpy(buffer+slide,temp,length);
167 free(temp);
168}
169_int8 IsCommandDelimitation(char c){
170 if(c=='\n'||c==':'||c=='\0') return 1;
171 return 0;
172}
173BOOL IsBlank(char c){
174 if(c==' '||c=='\t') return 1;
175 return 0;
176}
177int GetOneParameter(char *Parameter,int pos,char *retAns){
178 int i,i2,i3,IsStr;
179 for(i=pos,i2=0,IsStr=0;;i++,i2++){
180 if(Parameter[i]=='\"') IsStr^=1;
181 else if(Parameter[i]=='('&&IsStr==0){
182 i3=GetStringInPare(retAns+i2,Parameter+i);
183 i+=i3-1;
184 i2+=i3-1;
185 continue;
186 }
187 else if(Parameter[i]=='['&&IsStr==0){
188 i3=GetStringInBracket(retAns+i2,Parameter+i);
189 i+=i3-1;
190 i2+=i3-1;
191 continue;
192 }
193 else if(Parameter[i]==','&&IsStr==0){
194 retAns[i2]=0;
195 i++;
196 break;
197 }
198 retAns[i2]=Parameter[i];
199 if(IsCommandDelimitation(Parameter[i])&&IsStr==0){
200 retAns[i2]=0;
201 break;
202 }
203 }
204 return i;
205}
206int JumpOneParameter(char *Parameter,int i){
207 int i2,IsStr;
208 for(i2=0,IsStr=0;;i++,i2++){
209 if(Parameter[i]=='\"') IsStr^=1;
210 else if(Parameter[i]=='('&&IsStr==0){
211 i=JumpStringInPare(Parameter,i+1);
212 continue;
213 }
214 else if(Parameter[i]=='['&&IsStr==0){
215 i=JumpStringInBracket(Parameter,i+1);
216 continue;
217 }
218 else if(Parameter[i]==','&&IsStr==0){
219 i++;
220 break;
221 }
222 if(IsCommandDelimitation(Parameter[i])) break;
223 }
224 return i;
225}
226int GetStringInQuotation(char *buffer,char *ReadBuffer){
227 int i;
228
229 if(ReadBuffer[0]=='\"'){
230 buffer[0]=ReadBuffer[0];
231 i=1;
232 }
233 else i=0;
234
235 for(;;i++){
236 buffer[i]=ReadBuffer[i];
237 if(IsDBCSLeadByte(ReadBuffer[i])){
238 i++;
239 buffer[i]=ReadBuffer[i];
240 continue;
241 }
242 if(ReadBuffer[i]=='\"'){
243 i++;
244 buffer[i]=0;
245 break;
246 }
247 if(ReadBuffer[i]=='\0') return 0;
248 }
249 return i;
250}
251int GetStringInPare(char *buffer,const char *ReadBuffer){
252 int i,IsStr,PareNum;
253 for(i=0,IsStr=0,PareNum=0;;i++){
254 buffer[i]=ReadBuffer[i];
255 if(IsDBCSLeadByte(ReadBuffer[i])){
256 i++;
257 buffer[i]=ReadBuffer[i];
258 continue;
259 }
260 if(ReadBuffer[i]=='\"') IsStr^=1;
261 else if(ReadBuffer[i]=='('&&IsStr==0) PareNum++;
262 else if(ReadBuffer[i]==')'&&IsStr==0){
263 PareNum--;
264 if(PareNum==0){
265 i++;
266 buffer[i]=0;
267 break;
268 }
269 }
270 else if(ReadBuffer[i]=='\0') return 0;
271 }
272 return i;
273}
274int GetStringInPare_RemovePare(char *buffer,char *ReadBuffer){
275 int i,IsStr,PareNum;
276 for(i=0,IsStr=0,PareNum=1;;i++){
277 buffer[i]=ReadBuffer[i];
278 if(IsDBCSLeadByte(ReadBuffer[i])){
279 i++;
280 buffer[i]=ReadBuffer[i];
281 continue;
282 }
283 if(ReadBuffer[i]=='\"') IsStr^=1;
284 else if(ReadBuffer[i]=='('&&IsStr==0) PareNum++;
285 else if(ReadBuffer[i]==')'&&IsStr==0){
286 PareNum--;
287 if(PareNum==0){
288 buffer[i]=0;
289 break;
290 }
291 }
292 else if(ReadBuffer[i]=='\0') return 0;
293 }
294 return i;
295}
296int GetStringInBracket(char *buffer,const char *ReadBuffer){
297 int i,IsStr,PareNum;
298 for(i=0,IsStr=0,PareNum=0;;i++){
299 buffer[i]=ReadBuffer[i];
300 if(IsDBCSLeadByte(ReadBuffer[i])){
301 i++;
302 buffer[i]=ReadBuffer[i];
303 continue;
304 }
305 if(ReadBuffer[i]=='\"') IsStr^=1;
306 else if(ReadBuffer[i]=='['&&IsStr==0) PareNum++;
307 else if(ReadBuffer[i]==']'&&IsStr==0){
308 PareNum--;
309 if(PareNum==0){
310 i++;
311 buffer[i]=0;
312 break;
313 }
314 }
315 else if(ReadBuffer[i]=='\0') return 0;
316 }
317 return i;
318}
319int JumpStringInPare(char *buffer,int pos){
320 int PareNum;
321 for(PareNum=1;;pos++){
322 if(buffer[pos]=='\"'){
323 for(pos++;;pos++){
324 if(buffer[pos]=='\"') break;
325 }
326 continue;
327 }
328 else if(buffer[pos]=='(') PareNum++;
329 else if(buffer[pos]==')'){
330 PareNum--;
331 if(PareNum==0) return pos;
332 }
333 else if(buffer[pos]=='\0') break;
334 }
335 return 0;
336}
337int JumpStringInBracket(char *buffer,int pos){
338 int PareNum;
339 for(PareNum=1;;pos++){
340 if(buffer[pos]=='\"'){
341 for(pos++;;pos++){
342 if(buffer[pos]=='\"') break;
343 }
344 continue;
345 }
346 else if(buffer[pos]=='[') PareNum++;
347 else if(buffer[pos]==']'){
348 PareNum--;
349 if(PareNum==0) return pos;
350 }
351 else if(buffer[pos]=='\0') break;
352 }
353 return 0;
354}
355
356int GetCpFromLine(int LineNum){
357 extern char *basbuf;
358
359 int i,nCount=0;
360 for(i=0;;i++){
361 if(basbuf[i]=='\n'){
362 nCount++;
363 if(LineNum==nCount){
364 for(i++;;i++){
365 if(IsCommandDelimitation(basbuf[i])) break;
366 }
367 return i;
368 }
369 }
370 }
371}
372BOOL GetLineNum(int pos,int *pLine,char *FileName){
373 extern INCLUDEFILEINFO IncludeFileInfo;
374 extern char *basbuf;
375 int i,i2,i3,i4,i5;
376
377 i=pos;
378 if(basbuf[i]=='\n') i--;
379 for(i3=0,i2=0;i3<i;i3++){
380 if(basbuf[i3]=='\n') i2++;
381 if(basbuf[i3]=='\0') return 0;
382 }
383 i4=0;
384 while(IncludeFileInfo.LineOfFile[i2]!=IncludeFileInfo.LineOfFile[i4]) i4++;
385 for(i3=0,i5=0;i5<i4;i3++){
386 if(basbuf[i3]=='\n') i5++;
387 if(basbuf[i3]=='\0') return 0;
388 }
389 for(i5=0;i4<i2;i3++){
390 if(basbuf[i3]=='\n'){
391 i4++;
392 i5++;
393 if(IncludeFileInfo.LineOfFile[i2]<IncludeFileInfo.LineOfFile[i4]){
394 for(;IncludeFileInfo.LineOfFile[i2]!=IncludeFileInfo.LineOfFile[i4];i3++){
395 if(basbuf[i3]=='\n') i4++;
396 }
397 }
398 }
399 if(basbuf[i3]=='\0') return 0;
400 }
401
402 if(IncludeFileInfo.LineOfFile[i2]==-1){
403 //ファイル・行番号を特定できなかった場合
404 *pLine=-1;
405 FileName[0]=0;
406 }
407 else{
408 //行番号をセット
409 *pLine=i5;
410
411 //ファイル名をセット
412 lstrcpy(FileName,IncludeFileInfo.ppFileNames[IncludeFileInfo.LineOfFile[i2]]);
413 }
414
415 return 1;
416}
417
418char GetEndXXXCommand(char es){
419 switch(es){
420 case ESC_SUB:
421 return ESC_ENDSUB;
422 case ESC_FUNCTION:
423 return ESC_ENDFUNCTION;
424 case ESC_MACRO:
425 return ESC_ENDMACRO;
426 case ESC_TYPE:
427 return ESC_ENDTYPE;
428 case ESC_CLASS:
429 return ESC_ENDCLASS;
430 case ESC_INTERFACE:
431 return ESC_ENDINTERFACE;
432 case ESC_ENUM:
433 return ESC_ENDENUM;
434 }
435 return 0;
436}
437void GetDefaultNameFromES(char es,char *name){
438 name[0]=0;
439 switch(es){
440 case ESC_PSMEM:
441 lstrcpy(name,"->");
442 break;
443 case ESC_MOD:
444 lstrcpy(name,"Mod");
445 break;
446 case ESC_AND:
447 lstrcpy(name,"And");
448 break;
449 case ESC_OR:
450 lstrcpy(name,"Or");
451 break;
452 case ESC_XOR:
453 lstrcpy(name,"Xor");
454 break;
455 case ESC_NOT:
456 lstrcpy(name,"Not");
457 break;
458 case ESC_AS:
459 lstrcpy(name,"As");
460 break;
461 case ESC_THEN:
462 lstrcpy(name,"Then");
463 break;
464 case ESC_ELSE:
465 lstrcpy(name,"Else");
466 break;
467 case ESC_IF:
468 lstrcpy(name,"If");
469 break;
470 case ESC_ELSEIF:
471 lstrcpy(name,"ElseIf");
472 break;
473 case ESC_ENDIF:
474 lstrcpy(name,"End If");
475 break;
476 case ESC_DEF:
477 lstrcpy(name,"Def");
478 break;
479 case ESC_DECLARE:
480 lstrcpy(name,"Declare");
481 break;
482 case ESC_ABSTRACT:
483 lstrcpy(name,"Abstract");
484 break;
485 case ESC_VIRTUAL:
486 lstrcpy(name,"Virtual");
487 break;
488 case ESC_OVERRIDE:
489 lstrcpy(name,"Override");
490 break;
491 case ESC_SUB:
492 lstrcpy(name,"Sub");
493 break;
494 case ESC_ENDSUB:
495 lstrcpy(name,"End Sub");
496 break;
497 case ESC_EXITSUB:
498 lstrcpy(name,"Exit Sub");
499 break;
500 case ESC_FUNCTION:
501 lstrcpy(name,"Function");
502 break;
503 case ESC_ENDFUNCTION:
504 lstrcpy(name,"End Function");
505 break;
506 case ESC_EXITFUNCTION:
507 lstrcpy(name,"Exit Function");
508 break;
509 case ESC_BYVAL:
510 lstrcpy(name,"ByVal ");
511 break;
512 case ESC_BYREF:
513 lstrcpy(name,"ByRef ");
514 break;
515 case ESC_TYPE:
516 lstrcpy(name,"Type");
517 break;
518 case ESC_ENDTYPE:
519 lstrcpy(name,"End Type");
520 break;
521 case ESC_EXITFOR:
522 lstrcpy(name,"Exit For");
523 break;
524 case ESC_EXITWHILE:
525 lstrcpy(name,"Exit While");
526 break;
527 case ESC_EXITDO:
528 lstrcpy(name,"Exit Do");
529 break;
530 case ESC_SELECTCASE:
531 lstrcpy(name,"Select Case");
532 break;
533 case ESC_CASE:
534 lstrcpy(name,"Case");
535 break;
536 case ESC_CASEELSE:
537 lstrcpy(name,"Case Else");
538 break;
539 case ESC_ENDSELECT:
540 lstrcpy(name,"End Select");
541 break;
542 case ESC_CONST:
543 lstrcpy(name,"Const");
544 break;
545 case ESC_TYPEDEF:
546 lstrcpy(name,"TypeDef");
547 break;
548 case ESC_WITH:
549 lstrcpy(name,"With");
550 break;
551 case ESC_ENDWITH:
552 lstrcpy(name,"End With");
553 break;
554 case ESC_CDECL:
555 lstrcpy(name,"cdecl");
556 break;
557 case ESC_MACRO:
558 lstrcpy(name,"Macro");
559 break;
560 case ESC_ENDMACRO:
561 lstrcpy(name,"End Macro");
562 break;
563 case ESC_EXITMACRO:
564 lstrcpy(name,"Exit Macro");
565 break;
566 case ESC_EXPORT:
567 lstrcpy(name,"Export");
568 break;
569 case ESC_CONTINUE:
570 lstrcpy(name,"Continue");
571 break;
572 case ESC_CLASS:
573 lstrcpy(name,"Class");
574 break;
575 case ESC_ENDCLASS:
576 lstrcpy(name,"End Class");
577 break;
578 case ESC_INTERFACE:
579 lstrcpy(name,"Interface");
580 break;
581 case ESC_ENDINTERFACE:
582 lstrcpy(name,"End Interface");
583 break;
584 case ESC_ENUM:
585 lstrcpy(name,"Enum");
586 break;
587 case ESC_ENDENUM:
588 lstrcpy(name,"End Enum");
589 break;
590 }
591}
592void GetCalcName(int idCalc,char *name){
593 switch(idCalc){
594 case CALC_XOR:
595 lstrcpy(name,"xor");
596 break;
597 case CALC_OR:
598 lstrcpy(name,"or");
599 break;
600 case CALC_AND:
601 lstrcpy(name,"and");
602 break;
603 case CALC_NOT:
604 lstrcpy(name,"Not");
605 break;
606
607 case CALC_PE:
608 lstrcpy(name,"<=");
609 break;
610 case CALC_QE:
611 lstrcpy(name,">=");
612 break;
613 case CALC_NOTEQUAL:
614 lstrcpy(name,"<>");
615 break;
616 case CALC_SUBSITUATION:
617 case CALC_EQUAL:
618 lstrcpy(name,"=");
619 break;
620 case CALC_P:
621 lstrcpy(name,"<");
622 break;
623 case CALC_Q:
624 lstrcpy(name,">");
625 break;
626
627 case CALC_SHL:
628 lstrcpy(name,"<<");
629 break;
630 case CALC_SHR:
631 lstrcpy(name,">>");
632 break;
633 case CALC_ADDITION:
634 lstrcpy(name,"+");
635 break;
636 case CALC_SUBTRACTION:
637 lstrcpy(name,"-");
638 break;
639 case CALC_MOD:
640 lstrcpy(name,"mod");
641 break;
642 case CALC_PRODUCT:
643 lstrcpy(name,"*");
644 break;
645 case CALC_QUOTIENT:
646 lstrcpy(name,"/");
647 break;
648 case CALC_INTQUOTIENT:
649 lstrcpy(name,"\\");
650 break;
651 case CALC_AS:
652 lstrcpy(name,"As");
653 break;
654 case CALC_MINUSMARK:
655 lstrcpy(name,"-");
656 break;
657 case CALC_POWER:
658 lstrcpy(name,"^");
659 break;
660
661 case CALC_ARRAY_GET:
662 lstrcpy(name,"[]");
663 break;
664 case CALC_ARRAY_SET:
665 lstrcpy(name,"[]=");
666 break;
667 }
668}
669
670BOOL IsFile(char *path){
671 WIN32_FIND_DATA wfd;
672 HANDLE hFind;
673
674 hFind=FindFirstFile(path,&wfd);
675 if(hFind==INVALID_HANDLE_VALUE){
676 return 0;
677 }
678 FindClose(hFind);
679
680 return 1;
681}
682BOOL GetFolder(HWND hWnd,char *folder,char *OpenFolderTitle){
683 BROWSEINFO BrowseInfo;
684 LPITEMIDLIST pidlBrowse;
685 LPMALLOC g_pMalloc;
686 char temporary[MAX_PATH];
687
688 BrowseInfo.hwndOwner=hWnd;
689 BrowseInfo.pszDisplayName=temporary;
690 BrowseInfo.pidlRoot=NULL;
691 BrowseInfo.lpszTitle=OpenFolderTitle;
692 BrowseInfo.ulFlags=BIF_RETURNONLYFSDIRS;
693 BrowseInfo.lpfn=NULL;
694 BrowseInfo.lParam=(LPARAM)0;
695 BrowseInfo.iImage=0;
696 pidlBrowse=SHBrowseForFolder(&BrowseInfo);
697 if(pidlBrowse){
698 if(SHGetMalloc(&g_pMalloc)!=0) return 0;
699 SHGetPathFromIDList(pidlBrowse,folder);
700 g_pMalloc->Free(pidlBrowse);
701 g_pMalloc->Release();
702 return 1;
703 }
704 return 0;
705}
Note: See TracBrowser for help on using the repository browser.