소스 검색

cmLex.h/c : Added cmLexTokenIsUnsigned() and cmLexTokenIsSignlePrecision().

master
kpl 10 년 전
부모
커밋
b4d99528f9
2개의 변경된 파일96개의 추가작업 그리고 92개의 파일을 삭제
  1. 84
    89
      cmLex.c
  2. 12
    3
      cmLex.h

+ 84
- 89
cmLex.c 파일 보기

@@ -7,6 +7,12 @@
7 7
 #include "cmMallocDebug.h"
8 8
 #include "cmFile.h"
9 9
 
10
+enum
11
+{
12
+  kRealFloatLexFl   = 0x01,
13
+  kIntUnsignedLexFl = 0x02
14
+};
15
+
10 16
 typedef struct
11 17
 {
12 18
   unsigned        code;
@@ -29,6 +35,7 @@ cmLexErrorRecd cmLexErrorArray[] =
29 35
   { kMemAllocErrLexRC,     "An attempted memory allocation failed"},
30 36
   { kEofRC,                "The end of the input text was encountered (this is a normal condition not an error)"},
31 37
   { kInvalidLexTIdLexRC,   "An invalid token id was encountered."},
38
+  { kSignErrorLexRC,       "A signed integer has a 'u' or 'U' suffix."},
32 39
   { kInvalidLexRC,         "Unknown lexer error code." }
33 40
 };
34 41
 
@@ -75,6 +82,7 @@ typedef struct cmLex_str
75 82
 
76 83
   cmChar_t*         textBuf;         // text buf used by cmLexSetFile()
77 84
 
85
+  unsigned          attrFlags;       // used to store the int and real suffix type flags
78 86
 } cmLex;
79 87
 
80 88
 
@@ -148,7 +156,7 @@ unsigned _cmLexRealMatcher(  cmLex* p, const cmChar_t* cp, unsigned cn, const cm
148 156
   unsigned i  = 0;
149 157
   unsigned n  = 0;     // decimal point counter
150 158
   unsigned d  = 0;     // digit counter
151
-  bool     fl = false; // true if this real includes an exponent
159
+  bool     fl = false; // expo flag  
152 160
 
153 161
   for(; i<cn && n<=1; ++i)
154 162
   {
@@ -170,10 +178,12 @@ unsigned _cmLexRealMatcher(  cmLex* p, const cmChar_t* cp, unsigned cn, const cm
170 178
   // if there was at least one digit and the next char is an 'e'
171 179
   if( d>0 && i<cn && (cp[i] == 'e' || cp[i] == 'E') )
172 180
   {
173
-    d=0;
181
+    unsigned e=0;
174 182
     ++i;
175 183
     unsigned j = i;
176 184
 
185
+    fl = false;
186
+
177 187
     for(; i<cn; ++i)
178 188
     {
179 189
       if( i==j && cp[i]=='-' ) // allow the char following the e to be '-'
@@ -181,6 +191,7 @@ unsigned _cmLexRealMatcher(  cmLex* p, const cmChar_t* cp, unsigned cn, const cm
181 191
 
182 192
       if( isdigit(cp[i]) )
183 193
       {
194
+        ++e;
184 195
         ++d;
185 196
         continue;
186 197
       }
@@ -190,17 +201,42 @@ unsigned _cmLexRealMatcher(  cmLex* p, const cmChar_t* cp, unsigned cn, const cm
190 201
     }
191 202
 
192 203
     // an exp exists if digits follwed the 'e'
193
-    fl = d > 0;
204
+    fl = e > 0;
194 205
      
195 206
   }
196 207
 
197
-  return i>1 && (n==1 || fl) ? i : 0;
208
+  // if at least one digit was found 
209
+  if( d>0 )
210
+  {
211
+    // Note that this path allows a string w/o a decimal pt to trigger a match.
212
+
213
+    if(i<cn)
214
+    {
215
+      // if the real has a suffix
216
+      switch(cp[i])
217
+      {
218
+        case 'F':
219
+        case 'f':
220
+          p->attrFlags = cmSetFlag(p->attrFlags,kRealFloatLexFl);
221
+          ++i;
222
+          break;
223
+      }
224
+
225
+    }
226
+    
227
+    // match w/o suffix return
228
+    if( d>0 && (fl || n==1 || cmIsFlag(p->attrFlags,kRealFloatLexFl)) )
229
+      return i;
230
+  }
231
+
232
+  return 0; // no-match return
198 233
 }
199 234
 
200 235
 unsigned _cmLexIntMatcher(   cmLex* p, const cmChar_t* cp, unsigned cn, const cmChar_t* keyStr )
201 236
 {
202 237
   unsigned i = 0;
203 238
   bool signFl = false;
239
+
204 240
   for(; i<cn; ++i)
205 241
   {
206 242
     if( i==0 && cp[i]=='-' )
@@ -222,9 +258,35 @@ unsigned _cmLexIntMatcher(   cmLex* p, const cmChar_t* cp, unsigned cn, const cm
222 258
   //
223 259
   // The current implementation recognizes all numeric strings 
224 260
   // containing a decimal point as reals. 
225
- 
226 261
 
227
-  return signFl && i==1 ? 0 : i;
262
+  // if no integer was found
263
+  if( (signFl && i==0) || i==0 )
264
+    return 0;
265
+
266
+
267
+  // check for suffix
268
+  if(i<cn )
269
+  {
270
+    
271
+    switch(cp[i])
272
+    {
273
+      case 'u':
274
+      case 'U':
275
+        if( signFl )
276
+          _cmLexError(p,kSignErrorLexRC,"A signed integer has a 'u' or 'U' suffix.");
277
+        else
278
+        {
279
+          p->attrFlags = cmSetFlag(p->attrFlags,kIntUnsignedLexFl);          
280
+          ++i;
281
+        }
282
+        break;
283
+
284
+      default:
285
+        break;
286
+    }
287
+  }
288
+
289
+  return  i;
228 290
 }
229 291
 
230 292
 unsigned _cmLexHexMatcher(   cmLex* p, const cmChar_t* cp, unsigned cn, const cmChar_t* keyStr )
@@ -387,23 +449,6 @@ cmLexH cmLexInit( const cmChar_t* cp, unsigned cn, unsigned flags, cmRpt_t* rpt
387 449
   
388 450
   _cmLexSetTextBuffer( p, cp, cn );
389 451
 
390
-  /*
391
-  p->cp              = (cn==0)    ? NULL : cp;
392
-  p->cn              = (cp==NULL) ? 0    : cn;
393
-
394
-  p->ci              = 0;
395
-
396
-
397
-  p->curTokenId      = kErrorLexTId;
398
-  p->curTokenCharIdx = cmInvalidIdx;
399
-  p->curTokenCharCnt = 0;
400
-
401
-  p->curLine         = 0;
402
-  p->curCol          = 0;
403
-  p->nextLine        = 0;
404
-  p->nextCol         = 0;
405
-  */
406
-
407 452
   int init_mfn       = 10;
408 453
   p->mfp             = cmMemAllocZ( cmLexMatcher, init_mfn );
409 454
   p->mfn             = init_mfn;
@@ -537,70 +582,6 @@ cmRC_t cmLexSetFile( cmLexH h, const cmChar_t* fn )
537 582
   return rc;
538 583
 }
539 584
 
540
-/*
541
-cmRC_t cmLexSetFile( cmLexH h, const cmChar_t* fn )
542
-{
543
-  cmRC_t   rc      = kOkLexRC;
544
-  FILE*    fp      = NULL;
545
-  cmLex*   p       = _cmLexHandleToPtr(h);
546
-  unsigned n       = 0;
547
-
548
-  assert( fn != NULL && p != NULL );
549
-  
550
-  // open the file
551
-  if((fp = fopen(fn,"rb")) == NULL )
552
-    return _cmLexError(p,kFileOpenErrLexRC,"Unable to open the file:'%s'.",fn);
553
-
554
-  // seek to the end
555
-  if( fseek(fp,0,SEEK_END) != 0 )
556
-  {
557
-    rc= _cmLexError(p,kFileSeekErrLexRC,"Unable to seek to the end of '%s'.",fn);
558
-    goto errLabel;
559
-  }
560
-
561
-  // get the length of the file
562
-  if( (n=ftell(fp)) == 0 )
563
-  {
564
-    rc = _cmLexError(p,kFileOpenErrLexRC,"The file '%s' appears to be empty.",fn);
565
-    goto errLabel;
566
-  }
567
-
568
-  // rewind the file
569
-  if( fseek(fp,0,SEEK_SET) != 0 )
570
-  {
571
-    rc = _cmLexError(p,kFileSeekErrLexRC,"Unable to seek to the beginning of '%s'.",fn);
572
-    goto errLabel;
573
-  }
574
-
575
-  // allocate the text buffer
576
-  if((p->textBuf = cmMemResizeZ( char, p->textBuf, n+1)) == NULL )
577
-  {
578
-    rc = _cmLexError(p,kMemAllocErrLexRC,"Unable to allocate the text file buffer for:'%s'.",fn);
579
-    goto errLabel;
580
-  }
581
-
582
-  // read the file into the text buffer
583
-  if( fread(p->textBuf,n,1,fp) != 1 )
584
-  {
585
-    rc = _cmLexError(p,kFileReadErrLexRC,"File read failed on:'%s'.",fn);
586
-    goto errLabel;
587
-  }  
588
-
589
-  if((rc = _cmLexSetTextBuffer( p, p->textBuf, n )) != kOkLexRC )
590
-    goto errLabel;
591
-
592
- errLabel:
593
-
594
-  // close the file
595
-  if( fclose(fp) != 0 )
596
-  {
597
-    rc =  _cmLexError(p,kFileCloseErrLexRC,"File close failed on:'%s'.",fn);
598
-    goto errLabel;
599
-  }
600
-
601
-  return rc;
602
-}
603
-*/
604 585
 
605 586
 cmLexMatcher* _cmLexFindUserToken( cmLex* p, unsigned id, const cmChar_t* tokenStr )
606 587
 {
@@ -689,9 +670,9 @@ unsigned           cmLexGetNextToken( cmLexH h )
689 670
     p->curTokenId      = kErrorLexTId;
690 671
     p->curTokenCharIdx = cmInvalidIdx;
691 672
     p->curTokenCharCnt = 0;
673
+    p->attrFlags       = 0;
692 674
 
693
-
694
-    // try each mater
675
+    // try each matcher
695 676
     for(; mi<p->mfi; ++mi)
696 677
       if( p->mfp[mi].enableFl )
697 678
       {
@@ -701,6 +682,7 @@ unsigned           cmLexGetNextToken( cmLexH h )
701 682
         else
702 683
           charCnt = p->mfp[mi].userPtr( p->cp + p->ci, p->cn - p->ci);
703 684
 
685
+        // notice if the matcher set the error code
704 686
         if( cmErrLastRC(&p->err) != kOkLexRC )
705 687
           return kErrorLexTId;
706 688
 
@@ -823,6 +805,19 @@ float              cmLexTokenFloat(        cmLexH h )
823 805
 double             cmLexTokenDouble(        cmLexH h )
824 806
 {  return strtod( cmLexTokenText(h),NULL ); }
825 807
 
808
+
809
+bool               cmLexTokenIsUnsigned( cmLexH h )
810
+{
811
+  cmLex* p = _cmLexHandleToPtr(h);
812
+  return p->curTokenId == kIntLexTId && cmIsFlag(p->attrFlags,kIntUnsignedLexFl);
813
+}
814
+
815
+bool               cmLexTokenIsSinglePrecision( cmLexH h )
816
+{
817
+  cmLex* p = _cmLexHandleToPtr(h);
818
+  return p->curTokenId == kRealLexTId && cmIsFlag(p->attrFlags,kRealFloatLexFl);
819
+}
820
+
826 821
 unsigned cmLexCurrentLineNumber( cmLexH h )
827 822
 { 
828 823
   cmLex* p = _cmLexHandleToPtr(h);

+ 12
- 3
cmLex.h 파일 보기

@@ -50,7 +50,8 @@ enum
50 50
   kMemAllocErrLexRC,       //< 10  An attempted memory allocation failed
51 51
   kEofRC,                  //< 11 The end of the input text was encountered (this is a normal condition not an error)
52 52
   kInvalidLexTIdLexRC,     //< 12 An invalid lex token id was encountered.
53
-  kInvalidLexRC            //< 13 Sentinal value.
53
+  kSignErrorLexRC,         //< 13 An signed integer has a 'u' or 'U' suffix."
54
+  kInvalidLexRC            //< 1r Sentinal value.
54 55
 
55 56
 };
56 57
 
@@ -119,15 +120,23 @@ unsigned           cmLexTokenCharCount(  cmLexH h );
119 120
 // Return the value of the current token as an integer.
120 121
 int                cmLexTokenInt( cmLexH h );
121 122
 
122
-// Return the value of the current token as an integer.
123
+// Return the value of the current token as an unsigned integer.
123 124
 unsigned           cmLexTokenUInt( cmLexH h );
124 125
 
125
-// Return the value of the current token as an integer.
126
+// Return the value of the current token as a float.
126 127
 float              cmLexTokenFloat( cmLexH h );
127 128
 
128 129
 // Return the value of the current token as a double.
129 130
 double             cmLexTokenDouble( cmLexH h );
130 131
 
132
+// Return true if the current token is an int and it was suffixed
133
+// with 'u' to indicate that it is unsigned.
134
+bool               cmLexTokenIsUnsigned( cmLexH h );
135
+
136
+// Return true if the current token is a real and it was suffexed 
137
+// with 'f' to indicate that it is a single precision float.
138
+bool               cmLexTokenIsSinglePrecision( cmLexH h );
139
+
131 140
 // Return the line number associated with the current token 
132 141
 unsigned           cmLexCurrentLineNumber( cmLexH h );
133 142
 

Loading…
취소
저장