2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.lib.java.lexer;
44
import org.netbeans.api.java.lexer.JavaStringTokenId;
45
import org.netbeans.api.lexer.Token;
46
import org.netbeans.spi.lexer.Lexer;
47
import org.netbeans.spi.lexer.LexerInput;
48
import org.netbeans.spi.lexer.LexerRestartInfo;
49
import org.netbeans.spi.lexer.TokenFactory;
52
* Lexical analyzer for java string language.
54
* @author Miloslav Metelka
58
public class JavaStringLexer implements Lexer<JavaStringTokenId> {
60
private static final int EOF = LexerInput.EOF;
62
private LexerInput input;
64
private TokenFactory<JavaStringTokenId> tokenFactory;
66
public JavaStringLexer(LexerRestartInfo<JavaStringTokenId> info) {
67
this.input = info.input();
68
this.tokenFactory = info.tokenFactory();
69
assert (info.state() == null); // passed argument always null
72
public Object state() {
76
public Token<JavaStringTokenId> nextToken() {
78
int ch = input.read();
81
if (input.readLength() > 0)
82
return token(JavaStringTokenId.TEXT);
86
if (input.readLength() > 1) {// already read some text
88
return tokenFactory.createToken(JavaStringTokenId.TEXT, input.readLength());
90
switch (ch = input.read()) {
92
return token(JavaStringTokenId.BACKSPACE);
94
return token(JavaStringTokenId.FORM_FEED);
96
return token(JavaStringTokenId.NEWLINE);
98
return token(JavaStringTokenId.CR);
100
return token(JavaStringTokenId.TAB);
102
return token(JavaStringTokenId.SINGLE_QUOTE);
104
return token(JavaStringTokenId.DOUBLE_QUOTE);
106
return token(JavaStringTokenId.BACKSLASH);
108
while ('u' == (ch = input.read())) {}; //NOI18N
110
for(int i = 0; ; i++) {
111
ch = Character.toLowerCase(ch);
113
if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f')) { //NOI18N
115
return token(JavaStringTokenId.UNICODE_ESCAPE_INVALID);
118
if (i == 3) { // four digits checked, valid sequence
119
return token(JavaStringTokenId.UNICODE_ESCAPE);
125
case '0': case '1': case '2': case '3': //NOI18N
126
switch (input.read()) {
127
case '0': case '1': case '2': case '3': //NOI18N
128
case '4': case '5': case '6': case '7': //NOI18N
129
switch (input.read()) {
130
case '0': case '1': case '2': case '3': //NOI18N
131
case '4': case '5': case '6': case '7': //NOI18N
132
return token(JavaStringTokenId.OCTAL_ESCAPE);
135
return token(JavaStringTokenId.OCTAL_ESCAPE_INVALID);
138
return token(JavaStringTokenId.OCTAL_ESCAPE_INVALID);
141
return token(JavaStringTokenId.ESCAPE_SEQUENCE_INVALID);
142
} // end of switch (ch)
143
} // end of while(true)
146
private Token<JavaStringTokenId> token(JavaStringTokenId id) {
147
return tokenFactory.createToken(id);
150
public void release() {