home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / digital marsC compier / dm / include / regexp.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-17  |  2.9 KB  |  107 lines

  1.  
  2. // Regular Expressions
  3. // Copyright (c) 2000-2001 by Digital Mars
  4. // All Rights Reserved
  5. // Written by Walter Bright
  6.  
  7. #ifndef REGEXP_H
  8. #define REGEXP_H
  9.  
  10. #include <tchar.h>
  11.  
  12. /*
  13.     Escape sequences:
  14.  
  15.     \nnn starts out a 1, 2 or 3 digit octal sequence,
  16.     where n is an octal digit. If nnn is larger than
  17.     0377, then the 3rd digit is not part of the sequence
  18.     and is not consumed.
  19.     For maximal portability, use exactly 3 digits.
  20.  
  21.     \xXX starts out a 1 or 2 digit hex sequence. X
  22.     is a hex character. If the first character after the \x
  23.     is not a hex character, the value of the sequence is 'x'
  24.     and the XX are not consumed.
  25.     For maximal portability, use exactly 2 digits.
  26.  
  27.     \uUUUU is a unicode sequence. There are exactly
  28.     4 hex characters after the \u, if any are not, then
  29.     the value of the sequence is 'u', and the UUUU are not
  30.     consumed.
  31.  
  32.     Character classes:
  33.  
  34.     [a-b], where a is greater than b, will produce
  35.     an error.
  36.  */
  37.  
  38. typedef struct regmatch
  39. {
  40.     int rm_so;            // index of start of match
  41.     int rm_eo;            // index past end of match
  42. } regmatch_t;
  43.  
  44. struct Range;
  45. struct RegBuffer;
  46.  
  47. struct RegExp
  48. {
  49.     RegExp();
  50.     ~RegExp();
  51.  
  52.     unsigned re_nsub;        // number of parenthesized subexpression matches
  53.     regmatch_t *pmatch;        // array [re_nsub + 1]
  54.  
  55.     TCHAR *input;        // the string to search
  56.  
  57.     // per instance:
  58.  
  59.     int ref;            // !=0 means don't make our own copy of pattern
  60.     TCHAR *pattern;        // source text of the regular expression
  61.  
  62.     TCHAR flags[3 + 1];        // source text of the attributes parameter
  63.                 // (3 TCHARs max plus terminating 0)
  64.     int errors;
  65.  
  66.     unsigned attributes;
  67.  
  68.     #define REAglobal      1    // has the g attribute
  69.     #define REAignoreCase 2    // has the i attribute
  70.     #define REAmultiline  4    // if treat as multiple lines separated
  71.                 // by newlines, or as a single line
  72.     #define REAdotmatchlf 8    // if . matches \n
  73.  
  74.     int compile(TCHAR *pattern, TCHAR *attributes, int ref);
  75.     int test(TCHAR *string, int startindex = 0);
  76.  
  77.     TCHAR *replace(TCHAR *format);
  78.     TCHAR *replace2(TCHAR *format);
  79.     static TCHAR *replace3(TCHAR *format, TCHAR *input,
  80.     unsigned re_nsub, regmatch_t *pmatch);
  81.     static TCHAR *replace4(TCHAR *input, regmatch_t *match, TCHAR *replacement);
  82.  
  83. private:
  84.     TCHAR *src;            // current source pointer
  85.     TCHAR *src_start;        // starting position for match
  86.     TCHAR *p;            // position of parser in pattern
  87.     regmatch_t match;        // match for the entire regular expression
  88.                 // (serves as storage for pmatch[0])
  89.  
  90.     char *program;
  91.     RegBuffer *buf;
  92.  
  93.     void printProgram(char *prog);
  94.     int trymatch(char *prog, char *progend);
  95.     int parseRegexp();
  96.     int parsePiece();
  97.     int parseAtom();
  98.     int parseRange();
  99.     int escape();
  100.     void error(char *msg);
  101.     void optimize();
  102.     int startchars(Range *r, char *prog, char *progend);
  103. };
  104.  
  105. #endif
  106.  
  107.