1. /*
  2. * @(#)StreamTokenizer.java 1.39 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.io;
  8. /**
  9. * The <code>StreamTokenizer</code> class takes an input stream and
  10. * parses it into "tokens", allowing the tokens to be
  11. * read one at a time. The parsing process is controlled by a table
  12. * and a number of flags that can be set to various states. The
  13. * stream tokenizer can recognize identifiers, numbers, quoted
  14. * strings, and various comment styles.
  15. * <p>
  16. * Each byte read from the input stream is regarded as a character
  17. * in the range <code>'\u0000'</code> through <code>'\u00FF'</code>.
  18. * The character value is used to look up five possible attributes of
  19. * the character: <i>white space</i>, <i>alphabetic</i>,
  20. * <i>numeric</i>, <i>string quote</i>, and <i>comment character</i>.
  21. * Each character can have zero or more of these attributes.
  22. * <p>
  23. * In addition, an instance has four flags. These flags indicate:
  24. * <ul>
  25. * <li>Whether line terminators are to be returned as tokens or treated
  26. * as white space that merely separates tokens.
  27. * <li>Whether C-style comments are to be recognized and skipped.
  28. * <li>Whether C++-style comments are to be recognized and skipped.
  29. * <li>Whether the characters of identifiers are converted to lowercase.
  30. * </ul>
  31. * <p>
  32. * A typical application first constructs an instance of this class,
  33. * sets up the syntax tables, and then repeatedly loops calling the
  34. * <code>nextToken</code> method in each iteration of the loop until
  35. * it returns the value <code>TT_EOF</code>.
  36. *
  37. * @author James Gosling
  38. * @version 1.39, 01/23/03
  39. * @see java.io.StreamTokenizer#nextToken()
  40. * @see java.io.StreamTokenizer#TT_EOF
  41. * @since JDK1.0
  42. */
  43. public class StreamTokenizer {
  44. /* Only one of these will be non-null */
  45. private Reader reader = null;
  46. private InputStream input = null;
  47. private char buf[] = new char[20];
  48. /**
  49. * The next character to be considered by the nextToken method. May also
  50. * be NEED_CHAR to indicate that a new character should be read, or SKIP_LF
  51. * to indicate that a new character should be read and, if it is a '\n'
  52. * character, it should be discarded and a second new character should be
  53. * read.
  54. */
  55. private int peekc = NEED_CHAR;
  56. private static final int NEED_CHAR = Integer.MAX_VALUE;
  57. private static final int SKIP_LF = Integer.MAX_VALUE - 1;
  58. private boolean pushedBack;
  59. private boolean forceLower;
  60. /** The line number of the last token read */
  61. private int LINENO = 1;
  62. private boolean eolIsSignificantP = false;
  63. private boolean slashSlashCommentsP = false;
  64. private boolean slashStarCommentsP = false;
  65. private byte ctype[] = new byte[256];
  66. private static final byte CT_WHITESPACE = 1;
  67. private static final byte CT_DIGIT = 2;
  68. private static final byte CT_ALPHA = 4;
  69. private static final byte CT_QUOTE = 8;
  70. private static final byte CT_COMMENT = 16;
  71. /**
  72. * After a call to the <code>nextToken</code> method, this field
  73. * contains the type of the token just read. For a single character
  74. * token, its value is the single character, converted to an integer.
  75. * For a quoted string token (see , its value is the quote character.
  76. * Otherwise, its value is one of the following:
  77. * <ul>
  78. * <li><code>TT_WORD</code> indicates that the token is a word.
  79. * <li><code>TT_NUMBER</code> indicates that the token is a number.
  80. * <li><code>TT_EOL</code> indicates that the end of line has been read.
  81. * The field can only have this value if the
  82. * <code>eolIsSignificant</code> method has been called with the
  83. * argument <code>true</code>.
  84. * <li><code>TT_EOF</code> indicates that the end of the input stream
  85. * has been reached.
  86. * </ul>
  87. * <p>
  88. * The initial value of this field is -4.
  89. *
  90. * @see java.io.StreamTokenizer#eolIsSignificant(boolean)
  91. * @see java.io.StreamTokenizer#nextToken()
  92. * @see java.io.StreamTokenizer#quoteChar(int)
  93. * @see java.io.StreamTokenizer#TT_EOF
  94. * @see java.io.StreamTokenizer#TT_EOL
  95. * @see java.io.StreamTokenizer#TT_NUMBER
  96. * @see java.io.StreamTokenizer#TT_WORD
  97. */
  98. public int ttype = TT_NOTHING;
  99. /**
  100. * A constant indicating that the end of the stream has been read.
  101. */
  102. public static final int TT_EOF = -1;
  103. /**
  104. * A constant indicating that the end of the line has been read.
  105. */
  106. public static final int TT_EOL = '\n';
  107. /**
  108. * A constant indicating that a number token has been read.
  109. */
  110. public static final int TT_NUMBER = -2;
  111. /**
  112. * A constant indicating that a word token has been read.
  113. */
  114. public static final int TT_WORD = -3;
  115. /* A constant indicating that no token has been read, used for
  116. * initializing ttype. FIXME This could be made public and
  117. * made available as the part of the API in a future release.
  118. */
  119. private static final int TT_NOTHING = -4;
  120. /**
  121. * If the current token is a word token, this field contains a
  122. * string giving the characters of the word token. When the current
  123. * token is a quoted string token, this field contains the body of
  124. * the string.
  125. * <p>
  126. * The current token is a word when the value of the
  127. * <code>ttype</code> field is <code>TT_WORD</code>. The current token is
  128. * a quoted string token when the value of the <code>ttype</code> field is
  129. * a quote character.
  130. * <p>
  131. * The initial value of this field is null.
  132. *
  133. * @see java.io.StreamTokenizer#quoteChar(int)
  134. * @see java.io.StreamTokenizer#TT_WORD
  135. * @see java.io.StreamTokenizer#ttype
  136. */
  137. public String sval;
  138. /**
  139. * If the current token is a number, this field contains the value
  140. * of that number. The current token is a number when the value of
  141. * the <code>ttype</code> field is <code>TT_NUMBER</code>.
  142. * <p>
  143. * The initial value of this field is 0.0.
  144. *
  145. * @see java.io.StreamTokenizer#TT_NUMBER
  146. * @see java.io.StreamTokenizer#ttype
  147. */
  148. public double nval;
  149. /** Private constructor that initializes everything except the streams. */
  150. private StreamTokenizer() {
  151. wordChars('a', 'z');
  152. wordChars('A', 'Z');
  153. wordChars(128 + 32, 255);
  154. whitespaceChars(0, ' ');
  155. commentChar('/');
  156. quoteChar('"');
  157. quoteChar('\'');
  158. parseNumbers();
  159. }
  160. /**
  161. * Creates a stream tokenizer that parses the specified input
  162. * stream. The stream tokenizer is initialized to the following
  163. * default state:
  164. * <ul>
  165. * <li>All byte values <code>'A'</code> through <code>'Z'</code>,
  166. * <code>'a'</code> through <code>'z'</code>, and
  167. * <code>'\u00A0'</code> through <code>'\u00FF'</code> are
  168. * considered to be alphabetic.
  169. * <li>All byte values <code>'\u0000'</code> through
  170. * <code>'\u0020'</code> are considered to be white space.
  171. * <li><code>'/'</code> is a comment character.
  172. * <li>Single quote <code>'\''</code> and double quote <code>'"'</code>
  173. * are string quote characters.
  174. * <li>Numbers are parsed.
  175. * <li>Ends of lines are treated as white space, not as separate tokens.
  176. * <li>C-style and C++-style comments are not recognized.
  177. * </ul>
  178. *
  179. * @deprecated As of JDK version 1.1, the preferred way to tokenize an
  180. * input stream is to convert it into a character stream, for example:
  181. * <blockquote><pre>
  182. * Reader r = new BufferedReader(new InputStreamReader(is));
  183. * StreamTokenizer st = new StreamTokenizer(r);
  184. * </pre></blockquote>
  185. *
  186. * @param is an input stream.
  187. * @see java.io.BufferedReader
  188. * @see java.io.InputStreamReader
  189. * @see java.io.StreamTokenizer#StreamTokenizer(java.io.Reader)
  190. */
  191. public StreamTokenizer(InputStream is) {
  192. this();
  193. if (is == null) {
  194. throw new NullPointerException();
  195. }
  196. input = is;
  197. }
  198. /**
  199. * Create a tokenizer that parses the given character stream.
  200. *
  201. * @param r a Reader object providing the input stream.
  202. * @since JDK1.1
  203. */
  204. public StreamTokenizer(Reader r) {
  205. this();
  206. if (r == null) {
  207. throw new NullPointerException();
  208. }
  209. reader = r;
  210. }
  211. /**
  212. * Resets this tokenizer's syntax table so that all characters are
  213. * "ordinary." See the <code>ordinaryChar</code> method
  214. * for more information on a character being ordinary.
  215. *
  216. * @see java.io.StreamTokenizer#ordinaryChar(int)
  217. */
  218. public void resetSyntax() {
  219. for (int i = ctype.length; --i >= 0;)
  220. ctype[i] = 0;
  221. }
  222. /**
  223. * Specifies that all characters <i>c</i> in the range
  224. * <code>low <= <i>c</i> <= high</code>
  225. * are word constituents. A word token consists of a word constituent
  226. * followed by zero or more word constituents or number constituents.
  227. *
  228. * @param low the low end of the range.
  229. * @param hi the high end of the range.
  230. */
  231. public void wordChars(int low, int hi) {
  232. if (low < 0)
  233. low = 0;
  234. if (hi >= ctype.length)
  235. hi = ctype.length - 1;
  236. while (low <= hi)
  237. ctype[low++] |= CT_ALPHA;
  238. }
  239. /**
  240. * Specifies that all characters <i>c</i> in the range
  241. * <code>low <= <i>c</i> <= high</code>
  242. * are white space characters. White space characters serve only to
  243. * separate tokens in the input stream.
  244. *
  245. * <p>Any other attribute settings for the characters in the specified
  246. * range are cleared.
  247. *
  248. * @param low the low end of the range.
  249. * @param hi the high end of the range.
  250. */
  251. public void whitespaceChars(int low, int hi) {
  252. if (low < 0)
  253. low = 0;
  254. if (hi >= ctype.length)
  255. hi = ctype.length - 1;
  256. while (low <= hi)
  257. ctype[low++] = CT_WHITESPACE;
  258. }
  259. /**
  260. * Specifies that all characters <i>c</i> in the range
  261. * <code>low <= <i>c</i> <= high</code>
  262. * are "ordinary" in this tokenizer. See the
  263. * <code>ordinaryChar</code> method for more information on a
  264. * character being ordinary.
  265. *
  266. * @param low the low end of the range.
  267. * @param hi the high end of the range.
  268. * @see java.io.StreamTokenizer#ordinaryChar(int)
  269. */
  270. public void ordinaryChars(int low, int hi) {
  271. if (low < 0)
  272. low = 0;
  273. if (hi >= ctype.length)
  274. hi = ctype.length - 1;
  275. while (low <= hi)
  276. ctype[low++] = 0;
  277. }
  278. /**
  279. * Specifies that the character argument is "ordinary"
  280. * in this tokenizer. It removes any special significance the
  281. * character has as a comment character, word component, string
  282. * delimiter, white space, or number character. When such a character
  283. * is encountered by the parser, the parser treates it as a
  284. * single-character token and sets <code>ttype</code> field to the
  285. * character value.
  286. *
  287. * @param ch the character.
  288. * @see java.io.StreamTokenizer#ttype
  289. */
  290. public void ordinaryChar(int ch) {
  291. if (ch >= 0 && ch < ctype.length)
  292. ctype[ch] = 0;
  293. }
  294. /**
  295. * Specified that the character argument starts a single-line
  296. * comment. All characters from the comment character to the end of
  297. * the line are ignored by this stream tokenizer.
  298. *
  299. * <p>Any other attribute settings for the specified character are cleared.
  300. *
  301. * @param ch the character.
  302. */
  303. public void commentChar(int ch) {
  304. if (ch >= 0 && ch < ctype.length)
  305. ctype[ch] = CT_COMMENT;
  306. }
  307. /**
  308. * Specifies that matching pairs of this character delimit string
  309. * constants in this tokenizer.
  310. * <p>
  311. * When the <code>nextToken</code> method encounters a string
  312. * constant, the <code>ttype</code> field is set to the string
  313. * delimiter and the <code>sval</code> field is set to the body of
  314. * the string.
  315. * <p>
  316. * If a string quote character is encountered, then a string is
  317. * recognized, consisting of all characters after (but not including)
  318. * the string quote character, up to (but not including) the next
  319. * occurrence of that same string quote character, or a line
  320. * terminator, or end of file. The usual escape sequences such as
  321. * <code>"\n"</code> and <code>"\t"</code> are recognized and
  322. * converted to single characters as the string is parsed.
  323. *
  324. * <p>Any other attribute settings for the specified character are cleared.
  325. *
  326. * @param ch the character.
  327. * @see java.io.StreamTokenizer#nextToken()
  328. * @see java.io.StreamTokenizer#sval
  329. * @see java.io.StreamTokenizer#ttype
  330. */
  331. public void quoteChar(int ch) {
  332. if (ch >= 0 && ch < ctype.length)
  333. ctype[ch] = CT_QUOTE;
  334. }
  335. /**
  336. * Specifies that numbers should be parsed by this tokenizer. The
  337. * syntax table of this tokenizer is modified so that each of the twelve
  338. * characters:
  339. * <blockquote><pre>
  340. * 0 1 2 3 4 5 6 7 8 9 . -
  341. * </pre></blockquote>
  342. * <p>
  343. * has the "numeric" attribute.
  344. * <p>
  345. * When the parser encounters a word token that has the format of a
  346. * double precision floating-point number, it treats the token as a
  347. * number rather than a word, by setting the the <code>ttype</code>
  348. * field to the value <code>TT_NUMBER</code> and putting the numeric
  349. * value of the token into the <code>nval</code> field.
  350. *
  351. * @see java.io.StreamTokenizer#nval
  352. * @see java.io.StreamTokenizer#TT_NUMBER
  353. * @see java.io.StreamTokenizer#ttype
  354. */
  355. public void parseNumbers() {
  356. for (int i = '0'; i <= '9'; i++)
  357. ctype[i] |= CT_DIGIT;
  358. ctype['.'] |= CT_DIGIT;
  359. ctype['-'] |= CT_DIGIT;
  360. }
  361. /**
  362. * Determines whether or not ends of line are treated as tokens.
  363. * If the flag argument is true, this tokenizer treats end of lines
  364. * as tokens; the <code>nextToken</code> method returns
  365. * <code>TT_EOL</code> and also sets the <code>ttype</code> field to
  366. * this value when an end of line is read.
  367. * <p>
  368. * A line is a sequence of characters ending with either a
  369. * carriage-return character (<code>'\r'</code>) or a newline
  370. * character (<code>'\n'</code>). In addition, a carriage-return
  371. * character followed immediately by a newline character is treated
  372. * as a single end-of-line token.
  373. * <p>
  374. * If the <code>flag</code> is false, end-of-line characters are
  375. * treated as white space and serve only to separate tokens.
  376. *
  377. * @param flag <code>true</code> indicates that end-of-line characters
  378. * are separate tokens; <code>false</code> indicates that
  379. * end-of-line characters are white space.
  380. * @see java.io.StreamTokenizer#nextToken()
  381. * @see java.io.StreamTokenizer#ttype
  382. * @see java.io.StreamTokenizer#TT_EOL
  383. */
  384. public void eolIsSignificant(boolean flag) {
  385. eolIsSignificantP = flag;
  386. }
  387. /**
  388. * Determines whether or not the tokenizer recognizes C-style comments.
  389. * If the flag argument is <code>true</code>, this stream tokenizer
  390. * recognizes C-style comments. All text between successive
  391. * occurrences of <code>/*</code> and <code>*/</code> are discarded.
  392. * <p>
  393. * If the flag argument is <code>false</code>, then C-style comments
  394. * are not treated specially.
  395. *
  396. * @param flag <code>true</code> indicates to recognize and ignore
  397. * C-style comments.
  398. */
  399. public void slashStarComments(boolean flag) {
  400. slashStarCommentsP = flag;
  401. }
  402. /**
  403. * Determines whether or not the tokenizer recognizes C++-style comments.
  404. * If the flag argument is <code>true</code>, this stream tokenizer
  405. * recognizes C++-style comments. Any occurrence of two consecutive
  406. * slash characters (<code>'/'</code>) is treated as the beginning of
  407. * a comment that extends to the end of the line.
  408. * <p>
  409. * If the flag argument is <code>false</code>, then C++-style
  410. * comments are not treated specially.
  411. *
  412. * @param flag <code>true</code> indicates to recognize and ignore
  413. * C++-style comments.
  414. */
  415. public void slashSlashComments(boolean flag) {
  416. slashSlashCommentsP = flag;
  417. }
  418. /**
  419. * Determines whether or not word token are automatically lowercased.
  420. * If the flag argument is <code>true</code>, then the value in the
  421. * <code>sval</code> field is lowercased whenever a word token is
  422. * returned (the <code>ttype</code> field has the
  423. * value <code>TT_WORD</code> by the <code>nextToken</code> method
  424. * of this tokenizer.
  425. * <p>
  426. * If the flag argument is <code>false</code>, then the
  427. * <code>sval</code> field is not modified.
  428. *
  429. * @param fl <code>true</code> indicates that all word tokens should
  430. * be lowercased.
  431. * @see java.io.StreamTokenizer#nextToken()
  432. * @see java.io.StreamTokenizer#ttype
  433. * @see java.io.StreamTokenizer#TT_WORD
  434. */
  435. public void lowerCaseMode(boolean fl) {
  436. forceLower = fl;
  437. }
  438. /** Read the next character */
  439. private int read() throws IOException {
  440. if (reader != null)
  441. return reader.read();
  442. else if (input != null)
  443. return input.read();
  444. else
  445. throw new IllegalStateException();
  446. }
  447. /**
  448. * Parses the next token from the input stream of this tokenizer.
  449. * The type of the next token is returned in the <code>ttype</code>
  450. * field. Additional information about the token may be in the
  451. * <code>nval</code> field or the <code>sval</code> field of this
  452. * tokenizer.
  453. * <p>
  454. * Typical clients of this
  455. * class first set up the syntax tables and then sit in a loop
  456. * calling nextToken to parse successive tokens until TT_EOF
  457. * is returned.
  458. *
  459. * @return the value of the <code>ttype</code> field.
  460. * @exception IOException if an I/O error occurs.
  461. * @see java.io.StreamTokenizer#nval
  462. * @see java.io.StreamTokenizer#sval
  463. * @see java.io.StreamTokenizer#ttype
  464. */
  465. public int nextToken() throws IOException {
  466. if (pushedBack) {
  467. pushedBack = false;
  468. return ttype;
  469. }
  470. byte ct[] = ctype;
  471. sval = null;
  472. int c = peekc;
  473. if (c < 0)
  474. c = NEED_CHAR;
  475. if (c == SKIP_LF) {
  476. c = read();
  477. if (c < 0)
  478. return ttype = TT_EOF;
  479. if (c == '\n')
  480. c = NEED_CHAR;
  481. }
  482. if (c == NEED_CHAR) {
  483. c = read();
  484. if (c < 0)
  485. return ttype = TT_EOF;
  486. }
  487. ttype = c; /* Just to be safe */
  488. /* Set peekc so that the next invocation of nextToken will read
  489. * another character unless peekc is reset in this invocation
  490. */
  491. peekc = NEED_CHAR;
  492. int ctype = c < 256 ? ct[c] : CT_ALPHA;
  493. while ((ctype & CT_WHITESPACE) != 0) {
  494. if (c == '\r') {
  495. LINENO++;
  496. if (eolIsSignificantP) {
  497. peekc = SKIP_LF;
  498. return ttype = TT_EOL;
  499. }
  500. c = read();
  501. if (c == '\n')
  502. c = read();
  503. } else {
  504. if (c == '\n') {
  505. LINENO++;
  506. if (eolIsSignificantP) {
  507. return ttype = TT_EOL;
  508. }
  509. }
  510. c = read();
  511. }
  512. if (c < 0)
  513. return ttype = TT_EOF;
  514. ctype = c < 256 ? ct[c] : CT_ALPHA;
  515. }
  516. if ((ctype & CT_DIGIT) != 0) {
  517. boolean neg = false;
  518. if (c == '-') {
  519. c = read();
  520. if (c != '.' && (c < '0' || c > '9')) {
  521. peekc = c;
  522. return ttype = '-';
  523. }
  524. neg = true;
  525. }
  526. double v = 0;
  527. int decexp = 0;
  528. int seendot = 0;
  529. while (true) {
  530. if (c == '.' && seendot == 0)
  531. seendot = 1;
  532. else if ('0' <= c && c <= '9') {
  533. v = v * 10 + (c - '0');
  534. decexp += seendot;
  535. } else
  536. break;
  537. c = read();
  538. }
  539. peekc = c;
  540. if (decexp != 0) {
  541. double denom = 10;
  542. decexp--;
  543. while (decexp > 0) {
  544. denom *= 10;
  545. decexp--;
  546. }
  547. /* Do one division of a likely-to-be-more-accurate number */
  548. v = v / denom;
  549. }
  550. nval = neg ? -v : v;
  551. return ttype = TT_NUMBER;
  552. }
  553. if ((ctype & CT_ALPHA) != 0) {
  554. int i = 0;
  555. do {
  556. if (i >= buf.length) {
  557. char nb[] = new char[buf.length * 2];
  558. System.arraycopy(buf, 0, nb, 0, buf.length);
  559. buf = nb;
  560. }
  561. buf[i++] = (char) c;
  562. c = read();
  563. ctype = c < 0 ? CT_WHITESPACE : c < 256 ? ct[c] : CT_ALPHA;
  564. } while ((ctype & (CT_ALPHA | CT_DIGIT)) != 0);
  565. peekc = c;
  566. sval = String.copyValueOf(buf, 0, i);
  567. if (forceLower)
  568. sval = sval.toLowerCase();
  569. return ttype = TT_WORD;
  570. }
  571. if ((ctype & CT_QUOTE) != 0) {
  572. ttype = c;
  573. int i = 0;
  574. /* Invariants (because \Octal needs a lookahead):
  575. * (i) c contains char value
  576. * (ii) d contains the lookahead
  577. */
  578. int d = read();
  579. while (d >= 0 && d != ttype && d != '\n' && d != '\r') {
  580. if (d == '\\') {
  581. c = read();
  582. int first = c; /* To allow \377, but not \477 */
  583. if (c >= '0' && c <= '7') {
  584. c = c - '0';
  585. int c2 = read();
  586. if ('0' <= c2 && c2 <= '7') {
  587. c = (c << 3) + (c2 - '0');
  588. c2 = read();
  589. if ('0' <= c2 && c2 <= '7' && first <= '3') {
  590. c = (c << 3) + (c2 - '0');
  591. d = read();
  592. } else
  593. d = c2;
  594. } else
  595. d = c2;
  596. } else {
  597. switch (c) {
  598. case 'a':
  599. c = 0x7;
  600. break;
  601. case 'b':
  602. c = '\b';
  603. break;
  604. case 'f':
  605. c = 0xC;
  606. break;
  607. case 'n':
  608. c = '\n';
  609. break;
  610. case 'r':
  611. c = '\r';
  612. break;
  613. case 't':
  614. c = '\t';
  615. break;
  616. case 'v':
  617. c = 0xB;
  618. break;
  619. }
  620. d = read();
  621. }
  622. } else {
  623. c = d;
  624. d = read();
  625. }
  626. if (i >= buf.length) {
  627. char nb[] = new char[buf.length * 2];
  628. System.arraycopy(buf, 0, nb, 0, buf.length);
  629. buf = nb;
  630. }
  631. buf[i++] = (char)c;
  632. }
  633. /* If we broke out of the loop because we found a matching quote
  634. * character then arrange to read a new character next time
  635. * around; otherwise, save the character.
  636. */
  637. peekc = (d == ttype) ? NEED_CHAR : d;
  638. sval = String.copyValueOf(buf, 0, i);
  639. return ttype;
  640. }
  641. if (c == '/' && (slashSlashCommentsP || slashStarCommentsP)) {
  642. c = read();
  643. if (c == '*' && slashStarCommentsP) {
  644. int prevc = 0;
  645. while ((c = read()) != '/' || prevc != '*') {
  646. if (c == '\r') {
  647. LINENO++;
  648. c = read();
  649. if (c == '\n') {
  650. c = read();
  651. }
  652. } else {
  653. if (c == '\n') {
  654. LINENO++;
  655. c = read();
  656. }
  657. }
  658. if (c < 0)
  659. return ttype = TT_EOF;
  660. prevc = c;
  661. }
  662. return nextToken();
  663. } else if (c == '/' && slashSlashCommentsP) {
  664. while ((c = read()) != '\n' && c != '\r' && c >= 0);
  665. peekc = c;
  666. return nextToken();
  667. } else {
  668. /* Now see if it is still a single line comment */
  669. if ((ct['/'] & CT_COMMENT) != 0) {
  670. while ((c = read()) != '\n' && c != '\r' && c >= 0);
  671. peekc = c;
  672. return nextToken();
  673. } else {
  674. peekc = c;
  675. return ttype = '/';
  676. }
  677. }
  678. }
  679. if ((ctype & CT_COMMENT) != 0) {
  680. while ((c = read()) != '\n' && c != '\r' && c >= 0);
  681. peekc = c;
  682. return nextToken();
  683. }
  684. return ttype = c;
  685. }
  686. /**
  687. * Causes the next call to the <code>nextToken</code> method of this
  688. * tokenizer to return the current value in the <code>ttype</code>
  689. * field, and not to modify the value in the <code>nval</code> or
  690. * <code>sval</code> field.
  691. *
  692. * @see java.io.StreamTokenizer#nextToken()
  693. * @see java.io.StreamTokenizer#nval
  694. * @see java.io.StreamTokenizer#sval
  695. * @see java.io.StreamTokenizer#ttype
  696. */
  697. public void pushBack() {
  698. if (ttype != TT_NOTHING) /* No-op if nextToken() not called */
  699. pushedBack = true;
  700. }
  701. /**
  702. * Return the current line number.
  703. *
  704. * @return the current line number of this stream tokenizer.
  705. */
  706. public int lineno() {
  707. return LINENO;
  708. }
  709. /**
  710. * Returns the string representation of the current stream token and
  711. * the line number it occurs on.
  712. *
  713. * <p>The precise string returned is unspecified, although the following
  714. * example can be considered typical:
  715. *
  716. * <blockquote><pre>Token['a'], line 10</pre></blockquote>
  717. *
  718. * @return a string representation of the token
  719. * @see java.io.StreamTokenizer#nval
  720. * @see java.io.StreamTokenizer#sval
  721. * @see java.io.StreamTokenizer#ttype
  722. */
  723. public String toString() {
  724. String ret;
  725. switch (ttype) {
  726. case TT_EOF:
  727. ret = "EOF";
  728. break;
  729. case TT_EOL:
  730. ret = "EOL";
  731. break;
  732. case TT_WORD:
  733. ret = sval;
  734. break;
  735. case TT_NUMBER:
  736. ret = "n=" + nval;
  737. break;
  738. case TT_NOTHING:
  739. ret = "NOTHING";
  740. break;
  741. default: {
  742. /*
  743. * ttype is the first character of either a quoted string or
  744. * is an ordinary character. ttype can definitely not be less
  745. * than 0, since those are reserved values used in the previous
  746. * case statements
  747. */
  748. if (ttype < 256 &&
  749. ((ctype[ttype] & CT_QUOTE) != 0)) {
  750. ret = sval;
  751. break;
  752. }
  753. char s[] = new char[3];
  754. s[0] = s[2] = '\'';
  755. s[1] = (char) ttype;
  756. ret = new String(s);
  757. break;
  758. }
  759. }
  760. return "Token[" + ret + "], line " + LINENO;
  761. }
  762. }