28 import java.util.Locale; |
28 import java.util.Locale; |
29 |
29 |
30 import com.sun.tools.javac.api.Formattable; |
30 import com.sun.tools.javac.api.Formattable; |
31 import com.sun.tools.javac.api.Messages; |
31 import com.sun.tools.javac.api.Messages; |
32 import com.sun.tools.javac.parser.Tokens.Token.Tag; |
32 import com.sun.tools.javac.parser.Tokens.Token.Tag; |
|
33 import com.sun.tools.javac.util.List; |
33 import com.sun.tools.javac.util.Name; |
34 import com.sun.tools.javac.util.Name; |
34 import com.sun.tools.javac.util.Context; |
35 import com.sun.tools.javac.util.Context; |
|
36 import com.sun.tools.javac.util.ListBuffer; |
35 import com.sun.tools.javac.util.Names; |
37 import com.sun.tools.javac.util.Names; |
36 |
38 |
37 /** A class that defines codes/utilities for Java source tokens |
39 /** A class that defines codes/utilities for Java source tokens |
38 * returned from lexical analysis. |
40 * returned from lexical analysis. |
39 * |
41 * |
279 public String toString(Locale locale, Messages messages) { |
281 public String toString(Locale locale, Messages messages) { |
280 return name != null ? toString() : messages.getLocalizedString(locale, "compiler.misc." + toString()); |
282 return name != null ? toString() : messages.getLocalizedString(locale, "compiler.misc." + toString()); |
281 } |
283 } |
282 } |
284 } |
283 |
285 |
|
286 public interface Comment { |
|
287 |
|
288 enum CommentStyle { |
|
289 LINE, |
|
290 BLOCK, |
|
291 JAVADOC, |
|
292 } |
|
293 |
|
294 String getText(); |
|
295 CommentStyle getStyle(); |
|
296 boolean isDeprecated(); |
|
297 } |
|
298 |
284 /** |
299 /** |
285 * This is the class representing a javac token. Each token has several fields |
300 * This is the class representing a javac token. Each token has several fields |
286 * that are set by the javac lexer (i.e. start/end position, string value, etc). |
301 * that are set by the javac lexer (i.e. start/end position, string value, etc). |
287 */ |
302 */ |
288 public static class Token { |
303 public static class Token { |
302 public final int pos; |
317 public final int pos; |
303 |
318 |
304 /** The end position of this token */ |
319 /** The end position of this token */ |
305 public final int endPos; |
320 public final int endPos; |
306 |
321 |
307 /** Is this token preceeded by a deprecated comment? */ |
322 /** Comment reader associated with this token */ |
308 public final boolean deprecatedFlag; |
323 public final List<Comment> comments; |
309 |
324 |
310 /** Is this token preceeded by a deprecated comment? */ |
325 Token(TokenKind kind, int pos, int endPos, List<Comment> comments) { |
311 public String docComment; |
|
312 |
|
313 Token(TokenKind kind, int pos, int endPos, |
|
314 boolean deprecatedFlag) { |
|
315 this.kind = kind; |
326 this.kind = kind; |
316 this.pos = pos; |
327 this.pos = pos; |
317 this.endPos = endPos; |
328 this.endPos = endPos; |
318 this.deprecatedFlag = deprecatedFlag; |
329 this.comments = comments; |
319 checkKind(); |
330 checkKind(); |
320 } |
331 } |
321 |
332 |
322 Token[] split(Tokens tokens) { |
333 Token[] split(Tokens tokens) { |
323 if (kind.name.length() < 2 || kind.tag != Tag.DEFAULT) { |
334 if (kind.name.length() < 2 || kind.tag != Tag.DEFAULT) { |
329 |
340 |
330 if (t1 == null || t2 == null) { |
341 if (t1 == null || t2 == null) { |
331 throw new AssertionError("Cant split - bad subtokens"); |
342 throw new AssertionError("Cant split - bad subtokens"); |
332 } |
343 } |
333 return new Token[] { |
344 return new Token[] { |
334 new Token(t1, pos, pos + t1.name.length(), deprecatedFlag), |
345 new Token(t1, pos, pos + t1.name.length(), comments), |
335 new Token(t2, pos + t1.name.length(), endPos, false) |
346 new Token(t2, pos + t1.name.length(), endPos, null) |
336 }; |
347 }; |
337 } |
348 } |
338 |
349 |
339 protected void checkKind() { |
350 protected void checkKind() { |
340 if (kind.tag != Tag.DEFAULT) { |
351 if (kind.tag != Tag.DEFAULT) { |
351 } |
362 } |
352 |
363 |
353 public int radix() { |
364 public int radix() { |
354 throw new UnsupportedOperationException(); |
365 throw new UnsupportedOperationException(); |
355 } |
366 } |
|
367 |
|
368 /** |
|
369 * Preserve classic semantics - if multiple javadocs are found on the token |
|
370 * the last one is returned |
|
371 */ |
|
372 public String comment(Comment.CommentStyle style) { |
|
373 List<Comment> readers = getReaders(Comment.CommentStyle.JAVADOC); |
|
374 return readers.isEmpty() ? |
|
375 null : |
|
376 readers.head.getText(); |
|
377 } |
|
378 |
|
379 /** |
|
380 * Preserve classic semantics - deprecated should be set if at least one |
|
381 * javadoc comment attached to this token contains the '@deprecated' string |
|
382 */ |
|
383 public boolean deprecatedFlag() { |
|
384 for (Comment r : getReaders(Comment.CommentStyle.JAVADOC)) { |
|
385 if (r.isDeprecated()) { |
|
386 return true; |
|
387 } |
|
388 } |
|
389 return false; |
|
390 } |
|
391 |
|
392 private List<Comment> getReaders(Comment.CommentStyle style) { |
|
393 if (comments == null) { |
|
394 return List.nil(); |
|
395 } else { |
|
396 ListBuffer<Comment> buf = ListBuffer.lb(); |
|
397 for (Comment r : comments) { |
|
398 if (r.getStyle() == style) { |
|
399 buf.add(r); |
|
400 } |
|
401 } |
|
402 return buf.toList(); |
|
403 } |
|
404 } |
356 } |
405 } |
357 |
406 |
358 final static class NamedToken extends Token { |
407 final static class NamedToken extends Token { |
359 /** The name of this token */ |
408 /** The name of this token */ |
360 public final Name name; |
409 public final Name name; |
361 |
410 |
362 public NamedToken(TokenKind kind, int pos, int endPos, Name name, boolean deprecatedFlag) { |
411 public NamedToken(TokenKind kind, int pos, int endPos, Name name, List<Comment> comments) { |
363 super(kind, pos, endPos, deprecatedFlag); |
412 super(kind, pos, endPos, comments); |
364 this.name = name; |
413 this.name = name; |
365 } |
414 } |
366 |
415 |
367 protected void checkKind() { |
416 protected void checkKind() { |
368 if (kind.tag != Tag.NAMED) { |
417 if (kind.tag != Tag.NAMED) { |
378 |
427 |
379 static class StringToken extends Token { |
428 static class StringToken extends Token { |
380 /** The string value of this token */ |
429 /** The string value of this token */ |
381 public final String stringVal; |
430 public final String stringVal; |
382 |
431 |
383 public StringToken(TokenKind kind, int pos, int endPos, String stringVal, boolean deprecatedFlag) { |
432 public StringToken(TokenKind kind, int pos, int endPos, String stringVal, List<Comment> comments) { |
384 super(kind, pos, endPos, deprecatedFlag); |
433 super(kind, pos, endPos, comments); |
385 this.stringVal = stringVal; |
434 this.stringVal = stringVal; |
386 } |
435 } |
387 |
436 |
388 protected void checkKind() { |
437 protected void checkKind() { |
389 if (kind.tag != Tag.STRING) { |
438 if (kind.tag != Tag.STRING) { |
399 |
448 |
400 final static class NumericToken extends StringToken { |
449 final static class NumericToken extends StringToken { |
401 /** The 'radix' value of this token */ |
450 /** The 'radix' value of this token */ |
402 public final int radix; |
451 public final int radix; |
403 |
452 |
404 public NumericToken(TokenKind kind, int pos, int endPos, String stringVal, int radix, boolean deprecatedFlag) { |
453 public NumericToken(TokenKind kind, int pos, int endPos, String stringVal, int radix, List<Comment> comments) { |
405 super(kind, pos, endPos, stringVal, deprecatedFlag); |
454 super(kind, pos, endPos, stringVal, comments); |
406 this.radix = radix; |
455 this.radix = radix; |
407 } |
456 } |
408 |
457 |
409 protected void checkKind() { |
458 protected void checkKind() { |
410 if (kind.tag != Tag.NUMERIC) { |
459 if (kind.tag != Tag.NUMERIC) { |