source: dev/trunk/jenga/src/smoothie/LexicalAnalysis.cpp@ 362

Last change on this file since 362 was 362, checked in by dai_9181, 17 years ago
File size: 9.5 KB
Line 
1#include <jenga/include/common/Exception.h>
2#include <jenga/include/smoothie/SmoothieException.h>
3#include <jenga/include/smoothie/LexicalAnalysis.h>
4#include <jenga/include/smoothie/BasicFixed.h>
5
6#include <windows.h>
7
8bool IsVariableTopChar(char c){
9 if((c>='A'&&c<='Z')||(c>='a'&&c<='z')||c=='_')
10 {
11 return true;
12 }
13 return false;
14}
15bool IsVariableChar(char c){
16 if((c>='A'&&c<='Z')||(c>='a'&&c<='z')||(c>='0'&&c<='9')||
17 c=='%'||c=='!'||c=='#'||c=='$'||
18 c=='_'||c=='.')
19 {
20 return true;
21 }
22 return false;
23}
24bool IsBlank(char c){
25 if(c==' '||c=='\t') return 1;
26 return 0;
27}
28int GetOneParameter(const char *Parameter,int pos,char *retAns){
29 int i,i2,i3,IsStr;
30 for(i=pos,i2=0,IsStr=0;;i++,i2++){
31 if(Parameter[i]=='\"') IsStr^=1;
32 else if(Parameter[i]=='('&&IsStr==0){
33 i3=GetStringInPare(retAns+i2,Parameter+i);
34 i+=i3-1;
35 i2+=i3-1;
36 continue;
37 }
38 else if(Parameter[i]=='['&&IsStr==0){
39 i3=GetStringInBracket(retAns+i2,Parameter+i);
40 i+=i3-1;
41 i2+=i3-1;
42 continue;
43 }
44 else if(Parameter[i]==','&&IsStr==0){
45 retAns[i2]=0;
46 i++;
47 break;
48 }
49
50 if(IsCommandDelimitation(Parameter[i])&&IsStr==0
51 || Parameter[i] == ')' && IsStr == 0 ){
52 retAns[i2]=0;
53 break;
54 }
55
56 retAns[i2]=Parameter[i];
57 }
58 return i;
59}
60int JumpOneParameter(char *Parameter,int i){
61 int i2,IsStr;
62 for(i2=0,IsStr=0;;i++,i2++){
63 if(Parameter[i]=='\"') IsStr^=1;
64 else if(Parameter[i]=='('&&IsStr==0){
65 i=JumpStringInPare(Parameter,i+1);
66 continue;
67 }
68 else if(Parameter[i]=='['&&IsStr==0){
69 i=JumpStringInBracket(Parameter,i+1);
70 continue;
71 }
72 else if(Parameter[i]==','&&IsStr==0){
73 i++;
74 break;
75 }
76 if(IsCommandDelimitation(Parameter[i])) break;
77 }
78 return i;
79}
80int GetStringInQuotation(char *buffer,char *ReadBuffer){
81 int i;
82
83 if(ReadBuffer[0]=='\"'){
84 buffer[0]=ReadBuffer[0];
85 i=1;
86 }
87 else i=0;
88
89 for(;;i++){
90 buffer[i]=ReadBuffer[i];
91 if(IsDBCSLeadByte(ReadBuffer[i])){
92 i++;
93 buffer[i]=ReadBuffer[i];
94 continue;
95 }
96 if(ReadBuffer[i]=='\"'){
97 i++;
98 buffer[i]=0;
99 break;
100 }
101 if(ReadBuffer[i]=='\0') return 0;
102 }
103 return i;
104}
105int GetStringInPare(char *buffer,const char *ReadBuffer){
106 int i,IsStr,PareNum;
107 for(i=0,IsStr=0,PareNum=0;;i++){
108 buffer[i]=ReadBuffer[i];
109 if(IsDBCSLeadByte(ReadBuffer[i])){
110 i++;
111 buffer[i]=ReadBuffer[i];
112 continue;
113 }
114 if(ReadBuffer[i]=='\"') IsStr^=1;
115 else if(ReadBuffer[i]=='('&&IsStr==0) PareNum++;
116 else if(ReadBuffer[i]==')'&&IsStr==0){
117 PareNum--;
118 if(PareNum==0){
119 i++;
120 buffer[i]=0;
121 break;
122 }
123 }
124 else if(ReadBuffer[i]=='\0') return 0;
125 }
126 return i;
127}
128int GetStringInBracket(char *buffer,const char *ReadBuffer){
129 int i,IsStr,PareNum;
130 for(i=0,IsStr=0,PareNum=0;;i++){
131 buffer[i]=ReadBuffer[i];
132 if(IsDBCSLeadByte(ReadBuffer[i])){
133 i++;
134 buffer[i]=ReadBuffer[i];
135 continue;
136 }
137 if(ReadBuffer[i]=='\"') IsStr^=1;
138 else if(ReadBuffer[i]=='['&&IsStr==0) PareNum++;
139 else if(ReadBuffer[i]==']'&&IsStr==0){
140 PareNum--;
141 if(PareNum==0){
142 i++;
143 buffer[i]=0;
144 break;
145 }
146 }
147 else if(ReadBuffer[i]=='\0') return 0;
148 }
149 return i;
150}
151int GetStringInGenericBracket(char *buffer,const char *ReadBuffer){
152 int i,IsStr,PareNum;
153 for(i=0,IsStr=0,PareNum=0;;i++){
154 buffer[i]=ReadBuffer[i];
155 if(IsDBCSLeadByte(ReadBuffer[i])){
156 i++;
157 buffer[i]=ReadBuffer[i];
158 continue;
159 }
160 if(ReadBuffer[i]=='\"') IsStr^=1;
161 else if(ReadBuffer[i]=='<'&&IsStr==0) PareNum++;
162 else if(ReadBuffer[i]=='>'&&IsStr==0){
163 PareNum--;
164 if(PareNum==0){
165 i++;
166 buffer[i]=0;
167 break;
168 }
169 }
170 else if(ReadBuffer[i]=='\0') return 0;
171 }
172 return i;
173}
174int JumpStringInPare(const char *buffer,int pos){
175 int PareNum;
176 for(PareNum=1;;pos++){
177 if(buffer[pos]=='\"'){
178 for(pos++;;pos++){
179 if(buffer[pos]=='\"') break;
180 }
181 continue;
182 }
183 else if(buffer[pos]=='(') PareNum++;
184 else if(buffer[pos]==')'){
185 PareNum--;
186 if(PareNum==0) return pos;
187 }
188 else if(buffer[pos]=='\0') break;
189 }
190 return 0;
191}
192int JumpStringInBracket(const char *buffer,int pos){
193 int PareNum;
194 for(PareNum=1;;pos++){
195 if(buffer[pos]=='\"'){
196 for(pos++;;pos++){
197 if(buffer[pos]=='\"') break;
198 }
199 continue;
200 }
201 else if(buffer[pos]=='[') PareNum++;
202 else if(buffer[pos]==']'){
203 PareNum--;
204 if(PareNum==0) return pos;
205 }
206 else if(buffer[pos]=='\0') break;
207 }
208 return 0;
209}
210bool IsCommandDelimitation( char c ){
211 if( c == '\n' || c == ':' || c == '\0' ){
212 return true;
213 }
214
215 return false;
216}
217
218int GetStringInPare_RemovePare(char *buffer,const char *ReadBuffer){
219 int i,IsStr,PareNum;
220 for(i=0,IsStr=0,PareNum=1;;i++){
221 buffer[i]=ReadBuffer[i];
222 if(IsDBCSLeadByte(ReadBuffer[i])){
223 i++;
224 buffer[i]=ReadBuffer[i];
225 continue;
226 }
227 if(ReadBuffer[i]=='\"') IsStr^=1;
228 else if(ReadBuffer[i]=='('&&IsStr==0) PareNum++;
229 else if(ReadBuffer[i]==')'&&IsStr==0){
230 PareNum--;
231 if(PareNum==0){
232 buffer[i]=0;
233 break;
234 }
235 }
236 else if(ReadBuffer[i]=='\0') return 0;
237 }
238 return i;
239}
240
241void SplitParameter( const std::string &src, Jenga::Common::Strings &dests, bool isClear )
242{
243 const char *buffer = src.c_str();
244 int i = 0;
245
246 if( isClear )
247 {
248 dests.clear();
249 }
250
251 while( buffer[i] )
252 {
253 char result[8192];
254 i = GetOneParameter( buffer, i, result );
255 dests.push_back( result );
256 }
257}
258
259
260bool SplitMemberName( const char *desc, char *object, char *member, ReferenceKind &refType ){
261 int lastIndex = -1;
262 for( int i=0; desc[i]; i++ ){
263 if( desc[i] == '(' ){
264 i=JumpStringInPare(desc,i+1);
265 continue;
266 }
267 else if( desc[i] == '[' ){
268 i=JumpStringInBracket(desc,i+1);
269 continue;
270 }
271 else if(desc[i]=='.'||(desc[i]==1&&desc[i+1]==ESC_PSMEM)){
272 lastIndex = i;
273 }
274 }
275 if( lastIndex == -1 ){
276 if( object )
277 {
278 object[0] = 0;
279 }
280 lstrcpy( member, desc );
281 return false;
282 }
283
284 if(desc[lastIndex]=='.'){
285 lstrcpy(member,desc+lastIndex+1);
286 refType = RefDot;
287 }
288 else{
289 lstrcpy(member,desc+lastIndex+2);
290 refType = RefPointer;
291 }
292
293 if( object ){
294 lstrcpy( object, desc );
295 object[lastIndex]=0;
296 }
297
298 return true;
299}
300bool SplitMemberName( const char *desc, char *object, char *member ){
301 ReferenceKind dummyRefType;
302 return SplitMemberName( desc, object, member, dummyRefType );
303}
304
305void SplitSyntacticForAs( const char *src, char *varName, char *typeName)
306{
307 int i = 0;
308 for( ; ; i++ )
309 {
310 if( src[i] == '\0' || src[i] == 1 && src[i+1] == ESC_AS )
311 {
312 varName[i] = 0;
313 break;
314 }
315 varName[i] = src[i];
316 }
317
318 if( src[i] )
319 {
320 lstrcpy( typeName, src + i + 2 );
321 }
322 else
323 {
324 typeName[i] = 0;
325 }
326}
327
328char *calcNames[255] = {
329 "xor",
330};
331void InitCalcNames()
332{
333 if( calcNames[CALC_XOR] )
334 {
335 return;
336 }
337
338 memset( calcNames, 0, 255 * sizeof(char *) );
339 calcNames[CALC_XOR] = "xor";
340 calcNames[CALC_OR] = "or";
341 calcNames[CALC_AND] = "and";
342 calcNames[CALC_NOT] = "Not";
343 calcNames[CALC_PE] = "<=";
344 calcNames[CALC_QE] = ">=";
345 calcNames[CALC_NOTEQUAL] = "<>";
346 calcNames[CALC_EQUAL] = "=(compare)";
347 calcNames[CALC_P] = "<";
348 calcNames[CALC_Q] = ">";
349 calcNames[CALC_SHL] = "<<";
350 calcNames[CALC_SHR] = ">>";
351 calcNames[CALC_ADDITION] = "+";
352 calcNames[CALC_SUBTRACTION] = "-";
353 calcNames[CALC_STRPLUS] = "&";
354 calcNames[CALC_MOD] = "mod";
355 calcNames[CALC_PRODUCT] = "*";
356 calcNames[CALC_QUOTIENT] = "/";
357 calcNames[CALC_INTQUOTIENT] = "\\";
358 calcNames[CALC_AS] = "As";
359 calcNames[CALC_BYVAL] = "ByVal";
360 calcNames[CALC_MINUSMARK] = "-(mark)";
361 calcNames[CALC_POWER] = "^";
362 calcNames[CALC_SUBSITUATION] = "=";
363 calcNames[CALC_ARRAY_GET] = "[]";
364 calcNames[CALC_ARRAY_SET] = "[]=";
365}
366void GetCalcName(int idCalc,char *name){
367 InitCalcNames();
368
369 if( calcNames[idCalc] == NULL )
370 {
371 SmoothieException::Throw();
372 }
373 lstrcpy( name, calcNames[idCalc] );
374}
375BYTE ToCalcId( const char *name )
376{
377 InitCalcNames();
378
379 for( int i=0; i<255; i++ )
380 {
381 if( calcNames[i] )
382 {
383 if( lstrcmp( name, calcNames[i] ) == 0 )
384 {
385 return i;
386 }
387 }
388 }
389 SmoothieException::Throw();
390 return 0;
391}
392
393std::string Operator_NaturalStringToCalcMarkString( const std::string &name )
394{
395 if( name[0] == 1 && name[1] == ESC_OPERATOR )
396 {
397 BYTE calcId = ToCalcId( name.c_str()+2 );
398 char temporary[255];
399 temporary[0] = name[0];
400 temporary[1] = name[1];
401 temporary[2] = calcId;
402 temporary[3] = 0;
403 return temporary;
404 }
405 return name;
406}
407std::string Operator_CalcMarkStringToNaturalString( const std::string &name )
408{
409 if( name[0] == 1 && name[1] == ESC_OPERATOR )
410 {
411 BYTE calcId = name[2];
412 char temporary[255], calcName[255];
413 GetCalcName( calcId, calcName );
414 temporary[0] = name[0];
415 temporary[1] = name[1];
416 lstrcpy( temporary+2, calcName );
417 return temporary;
418 }
419 return name;
420}
421
422bool IsGenericTypeSourcePart( const char *buffer )
423{
424 if( buffer[0] != '<' )
425 {
426 Jenga::Throw( "意図しない文字列に対してIsGenericTypeSourcePart関数が呼ばれた" );
427 return false;
428 }
429
430 int i=1, scopeCount=1;
431 for( ; ; i++ )
432 {
433 if( buffer[i] == '\0' )
434 {
435 break;
436 }
437 if( buffer[i] == '<' )
438 {
439 scopeCount++;
440 }
441 else if( buffer[i] == '>' )
442 {
443 scopeCount--;
444
445 if( scopeCount == 0 )
446 {
447 break;
448 }
449 }
450 }
451
452 if( buffer[i] != '>' )
453 {
454 return false;
455 }
456
457
458 /////////////////////////////////////////////////////////////////
459 // '>'に続く文字を判定する
460 /////////////////////////////////////////////////////////////////
461
462 i++;
463 while( IsBlank( buffer[i] ) )
464 {
465 i++;
466 }
467
468 if( IsVariableChar( buffer[i] ) )
469 {
470 return false;
471 }
472
473 return true;
474}
Note: See TracBrowser for help on using the repository browser.