source: dev/trunk/abdev/BasicCompiler_Common/include/Source.h@ 406

Last change on this file since 406 was 322, checked in by dai_9181, 17 years ago

コンパイラ組み込みテンプレートエンジンを実装。
静的リンクライブラリ、デバッグ情報の内部形式をテキストからバイナリに変更した。

File size: 5.9 KB
Line 
1#pragma once
2
3#include <map>
4#include <string>
5#include <vector>
6
7#include <windows.h>
8#include <stdlib.h>
9
10#include <jenga/include/common/Exception.h>
11#include <jenga/include/smoothie/BasicFixed.h>
12
13#include <BoostSerializationSupport.h>
14
15class IncludedFilesRelation
16{
17 std::vector<std::string> filePaths;
18 std::vector<int> lineFileNumbers;
19
20 // XMLシリアライズ用
21private:
22 friend class boost::serialization::access;
23 template<class Archive> void serialize(Archive& ar, const unsigned int version)
24 {
25 trace_for_serialize( "serializing - IncludedFilesRelation" );
26
27 ar & BOOST_SERIALIZATION_NVP( filePaths );
28 ar & BOOST_SERIALIZATION_NVP( lineFileNumbers );
29 }
30
31public:
32 IncludedFilesRelation()
33 {
34 }
35 ~IncludedFilesRelation()
36 {
37 }
38
39 const int GetFileNumber( int lineNumber ) const
40 {
41 return lineFileNumbers[lineNumber];
42 }
43 const std::string &GetFilePath( int lineNumber ) const
44 {
45 return filePaths[GetFileNumber( lineNumber )];
46 }
47 const std::string &GetFilePathFromFileNumber( int fileNumber ) const
48 {
49 return filePaths[fileNumber];
50 }
51 int GetFileCounts() const
52 {
53 return (int)filePaths.size();
54 }
55
56 int AddFile( const std::string &filePath )
57 {
58 filePaths.push_back( filePath );
59 return (int)filePaths.size()-1;
60 }
61 void AddLine( int fileNumber )
62 {
63 lineFileNumbers.push_back( fileNumber );
64 }
65
66 int GetLineCounts() const
67 {
68 return (int)lineFileNumbers.size();
69 }
70};
71
72class Text{
73protected:
74 char *buffer;
75 int length;
76
77public:
78
79 Text(){
80 buffer = (char *)calloc( 1, 1 );
81 length = 0;
82 }
83 Text( const Text &text )
84 : length( text.length )
85 {
86 buffer = (char *)malloc( length + 1 );
87 memcpy( buffer, text.buffer, length );
88 buffer[length] = 0;
89 }
90 ~Text(){
91 free( buffer );
92 }
93 void Clear()
94 {
95 length = 0;
96 }
97 void Add( const std::string &str )
98 {
99 buffer = (char *)realloc( buffer, length + str.size() + 1 );
100 lstrcpy( buffer + length, str.c_str() );
101 length += (int)str.size();
102 }
103 void Add( const std::vector<char> &str )
104 {
105 buffer = (char *)realloc( buffer, length + str.size() + 1 );
106 lstrcpy( buffer + length, &str[0] );
107 length += (int)str.size();
108 }
109
110 bool ReadFile( const string &filePath );
111
112 static void Text::SlideString(char *buffer, int slide){
113 char *temp;
114 temp=(char *)malloc(lstrlen(buffer)+1);
115 lstrcpy(temp,buffer);
116 lstrcpy(buffer+slide,temp);
117 free(temp);
118 }
119};
120
121class BasicSource : public Text
122{
123 static const string generateDirectiveName;
124
125 IncludedFilesRelation includedFilesRelation;
126
127 // XMLシリアライズ用
128private:
129 friend class boost::serialization::access;
130 BOOST_SERIALIZATION_SPLIT_MEMBER();
131 template<class Archive> void load(Archive& ar, const unsigned int version)
132 {
133 trace_for_serialize( "serializing(load) - BasicSource" );
134
135 std::string _buffer;
136 ar & BOOST_SERIALIZATION_NVP( _buffer );
137 ar & BOOST_SERIALIZATION_NVP( length );
138 ar & BOOST_SERIALIZATION_NVP( includedFilesRelation );
139
140 // 読み込み後の処理
141 Realloc( length );
142 for( int i=0; i<length; i++ )
143 {
144 ULONG_PTR l1 = ( ( _buffer[i*3] >= 'a' ) ? ( _buffer[i*3] - 'a' + 0x0a ) : ( _buffer[i*3] - '0' ) ) * 0x10;
145 ULONG_PTR l2 = ( _buffer[i*3+1] >= 'a' ) ? ( _buffer[i*3+1] - 'a' + 0x0a ) : ( _buffer[i*3+1] - '0' );
146 ULONG_PTR l = l1 + l2;
147 buffer[i] = static_cast<char>(l);
148 }
149 buffer[length] = 0;
150 }
151 template<class Archive> void save(Archive& ar, const unsigned int version) const
152 {
153 trace_for_serialize( "serializing(save) - BasicSource" );
154
155 // 保存準備
156 char *tempCode = (char *)calloc( (length+1) * 3, 1 );
157 for( int i=0; i<length; i++ )
158 {
159 char temp[32];
160 sprintf( temp, "%02x,", (unsigned char)buffer[i] );
161 tempCode[i*3] = temp[0];
162 tempCode[i*3+1] = temp[1];
163 tempCode[i*3+2] = temp[2];
164 }
165
166 std::string _buffer = tempCode;
167 free( tempCode );
168
169 ar & BOOST_SERIALIZATION_NVP( _buffer );
170 ar & BOOST_SERIALIZATION_NVP( length );
171 ar & BOOST_SERIALIZATION_NVP( includedFilesRelation );
172 }
173
174private:
175 void Realloc( int newLength )
176 {
177 bool isEqualBasbuf = false;
178 extern char *basbuf;
179 if( basbuf == buffer + 2 )
180 {
181 isEqualBasbuf = true;
182 }
183
184 buffer = (char *)realloc( buffer, newLength + 255 );
185
186 length = newLength;
187
188 if( isEqualBasbuf )
189 {
190 basbuf = buffer + 2;
191 }
192 }
193
194 void IncludeFiles();
195
196 void ChangeReturnLineChar();
197
198 void RemoveComments();
199
200 bool ReadFile_InIncludeDirective( const string &filePath );
201 void DirectiveIncludeOrRequire();
202
203 void RemoveReturnLineUnderbar();
204
205 void Initialize( const std::string &source );
206
207public:
208 BasicSource(){}
209 BasicSource( const BasicSource &basicSource )
210 : Text( basicSource )
211 , includedFilesRelation( basicSource.includedFilesRelation )
212 {
213 }
214 BasicSource( const std::string &source )
215 {
216 Initialize( source );
217 }
218 ~BasicSource(){}
219
220 char *GetBuffer(){
221 return buffer+2;
222 }
223 const char *GetBuffer() const
224 {
225 return buffer+2;
226 }
227 int GetLength() const
228 {
229 return length-2;
230 }
231 void _ResetLength()
232 {
233 length = lstrlen( buffer );
234 }
235
236 const IncludedFilesRelation &GetIncludedFilesRelation() const
237 {
238 return includedFilesRelation;
239 }
240
241 void SetBuffer( const char *buffer );
242
243 bool ReadFile( const string &filePath );
244
245 bool Generate( const string &genName, const char *buffer );
246
247 void Addition( const char *buffer );
248
249 bool GetLineInfo( int sourceCodePos, int &line, std::string &fileName ) const;
250
251 void operator = ( const BasicSource &source ){
252 Realloc( source.length );
253 lstrcpy( buffer, source.buffer );
254 }
255
256 char operator[]( int index ) const
257 {
258 if( index>GetLength() )
259 {
260 Jenga::Throw( "BasicSource bad access" );
261 }
262 return buffer[2+index];
263 }
264};
265typedef std::vector<BasicSource> BasicSources;
266
267class SourceTemplate
268{
269 std::string source;
270public:
271 SourceTemplate( const std::string &filePath );
272 ~SourceTemplate()
273 {
274 }
275
276 std::string GetResult( const std::map<std::string,std::string> &values );
277};
Note: See TracBrowser for help on using the repository browser.