source: dev/trunk/ab5.0/abdev/ab_common/src/Lexical/Source.cpp@ 707

Last change on this file since 707 was 706, checked in by dai_9181, 16 years ago

インクルードファイルの列挙時、ワイルドカード指定が外されていたので元に戻す。
その他、不要なコメントを排除(SVNは差分が残るので、古いソースコードのコメント化は不要です)。

File size: 21.1 KB
Line 
1#include "stdafx.h"
2#include <hash_set>
3
4const std::string BasicSource::generateDirectiveName = "#generate";
5
6
7class CDefine{
8 std::vector<std::string> names;
9public:
10 void Free();
11 void Init( bool isDebug, bool isDll, bool isUnicode, int majorVer );
12
13 BOOL add(char const *name);
14 BOOL undef(char const *name);
15 BOOL check(char const *name);
16 void preprocessor_ifdef(char *buffer,bool isNot);
17 void DirectiveIfdef(char *buffer);
18};
19CDefine objDefine;
20
21
22//////////////////////////////////////
23// #requireの管理
24//////////////////////////////////////
25namespace
26{
27class CRequireFiles{
28 stdext::hash_set<std::string> filepaths;
29public:
30 void clear(){
31 filepaths.clear();
32 }
33 //既に存在するものを追加しようとするとfalseを返す(旧IsIncludedと逆なことに注意)
34 bool TryAdd( const std::string &includeFilePath ){
35 char tempPath[MAX_PATH];
36 DWORD len = GetShortPathName(includeFilePath.c_str(), tempPath, MAX_PATH);
37 if (len >= MAX_PATH){
38 return false;
39 }
40 for( DWORD i = 0; i < len; ++i ){
41 char c = toupper(tempPath[i]);
42 if (c == '/'){
43 tempPath[i] = '\\';
44 }
45 else{
46 tempPath[i] = c;
47 }
48 }
49 return filepaths.insert( tempPath ).second;
50 }
51};
52CRequireFiles requireFiles;
53} //namespace
54
55//////////////////////////////////////
56// #define間するクラス
57//////////////////////////////////////
58
59void CDefine::Init( bool isDebug, bool isDll, bool isUnicode, int majorVer )
60{
61 names.clear();
62
63 if( isDebug )
64 {
65 add("_DEBUG");
66 }
67
68#ifdef _AMD64_
69 add("_WIN64");
70#endif
71
72 if( isDll )
73 {
74 add("_DLL");
75 }
76
77 if( isUnicode )
78 {
79 add( "UNICODE" );
80 }
81
82 char temporary[255];
83 sprintf(temporary,"_AB_VER%d",majorVer);
84 add(temporary);
85}
86BOOL CDefine::add(char const *name)
87{
88 //重複チェック
89 if(check(name)) return 0;
90
91 //追加
92 names.push_back( name );
93
94 return 1;
95}
96BOOL CDefine::undef(char const *name){
97 std::vector<std::string>::iterator i = names.begin();
98 BOOST_FOREACH( const std::string &temp, names ){
99 if( temp == name ){
100 names.erase( i );
101 return 1;
102 }
103 i++;
104 }
105
106 return 0;
107}
108BOOL CDefine::check(char const *name){
109
110 //重複チェック
111 BOOST_FOREACH( const std::string &temp, names ){
112 if( temp == name ){
113 return 1;
114 }
115 }
116 return 0;
117}
118
119int Search_endif(char const *buffer,int i, int *pLine = 0){
120 for(;;i++){
121 if(buffer[i]=='\0') break;
122
123 if( buffer[i] == '\n' ){
124 if( pLine ){
125 (*pLine)++;
126 }
127 }
128
129 if(buffer[i-1]=='\n'){
130 if(_memicmp(buffer+i,"#ifdef",6)==0||_memicmp(buffer+i,"#ifndef",7)==0){
131 i=Search_endif(buffer,i+6, pLine);
132 if(buffer[i]=='\0') break;
133 continue;
134 }
135 else if(_memicmp(buffer+i,"#endif",6)==0){
136 break;
137 }
138 }
139 }
140 return i;
141}
142
143void CDefine::preprocessor_ifdef(char *buffer,bool isNot){
144 int i,i2,i3;
145 char temporary[VN_SIZE];
146
147 if(isNot) i=strlen("#ifndef");
148 else i=strlen("#ifdef");
149 while(buffer[i]==' '||buffer[i]=='\t') i++;
150
151 for(i2=0;;i++,i2++){
152 if(buffer[i]=='\n'||buffer[i]=='\0'){
153 temporary[i2]=0;
154 break;
155 }
156 temporary[i2]=buffer[i];
157 }
158
159 int sw=0;
160 if(check(temporary)) sw=1;
161
162 if(isNot){
163 //#ifndefのとき(反対にする)
164 if(sw) sw=0;
165 else sw=1;
166 }
167
168 //#ifdefの行を消去
169 Text::SlideString(buffer+i,-i);
170 i=0;
171
172 BOOL bElse=0;
173 if(sw){
174 //TRUEのとき
175
176 //#else、#endifを探索
177 for(;;i++){
178 if(buffer[i]=='\0') break;
179
180 if(i==0||buffer[i-1]=='\n'){
181 if(memicmp(buffer+i,"#ifdef",6)==0||memicmp(buffer+i,"#ifndef",7)==0){
182 i=Search_endif(buffer,i+6);
183 if(buffer[i]=='\0') break;
184 continue;
185 }
186 else if(memicmp(buffer+i,"#else",5)==0){
187 i2=5;
188 bElse=1;
189 break;
190 }
191 else if(memicmp(buffer+i,"#endif",6)==0){
192 i2=6;
193 bElse=0;
194 break;
195 }
196 }
197 }
198
199 //行を消去
200 Text::SlideString(buffer+i+i2,-i2);
201
202 if(bElse){
203 //#elseがある場合はその区間を消去
204
205 for(i2=i,i3=0;;i2++){
206 if(buffer[i2]=='\0') break;
207
208 if(buffer[i2]=='\n') i3++;
209
210 if(i2==0||buffer[i2-1]=='\n'){
211 if(memicmp(buffer+i2,"#ifdef",6)==0||memicmp(buffer+i2,"#ifndef",7)==0){
212 i2=Search_endif(buffer,i2+6, &i3 );
213 if(buffer[i2]=='\0') break;
214 continue;
215 }
216 if(memicmp(buffer+i2,"#endif",6)==0){
217 i2+=6;
218 break;
219 }
220 }
221 }
222
223 //ソースコード区間を消去し、改行コードを挿入
224 Text::SlideString(buffer+i2,i-i2+i3);
225 memset(buffer+i,'\n',i3);
226 }
227 }
228 else{
229 //FALSEのとき
230
231 //#else、#endifを探索
232 for(i2=i,i3=0;;i2++){
233 if(buffer[i2]=='\0') break;
234
235 if(buffer[i2]=='\n') i3++;
236
237 if(i2==0||buffer[i2-1]=='\n'){
238 if(memicmp(buffer+i2,"#ifdef",6)==0||memicmp(buffer+i2,"#ifndef",7)==0){
239 i2=Search_endif(buffer,i2+6, &i3 );
240 if(buffer[i2]=='\0') break;
241 continue;
242 }
243 else if(memicmp(buffer+i2,"#else",5)==0){
244 i2+=5;
245 bElse=1;
246 break;
247 }
248 else if(memicmp(buffer+i2,"#endif",6)==0){
249 i2+=6;
250 bElse=0;
251 break;
252 }
253 }
254 }
255
256 //ソースコード区間を消去し、改行コードを挿入
257 Text::SlideString(buffer+i2,i-i2+i3);
258 memset(buffer+i,'\n',i3);
259
260 if(bElse){
261 //#endifを探索
262 for(;;i++){
263 if(buffer[i]=='\0') break;
264
265 if(i==0||buffer[i-1]=='\n'){
266 if(memicmp(buffer+i,"#ifdef",6)==0||memicmp(buffer+i,"#ifndef",7)==0){
267 i=Search_endif(buffer,i+6);
268 if(buffer[i]=='\0') break;
269 continue;
270 }
271 else if(memicmp(buffer+i,"#endif",6)==0){
272 i2=6;
273 bElse=0;
274 break;
275 }
276 }
277 }
278
279 //行を消去
280 Text::SlideString(buffer+i+i2,-i2);
281 }
282 }
283}
284
285
286void CDefine::DirectiveIfdef(char *buffer){
287 int i,i2,i3,sw;
288 char temporary[VN_SIZE];
289
290 for(i=0;;i++){
291 if(buffer[i]=='\0') break;
292
293 if(i==0||(i>=1&&buffer[i-1]=='\n')){
294 sw=0;
295 if(memicmp(buffer+i,"#define",7)==0){
296 i2=i+7;
297 while(buffer[i2]==' '||buffer[i2]=='\t') i2++;
298
299 for(i3=0;;i2++,i3++){
300 if(buffer[i2]=='\n'||buffer[i2]=='\0'){
301 temporary[i3]=0;
302 break;
303 }
304 temporary[i3]=buffer[i2];
305 }
306
307 add(temporary);
308
309 i2-=i;
310
311 //ディレクティブを消去
312 Text::SlideString(buffer+i+i2,-i2);
313 }
314 if(memicmp(buffer+i,"#undef",6)==0){
315 i2=i+7;
316 while(buffer[i2]==' '||buffer[i2]=='\t') i2++;
317
318 for(i3=0;;i2++,i3++){
319 if(buffer[i2]=='\n'||buffer[i2]=='\0'){
320 temporary[i3]=0;
321 break;
322 }
323 temporary[i3]=buffer[i2];
324 }
325
326 undef(temporary);
327
328 i2-=i;
329
330 //ディレクティブを消去
331 Text::SlideString(buffer+i+i2,-i2);
332 }
333 else if(memicmp(buffer+i,"#ifdef",6)==0){
334 preprocessor_ifdef(buffer+i,false);
335 continue;
336 }
337 else if(memicmp(buffer+i,"#ifndef",7)==0){
338 preprocessor_ifdef(buffer+i,true);
339 continue;
340 }
341 else continue;
342 }
343 }
344}
345
346
347
348
349bool Text::ReadFile( const std::string &filePath ){
350 //ファイルオープン
351 HANDLE hFile=CreateFile(filePath.c_str(),GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
352 if(hFile==INVALID_HANDLE_VALUE){
353 return false;
354 }
355
356 length = GetFileSize( hFile, NULL );
357
358 buffer = (char *)realloc( buffer, length + 1 );
359
360 //読み込み
361 DWORD dwAccBytes;
362 ::ReadFile(hFile,buffer,length,&dwAccBytes,0);
363 buffer[dwAccBytes]=0;
364
365 //ファイルクローズ
366 CloseHandle(hFile);
367
368 return true;
369}
370
371void BasicSource::ChangeReturnLineChar(){
372 int i,i2;
373
374 bool isMustChange = false;
375 for( i=0; ; i++ ){
376 if( buffer[i] == '\0' ){
377 break;
378 }
379 if( buffer[i]=='\n' )
380 {
381 if( i>0 )
382 {
383 if( buffer[i-1] == '\r' )
384 {
385 isMustChange = true;
386 }
387 }
388 }
389 }
390
391 if( !isMustChange )
392 {
393 // 改行コードの変換は必要ない
394 return;
395 }
396
397#ifdef _DEBUG
398 //改行コードの整合性チェック
399 for( i=0; ; i++ ){
400 if( buffer[i] == '\0' ){
401 break;
402 }
403 if( buffer[i]!='\r' && buffer[i+1]=='\n'
404 || buffer[i]=='\r' && buffer[i+1]!='\n' ){
405 char temporary[255];
406 strncpy( temporary, buffer + i-100, 130 );
407 temporary[130] = 0;
408 for(int i2=0; ;i2++){
409 if(temporary[i2]=='\r') temporary[i2]='A';
410 if(temporary[i2]=='\n') temporary[i2]='B';
411 if(temporary[i2]=='\0') break;
412 }
413
414 extern HWND hOwnerEditor;
415 MessageBox( hOwnerEditor, temporary, "改行コードの整合性チェック", MB_OK | MB_ICONEXCLAMATION );
416 }
417 }
418#endif
419
420 //改行コードのCRLFをLFに変換
421 for(i=0,i2=0;;i++,i2++){
422 if(buffer[i]=='\r'&&buffer[i+1]=='\n') i++;
423 buffer[i2]=buffer[i];
424 if(buffer[i]=='\0') break;
425 }
426
427 length = i;
428}
429
430void BasicSource::RemoveComments(){
431 int i,i2,i3,IsStr;
432 char *temporary=static_cast<char *>(malloc(strlen(buffer)+1));
433 for(i=0,i2=0,i3=0,IsStr=0;;i++,i2++){
434 if(buffer[i]=='\"') IsStr^=1;
435 if(buffer[i]=='\n'||buffer[i]=='\0'){
436 i2--;
437 while(temporary[i2]==' '||temporary[i2]=='\t') i2--;
438 i2++;
439
440 if(i3){
441 //複数行に渡る注釈文の中に改行が存在するとき
442 memset(temporary+i2,'\n',i3);
443 i2+=i3;
444 i3=0;
445 }
446 }
447 if(buffer[i]=='\''&&IsStr==0){
448 //注釈文
449 i2--;
450 while(temporary[i2]==' '||temporary[i2]=='\t') i2--;
451 i2++;
452 while(buffer[i]!='\n'&&buffer[i]!='\0') i++;
453 }
454 if(buffer[i]=='/'&&buffer[i+1]=='*'&&IsStr==0){
455 //注釈文(複数行)
456 i+=2;
457 i3=0;
458 while(!(buffer[i]=='*'&&buffer[i+1]=='/')){
459 if(buffer[i]=='\n') i3++;
460 if(buffer[i]=='\0') break;
461 i++;
462 }
463 if(buffer[i]){
464 i+=2;
465 }
466 i--;
467 i2--;
468 continue;
469 }
470 temporary[i2]=buffer[i];
471 if(buffer[i]=='\0') break;
472 }
473 strcpy(buffer,temporary);
474 free(temporary);
475}
476
477bool BasicSource::ReadFile_InIncludeDirective( const std::string &filePath ){
478 if( !Text::ReadFile( filePath ) ){
479 return false;
480 }
481
482 // 改行コードをCRLFからLFに変換
483 ChangeReturnLineChar();
484
485 // コメントを削除
486 RemoveComments();
487
488 // #ifdefディレクティブを処理
489 objDefine.DirectiveIfdef( buffer );
490
491 // アンダーバーによる改行を正規表現に戻す
492 RemoveReturnLineUnderbar();
493
494 // ダミー改行をセット
495 Realloc( length + 2 );
496 Text::SlideString( buffer, 2 );
497 buffer[0] = '\n';
498 buffer[1] = '\n';
499
500 return true;
501}
502
503void BasicSource::DirectiveIncludeOrRequire( const std::string &mainSourceFilePath, const std::string &includeDirPath )
504{
505 int i,i2,i3,sw1,LineNum,FileLayer[255],layer,LastFileByte[255];
506 char temporary[MAX_PATH],temp2[MAX_PATH+255],*LayerDir[255];
507
508 layer=0;
509 FileLayer[layer]=0;
510 LastFileByte[layer]=GetLength();
511 LineNum=0;
512
513 if( includedFilesRelation.GetLineCounts() != 0 )
514 {
515 Jenga::Throw( "インクルードファイル構造の初期値が不正" );
516 }
517
518 // メインソースコード
519 FileLayer[layer] = includedFilesRelation.AddFile( mainSourceFilePath );
520
521 //参照ディレクトリ
522 std::string mainSourceFileDir = Jenga::Common::Path::ExtractDirPath( mainSourceFilePath );
523 LayerDir[0]=(char *)malloc(mainSourceFileDir.size()+1);
524 strcpy(LayerDir[0],mainSourceFileDir.c_str());
525
526 for(i=0;;i++){
527 if(buffer[i]=='\0'){
528 break;
529 }
530 if(buffer[i]=='\n'){
531 includedFilesRelation.AddLine( FileLayer[layer] );
532 }
533 if(i>LastFileByte[layer]){
534 free(LayerDir[layer]);
535 LayerDir[layer]=0;
536 layer--;
537 }
538 if((buffer[i-1]=='\n'||i==0)&&buffer[i]=='#'){
539 bool isRequire = false;
540
541 int includeDirectiveLength;
542
543 char findStr[1024];
544 if(memcmp( buffer + i + 1, "include", 7 ) == 0
545 || memcmp( buffer + i + 1, "require", 7 ) == 0)
546 {
547 //#requireの場合
548 if( buffer[i + 1] == 'r' ) isRequire = true;
549
550 i2=i+8;
551 while(buffer[i2]==' '||buffer[i2]=='\t') i2++;
552
553 if(buffer[i2]=='\"') sw1=0;
554 else if(buffer[i2]=='<') sw1=1;
555 i2++;
556
557 for(i3=0;;i2++,i3++){
558 if((buffer[i2]=='\"'&&sw1==0)||(buffer[i2]=='>'&&sw1==1)||buffer[i2]=='\n'||buffer[i2]=='\0'){
559 temporary[i3]=0;
560 break;
561 }
562 temporary[i3]=buffer[i2];
563 }
564 while(buffer[i2]!='\n'&&buffer[i2]!='\0') i2++;
565
566 includeDirectiveLength = i2 - i;
567
568 if(sw1){
569 sprintf(temp2,"%s\\%s", includeDirPath.c_str(), temporary );
570 strcpy(findStr,temp2);
571 }
572 else{
573 Jenga::Common::Directory dir( LayerDir[layer] );
574 strcpy( findStr, dir.GetFullPath( temporary ).c_str() );
575 }
576 }
577 else if(memcmp(buffer+i+1,"prompt",6)==0){
578 includeDirectiveLength = 7;
579 sprintf(findStr,"%s\\basic\\prompt.sbp", includeDirPath.c_str() );
580 }
581 else if(memcmp(buffer+i+1,"N88BASIC",8)==0){
582 includeDirectiveLength = 9;
583 sprintf(findStr,"%s\\basic\\prompt.sbp", includeDirPath.c_str() );
584 }
585 else if(memcmp(buffer+i+1,"console",7)==0){
586 //サブシステム タイプをCUIに変更
587 extern unsigned short TypeOfSubSystem;
588 TypeOfSubSystem=IMAGE_SUBSYSTEM_WINDOWS_CUI;
589
590 includeDirectiveLength = 8;
591 sprintf(findStr,"%s\\basic\\dos_console.sbp", includeDirPath.c_str() );
592 }
593 else continue;
594
595 // インクルードファイルを列挙(ワイルドカード指定を想定)
596 Jenga::Common::Strings resultOfFullPath;
597 Jenga::Common::FileSystem::SearchFiles( resultOfFullPath, findStr );
598
599 if( resultOfFullPath.empty() )
600 {
601 this->cannotIncludePath = findStr;
602 this->cannotIncludeSourcePos = i;
603 includedFilesRelation.AddLine( FileLayer[layer] );
604 break;
605 }
606
607 for( int j=static_cast<int>(resultOfFullPath.size()-1); j>=0; j-- )
608 {
609 const std::string &sourceFilePath = resultOfFullPath[j];
610
611 const int headIndex = i;
612
613 if( headIndex == 0 && Jenga::Common::Path( sourceFilePath ).GetFileName() == "basic" )
614 {
615 // basic.sbpインクルード時は何もしない
616 // ワイルドカードで複数ファイルを指定した場合、2つ目以上のファイルの場合もこちら
617 }
618 else
619 {
620 //ディレクティブが消えるため、一行減ってしまうのを防ぐ(basic.sbpを除く)
621 Text::SlideString( buffer + headIndex + includeDirectiveLength, 1 );
622 buffer[headIndex+includeDirectiveLength]='\n';
623 for(i3=0;i3<=layer;i3++) LastFileByte[i3]++;
624 }
625
626 layer++;
627 FileLayer[layer] = includedFilesRelation.AddFile( sourceFilePath );
628
629 //#requireの場合では、既に読み込まれているファイルは読み込まないようにする
630 BasicSource source;
631
632 if( !requireFiles.TryAdd( sourceFilePath ) && isRequire ){
633 //既に読み込まれているときは空データ
634 source.SetBuffer( "" );
635 }
636 else{
637 //インクルードファイルを読み込む
638 if( !source.ReadFile_InIncludeDirective( sourceFilePath ) )
639 {
640 _ASSERTE( false );
641 }
642 }
643
644 Realloc( strlen(buffer) + source.GetLength() );
645 Text::SlideString(
646 buffer + headIndex + includeDirectiveLength,
647 source.GetLength() - includeDirectiveLength
648 );
649 memcpy(
650 buffer + headIndex,
651 source.GetBuffer(),
652 source.GetLength()
653 );
654
655 //新しい参照ディレクトリをセット
656 char temp4[MAX_PATH];
657 _splitpath(sourceFilePath.c_str(),temp2,temp4,0,0);
658 strcat(temp2,temp4);
659 LayerDir[layer]=(char *)malloc(strlen(temp2)+1);
660 strcpy(LayerDir[layer],temp2);
661
662 //ファイル範囲をスライド
663 LastFileByte[layer] = headIndex + source.GetLength() - 1;
664 for(i3=0;i3<layer;i3++)
665 {
666 LastFileByte[i3] += source.GetLength() - includeDirectiveLength;
667 }
668
669 includeDirectiveLength = 0;
670 }
671 i--;
672 }
673 }
674
675 free(LayerDir[0]);
676
677 length = strlen(buffer);
678}
679
680int KillReturnCode_InParameter(char *buffer,int *pRnum,char cBeginPare,char cEndPare){
681 int i,i2,i3,IsStr;
682
683 //カッコ'('直下の改行
684 while(buffer[0]=='\n'){
685 Text::SlideString(buffer+1,-1);
686 (*pRnum)++;
687 }
688
689 for(i=0,IsStr=0;;i++){
690 if(IsDBCSLeadByte(buffer[i])&&buffer[i+1]){
691 i++;
692 continue;
693 }
694
695 if(buffer[i]=='\"') IsStr^=1;
696
697 if(buffer[i]=='\0') break; //エラー
698 if(buffer[i]=='\n'){
699 i2=0;
700 i3=0;
701 while(buffer[i+i2]=='\n'){
702 i2++;
703 i3++;
704 while(buffer[i+i2]==' '||buffer[i+i2]=='\t') i2++;
705 }
706 while(buffer[i+i2]==' '||buffer[i+i2]=='\t') i2++;
707
708 if(buffer[i+i2]==cEndPare){
709 Text::SlideString(buffer+i+i2,-i2);
710 (*pRnum)+=i3;
711 break;
712 }
713
714 //エラー
715 break;
716 }
717
718 if(buffer[i]=='('&&IsStr==0){
719 i++;
720 i2=KillReturnCode_InParameter(buffer+i,pRnum,'(',')');
721 i+=i2;
722 if(buffer[i]!=')') break;
723 continue;
724 }
725 if(buffer[i]=='['&&IsStr==0){
726 i++;
727 i2=KillReturnCode_InParameter(buffer+i,pRnum,'[',']');
728 i+=i2;
729 if(buffer[i]!=']') break;
730 continue;
731 }
732 if(buffer[i]==cEndPare&&IsStr==0) break;
733
734 if(buffer[i]==','&&buffer[i+1]=='\n'&&IsStr==0){
735 i++;
736 while(buffer[i]=='\n'){
737 Text::SlideString(buffer+i+1,-1);
738 (*pRnum)++;
739 }
740 i--;
741 }
742 }
743 return i;
744}
745void BasicSource::RemoveReturnLineUnderbar(){
746 int i,i2;
747
748 //アンダーバーによる改行
749 for(i=0;;i++){
750 i2=0;
751 while(buffer[i]=='_'&&buffer[i+1]=='\n'){
752 i2++;
753 Text::SlideString(buffer+i+2,-2);
754 while(buffer[i]=='\n'){
755 Text::SlideString(buffer+i+1,-1);
756 i2++;
757 }
758 for(;;i++){
759 if(buffer[i]=='_'&&buffer[i+1]=='\n') break;
760 if(buffer[i]=='\n'||buffer[i]=='\0'){
761 Text::SlideString(buffer+i,i2);
762 memset(buffer+i,'\n',i2);
763 break;
764 }
765 }
766 }
767 if(buffer[i]=='\0') break;
768 }
769
770 //カッコ内パラメータの改行
771 int IsStr,rnum;
772 for(i=0,IsStr=0,rnum=0;;i++){
773 if(IsDBCSLeadByte(buffer[i])&&buffer[i+1]){
774 i++;
775 continue;
776 }
777 if(buffer[i]=='\0') break;
778 if(buffer[i]=='\n'){
779 if(rnum){
780 Text::SlideString(buffer+i+1,rnum);
781 memset(buffer+i+1,'\n',rnum);
782 rnum=0;
783 }
784 }
785 if(buffer[i]=='\"') IsStr^=1;
786 if(buffer[i]=='('&&IsStr==0){
787 i++;
788 i2=KillReturnCode_InParameter(buffer+i,&rnum,'(',')');
789 i+=i2;
790 if(buffer[i]!=')') break;
791 }
792 if(buffer[i]=='['&&IsStr==0){
793 i++;
794 i2=KillReturnCode_InParameter(buffer+i,&rnum,'[',']');
795 i+=i2;
796 if(buffer[i]!=']') break;
797 }
798 }
799
800 length = strlen(buffer);
801}
802
803void BasicSource::Initialize( const std::string &source )
804{
805 Clear();
806 Add( source );
807
808 // 改行コードをCRLFからLFに変換
809 ChangeReturnLineChar();
810
811 // コメントを削除
812 RemoveComments();
813
814 //最終行には文字を含ませないようにする
815 if( strlen(buffer)>0 && buffer[strlen(buffer)-1] != '\n' )
816 {
817 Realloc( length + 1 );
818 strcat( buffer, "\n" );
819 }
820
821 // アンダーバーによる改行を正規表現に戻す
822 RemoveReturnLineUnderbar();
823}
824
825void BasicSource::SetBuffer( const char *buffer ){
826 this->buffer = (char *)calloc( strlen(buffer) + 1, 1 );
827 strcpy( this->buffer, buffer );
828 length = strlen(buffer);
829
830 // ダミー改行をセット
831 Realloc( length + 2 );
832 Text::SlideString( this->buffer, 2 );
833 this->buffer[0] = '\n';
834 this->buffer[1] = '\n';
835}
836
837bool BasicSource::ReadFile( const std::string &filePath, bool isDebug, bool isDll, bool isUnicode, int majorVer, const std::string &mainSourceFilePath, const std::string &includeDirPath )
838{
839 if( !Text::ReadFile( filePath ) ){
840 return false;
841 }
842
843 // 改行コードをCRLFからLFに変換
844 ChangeReturnLineChar();
845
846 // basic.sbpをインクルード
847 //const char *headCode = "#include <basic.sbp>\n";
848 const char *headCode = "";
849 Realloc( length + strlen(headCode) );
850 Text::SlideString( buffer, strlen(headCode) );
851 memcpy( buffer, headCode, strlen(headCode) );
852
853 // #defineと#requireを初期化
854 // TODO: バージョン番号の識別子defineが未完成
855 objDefine.Init( isDebug, isDll, isUnicode, majorVer );
856 requireFiles.clear();
857
858 // コメントを削除
859 RemoveComments();
860
861 // #ifdefディレクティブを処理
862 objDefine.DirectiveIfdef( buffer );
863
864 //最終行には文字を含ませないようにする
865 Realloc( length + 1 );
866 strcat( buffer, "\n" );
867
868 // #include / #require ディレクティブを処理
869 DirectiveIncludeOrRequire( mainSourceFilePath, includeDirPath );
870
871 // アンダーバーによる改行を正規表現に戻す
872 RemoveReturnLineUnderbar();
873
874 // ダミー改行をセット
875 Realloc( length + 2 );
876 Text::SlideString( buffer, 2 );
877 buffer[0] = '\n';
878 buffer[1] = '\n';
879
880 extern char *basbuf;
881 basbuf = GetBuffer();
882
883 return true;
884}
885
886void BasicSource::Addition( const char *buffer ){
887 Realloc( length + strlen(buffer) );
888 strcat( this->buffer, buffer );
889}
890
891bool BasicSource::GetLineInfo( int sourceCodePos, int &line, std::string &filePath ) const
892{
893 int i2,i3,i4,i5;
894
895 const char *buffer = GetBuffer();
896 int i = sourceCodePos;
897
898 if(buffer[i]=='\n') i--;
899 for(i3=0,i2=0;i3<i;i3++){
900 if(buffer[i3]=='\n') i2++;
901 if(buffer[i3]=='\0') return 0;
902 }
903
904 if( includedFilesRelation.GetLineCounts() <= i2 )
905 {
906 //Jenga::Throw( "BasicSource::GetLineInfoメソッドで不正な行の情報を取得しようとした" );
907
908 //ファイル・行番号を特定できなかった場合
909 line = -1;
910 filePath = "";
911 return false;
912 }
913
914 i4=0;
915 while( includedFilesRelation.GetFileNumber( i2 ) != includedFilesRelation.GetFileNumber( i4 ) )
916 {
917 i4++;
918 }
919 for(i3=0,i5=0;i5<i4;i3++){
920 if(buffer[i3]=='\n') i5++;
921 if(buffer[i3]=='\0') return 0;
922 }
923 for(i5=0;i4<i2;i3++){
924 if(buffer[i3]=='\n'){
925 i4++;
926 i5++;
927 if( includedFilesRelation.GetFileNumber( i2 ) < includedFilesRelation.GetFileNumber( i4 ) )
928 {
929 for( ;includedFilesRelation.GetFileNumber( i2 ) != includedFilesRelation.GetFileNumber( i4 ); i3++ ){
930 if(buffer[i3]=='\n') i4++;
931 }
932 }
933 }
934 if(buffer[i3]=='\0') return 0;
935 }
936
937 //行番号をセット
938 line = i5;
939
940 //ファイル名をセット
941 filePath = includedFilesRelation.GetFilePath( i2 );
942
943 return 1;
944}
945
946int SourceCodePosition::GetRelationalObjectModuleIndex() const
947{
948 if( this->IsNothing() )
949 {
950 _ASSERTE( false );
951 throw;
952 }
953
954 return relationalObjectModuleIndex;
955}
956bool SourceCodePosition::IsNothing() const
957{
958 if( this->relationalObjectModuleIndex == -1 && this->pos == -1 )
959 {
960 return true;
961 }
962
963 if( this->relationalObjectModuleIndex == -1 || this->pos == -1 )
964 {
965 _ASSERTE( false );
966 throw;
967 }
968
969 return false;
970}
Note: See TracBrowser for help on using the repository browser.