1 2 5 14 package org.jacorb.trading.client.typemgr; 15 16 import java.io.*; 17 import java.util.*; 18 19 20 24 public class Lex 25 { 26 private StringBuffer m_input; 27 private int m_token = ERROR; 28 private StringBuffer m_buffer = new StringBuffer (); 29 private String m_lexeme = null; 30 private int m_pos = 0; 31 private int m_line = 1; 32 private Hashtable m_literals = new Hashtable(); 33 private boolean m_eof = false; 34 35 36 public static final int ERROR = 0; 37 public static final int END = 1; public static final int LBRACE = 2; 39 public static final int RBRACE = 3; 40 public static final int LANGLE = 4; 41 public static final int RANGLE = 5; 42 public static final int COLON = 6; 43 public static final int DOUBLECOLON = 7; 44 public static final int SEMICOLON = 8; 45 public static final int COMMA = 9; 46 public static final int SERVICE = 10; 47 public static final int INTERFACE = 11; 48 public static final int IDENT = 12; 49 public static final int MANDATORY = 13; 50 public static final int READONLY = 14; 51 public static final int PROPERTY = 15; 52 public static final int UNSIGNED = 16; 53 public static final int BOOLEAN = 17; 54 public static final int CHAR = 18; 55 public static final int SHORT = 19; 56 public static final int LONG = 20; 57 public static final int FLOAT = 21; 58 public static final int DOUBLE = 22; 59 public static final int STRING = 23; 60 public static final int SEQUENCE = 24; 61 public static final int OTHER = 25; 62 63 64 65 66 private Lex() 67 { 68 } 69 70 71 74 public Lex(Reader reader) 75 { 76 BufferedReader br = new BufferedReader(reader); 77 m_input = new StringBuffer (); 78 79 boolean eof = false; 81 char[] chars = new char[512]; 82 while (! eof) { 83 try { 84 int len = br.read(chars); 85 if (len < 0) 86 eof = true; 87 else 88 m_input.append(chars, 0, len); 89 } 90 catch (IOException e) { 91 } 92 } 93 94 m_literals.put("service", new Integer (SERVICE)); 96 m_literals.put("interface", new Integer (INTERFACE)); 97 m_literals.put("mandatory", new Integer (MANDATORY)); 98 m_literals.put("readonly", new Integer (READONLY)); 99 m_literals.put("property", new Integer (PROPERTY)); 100 m_literals.put("unsigned", new Integer (UNSIGNED)); 101 m_literals.put("boolean", new Integer (BOOLEAN)); 102 m_literals.put("char", new Integer (CHAR)); 103 m_literals.put("short", new Integer (SHORT)); 104 m_literals.put("long", new Integer (LONG)); 105 m_literals.put("float", new Integer (FLOAT)); 106 m_literals.put("double", new Integer (DOUBLE)); 107 m_literals.put("string", new Integer (STRING)); 108 m_literals.put("sequence", new Integer (SEQUENCE)); 109 m_literals.put("other", new Integer (OTHER)); 110 } 111 112 113 116 public void nextToken() 117 throws LexException 118 { 119 int result = ERROR; 120 121 boolean done = false; 122 123 clearLexeme(); 124 125 while (! done) { 126 127 char c = nextChar(); 128 129 if (eof()) { 131 result = END; 132 break; 133 } 134 135 switch (c) { 136 case '{': 137 result = LBRACE; 138 done = true; 139 addLexeme(c); 140 break; 141 142 case '}': 143 result = RBRACE; 144 done = true; 145 addLexeme(c); 146 break; 147 148 case '<': 149 result = LANGLE; 150 done = true; 151 addLexeme(c); 152 break; 153 154 case '>': 155 result = RANGLE; 156 done = true; 157 addLexeme(c); 158 break; 159 160 167 168 case ':': { 169 addLexeme(c); 170 c = nextChar(); 171 if (c != ':') { 172 pushBack(c); 173 result = COLON; 174 done = true; 175 } 176 else { 177 result = DOUBLECOLON; 178 addLexeme(c); 179 done = true; 180 } 181 } 182 break; 183 184 case ';': 185 result = SEMICOLON; 186 done = true; 187 addLexeme(c); 188 break; 189 190 case ',': 191 result = COMMA; 192 done = true; 193 addLexeme(c); 194 break; 195 196 case '/': { 197 c = nextChar(); 198 if (c != '/') 199 throw new LexException("expected comment", m_line); 200 201 while ((c = nextChar()) != '\n') 203 ; 204 205 continue; 206 } 207 208 case ' ': 209 case '\t': 210 case '\n': 211 case '\r': 212 continue; 214 } 216 217 if (! done) { 220 if (c != '_' && ! Character.isLetter(c)) 223 throw new LexException("unexpected input", m_line); 224 225 227 while (isIdent(c) && ! eof()) { 228 addLexeme(c); 229 c = nextChar(); 230 } 231 232 if (! eof()) 233 pushBack(c); 234 235 String lexeme = getLexeme(); 237 Integer val = (Integer )m_literals.get(lexeme); 238 239 if (val == null) 241 result = IDENT; 242 else 243 result = val.intValue(); 244 245 done = true; 246 } 247 248 } 250 m_token = result; 251 } 252 253 254 257 public int getToken() 258 { 259 return m_token; 260 } 261 262 263 266 public String getLexeme() 267 { 268 if (m_lexeme == null) 269 m_lexeme = m_buffer.toString(); 270 271 return m_lexeme; 272 } 273 274 275 278 public int getLine() 279 { 280 return m_line; 281 } 282 283 284 protected boolean eof() 285 { 286 return m_eof; 287 } 288 289 290 protected char nextChar() 291 { 292 char result = 0; 293 294 if (m_pos < m_input.length()) { 295 result = m_input.charAt(m_pos); 296 m_pos++; 297 298 if (result == '\n') 299 m_line++; 300 } 301 else 302 m_eof = true; 303 304 return result; 305 } 306 307 308 protected void pushBack(char c) 309 { 310 if (c != 0) 311 m_pos--; 312 } 313 314 315 protected boolean isIdent(char c) 316 { 317 return (Character.isLetter(c) || Character.isDigit(c) || (c == '_')); 318 } 319 320 321 protected void clearLexeme() 322 { 323 m_lexeme = null; 324 m_buffer.setLength(0); 325 } 326 327 328 protected void addLexeme(char c) 329 { 330 m_buffer.append(c); 331 } 332 333 447 } 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | Popular Tags |