KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

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

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

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

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

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

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

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

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

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

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

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