1 2 package com.sun.java_cup.internal; 3 4 22 23 public class lr_item_core { 24 25 26 27 28 29 33 public lr_item_core(production prod, int pos) throws internal_error 34 { 35 symbol after_dot = null; 36 production_part part; 37 38 if (prod == null) 39 throw new internal_error( 40 "Attempt to create an lr_item_core with a null production"); 41 42 _the_production = prod; 43 44 if (pos < 0 || pos > _the_production.rhs_length()) 45 throw new internal_error( 46 "Attempt to create an lr_item_core with a bad dot position"); 47 48 _dot_pos = pos; 49 50 51 _core_hash_cache = 13*_the_production.hashCode() + pos; 52 53 54 if (_dot_pos < _the_production.rhs_length()) 55 { 56 part = _the_production.rhs(_dot_pos); 57 if (!part.is_action()) 58 _symbol_after_dot = ((symbol_part)part).the_symbol(); 59 } 60 } 61 62 63 64 67 public lr_item_core(production prod) throws internal_error 68 { 69 this(prod,0); 70 } 71 72 73 74 75 76 77 protected production _the_production; 78 79 80 public production the_production() {return _the_production;} 81 82 83 84 88 protected int _dot_pos; 89 90 94 public int dot_pos() {return _dot_pos;} 95 96 97 98 99 protected int _core_hash_cache; 100 101 102 103 104 protected symbol _symbol_after_dot = null; 105 106 107 108 109 public boolean dot_at_end() 110 { 111 return _dot_pos >= _the_production.rhs_length(); 112 } 113 114 115 116 118 public symbol symbol_after_dot() 119 { 120 121 return _symbol_after_dot; 122 } 123 124 125 126 129 public non_terminal dot_before_nt() 130 { 131 symbol sym; 132 133 134 sym = symbol_after_dot(); 135 136 137 if (sym != null && sym.is_non_term()) 138 return (non_terminal)sym; 139 else 140 return null; 141 } 142 143 144 145 148 public lr_item_core shift_core() throws internal_error 149 { 150 if (dot_at_end()) 151 throw new internal_error( 152 "Attempt to shift past end of an lr_item_core"); 153 154 return new lr_item_core(_the_production, _dot_pos+1); 155 } 156 157 158 159 162 public boolean core_equals(lr_item_core other) 163 { 164 return other != null && 165 _the_production.equals(other._the_production) && 166 _dot_pos == other._dot_pos; 167 } 168 169 170 171 172 public boolean equals(lr_item_core other) {return core_equals(other);} 173 174 175 176 177 public boolean equals(Object other) 178 { 179 if (!(other instanceof lr_item_core)) 180 return false; 181 else 182 return equals((lr_item_core)other); 183 } 184 185 186 187 188 public int core_hashCode() 189 { 190 return _core_hash_cache; 191 } 192 193 194 195 196 public int hashCode() 197 { 198 return _core_hash_cache; 199 } 200 201 202 203 206 protected int obj_hash() 207 { 208 return super.hashCode(); 209 } 210 211 212 213 216 public String to_simple_string() throws internal_error 217 { 218 String result; 219 production_part part; 220 221 if (_the_production.lhs() != null && 222 _the_production.lhs().the_symbol() != null && 223 _the_production.lhs().the_symbol().name() != null) 224 result = _the_production.lhs().the_symbol().name(); 225 else 226 result = "$$NULL$$"; 227 228 result += " ::= "; 229 230 for (int i = 0; i<_the_production.rhs_length(); i++) 231 { 232 233 if (i == _dot_pos) 234 result += "(*) "; 235 236 237 if (_the_production.rhs(i) == null) 238 { 239 result += "$$NULL$$ "; 240 } 241 else 242 { 243 part = _the_production.rhs(i); 244 if (part == null) 245 result += "$$NULL$$ "; 246 else if (part.is_action()) 247 result += "{ACTION} "; 248 else if (((symbol_part)part).the_symbol() != null && 249 ((symbol_part)part).the_symbol().name() != null) 250 result += ((symbol_part)part).the_symbol().name() + " "; 251 else 252 result += "$$NULL$$ "; 253 } 254 } 255 256 257 if (_dot_pos == _the_production.rhs_length()) 258 result += "(*) "; 259 260 return result; 261 } 262 263 264 265 266 public String toString() 267 { 268 269 try { 270 return to_simple_string(); 271 } catch(internal_error e) { 272 e.crash(); 273 return null; 274 } 275 } 276 277 278 279 } 280 281 | Popular Tags |