1 33 34 package edu.rice.cs.drjava.model.definitions.reducedmodel; 35 36 42 public abstract class AbstractReducedModel implements ReducedModelStates { 43 44 45 public static final char PTR_CHAR = '#'; 46 47 50 TokenList _tokens; 51 52 55 TokenList.Iterator _cursor; 56 57 58 public AbstractReducedModel() { 59 _tokens = new TokenList(); 60 _cursor = _tokens._getIterator(); 61 _cursor.setBlockOffset(0); 63 } 64 65 68 int getBlockOffset() { return _cursor.getBlockOffset(); } 69 70 73 void setBlockOffset(int offset) { _cursor.setBlockOffset(offset); } 74 75 78 int absOffset() { 79 int off = _cursor.getBlockOffset(); 80 TokenList.Iterator it = _cursor._copy(); 81 if (! it.atStart()) it.prev(); 82 83 while (! it.atStart()) { 84 off += it.current().getSize(); 85 it.prev(); 86 } 87 it.dispose(); 88 return off; 89 } 90 91 92 public String simpleString() { 93 final StringBuilder val = new StringBuilder (); 94 ReducedToken tmp; 95 96 TokenList.Iterator it = _tokens._getIterator(); 97 it.next(); 99 if (_cursor.atStart()) val.append(PTR_CHAR).append(_cursor.getBlockOffset()); 100 101 while (!it.atEnd()) { 102 tmp = it.current(); 103 104 if (!_cursor.atStart() && !_cursor.atEnd() && (tmp == _cursor.current())) { 105 val.append(PTR_CHAR).append(_cursor.getBlockOffset()); 106 } 107 108 val.append('|').append(tmp).append('|').append('\t'); 109 it.next(); 110 } 111 112 if (_cursor.atEnd()) val.append(PTR_CHAR).append(_cursor.getBlockOffset()); 113 114 val.append("|end|"); 115 it.dispose(); 116 return val.toString(); 117 } 118 119 121 public abstract void insertChar(char ch); 122 123 140 public void _insertGap( int length ) { 141 if (_cursor.atStart()) { 142 if (_gapToRight()) { 143 _cursor.next(); 144 _augmentCurrentGap(length); } 146 else _insertNewGap(length); } 148 else if (_cursor.atEnd()) { 149 if (_gapToLeft()) { 150 _augmentGapToLeft(length); 151 } 154 else _insertNewGap(length); } 156 else if ((_cursor.getBlockOffset() > 0) && _cursor.current().isMultipleCharBrace()) 158 insertGapBetweenMultiCharBrace(length); 159 else if (_cursor.current().isGap()) { 161 _cursor.current().grow(length); 162 _cursor.setBlockOffset(_cursor.getBlockOffset() + length); 163 } 164 else if (!_cursor.atFirstItem() && _cursor.prevItem().isGap()) 165 _cursor.prevItem().grow(length); 167 else _insertNewGap(length); return; 170 } 171 172 181 protected abstract void insertGapBetweenMultiCharBrace(int length); 182 183 189 public TokenList.Iterator makeCopyCursor() { 190 return _cursor._copy(); 191 } 192 193 198 protected ReducedModelState getStateAtCurrent() { 199 return _cursor.getStateAtCurrent(); 200 } 201 202 203 protected boolean _gapToRight() { 204 return (!_tokens.isEmpty() && !_cursor.atEnd() && 206 !_cursor.atLastItem() && _cursor.nextItem().isGap()); 207 } 208 209 210 protected boolean _gapToLeft() { 211 return (!_tokens.isEmpty() && !_cursor.atStart() && !_cursor.atFirstItem() &&_cursor.prevItem().isGap()); 213 } 214 215 218 protected void _augmentGapToLeft(int length) { _cursor.prevItem().grow(length); } 219 220 223 protected void _augmentCurrentGap(int length) { 224 _cursor.current().grow(length); 225 _cursor.setBlockOffset(length); 226 } 227 228 231 protected void _insertNewGap(int length) { 232 _cursor.insert(new Gap(length, getStateAtCurrent())); 233 _cursor.next(); 234 _cursor.setBlockOffset(0); 235 } 236 237 241 protected abstract ReducedModelState moveWalkerGetState(int relLocation); 242 243 244 protected abstract void resetWalkerLocationToCursor(); 245 246 250 protected ReducedToken current() { return _cursor.current(); } 251 252 255 protected void next() { _cursor.next(); } 256 257 263 protected void prev() { 264 _cursor.prev(); 265 } 266 267 } 268 | Popular Tags |