source: dev/trunk/abdev/BasicCompiler_Common/src/Source.cpp@ 331

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