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

Last change on this file since 333 was 333, checked in by dai_9181, 16 years ago
File size: 8.9 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
241bool SplitMemberName( const char *desc, char *object, char *member, ReferenceKind &refType ){
242 int lastIndex = -1;
243 for( int i=0; desc[i]; i++ ){
244 if( desc[i] == '(' ){
245 i=JumpStringInPare(desc,i+1);
246 continue;
247 }
248 else if( desc[i] == '[' ){
249 i=JumpStringInBracket(desc,i+1);
250 continue;
251 }
252 else if(desc[i]=='.'||(desc[i]==1&&desc[i+1]==ESC_PSMEM)){
253 lastIndex = i;
254 }
255 }
256 if( lastIndex == -1 ){
257 if( object )
258 {
259 object[0] = 0;
260 }
261 lstrcpy( member, desc );
262 return false;
263 }
264
265 if(desc[lastIndex]=='.'){
266 lstrcpy(member,desc+lastIndex+1);
267 refType = RefDot;
268 }
269 else{
270 lstrcpy(member,desc+lastIndex+2);
271 refType = RefPointer;
272 }
273
274 if( object ){
275 lstrcpy( object, desc );
276 object[lastIndex]=0;
277 }
278
279 return true;
280}
281bool SplitMemberName( const char *desc, char *object, char *member ){
282 ReferenceKind dummyRefType;
283 return SplitMemberName( desc, object, member, dummyRefType );
284}
285
286char *calcNames[255] = {
287 "xor",
288};
289void InitCalcNames()
290{
291 if( calcNames[CALC_XOR] )
292 {
293 return;
294 }
295
296 memset( calcNames, 0, 255 * sizeof(char *) );
297 calcNames[CALC_XOR] = "xor";
298 calcNames[CALC_OR] = "or";
299 calcNames[CALC_AND] = "and";
300 calcNames[CALC_NOT] = "Not";
301 calcNames[CALC_PE] = "<=";
302 calcNames[CALC_QE] = ">=";
303 calcNames[CALC_NOTEQUAL] = "<>";
304 calcNames[CALC_EQUAL] = "=(compare)";
305 calcNames[CALC_P] = "<";
306 calcNames[CALC_Q] = ">";
307 calcNames[CALC_SHL] = "<<";
308 calcNames[CALC_SHR] = ">>";
309 calcNames[CALC_ADDITION] = "+";
310 calcNames[CALC_SUBTRACTION] = "-";
311 calcNames[CALC_STRPLUS] = "&";
312 calcNames[CALC_MOD] = "mod";
313 calcNames[CALC_PRODUCT] = "*";
314 calcNames[CALC_QUOTIENT] = "/";
315 calcNames[CALC_INTQUOTIENT] = "\\";
316 calcNames[CALC_AS] = "As";
317 calcNames[CALC_BYVAL] = "ByVal";
318 calcNames[CALC_MINUSMARK] = "-(mark)";
319 calcNames[CALC_POWER] = "^";
320 calcNames[CALC_SUBSITUATION] = "=";
321 calcNames[CALC_ARRAY_GET] = "[]";
322 calcNames[CALC_ARRAY_SET] = "[]=";
323}
324void GetCalcName(int idCalc,char *name){
325 InitCalcNames();
326
327 if( calcNames[idCalc] == NULL )
328 {
329 SmoothieException::Throw();
330 }
331 lstrcpy( name, calcNames[idCalc] );
332}
333BYTE ToCalcId( const char *name )
334{
335 InitCalcNames();
336
337 for( int i=0; i<255; i++ )
338 {
339 if( calcNames[i] )
340 {
341 if( lstrcmp( name, calcNames[i] ) == 0 )
342 {
343 return i;
344 }
345 }
346 }
347 SmoothieException::Throw();
348 return 0;
349}
350
351std::string Operator_NaturalStringToCalcMarkString( const std::string &name )
352{
353 if( name[0] == 1 && name[1] == ESC_OPERATOR )
354 {
355 BYTE calcId = ToCalcId( name.c_str()+2 );
356 char temporary[255];
357 temporary[0] = name[0];
358 temporary[1] = name[1];
359 temporary[2] = calcId;
360 temporary[3] = 0;
361 return temporary;
362 }
363 return name;
364}
365std::string Operator_CalcMarkStringToNaturalString( const std::string &name )
366{
367 if( name[0] == 1 && name[1] == ESC_OPERATOR )
368 {
369 BYTE calcId = name[2];
370 char temporary[255], calcName[255];
371 GetCalcName( calcId, calcName );
372 temporary[0] = name[0];
373 temporary[1] = name[1];
374 lstrcpy( temporary+2, calcName );
375 return temporary;
376 }
377 return name;
378}
379
380bool IsGenericTypeSourcePart( const char *buffer )
381{
382 if( buffer[0] != '<' )
383 {
384 Jenga::Throw( "意図しない文字列に対してIsGenericTypeSourcePart関数が呼ばれた" );
385 return false;
386 }
387
388 int i=1, scopeCount=1;
389 for( ; ; i++ )
390 {
391 if( buffer[i] == '\0' )
392 {
393 break;
394 }
395 if( buffer[i] == '<' )
396 {
397 scopeCount++;
398 }
399 else if( buffer[i] == '>' )
400 {
401 scopeCount--;
402
403 if( scopeCount == 0 )
404 {
405 break;
406 }
407 }
408 }
409
410 if( buffer[i] != '>' )
411 {
412 return false;
413 }
414
415
416 /////////////////////////////////////////////////////////////////
417 // '>'に続く文字を判定する
418 /////////////////////////////////////////////////////////////////
419
420 i++;
421 while( IsBlank( buffer[i] ) )
422 {
423 i++;
424 }
425
426 if( IsVariableChar( buffer[i] ) )
427 {
428 return false;
429 }
430
431 return true;
432}
Note: See TracBrowser for help on using the repository browser.