KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > text > syntax > javacc > lib > StringParserInput


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 package org.netbeans.modules.css.text.syntax.javacc.lib;
20
21 /**
22  * Support for JavaCC version 1.1. When JavaCC is required to read directly
23  * from string or char[].
24  * <p>
25  * Added support for JavaCC 3.2 generated TokenManagers: extends SimpleCharStream.
26  *
27  * @author Petr Kuzel
28  */

29 public class StringParserInput extends SimpleCharStream implements CharStream {
30     /** the buffer */
31     private char[] buffer;
32
33     /** the position in the buffer*/
34     private int pos;
35
36     /** Begin of current token, for backup operation */
37     private int begin;
38
39     /** Length of whole buffer */
40     private int len;
41     
42     /** buffer end. */
43     private int end;
44
45     public StringParserInput() {}
46
47     
48     public void setString(String JavaDoc s) {
49         buffer = s.toCharArray();
50         begin = pos = 0;
51         len = s.length();
52         end = len;
53     }
54     
55     /** Share buffer with e.g. syntax coloring. */
56     public void setBuffer(char[] buf, int offset, int len) {
57         buffer = buf;
58         begin = pos = offset;
59         this.len = len;
60         end = offset + len;
61     }
62
63     /**
64      * Returns the next character from the selected input. The method
65      * of selecting the input is the responsibility of the class
66      * implementing this interface. Can throw any java.io.IOException.
67      */

68     public char readChar() throws java.io.IOException JavaDoc {
69         if (pos >= end)
70             throw new java.io.EOFException JavaDoc ();
71         return buffer[pos++];
72     }
73
74     /**
75      * Returns the column position of the character last read.
76      * @deprecated
77      * @see #getEndColumn
78      */

79     public int getColumn() {
80         return 0;
81     }
82
83     /**
84      * Returns the line number of the character last read.
85      * @deprecated
86      * @see #getEndLine
87      */

88     public int getLine() {
89         return 0;
90     }
91
92     /**
93      * Returns the column number of the last character for current token (being
94      * matched after the last call to BeginTOken).
95      */

96     public int getEndColumn() {
97         return 0;
98     }
99
100     /**
101      * Returns the line number of the last character for current token (being
102      * matched after the last call to BeginTOken).
103      */

104     public int getEndLine() {
105         return 0;
106     }
107
108     /**
109      * Returns the column number of the first character for current token (being
110      * matched after the last call to BeginTOken).
111      */

112     public int getBeginColumn() {
113         return 0;
114     }
115
116     /**
117      * Returns the line number of the first character for current token (being
118      * matched after the last call to BeginTOken).
119      */

120     public int getBeginLine() {
121         return 0;
122     }
123
124     /**
125      * Backs up the input stream by amount steps. Lexer calls this method if it
126      * had already read some characters, but could not use them to match a
127      * (longer) token. So, they will be used again as the prefix of the next
128      * token and it is the implemetation's responsibility to do this right.
129      */

130     public void backup(int amount) {
131         if (pos > 1)
132             pos -= amount;
133     }
134
135     /**
136      * Returns the next character that marks the beginning of the next token.
137      * All characters must remain in the buffer between two successive calls
138      * to this method to implement backup correctly.
139      */

140     public char BeginToken() throws java.io.IOException JavaDoc {
141         begin = pos;
142         return readChar ();
143     }
144
145     /**
146      * Returns a string made up of characters from the marked token beginning
147      * to the current buffer position. Implementations have the choice of returning
148      * anything that they want to. For example, for efficiency, one might decide
149      * to just return null, which is a valid implementation.
150      */

151     public String JavaDoc GetImage() {
152         return new String JavaDoc(buffer, begin, pos-begin);
153     }
154
155     
156     /** @return token length. */
157     public int getLength() {
158         return pos - begin;
159     }
160     
161     /**
162      * Returns an array of characters that make up the suffix of length 'len' for
163      * the currently matched token. This is used to build up the matched string
164      * for use in actions in the case of MORE. A simple and inefficient
165      * implementation of this is as follows :
166      *
167      * {
168      * String t = GetImage();
169      * return t.substring(t.length() - len, t.length()).toCharArray();
170      * }
171      */

172     public char[] GetSuffix(int l) {
173         char[] ret = new char[l];
174         System.arraycopy(buffer, pos - l, ret, 0, l);
175         return ret;
176     }
177
178     /**
179      * The lexer calls this function to indicate that it is done with the stream
180      * and hence implementations can free any resources held by this class.
181      * Again, the body of this function can be just empty and it will not
182      * affect the lexer's operation.
183      */

184     public void Done() {
185     }
186
187     public String JavaDoc toString() {
188         return "StringParserInput\n Pos:" + pos + " len:" + len + " #################\n" + buffer; // NOI18N
189
}
190 }
191
Popular Tags