1 20 21 package JFlex; 22 23 24 32 public class RegExp { 33 34 38 int type; 39 40 41 48 public RegExp(int type) { 49 this.type = type; 50 } 51 52 53 54 62 public String print(String tab) { 63 return tab+toString(); 64 } 65 66 67 70 public String toString() { 71 return "type = "+type; 72 } 73 74 75 81 public boolean isCharClass(Macros macros) { 82 RegExp1 unary; 83 RegExp2 binary; 84 85 switch (type) { 86 case sym.CHAR: 87 case sym.CHAR_I: 88 case sym.CCLASS: 89 case sym.CCLASSNOT: 90 return true; 91 92 case sym.BAR: 93 binary = (RegExp2) this; 94 return binary.r1.isCharClass(macros) && binary.r2.isCharClass(macros); 95 96 case sym.MACROUSE: 97 unary = (RegExp1) this; 98 return macros.getDefinition((String ) unary.content).isCharClass(macros); 99 100 default: return false; 101 } 102 } 103 104 110 public int size(Macros macros) { 111 RegExp1 unary; 112 RegExp2 binary; 113 RegExp content; 114 115 switch ( type ) { 116 case sym.BAR: 117 binary = (RegExp2) this; 118 return binary.r1.size(macros) + binary.r2.size(macros) + 2; 119 120 case sym.CONCAT: 121 binary = (RegExp2) this; 122 return binary.r1.size(macros) + binary.r2.size(macros); 123 124 case sym.STAR: 125 unary = (RegExp1) this; 126 content = (RegExp) unary.content; 127 return content.size(macros) + 2; 128 129 case sym.PLUS: 130 unary = (RegExp1) this; 131 content = (RegExp) unary.content; 132 return content.size(macros) + 2; 133 134 case sym.QUESTION: 135 unary = (RegExp1) this; 136 content = (RegExp) unary.content; 137 return content.size(macros); 138 139 case sym.BANG: 140 unary = (RegExp1) this; 141 content = (RegExp) unary.content; 142 return content.size(macros) * content.size(macros); 143 146 case sym.TILDE: 147 unary = (RegExp1) this; 148 content = (RegExp) unary.content; 149 return content.size(macros) * content.size(macros) * 3; 150 152 case sym.STRING: 153 case sym.STRING_I: 154 unary = (RegExp1) this; 155 return ((String ) unary.content).length()+1; 156 157 case sym.CHAR: 158 case sym.CHAR_I: 159 return 2; 160 161 case sym.CCLASS: 162 case sym.CCLASSNOT: 163 return 2; 164 165 case sym.MACROUSE: 166 unary = (RegExp1) this; 167 return macros.getDefinition((String ) unary.content).size(macros); 168 } 169 170 throw new Error ("unknown regexp type "+type); 171 } 172 } 173 | Popular Tags |