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

Last change on this file since 343 was 341, checked in by dai_9181, 17 years ago
File size: 9.2 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
305char *calcNames[255] = {
306 "xor",
307};
308void InitCalcNames()
309{
310 if( calcNames[CALC_XOR] )
311 {
312 return;
313 }
314
315 memset( calcNames, 0, 255 * sizeof(char *) );
316 calcNames[CALC_XOR] = "xor";
317 calcNames[CALC_OR] = "or";
318 calcNames[CALC_AND] = "and";
319 calcNames[CALC_NOT] = "Not";
320 calcNames[CALC_PE] = "<=";
321 calcNames[CALC_QE] = ">=";
322 calcNames[CALC_NOTEQUAL] = "<>";
323 calcNames[CALC_EQUAL] = "=(compare)";
324 calcNames[CALC_P] = "<";
325 calcNames[CALC_Q] = ">";
326 calcNames[CALC_SHL] = "<<";
327 calcNames[CALC_SHR] = ">>";
328 calcNames[CALC_ADDITION] = "+";
329 calcNames[CALC_SUBTRACTION] = "-";
330 calcNames[CALC_STRPLUS] = "&";
331 calcNames[CALC_MOD] = "mod";
332 calcNames[CALC_PRODUCT] = "*";
333 calcNames[CALC_QUOTIENT] = "/";
334 calcNames[CALC_INTQUOTIENT] = "\\";
335 calcNames[CALC_AS] = "As";
336 calcNames[CALC_BYVAL] = "ByVal";
337 calcNames[CALC_MINUSMARK] = "-(mark)";
338 calcNames[CALC_POWER] = "^";
339 calcNames[CALC_SUBSITUATION] = "=";
340 calcNames[CALC_ARRAY_GET] = "[]";
341 calcNames[CALC_ARRAY_SET] = "[]=";
342}
343void GetCalcName(int idCalc,char *name){
344 InitCalcNames();
345
346 if( calcNames[idCalc] == NULL )
347 {
348 SmoothieException::Throw();
349 }
350 lstrcpy( name, calcNames[idCalc] );
351}
352BYTE ToCalcId( const char *name )
353{
354 InitCalcNames();
355
356 for( int i=0; i<255; i++ )
357 {
358 if( calcNames[i] )
359 {
360 if( lstrcmp( name, calcNames[i] ) == 0 )
361 {
362 return i;
363 }
364 }
365 }
366 SmoothieException::Throw();
367 return 0;
368}
369
370std::string Operator_NaturalStringToCalcMarkString( const std::string &name )
371{
372 if( name[0] == 1 && name[1] == ESC_OPERATOR )
373 {
374 BYTE calcId = ToCalcId( name.c_str()+2 );
375 char temporary[255];
376 temporary[0] = name[0];
377 temporary[1] = name[1];
378 temporary[2] = calcId;
379 temporary[3] = 0;
380 return temporary;
381 }
382 return name;
383}
384std::string Operator_CalcMarkStringToNaturalString( const std::string &name )
385{
386 if( name[0] == 1 && name[1] == ESC_OPERATOR )
387 {
388 BYTE calcId = name[2];
389 char temporary[255], calcName[255];
390 GetCalcName( calcId, calcName );
391 temporary[0] = name[0];
392 temporary[1] = name[1];
393 lstrcpy( temporary+2, calcName );
394 return temporary;
395 }
396 return name;
397}
398
399bool IsGenericTypeSourcePart( const char *buffer )
400{
401 if( buffer[0] != '<' )
402 {
403 Jenga::Throw( "意図しない文字列に対してIsGenericTypeSourcePart関数が呼ばれた" );
404 return false;
405 }
406
407 int i=1, scopeCount=1;
408 for( ; ; i++ )
409 {
410 if( buffer[i] == '\0' )
411 {
412 break;
413 }
414 if( buffer[i] == '<' )
415 {
416 scopeCount++;
417 }
418 else if( buffer[i] == '>' )
419 {
420 scopeCount--;
421
422 if( scopeCount == 0 )
423 {
424 break;
425 }
426 }
427 }
428
429 if( buffer[i] != '>' )
430 {
431 return false;
432 }
433
434
435 /////////////////////////////////////////////////////////////////
436 // '>'に続く文字を判定する
437 /////////////////////////////////////////////////////////////////
438
439 i++;
440 while( IsBlank( buffer[i] ) )
441 {
442 i++;
443 }
444
445 if( IsVariableChar( buffer[i] ) )
446 {
447 return false;
448 }
449
450 return true;
451}
Note: See TracBrowser for help on using the repository browser.