~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to java/lexer/src/org/netbeans/lib/java/lexer/JavaStringLexer.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.lib.java.lexer;
 
43
 
 
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;
 
50
 
 
51
/**
 
52
 * Lexical analyzer for java string language.
 
53
 *
 
54
 * @author Miloslav Metelka
 
55
 * @version 1.00
 
56
 */
 
57
 
 
58
public class JavaStringLexer implements Lexer<JavaStringTokenId> {
 
59
 
 
60
    private static final int EOF = LexerInput.EOF;
 
61
 
 
62
    private LexerInput input;
 
63
    
 
64
    private TokenFactory<JavaStringTokenId> tokenFactory;
 
65
    
 
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
 
70
    }
 
71
    
 
72
    public Object state() {
 
73
        return null;
 
74
    }
 
75
    
 
76
    public Token<JavaStringTokenId> nextToken() {
 
77
        while(true) {
 
78
            int ch = input.read();
 
79
            switch (ch) {
 
80
                case EOF:
 
81
                    if (input.readLength() > 0)
 
82
                        return token(JavaStringTokenId.TEXT);
 
83
                    else
 
84
                        return null;
 
85
                case '\\': //NOI18N
 
86
                    if (input.readLength() > 1) {// already read some text
 
87
                        input.backup(1);
 
88
                        return tokenFactory.createToken(JavaStringTokenId.TEXT, input.readLength());
 
89
                    }
 
90
                    switch (ch = input.read()) {
 
91
                        case 'b': //NOI18N
 
92
                            return token(JavaStringTokenId.BACKSPACE);
 
93
                        case 'f': //NOI18N
 
94
                            return token(JavaStringTokenId.FORM_FEED);
 
95
                        case 'n': //NOI18N
 
96
                            return token(JavaStringTokenId.NEWLINE);
 
97
                        case 'r': //NOI18N
 
98
                            return token(JavaStringTokenId.CR);
 
99
                        case 't': //NOI18N
 
100
                            return token(JavaStringTokenId.TAB);
 
101
                        case '\'': //NOI18N
 
102
                            return token(JavaStringTokenId.SINGLE_QUOTE);
 
103
                        case '"': //NOI18N
 
104
                            return token(JavaStringTokenId.DOUBLE_QUOTE);
 
105
                        case '\\': //NOI18N
 
106
                            return token(JavaStringTokenId.BACKSLASH);
 
107
                        case 'u': //NOI18N
 
108
                            while ('u' == (ch = input.read())) {}; //NOI18N
 
109
                            
 
110
                            for(int i = 0; ; i++) {
 
111
                                ch = Character.toLowerCase(ch);
 
112
                                
 
113
                                if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f')) { //NOI18N
 
114
                                    input.backup(1);
 
115
                                    return token(JavaStringTokenId.UNICODE_ESCAPE_INVALID);
 
116
                                }
 
117
                             
 
118
                                if (i == 3) { // four digits checked, valid sequence
 
119
                                    return token(JavaStringTokenId.UNICODE_ESCAPE);
 
120
                                }
 
121
                                
 
122
                                ch = input.read();
 
123
                            }
 
124
                            
 
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);
 
133
                                    }
 
134
                                    input.backup(1);
 
135
                                    return token(JavaStringTokenId.OCTAL_ESCAPE_INVALID);
 
136
                            }
 
137
                            input.backup(1);
 
138
                            return token(JavaStringTokenId.OCTAL_ESCAPE_INVALID);
 
139
                    }
 
140
                    input.backup(1);
 
141
                    return token(JavaStringTokenId.ESCAPE_SEQUENCE_INVALID);
 
142
            } // end of switch (ch)
 
143
        } // end of while(true)
 
144
    }
 
145
 
 
146
    private Token<JavaStringTokenId> token(JavaStringTokenId id) {
 
147
        return tokenFactory.createToken(id);
 
148
    }
 
149
 
 
150
    public void release() {
 
151
    }
 
152
 
 
153
}