KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > test > simple > SimpleJavadocLexer


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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

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

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