1 19 package org.netbeans.tax.decl.parser; 20 21 import java.io.*; 22 23 import org.netbeans.tax.*; 24 25 29 public class ParserReader extends PushbackReader { 30 31 32 34 35 39 public ParserReader (String source) { 40 super (new StringReader (source), 20); 41 42 } 44 45 46 50 56 57 public ParserReader trim () { 58 int ch; 59 while ( true ) { 60 try { 62 ch = read (); 63 if (ch == -1 || ! Character.isWhitespace ((char)ch)) 64 break; 65 } catch (IOException ex) { 66 ex.printStackTrace (); 67 } 68 } 69 70 try { 71 if (ch != -1) unread (ch); 72 } catch (IOException ex) { 73 ex.printStackTrace (); 74 } 75 76 return this; 77 } 78 79 82 public boolean startsWith (String prefix) { 83 char buf[] = new char[prefix.length ()]; 84 try { 85 read (buf); 86 boolean ret = new String (buf).equals (prefix); 88 if (ret) return true; 89 } catch (IOException ex) { 90 ex.printStackTrace (); 91 return false; 92 } 93 94 try { 95 unread (buf); 96 } catch (IOException ex) { 97 ex.printStackTrace (); 98 } 99 return false; 100 } 101 102 103 public int peek () { 104 try { 105 int ch = read (); 106 unread (ch); 107 return ch; 108 } catch (IOException ex) { 109 return -1; 110 } 111 } 112 113 public String getToken () { 115 StringBuffer sb = new StringBuffer (); 116 117 int ch = -1; 118 119 trim (); 120 121 boolean reading = true; int len = 0; 123 124 try { 125 readChars: 126 while (reading) { ch = read (); 128 if ( ch == -1 || Character.isWhitespace ((char)ch) ) 129 break; 130 switch (ch) { 131 case ')': case '(': case '?': case '+': case '*': 133 break readChars; 134 135 case ',': case '|': 137 if (len == 0) { 138 reading = false; } else { 140 break readChars; } 142 } 143 144 sb.append ((char)ch); 145 len++; 146 } 147 if (ch != -1 && reading) unread (ch); 148 } catch (IOException ex) { 149 } 151 152 String toret = sb.toString (); 153 return toret; 155 } 156 } 157 | Popular Tags |