KickJava   Java API By Example, From Geeks To Geeks.

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


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.JavaStringTokenId;
23 import org.netbeans.api.java.lexer.JavaTokenId;
24 import org.netbeans.api.lexer.Token;
25 import org.netbeans.spi.lexer.Lexer;
26 import org.netbeans.spi.lexer.LexerInput;
27 import org.netbeans.spi.lexer.LexerRestartInfo;
28 import org.netbeans.spi.lexer.TokenFactory;
29
30 /**
31  * Lexical analyzer for java string language.
32  *
33  * @author Miloslav Metelka
34  * @version 1.00
35  */

36
37 public class JavaStringLexer implements Lexer<JavaStringTokenId> {
38
39     private static final int EOF = LexerInput.EOF;
40
41     private LexerInput input;
42     
43     private TokenFactory<JavaStringTokenId> tokenFactory;
44     
45     public JavaStringLexer(LexerRestartInfo<JavaStringTokenId> info) {
46         this.input = info.input();
47         this.tokenFactory = info.tokenFactory();
48         assert (info.state() == null); // passed argument always null
49
}
50     
51     public Object JavaDoc state() {
52         return null;
53     }
54     
55     public Token<JavaStringTokenId> nextToken() {
56         while(true) {
57             int ch = input.read();
58             switch (ch) {
59                 case EOF:
60                     if (input.readLength() > 0)
61                         return token(JavaStringTokenId.TEXT);
62                     else
63                         return null;
64                 case '\\':
65                     if (input.readLength() > 1) {// already read some text
66
input.backup(1);
67                         return tokenFactory.createToken(JavaStringTokenId.TEXT, input.readLength());
68                     }
69                     switch (ch = input.read()) {
70                         case 'b':
71                             return token(JavaStringTokenId.BACKSPACE);
72                         case 'f':
73                             return token(JavaStringTokenId.FORM_FEED);
74                         case 'n':
75                             return token(JavaStringTokenId.NEWLINE);
76                         case 'r':
77                             return token(JavaStringTokenId.CR);
78                         case 't':
79                             return token(JavaStringTokenId.TAB);
80                         case '\'':
81                             return token(JavaStringTokenId.SINGLE_QUOTE);
82                         case '"':
83                             return token(JavaStringTokenId.DOUBLE_QUOTE);
84                         case '\\':
85                             return token(JavaStringTokenId.BACKSLASH);
86                         case '0': case '1': case '2': case '3':
87                             switch (input.read()) {
88                                 case '0': case '1': case '2': case '3':
89                                 case '4': case '5': case '6': case '7':
90                                     switch (input.read()) {
91                                         case '0': case '1': case '2': case '3':
92                                         case '4': case '5': case '6': case '7':
93                                             return token(JavaStringTokenId.OCTAL_ESCAPE);
94                                     }
95                                     return token(JavaStringTokenId.OCTAL_ESCAPE_INVALID);
96                             }
97                             return token(JavaStringTokenId.OCTAL_ESCAPE_INVALID);
98                     }
99                     return token(JavaStringTokenId.ESCAPE_SEQUENCE_INVALID);
100             } // end of switch (ch)
101
} // end of while(true)
102
}
103
104     private Token<JavaStringTokenId> token(JavaStringTokenId id) {
105         return tokenFactory.createToken(id);
106     }
107
108     public void release() {
109     }
110
111 }
112
Popular Tags