]>
gcc.gnu.org Git - gcc.git/blob - libjava/java/io/StreamTokenizer.java
1 /* Copyright (C) 1998, 1999 Cygnus Solutions
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 * @author Warren Levy <warrenl@cygnus.com>
13 * @date October 25, 1998.
15 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
16 * "The Java Language Specification", ISBN 0-201-63451-1
17 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
18 * Status: Believed complete and correct.
21 public class StreamTokenizer
23 /* A constant indicating that the end of the stream has been read. */
24 public static final int TT_EOF
= -1;
26 /* A constant indicating that the end of the line has been read. */
27 public static final int TT_EOL
= '\n';
29 /* A constant indicating that a number token has been read. */
30 public static final int TT_NUMBER
= -2;
32 /* A constant indicating that a word token has been read. */
33 public static final int TT_WORD
= -3;
35 /* Contains the type of the token read resulting from a call to nextToken. */
38 /* The String associated with word and string tokens. */
41 /* The numeric value associated with number tokens. */
44 /* Indicates whether end-of-line is recognized as a token. */
45 private boolean eolSignificant
= false;
47 /* Indicates whether word tokens are automatically made lower case. */
48 private boolean lowerCase
= false;
50 /* Indicates whether C++ style comments are recognized and skipped. */
51 private boolean slashSlash
= false;
53 /* Indicates whether C style comments are recognized and skipped. */
54 private boolean slashStar
= false;
56 /* Attribute tables of each byte from 0x00 to 0xFF. */
57 private boolean[] whitespace
;
58 private boolean[] alphabetic
;
59 private boolean[] numeric
;
60 private boolean[] quote
;
61 private boolean[] comment
;
63 /* The Reader associated with this class. */
64 private PushbackReader in
;
66 /* Indicates if a token has been pushed back. */
67 private boolean pushedBack
= false;
69 /* Contains the current line number of the reader. */
70 private int lineNumber
= 1;
72 // Deprecated in JDK 1.1.
73 public StreamTokenizer(InputStream is
)
75 this(new InputStreamReader(is
));
78 public StreamTokenizer(Reader r
)
80 in
= new PushbackReader(r
);
82 whitespace
= new boolean[256];
83 alphabetic
= new boolean[256];
84 numeric
= new boolean[256];
85 quote
= new boolean[256];
86 comment
= new boolean[256];
87 for (int i
= 0; i
< 256; i
++)
90 whitespaceChars(0x00, 0x20);
93 wordChars(0xA0, 0xFF);
100 public void commentChar(int ch
)
102 if (ch
>= 0 && ch
<= 255)
106 public void eolIsSignificant(boolean flag
)
108 eolSignificant
= flag
;
116 public void lowerCaseMode(boolean flag
)
121 private boolean isWhitespace(int ch
)
123 if (ch
>= 0 && ch
<= 255)
124 return whitespace
[ch
];
129 private boolean isAlphabetic(int ch
)
131 if (ch
>= 0 && ch
<= 255)
132 return alphabetic
[ch
];
139 private boolean isNumeric(int ch
)
141 if (ch
>= 0 && ch
<= 255)
147 private boolean isQuote(int ch
)
149 if (ch
>= 0 && ch
<= 255)
155 private boolean isComment(int ch
)
157 if (ch
>= 0 && ch
<= 255)
163 public int nextToken() throws IOException
174 // Skip whitespace. Deal with EOL along the way.
175 while (isWhitespace(ch
= in
.read()))
176 if (ch
== '\n' || ch
== '\r')
180 // Throw away \n if in combination with \r.
181 if (ch
== '\r' && (ch
= in
.read()) != '\n')
184 return (ttype
= TT_EOL
);
189 else if (isNumeric(ch
))
193 // Read ahead to see if this is an ordinary '-' rather than numeric.
196 if (isNumeric(ch
) && ch
!= '-')
199 return (ttype
= '-');
202 StringBuffer tokbuf
= new StringBuffer();
203 tokbuf
.append((char) ch
);
206 while (isNumeric(ch
= in
.read()) && ch
!= '-')
207 if (ch
== '.' && decCount
++ > 0)
210 tokbuf
.append((char) ch
);
214 nval
= Double
.valueOf(tokbuf
.toString()).doubleValue();
216 else if (isAlphabetic(ch
))
218 StringBuffer tokbuf
= new StringBuffer();
219 tokbuf
.append((char) ch
);
220 while (isAlphabetic(ch
= in
.read()) || isNumeric(ch
))
221 tokbuf
.append((char) ch
);
224 sval
= tokbuf
.toString();
228 else if (isComment(ch
))
230 while ((ch
= in
.read()) != '\n' && ch
!= '\r' && ch
!= TT_EOF
)
233 return nextToken(); // Recursive, but not too deep in normal cases.
235 else if (isQuote(ch
))
238 StringBuffer tokbuf
= new StringBuffer();
239 while ((ch
= in
.read()) != ttype
&& ch
!= '\n' && ch
!= '\r' &&
243 switch (ch
= in
.read())
265 if ((nextch
= ch1
= ch
) >= '0' && ch
<= '7')
268 if ((nextch
= in
.read()) >= '0' && nextch
<= '7')
270 ch
= ch
* 8 + nextch
- '0';
271 if ((nextch
= in
.read()) >= '0' && nextch
<= '7' &&
272 ch1
>= '0' && ch1
<= '3')
274 ch
= ch
* 8 + nextch
- '0';
283 tokbuf
.append((char) ch
);
286 // Throw away matching quote char.
290 sval
= tokbuf
.toString();
295 if ((ch
= in
.read()) == '/' && slashSlash
)
297 while ((ch
= in
.read()) != '\n' && ch
!= '\r' && ch
!= TT_EOF
)
300 return nextToken(); // Recursive, but not too deep in normal cases
302 else if (ch
== '*' && slashStar
)
308 if ((ch
= in
.read()) == '/')
312 else if (ch
== '\n' || ch
== '\r')
315 if (ch
== '\r' && (ch
= in
.read()) != '\n')
318 else if (ch
== TT_EOF
)
324 return nextToken(); // Recursive, but not too deep in normal cases
338 private void resetChar(int ch
)
340 whitespace
[ch
] = alphabetic
[ch
] = numeric
[ch
] = quote
[ch
] = comment
[ch
] =
344 public void ordinaryChar(int ch
)
346 if (ch
>= 0 && ch
<= 255)
350 public void ordinaryChars(int low
, int hi
)
356 for (int i
= low
; i
<= hi
; i
++)
360 public void parseNumbers()
362 for (int i
= 0; i
<= 9; i
++)
363 numeric
['0' + i
] = true;
369 public void pushBack()
371 // pushBack may cause the lineno method to return an incorrect value
372 // if lineno is called before the next call to nextToken.
376 public void quoteChar(int ch
)
378 if (ch
>= 0 && ch
<= 255)
382 public void resetSyntax()
384 ordinaryChars(0x00, 0xFF);
387 public void slashSlashComments(boolean flag
)
392 public void slashStarComments(boolean flag
)
397 public String
toString()
402 else if (ttype
== TT_EOL
)
404 else if (ttype
== TT_WORD
)
406 else if (ttype
== TT_NUMBER
)
407 tempstr
= "n=" + Double
.toString(nval
);
408 else // must be an ordinary char.
409 tempstr
= "\'" + (new Character((char) ttype
)).toString() + "\'";
411 return "Token[" + tempstr
+ "], line " + Integer
.toString(lineno());
414 public void whitespaceChars(int low
, int hi
)
420 for (int i
= low
; i
<= hi
; i
++)
421 whitespace
[i
] = true;
424 public void wordChars(int low
, int hi
)
430 for (int i
= low
; i
<= hi
; i
++)
431 alphabetic
[i
] = true;
This page took 0.053403 seconds and 5 git commands to generate.