KickJava   Java API By Example, From Geeks To Geeks.

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


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 simple string language.
30  *
31  * @author Miloslav Metelka
32  * @version 1.00
33  */

34
35 public class SimpleStringLexer implements Lexer<SimpleStringTokenId> {
36
37     private static final int EOF = LexerInput.EOF;
38
39     private LexerInput input;
40
41     private TokenFactory<SimpleStringTokenId> tokenFactory;
42     
43     public SimpleStringLexer(LexerRestartInfo<SimpleStringTokenId> 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<SimpleStringTokenId> nextToken() {
54         while(true) {
55             int ch = input.read();
56             switch (ch) {
57                 case EOF:
58                     if (input.readLength() > 0)
59                         return token(SimpleStringTokenId.TEXT);
60                     else
61                         return null;
62                 case '\\':
63                     if (input.readLength() > 1) {// already read some text
64
input.backup(1);
65                         return tokenFactory.createToken(SimpleStringTokenId.TEXT, input.readLength());
66                     }
67                     switch (ch = input.read()) {
68                         case 'b':
69                             return token(SimpleStringTokenId.BACKSPACE);
70                         case 'f':
71                             return token(SimpleStringTokenId.FORM_FEED);
72                         case 'n':
73                             return token(SimpleStringTokenId.NEWLINE);
74                         case 't':
75                             return token(SimpleStringTokenId.TAB);
76                         case '\'':
77                             return token(SimpleStringTokenId.SINGLE_QUOTE);
78                         case '"':
79                             return token(SimpleStringTokenId.DOUBLE_QUOTE);
80                         case '\\':
81                             return token(SimpleStringTokenId.BACKSLASH);
82                         case '0': case '1': case '2': case '3':
83                             switch (input.read()) {
84                                 case '0': case '1': case '2': case '3':
85                                 case '4': case '5': case '6': case '7':
86                                     switch (input.read()) {
87                                         case '0': case '1': case '2': case '3':
88                                         case '4': case '5': case '6': case '7':
89                                             return token(SimpleStringTokenId.OCTAL_ESCAPE);
90                                     }
91                                     return token(SimpleStringTokenId.OCTAL_ESCAPE_INVALID);
92                             }
93                             return token(SimpleStringTokenId.OCTAL_ESCAPE_INVALID);
94                     }
95                     return token(SimpleStringTokenId.ESCAPE_SEQUENCE_INVALID);
96             } // end of switch (ch)
97
} // end of while(true)
98
}
99
100     private Token<SimpleStringTokenId> token(SimpleStringTokenId id) {
101         return tokenFactory.createToken(id);
102     }
103     
104     public void release() {
105     }
106
107 }
108
Popular Tags