1 19 package org.netbeans.modules.java.source.save; 20 21 import org.netbeans.modules.java.source.save.SourceBuffer; 22 23 import com.sun.tools.javac.parser.*; 24 import com.sun.tools.javac.parser.Scanner; 25 import com.sun.tools.javac.util.Context; 26 import com.sun.tools.javac.util.Name; 27 import com.sun.tools.javac.util.Position; 28 import java.io.*; 29 import java.util.*; 30 31 import static com.sun.tools.javac.parser.Token.*; 32 33 37 public class TokenList extends AbstractList { 38 39 private String sourceFile; 40 private Token[] tokens; 41 42 54 public static TokenList scan(Context context, String sourceFile, 55 SourceBuffer srcBuffer) throws IOException { 56 TokenList tlist = new TokenList(sourceFile); 57 tlist.scan(context, srcBuffer); 58 return tlist; 59 } 60 61 66 private TokenList(String sourceFile) { 67 this.sourceFile = sourceFile; 68 } 69 70 73 protected void scan(Context context, SourceBuffer srcBuffer) 74 throws IOException { 75 Scanner.Factory factory = Scanner.Factory.instance(context); 76 Scanner scanner = factory.newScanner(srcBuffer); 77 List<Token> list = new ArrayList<Token>(); 78 scanner.nextToken(); while (scanner.token() != EOF) { 80 list.add(new Token(scanner)); 81 scanner.nextToken(); 82 } 83 tokens = (Token[])list.toArray(new Token[0]); 84 } 85 86 91 public Token get(int index) { 92 return tokens[index]; 93 } 94 95 98 public int size() { 99 return tokens.length; 100 } 101 102 110 public int indexOf(int pos) { 111 int low = 0; 113 int high = tokens.length - 1; 114 int idx; 115 while (true) { 116 idx = (low + high) / 2; 117 if (tokens[idx].pos == pos) 118 return idx; 119 if (low > high) 120 return -1; 121 if (tokens[idx].pos < pos) 122 low = idx + 1; 123 else 124 high = idx - 1; 125 } 126 } 127 128 136 public int indexOfEndPos(int endPos) { 137 int low = 0; 139 int high = tokens.length - 1; 140 int idx; 141 while (true) { 142 idx = (low + high) / 2; 143 if (tokens[idx].endPos == endPos) 144 return idx; 145 if (low > high) 146 return -1; 147 if (tokens[idx].endPos < endPos) 148 low = idx + 1; 149 else 150 high = idx - 1; 151 } 152 } 153 154 public String toString() { 155 return "TokenList of " + sourceFile + ": " + 156 tokens.length + " tokens"; 157 } 158 159 public static class Token { 160 private com.sun.tools.javac.parser.Token token; 161 private int pos; 162 private int endPos; 163 private Name name; 164 private String value; 165 166 private Token(Scanner scanner) { 167 token = scanner.token(); 168 pos = scanner.pos(); 169 endPos = scanner.endPos(); 170 switch (token) { 171 172 case INTLITERAL: 174 case LONGLITERAL: 175 case FLOATLITERAL: 176 case DOUBLELITERAL: 177 case CHARLITERAL: 178 case STRINGLITERAL: 179 value = scanner.stringVal(); 180 break; 181 182 case LPAREN: 184 case RPAREN: 185 case LBRACE: 186 case RBRACE: 187 case LBRACKET: 188 case RBRACKET: 189 case COMMA: 190 case SEMI: 191 case DOT: 192 break; 193 194 default: 196 name = scanner.name(); 197 } 198 } 199 200 public com.sun.tools.javac.parser.Token getToken() { 201 return token; 202 } 203 public int getPos() { 204 return pos; 205 } 206 public int getEndPos() { 207 return endPos; 208 } 209 public Name getName() { 210 return name; 211 } 212 public String getValue() { 213 return value; 214 } 215 public boolean isFlag() { 216 return token == PUBLIC || 218 token == PRIVATE || 219 token == PROTECTED || 220 token == STATIC || 221 token == FINAL || 222 token == SYNCHRONIZED || 223 token == VOLATILE || 224 token == TRANSIENT || 225 token == NATIVE || 226 token == INTERFACE || 227 token == ABSTRACT || 228 token == STRICTFP; 229 } 230 public boolean isIdentifier() { 231 return token == IDENTIFIER; 232 } 233 234 public String toString() { 235 StringBuffer sb = new StringBuffer (token.name()); 236 sb.append(": pos="); 237 sb.append(pos); 238 sb.append(':'); 239 sb.append(pos); 240 sb.append(", endPos="); 241 sb.append(endPos); 242 sb.append(':'); 243 sb.append(endPos); 244 if (name != null) { 245 sb.append(", name="); 246 sb.append(name.toString()); 247 } 248 if (value != null) { 249 sb.append(", value="); 250 sb.append(value); 251 } 252 return sb.toString(); 253 } 254 } 255 } 256 | Popular Tags |