1 #ifndef re2c_re_h 2 #define re2c_re_h 3 4 #include <stdio.h> 5 #include "tools/re2c/token.h" 6 #include "tools/re2c/ins.h" 7 8 typedef struct extop { 9 char op; 10 int minsize; 11 int maxsize; 12 } ExtOp; 13 14 typedef struct CharPtn { 15 unsigned int card; 16 struct CharPtn *fix; 17 struct CharPtn *nxt; 18 } CharPtn; 19 20 typedef struct CharSet { 21 CharPtn *fix; 22 CharPtn *freeHead, **freeTail; 23 CharPtn *rep[nChars]; 24 CharPtn ptn[nChars]; 25 } CharSet; 26 27 typedef struct Range { 28 struct Range *next; 29 unsigned int lb, ub; /* [lb,ub) */ 30 } Range; 31 32 static void 33 Range_init(Range *r, unsigned int l, unsigned int u) 34 { 35 r->next = NULL; 36 r->lb = l; 37 r->ub = u; 38 } 39 40 static Range * 41 Range_new(unsigned int l, unsigned int u) 42 { 43 Range *r = malloc(sizeof(Range)); 44 r->next = NULL; 45 r->lb = l; 46 r->ub = u; 47 return r; 48 } 49 50 static void 51 Range_copy(Range *ro, const Range *r) 52 { 53 ro->next = NULL; 54 ro->lb = r->lb; 55 ro->ub = r->ub; 56 } 57 58 static Range * 59 Range_new_copy(Range *r) 60 { 61 Range *ro = malloc(sizeof(Range)); 62 ro->next = NULL; 63 ro->lb = r->lb; 64 ro->ub = r->ub; 65 return ro; 66 } 67 68 void Range_out(FILE *, const Range *); 69 70 typedef enum { 71 NULLOP = 1, 72 MATCHOP, 73 RULEOP, 74 ALTOP, 75 CATOP, 76 CLOSEOP, 77 CLOSEVOP 78 } RegExpType; 79 80 typedef struct RegExp { 81 RegExpType type; 82 unsigned int size; 83 union { 84 /* for MatchOp */ 85 Range *match; 86 /* for RuleOp */ 87 struct { 88 struct RegExp *exp; 89 struct RegExp *ctx; 90 Ins *ins; 91 unsigned int accept; 92 Token *code; 93 unsigned int line; 94 } RuleOp; 95 /* for AltOp and CatOp*/ 96 struct { 97 struct RegExp *exp1, *exp2; 98 } AltCatOp; 99 /* for CloseOp */ 100 struct RegExp *exp; 101 /* for CloseVOp*/ 102 struct { 103 struct RegExp *exp; 104 int min; 105 int max; 106 } CloseVOp; 107 } d; 108 } RegExp; 109 110 static RegExp * 111 RegExp_isA(RegExp *r, RegExpType t) 112 { 113 return r->type == t ? r : NULL; 114 } 115 116 void RegExp_split(RegExp*, CharSet*); 117 void RegExp_calcSize(RegExp*, Char*); 118 unsigned int RegExp_fixedLength(RegExp*); 119 void RegExp_compile(RegExp*, Char*, Ins*); 120 void RegExp_display(RegExp*, FILE *); 121 122 static RegExp * 123 RegExp_new_NullOp(void) 124 { 125 RegExp *r = malloc(sizeof(RegExp)); 126 r->type = NULLOP; 127 return r; 128 } 129 130 static RegExp * 131 RegExp_new_MatchOp(Range *m) 132 { 133 RegExp *r = malloc(sizeof(RegExp)); 134 r->type = MATCHOP; 135 r->d.match = m; 136 return r; 137 } 138 139 RegExp *RegExp_new_RuleOp(RegExp*, RegExp*, Token*, unsigned int); 140 141 static RegExp * 142 RegExp_new_AltOp(RegExp *e1, RegExp *e2) 143 { 144 RegExp *r = malloc(sizeof(RegExp)); 145 r->type = ALTOP; 146 r->d.AltCatOp.exp1 = e1; 147 r->d.AltCatOp.exp2 = e2; 148 return r; 149 } 150 151 static RegExp * 152 RegExp_new_CatOp(RegExp *e1, RegExp *e2) 153 { 154 RegExp *r = malloc(sizeof(RegExp)); 155 r->type = CATOP; 156 r->d.AltCatOp.exp1 = e1; 157 r->d.AltCatOp.exp2 = e2; 158 return r; 159 } 160 161 static RegExp * 162 RegExp_new_CloseOp(RegExp *e) 163 { 164 RegExp *r = malloc(sizeof(RegExp)); 165 r->type = CLOSEOP; 166 r->d.exp = e; 167 return r; 168 } 169 170 static RegExp * 171 RegExp_new_CloseVOp(RegExp *e, int lb, int ub) 172 { 173 RegExp *r = malloc(sizeof(RegExp)); 174 r->type = CLOSEVOP; 175 r->d.CloseVOp.exp = e; 176 r->d.CloseVOp.min = lb; 177 r->d.CloseVOp.max = ub; 178 return r; 179 } 180 181 extern void genCode(FILE *, RegExp*); 182 extern RegExp *mkDiff(RegExp*, RegExp*); 183 extern RegExp *mkDot(void); 184 extern RegExp *strToRE(SubStr); 185 extern RegExp *strToCaseInsensitiveRE(SubStr); 186 extern RegExp *ranToRE(SubStr); 187 extern RegExp *invToRE(SubStr); 188 189 extern RegExp *mkAlt(RegExp*, RegExp*); 190 191 #endif 192