KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > java > lexer > JavadocLexer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.java.lexer;
21
22 import org.netbeans.api.java.lexer.JavadocTokenId;
23 import org.netbeans.api.lexer.Token;
24 import org.netbeans.spi.lexer.Lexer;
25 import org.netbeans.spi.lexer.LexerInput;
26 import org.netbeans.spi.lexer.LexerRestartInfo;
27 import org.netbeans.spi.lexer.TokenFactory;
28
29 /**
30  * Lexical analyzer for javadoc language.
31  *
32  * @author Miloslav Metelka
33  * @version 1.00
34  */

35
36 public class JavadocLexer implements Lexer<JavadocTokenId> {
37
38     private static final int EOF = LexerInput.EOF;
39
40     private LexerInput input;
41     
42     private TokenFactory<JavadocTokenId> tokenFactory;
43     
44     public JavadocLexer(LexerRestartInfo<JavadocTokenId> info) {
45         this.input = info.input();
46         this.tokenFactory = info.tokenFactory();
47         assert (info.state() == null); // passed argument always null
48
}
49     
50     public Object JavaDoc state() {
51         return null;
52     }
53     
54     public Token<JavadocTokenId> nextToken() {
55         int ch = input.read();
56         
57         if (ch == EOF) {
58             return null;
59         }
60         
61         if (Character.isJavaIdentifierStart(ch)) {
62             //TODO: EOF
63
while (Character.isJavaIdentifierPart(input.read()))
64                 ;
65             
66             input.backup(1);
67             return token(JavadocTokenId.IDENT);
68         }
69         
70         if ("@<.#".indexOf(ch) == (-1)) {
71             //TODO: EOF
72
ch = input.read();
73             
74             while (!Character.isJavaIdentifierStart(ch) && "@<.#".indexOf(ch) == (-1) && ch != EOF)
75                 ch = input.read();
76             
77             if (ch != EOF)
78                 input.backup(1);
79             return token(JavadocTokenId.OTHER_TEXT);
80         }
81         
82         switch (ch) {
83             case '@':
84                 while (true) {
85                     ch = input.read();
86                     
87                     if (!Character.isLetter(ch)) {
88                         input.backup(1);
89                         return tokenFactory.createToken(JavadocTokenId.TAG, input.readLength());
90                     }
91                 }
92             case '<':
93                 while (true) {
94                     ch = input.read();
95                     if (ch == '>' || ch == EOF) {
96                         return token(JavadocTokenId.HTML_TAG);
97                     }
98                 }
99             case '.':
100                 return token(JavadocTokenId.DOT);
101             case '#':
102                 return token(JavadocTokenId.HASH);
103         } // end of switch (ch)
104

105         assert false;
106         
107         return null;
108     }
109
110     private Token<JavadocTokenId> token(JavadocTokenId id) {
111         return tokenFactory.createToken(id);
112     }
113
114     public void release() {
115     }
116
117 }
118
Popular Tags