Sfoglia il codice sorgente

cmLex.h/c : Added optional quoted character token recognizer.

master
kevin 10 anni fa
parent
commit
83314506d7
2 ha cambiato i file con 35 aggiunte e 14 eliminazioni
  1. 19
    0
      cmLex.c
  2. 16
    14
      cmLex.h

+ 19
- 0
cmLex.c Vedi File

@@ -120,6 +120,8 @@ cmRC_t _cmLexError( cmLex* p, unsigned rc, const char* fmt, ... )
120 120
   return rc;
121 121
 }
122 122
 
123
+// Locate 'keyStr' in cp[cn] and return the index into cp[cn] of the character
124
+// following the last char in 'keyStr'.  If keyStr is not found return cmInvalidIdx.
123 125
 unsigned _cmLexScanTo( const cmChar_t* cp, unsigned cn, const cmChar_t* keyStr )
124 126
 {
125 127
   unsigned i = 0;
@@ -336,6 +338,20 @@ unsigned _cmLexQStrMatcher( cmLex* p, const cmChar_t* cp, unsigned cn, const cmC
336 338
   return 0;
337 339
 }
338 340
 
341
+unsigned _cmLexQCharMatcher( cmLex* p, const cmChar_t* cp, unsigned cn, const cmChar_t* keyStr )
342
+{
343
+  unsigned i = 0;
344
+  if( i >= cn || cp[i]!='\'' )
345
+    return 0;
346
+
347
+  i+=2;
348
+
349
+  if( i >= cn || cp[i]!='\'')
350
+    return 0;
351
+
352
+  return 3;
353
+}
354
+
339 355
 
340 356
 unsigned _cmLexBlockCmtMatcher( cmLex* p, const cmChar_t* cp, unsigned cn, const cmChar_t* keyStr )
341 357
 {  
@@ -474,6 +490,9 @@ cmLexH cmLexInit( const cmChar_t* cp, unsigned cn, unsigned flags, cmRpt_t* rpt
474 490
   _cmLexInstallMatcher( p, kBlockCmtLexTId, _cmLexBlockCmtMatcher, NULL, NULL  );
475 491
   _cmLexInstallMatcher( p, kLineCmtLexTId,  _cmLexLineCmtMatcher,  NULL, NULL  );
476 492
 
493
+  if( cmIsFlag(flags,kReturnQCharLexFl) )
494
+    _cmLexInstallMatcher( p, kQCharLexTId,    _cmLexQCharMatcher,    NULL, NULL  );
495
+
477 496
   h.h = p;
478 497
 
479 498
   _cmLexReset(p);

+ 16
- 14
cmLex.h Vedi File

@@ -11,18 +11,19 @@
11 11
 // Predefined Lexer Id's
12 12
 enum
13 13
 {
14
-  kErrorLexTId,    // 0  the lexer was unable to identify the current token
15
-  kUnknownLexTId,  // 1  the token is of an unknown type (only used when kReturnUnknownLexFl is set)
16
-  kEofLexTId,      // 2  the lexer reached the end of input
17
-  kSpaceLexTId,    // 3  white space
18
-  kRealLexTId,     // 4  real number (contains a decimal point or is in scientific notation) 
19
-  kIntLexTId,      // 5  decimal integer
20
-  kHexLexTId,      // 6  hexidecimal integer
21
-  kIdentLexTId,    // 7  identifier
22
-  kQStrLexTId,     // 8  quoted string
23
-  kBlockCmtLexTId, // 9  block comment
24
-  kLineCmtLexTId,  // 10  line comment
25
-  kUserLexTId      // 11 user registered token (See cmLexRegisterToken().)
14
+  kErrorLexTId,    //  0  the lexer was unable to identify the current token
15
+  kUnknownLexTId,  //  1  the token is of an unknown type (only used when kReturnUnknownLexFl is set)
16
+  kEofLexTId,      //  2  the lexer reached the end of input
17
+  kSpaceLexTId,    //  3  white space
18
+  kRealLexTId,     //  4  real number (contains a decimal point or is in scientific notation) 
19
+  kIntLexTId,      //  5  decimal integer
20
+  kHexLexTId,      //  6  hexidecimal integer
21
+  kIdentLexTId,    //  7  identifier
22
+  kQStrLexTId,     //  8  quoted string
23
+  kQCharLexTId,    //  9  quoted char
24
+  kBlockCmtLexTId, // 10  block comment
25
+  kLineCmtLexTId,  // 11  line comment
26
+  kUserLexTId      // 12 user registered token (See cmLexRegisterToken().)
26 27
 };
27 28
 
28 29
 // Lexer control flags used with cmLexInit().
@@ -31,7 +32,8 @@ enum
31 32
   kReturnSpaceLexFl    = 0x01, //< Return space tokens
32 33
   kReturnCommentsLexFl = 0x02, //< Return comment tokens
33 34
   kReturnUnknownLexFl  = 0x04, //< Return unknown tokens
34
-  kUserDefPriorityLexFl= 0x08  //< User defined tokens take priority even if a kIdentLexTId token has a longer match
35
+  kReturnQCharLexFl    = 0x08, //< Return quoted characters
36
+  kUserDefPriorityLexFl= 0x10  //< User defined tokens take priority even if a kIdentLexTId token has a longer match
35 37
 };
36 38
 
37 39
 // cmLex result codes.
@@ -84,7 +86,7 @@ cmRC_t             cmLexSetFile( cmLexH h, const cmChar_t* fn );
84 86
 cmRC_t             cmLexRegisterToken( cmLexH h, unsigned id, const cmChar_t* token );
85 87
 
86 88
 // Register a user defined token recognition function.  This function should return the count
87
-// of initial, consecutive, characters in 'cp' which match its token pattern.
89
+// of initial, consecutive, characters in 'cp[cn]' which match its token pattern.
88 90
 typedef unsigned (*cmLexUserMatcherPtr_t)( const cmChar_t* cp, unsigned cn );
89 91
 
90 92
 cmRC_t             cmLexRegisterMatcher( cmLexH h, unsigned id, cmLexUserMatcherPtr_t funcPtr );

Loading…
Annulla
Salva