< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/parser/Tokens.java

Print this page




  67     public static Tokens instance(Context context) {
  68         Tokens instance = context.get(tokensKey);
  69         if (instance == null)
  70             instance = new Tokens(context);
  71         return instance;
  72     }
  73 
  74     protected Tokens(Context context) {
  75         context.put(tokensKey, this);
  76         names = Names.instance(context);
  77         for (TokenKind t : TokenKind.values()) {
  78             if (t.name != null)
  79                 enterKeyword(t.name, t);
  80             else
  81                 tokenName[t.ordinal()] = null;
  82         }
  83 
  84         key = new TokenKind[maxKey+1];
  85         for (int i = 0; i <= maxKey; i++) key[i] = TokenKind.IDENTIFIER;
  86         for (TokenKind t : TokenKind.values()) {
  87             if (t.name != null)
  88             key[tokenName[t.ordinal()].getIndex()] = t;
  89         }
  90     }
  91 
  92     private void enterKeyword(String s, TokenKind token) {
  93         Name n = names.fromString(s);
  94         tokenName[token.ordinal()] = n;
  95         if (n.getIndex() > maxKey) maxKey = n.getIndex();
  96     }
  97 
  98     /**
  99      * Create a new token given a name; if the name corresponds to a token name,
 100      * a new token of the corresponding kind is returned; otherwise, an
 101      * identifier token is returned.
 102      */
 103     TokenKind lookupKind(Name name) {
 104         return (name.getIndex() > maxKey) ? TokenKind.IDENTIFIER : key[name.getIndex()];
 105     }
 106 
 107     TokenKind lookupKind(String name) {
 108         return lookupKind(names.fromString(name));


 209         SLASH("/"),
 210         AMP("&"),
 211         BAR("|"),
 212         CARET("^"),
 213         PERCENT("%"),
 214         LTLT("<<"),
 215         GTGT(">>"),
 216         GTGTGT(">>>"),
 217         PLUSEQ("+="),
 218         SUBEQ("-="),
 219         STAREQ("*="),
 220         SLASHEQ("/="),
 221         AMPEQ("&="),
 222         BAREQ("|="),
 223         CARETEQ("^="),
 224         PERCENTEQ("%="),
 225         LTLTEQ("<<="),
 226         GTGTEQ(">>="),
 227         GTGTGTEQ(">>>="),
 228         MONKEYS_AT("@"),




 229         CUSTOM;
 230 
 231         public final String name;
 232         public final Tag tag;
 233 
 234         TokenKind() {
 235             this(null, Tag.DEFAULT);
 236         }
 237 
 238         TokenKind(String name) {
 239             this(name, Tag.DEFAULT);
 240         }
 241 
 242         TokenKind(Tag tag) {
 243             this(null, tag);
 244         }
 245 
 246         TokenKind(String name, Tag tag) {
 247             this.name = name;
 248             this.tag = tag;


 259             case INTLITERAL:
 260                 return "token.integer";
 261             case LONGLITERAL:
 262                 return "token.long-integer";
 263             case FLOATLITERAL:
 264                 return "token.float";
 265             case DOUBLELITERAL:
 266                 return "token.double";
 267             case ERROR:
 268                 return "token.bad-symbol";
 269             case EOF:
 270                 return "token.end-of-input";
 271             case DOT: case COMMA: case SEMI: case LPAREN: case RPAREN:
 272             case LBRACKET: case RBRACKET: case LBRACE: case RBRACE:
 273                 return "'" + name + "'";
 274             default:
 275                 return name;
 276             }
 277         }
 278 




 279         public String getKind() {
 280             return "Token";
 281         }
 282 
 283         public String toString(Locale locale, Messages messages) {
 284             return name != null ? toString() : messages.getLocalizedString(locale, "compiler.misc." + toString());
 285         }
 286 
 287         @Override
 288         public boolean accepts(TokenKind that) {
 289             return this == that;
 290         }
 291     }
 292 
 293     public interface Comment {
 294 
 295         enum CommentStyle {
 296             LINE,
 297             BLOCK,
 298             JAVADOC,
 299         }
 300 
 301         String getText();
 302         int getSourcePos(int index);
 303         CommentStyle getStyle();
 304         boolean isDeprecated();
 305     }
 306 
 307     /**
 308      * This is the class representing a javac token. Each token has several fields
 309      * that are set by the javac lexer (i.e. start/end position, string value, etc).
 310      */
 311     public static class Token {
 312 
 313         /** tags constants **/
 314         enum Tag {
 315             DEFAULT,
 316             NAMED,
 317             STRING,
 318             NUMERIC

 319         }
 320 
 321         /** The token kind */
 322         public final TokenKind kind;
 323 
 324         /** The start position of this token */
 325         public final int pos;
 326 
 327         /** The end position of this token */
 328         public final int endPos;
 329 
 330         /** Comment reader associated with this token */
 331         public final List<Comment> comments;
 332 
 333         Token(TokenKind kind, int pos, int endPos, List<Comment> comments) {
 334             this.kind = kind;
 335             this.pos = pos;
 336             this.endPos = endPos;
 337             this.comments = comments;
 338             checkKind();




  67     public static Tokens instance(Context context) {
  68         Tokens instance = context.get(tokensKey);
  69         if (instance == null)
  70             instance = new Tokens(context);
  71         return instance;
  72     }
  73 
  74     protected Tokens(Context context) {
  75         context.put(tokensKey, this);
  76         names = Names.instance(context);
  77         for (TokenKind t : TokenKind.values()) {
  78             if (t.name != null)
  79                 enterKeyword(t.name, t);
  80             else
  81                 tokenName[t.ordinal()] = null;
  82         }
  83 
  84         key = new TokenKind[maxKey+1];
  85         for (int i = 0; i <= maxKey; i++) key[i] = TokenKind.IDENTIFIER;
  86         for (TokenKind t : TokenKind.values()) {
  87             if (t.name != null && !t.reserved())
  88                 key[tokenName[t.ordinal()].getIndex()] = t;
  89         }
  90     }
  91 
  92     private void enterKeyword(String s, TokenKind token) {
  93         Name n = names.fromString(s);
  94         tokenName[token.ordinal()] = n;
  95         if (n.getIndex() > maxKey) maxKey = n.getIndex();
  96     }
  97 
  98     /**
  99      * Create a new token given a name; if the name corresponds to a token name,
 100      * a new token of the corresponding kind is returned; otherwise, an
 101      * identifier token is returned.
 102      */
 103     TokenKind lookupKind(Name name) {
 104         return (name.getIndex() > maxKey) ? TokenKind.IDENTIFIER : key[name.getIndex()];
 105     }
 106 
 107     TokenKind lookupKind(String name) {
 108         return lookupKind(names.fromString(name));


 209         SLASH("/"),
 210         AMP("&"),
 211         BAR("|"),
 212         CARET("^"),
 213         PERCENT("%"),
 214         LTLT("<<"),
 215         GTGT(">>"),
 216         GTGTGT(">>>"),
 217         PLUSEQ("+="),
 218         SUBEQ("-="),
 219         STAREQ("*="),
 220         SLASHEQ("/="),
 221         AMPEQ("&="),
 222         BAREQ("|="),
 223         CARETEQ("^="),
 224         PERCENTEQ("%="),
 225         LTLTEQ("<<="),
 226         GTGTEQ(">>="),
 227         GTGTGTEQ(">>>="),
 228         MONKEYS_AT("@"),
 229         VAR("var", Tag.RESERVED),
 230         RECORD("record", Tag.RESERVED),
 231         SEALED("sealed", Tag.RESERVED),
 232         PERMITS("permits", Tag.RESERVED),
 233         CUSTOM;
 234 
 235         public final String name;
 236         public final Tag tag;
 237 
 238         TokenKind() {
 239             this(null, Tag.DEFAULT);
 240         }
 241 
 242         TokenKind(String name) {
 243             this(name, Tag.DEFAULT);
 244         }
 245 
 246         TokenKind(Tag tag) {
 247             this(null, tag);
 248         }
 249 
 250         TokenKind(String name, Tag tag) {
 251             this.name = name;
 252             this.tag = tag;


 263             case INTLITERAL:
 264                 return "token.integer";
 265             case LONGLITERAL:
 266                 return "token.long-integer";
 267             case FLOATLITERAL:
 268                 return "token.float";
 269             case DOUBLELITERAL:
 270                 return "token.double";
 271             case ERROR:
 272                 return "token.bad-symbol";
 273             case EOF:
 274                 return "token.end-of-input";
 275             case DOT: case COMMA: case SEMI: case LPAREN: case RPAREN:
 276             case LBRACKET: case RBRACKET: case LBRACE: case RBRACE:
 277                 return "'" + name + "'";
 278             default:
 279                 return name;
 280             }
 281         }
 282 
 283         public boolean reserved() {
 284             return tag == Tag.RESERVED;
 285         }
 286 
 287         public String getKind() {
 288             return "Token";
 289         }
 290 
 291         public String toString(Locale locale, Messages messages) {
 292             return name != null ? toString() : messages.getLocalizedString(locale, "compiler.misc." + toString());
 293         }
 294 
 295         @Override
 296         public boolean accepts(TokenKind that) {
 297             return this == that;
 298         }
 299     }
 300 
 301     public interface Comment {
 302 
 303         enum CommentStyle {
 304             LINE,
 305             BLOCK,
 306             JAVADOC,
 307         }
 308 
 309         String getText();
 310         int getSourcePos(int index);
 311         CommentStyle getStyle();
 312         boolean isDeprecated();
 313     }
 314 
 315     /**
 316      * This is the class representing a javac token. Each token has several fields
 317      * that are set by the javac lexer (i.e. start/end position, string value, etc).
 318      */
 319     public static class Token {
 320 
 321         /** tags constants **/
 322         enum Tag {
 323             DEFAULT,
 324             NAMED,
 325             STRING,
 326             NUMERIC,
 327             RESERVED;
 328         }
 329 
 330         /** The token kind */
 331         public final TokenKind kind;
 332 
 333         /** The start position of this token */
 334         public final int pos;
 335 
 336         /** The end position of this token */
 337         public final int endPos;
 338 
 339         /** Comment reader associated with this token */
 340         public final List<Comment> comments;
 341 
 342         Token(TokenKind kind, int pos, int endPos, List<Comment> comments) {
 343             this.kind = kind;
 344             this.pos = pos;
 345             this.endPos = endPos;
 346             this.comments = comments;
 347             checkKind();


< prev index next >