1 19 20 package org.netbeans.modules.languages.parser; 21 22 import org.netbeans.api.languages.*; 23 import org.netbeans.api.languages.CharInput; 24 import org.netbeans.api.languages.ASTToken; 25 import org.netbeans.modules.languages.Feature; 26 import org.netbeans.modules.languages.parser.Parser; 27 import org.netbeans.modules.languages.parser.Parser.Cookie; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 34 public abstract class TokenInput { 35 36 public static TokenInput create ( 37 String mimeType, 38 Parser parser, 39 CharInput input, 40 Set skip 41 ) { 42 return new TokenInputImpl ( 43 new TokenReader (mimeType, parser, input, skip), 44 input 45 ); 46 } 47 48 public static TokenInput create (ASTToken[] array) { 49 return new ArrayInput (array); 50 } 51 52 public static TokenInput create (List list) { 53 return new ListInput (list); 54 } 55 56 public abstract ASTToken read (); 57 58 public abstract void setIndex (int index); 59 60 public abstract int getIndex (); 61 62 public abstract int getOffset (); 63 64 public abstract ASTToken next (int index); 65 66 public abstract boolean eof (); 67 68 69 71 private static class TokenReader { 72 73 private String mimeType; 74 private Parser parser; 75 private CharInput input; 76 private Set skip; 77 private int state = -1; 78 private Cookie cookie = new MyCookie (); 79 80 81 private TokenReader ( 82 String mimeType, 83 Parser parser, 84 CharInput input, 85 Set skip 86 ) { 87 this.mimeType = mimeType; 88 this.parser = parser; 89 this.input = input; 90 this.skip = skip; 91 } 92 93 private ASTToken next; 94 95 public ASTToken nextToken (CharInput input) { 96 if (next == null) 97 next = readToken (input); 98 return next; 99 } 100 101 public ASTToken readToken (CharInput input) { 102 if (next != null) { 103 ASTToken p = next; 104 next = null; 105 return p; 106 } 107 ASTToken token = null; 108 do { 109 int start = input.getIndex (); 110 token = parser.read (cookie, input, mimeType); 111 if (token == null) return null; 112 } while (skip.contains (token.getType ())); 113 return token; 114 } 115 116 private class MyCookie implements Cookie { 117 public int getState () { 118 return state; 119 } 120 121 public void setState (int state) { 122 TokenReader.this.state = state; 123 } 124 125 public void setProperties (Feature tokenProperties) { 126 } 127 } 128 } 129 130 private static class TokenInputImpl extends TokenInput { 131 132 private TokenReader tokenReader; 133 private List <ASTToken> tokens = new ArrayList <ASTToken> (); 134 private int index = 0; 135 private CharInput input; 136 137 138 TokenInputImpl (TokenReader tokenReader, CharInput input) { 139 this.input = input; 140 this.tokenReader = tokenReader; 141 } 142 143 public ASTToken next (int i) { 144 while (index + i - 1 >= tokens.size ()) 145 tokens.add (tokenReader.readToken (input)); 146 return (ASTToken) tokens.get (index + i - 1); 147 } 148 149 public boolean eof () { 150 return next (1) == null; 151 } 152 153 public int getIndex () { 154 return index; 155 } 156 157 public int getOffset () { 158 ASTToken t = null; 159 if (eof ()) { 160 if (getIndex () == 0) return 0; 161 int i = tokens.size () - 1; 162 do { 163 t = ((ASTToken) tokens.get (i--)); 164 } while (t == null && i > 0); 165 if (t == null) return 0; 166 return t.getOffset () + t.getLength (); 167 } else { 168 t = (ASTToken) next (1); 169 return t.getOffset (); 170 } 171 } 172 173 public ASTToken read () { 174 ASTToken t = next (1); 175 if (t != null) index++; 176 return t; 177 } 178 179 public void setIndex (int index) { 180 if (index > tokens.size ()) 181 throw new IndexOutOfBoundsException (); 182 this.index = index; 183 } 184 185 public String getString (int from) { 186 throw new InternalError (); 187 } 188 189 public String toString () { 190 return input.toString (); 191 } 192 } 193 194 private static class ArrayInput extends TokenInput { 195 196 private ASTToken[] array; 197 private int index = 0; 198 private int length; 199 200 private ArrayInput (ASTToken[] array) { 201 this.array = array; 202 length = array.length; 203 } 204 205 public ASTToken read () { 206 if (index < length) 207 return array [index++]; 208 return null; 209 } 210 211 public void setIndex (int index) { 212 this.index = index; 213 } 214 215 public int getIndex () { 216 return index; 217 } 218 219 public int getOffset () { 220 ASTToken t = null; 221 if (eof ()) { 222 if (getIndex () == 0) return 0; 223 t = (ASTToken) array [array.length - 1]; 224 return t.getOffset () + t.getLength (); 225 } else { 226 t = (ASTToken) next (1); 227 return t.getOffset (); 228 } 229 } 230 231 public boolean eof () { 232 return index >= length; 233 } 234 235 public ASTToken next (int i) { 236 if (index + i - 1 < length) 237 return array [index + i - 1]; 238 return null; 239 } 240 241 public String toString () { 242 StringBuilder sb = new StringBuilder (); 243 int i = index, j = 0; 244 while (j < 10 && i < length) { 245 sb.append (array [i]).append (" "); 246 i++; j++; 247 } 248 return sb.toString (); 249 } 250 } 251 252 private static class ListInput extends TokenInput { 253 254 private List list; 255 private int index = 0; 256 private int length; 257 258 private ListInput (List list) { 259 this.list = list; 260 length = list.size (); 261 } 262 263 public ASTToken read () { 264 if (index < length) 265 return (ASTToken) list.get (index++); 266 return null; 267 } 268 269 public void setIndex (int index) { 270 this.index = index; 271 } 272 273 public int getIndex () { 274 return index; 275 } 276 277 public int getOffset () { 278 ASTToken t = null; 279 if (eof ()) { 280 if (getIndex () == 0) return 0; 281 t = ((ASTToken) list.get (list.size () - 1)); 282 return t.getOffset () + t.getLength (); 283 } else { 284 t = (ASTToken) next (1); 285 return t.getOffset (); 286 } 287 } 288 289 public boolean eof () { 290 return index >= length; 291 } 292 293 public ASTToken next (int i) { 294 if (index + i - 1 < length) 295 return (ASTToken) list.get (index + i - 1); 296 return null; 297 } 298 299 public String toString () { 300 StringBuilder sb = new StringBuilder (); 301 int i = index, j = 0; 302 while (j < 10 && i < length) { 303 sb.append (list.get (i)).append (" "); 304 i++; j++; 305 } 306 return sb.toString (); 307 } 308 } 309 } | Popular Tags |