1 4 13 package org.jacorb.trading.client.typemgr; 14 15 import java.io.*; 16 import java.util.*; 17 import org.omg.CORBA.*; 18 import org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.*; 19 20 public class Parser 21 { 22 private Lex m_lex; 23 private String m_currentIdent; 24 private String m_currentScopedName; 25 private String m_name; 26 private String m_interface; 27 private PropStruct m_currentProperty; 28 private PropStruct[] m_properties; 29 private String [] m_superTypes; 30 31 32 33 public Parser() 34 { 35 } 36 37 38 public void parse(Reader reader) 39 throws ParserException 40 { 41 m_currentProperty = new PropStruct(); 42 43 try { 44 m_lex = new Lex(reader); 45 m_lex.nextToken(); 46 47 if (m_lex.getToken() != Lex.END) 48 parseService(); 49 else 50 throw new ParserException("no input", 0); 51 } 52 catch (LexException e) { 53 throw new 54 ParserException("Lexical error: " + e.getMessage(), e.getLine()); 55 } 56 } 57 58 59 public String getName() 60 { 61 return m_name; 62 } 63 64 65 public String getInterface() 66 { 67 return m_interface; 68 } 69 70 71 public PropStruct[] getProperties() 72 { 73 return m_properties; 74 } 75 76 77 public String [] getSuperTypes() 78 { 79 return m_superTypes; 80 } 81 82 83 protected void parseService() 84 throws ParserException, LexException 85 { 86 int t = m_lex.getToken(); 87 if (t == Lex.SERVICE) { 88 m_lex.nextToken(); 89 90 parseScopedName(); 91 m_name = m_currentScopedName; 92 93 parseBaseTypes(); 94 95 if (m_lex.getToken() != Lex.LBRACE) 96 throw new ParserException("expected '{' symbol", m_lex.getLine()); 97 m_lex.nextToken(); 98 99 if (m_lex.getToken() != Lex.INTERFACE) 100 throw new ParserException( 101 "expected 'interface' declaration", m_lex.getLine()); 102 m_lex.nextToken(); 103 104 parseScopedName(); 105 m_interface = m_currentScopedName; 106 107 if (m_lex.getToken() != Lex.SEMICOLON) 108 throw new ParserException( 109 "expected ';' after 'interface' declaration", m_lex.getLine()); 110 m_lex.nextToken(); 111 112 parsePropertyList(); 113 114 if (m_lex.getToken() != Lex.RBRACE) 115 throw new ParserException("expected '}' symbol", m_lex.getLine()); 116 m_lex.nextToken(); 117 118 if (m_lex.getToken() != Lex.SEMICOLON) 119 throw new ParserException( 120 "expected ';' after 'service' declaration", m_lex.getLine()); 121 m_lex.nextToken(); 122 } 123 else 124 throw new ParserException( 125 "expected 'service' declaration", m_lex.getLine()); 126 } 127 128 129 protected void parseBaseTypes() 130 throws ParserException, LexException 131 { 132 Vector superTypes = new Vector(); 133 134 int t = m_lex.getToken(); 135 if (t == Lex.COLON) { 136 m_lex.nextToken(); 137 138 parseScopedName(); 139 superTypes.addElement(m_currentScopedName); 140 141 143 while (m_lex.getToken() == Lex.COMMA) { 144 m_lex.nextToken(); 145 parseScopedName(); 146 superTypes.addElement(m_currentScopedName); 147 } 148 } 149 150 m_superTypes = new String [superTypes.size()]; 152 superTypes.copyInto(m_superTypes); 153 } 154 155 156 protected void parseScopedName() 157 throws ParserException, LexException 158 { 159 int t = m_lex.getToken(); 160 161 if (t != Lex.IDENT && t != Lex.DOUBLECOLON) 162 throw new ParserException( 163 "expected scoped identifier", m_lex.getLine()); 164 165 boolean seenIdent = false; 166 StringBuffer name = new StringBuffer (); 167 int lastToken = t; 168 169 while (t == Lex.IDENT || t == Lex.DOUBLECOLON) { 170 name.append(m_lex.getLexeme()); 171 if (t == Lex.IDENT) 172 seenIdent = true; 173 lastToken = t; 174 m_lex.nextToken(); 175 t = m_lex.getToken(); 176 if (t == Lex.DOUBLECOLON && lastToken == Lex.DOUBLECOLON) 178 throw new ParserException( 179 "malformed scoped identifier", m_lex.getLine()); 180 } 181 182 if (! seenIdent || lastToken == Lex.DOUBLECOLON) 185 throw new ParserException( 186 "malformed scoped identifier", m_lex.getLine()); 187 188 m_currentScopedName = name.toString(); 189 } 190 191 192 protected void parseIdent() 193 throws ParserException, LexException 194 { 195 int t = m_lex.getToken(); 196 if (t == Lex.IDENT) { 197 m_currentIdent = m_lex.getLexeme(); 198 m_lex.nextToken(); 199 } 200 } 201 202 203 protected void parsePropertyList() 204 throws ParserException, LexException 205 { 206 Vector props = new Vector(); 207 208 int t = m_lex.getToken(); 209 while (t != Lex.RBRACE) { 210 parseProperty(); 211 212 props.addElement(new PropStruct(m_currentProperty.name, 214 m_currentProperty.value_type, m_currentProperty.mode)); 215 216 t = m_lex.getToken(); 217 } 218 219 m_properties = new PropStruct[props.size()]; 221 Enumeration e = props.elements(); 222 int count = 0; 223 while (e.hasMoreElements()) 224 m_properties[count++] = (PropStruct)e.nextElement(); 225 } 226 227 228 protected void parseProperty() 229 throws ParserException, LexException 230 { 231 m_currentProperty.mode = PropertyMode.PROP_NORMAL; 232 233 parseQualifierList(); 234 235 int t = m_lex.getToken(); 236 if (t == Lex.PROPERTY) { 237 m_lex.nextToken(); 238 239 parseIDLType(); 240 241 if (m_lex.getToken() != Lex.IDENT) 242 throw new ParserException( 243 "expected property identifier after IDL type", m_lex.getLine()); 244 245 parseIdent(); 246 247 m_currentProperty.name = m_currentIdent; 248 249 if (m_lex.getToken() != Lex.SEMICOLON) 250 throw new ParserException( 251 "expected ';' after property declaration", m_lex.getLine()); 252 m_lex.nextToken(); 253 } 254 else 255 throw new ParserException( 256 "invalid property declaration", m_lex.getLine()); 257 } 258 259 260 protected void parseQualifierList() 261 throws ParserException, LexException 262 { 263 int t = m_lex.getToken(); 264 while (t == Lex.MANDATORY || t == Lex.READONLY) { 265 PropertyMode mode = m_currentProperty.mode; 266 267 if (t == Lex.MANDATORY) { 268 if (mode == PropertyMode.PROP_NORMAL) 269 mode = PropertyMode.PROP_MANDATORY; 270 else if (mode == PropertyMode.PROP_READONLY) 271 mode = PropertyMode.PROP_MANDATORY_READONLY; 272 else 273 throw new ParserException( 274 "duplicate 'mandatory' qualifier", m_lex.getLine()); 275 } 276 else if (t == Lex.READONLY) { 277 if (mode == PropertyMode.PROP_NORMAL) 278 mode = PropertyMode.PROP_READONLY; 279 else if (mode == PropertyMode.PROP_MANDATORY) 280 mode = PropertyMode.PROP_MANDATORY_READONLY; 281 else 282 throw new ParserException( 283 "duplicate 'readonly' qualifier", m_lex.getLine()); 284 } 285 286 m_currentProperty.mode = mode; 287 288 m_lex.nextToken(); 289 t = m_lex.getToken(); 290 } 291 } 292 293 294 protected void parseIDLType() 295 throws ParserException, LexException 296 { 297 int t = m_lex.getToken(); 298 299 if (t == Lex.SEQUENCE) { 300 m_lex.nextToken(); 301 302 if (m_lex.getToken() != Lex.LANGLE) 303 throw new ParserException( 304 "expected '<' after 'sequence'", m_lex.getLine()); 305 m_lex.nextToken(); 306 307 parseType(true); 308 309 if (m_lex.getToken() != Lex.RANGLE) 310 throw new ParserException( 311 "expected '>' after sequence type", m_lex.getLine()); 312 m_lex.nextToken(); 313 } 314 else if (t == Lex.OTHER) { 315 m_currentProperty.value_type = ORB.init().get_primitive_tc(TCKind.tk_null); 316 m_lex.nextToken(); 317 } 318 else 319 parseType(false); 320 } 321 322 323 protected void parseType(boolean seq) 324 throws ParserException, LexException 325 { 326 ORB orb = ORB.init(); 327 328 int t = m_lex.getToken(); 329 switch (t) { 330 case Lex.UNSIGNED: 331 m_lex.nextToken(); 332 t = m_lex.getToken(); 333 if (t == Lex.SHORT) { 334 TypeCode shortTC = orb.get_primitive_tc(TCKind.tk_short); 335 if (seq) 336 m_currentProperty.value_type = orb.create_sequence_tc(0, shortTC); 337 else 338 m_currentProperty.value_type = shortTC; 339 m_lex.nextToken(); 340 } 341 else if (t == Lex.LONG) { 342 TypeCode ulongTC = orb.get_primitive_tc(TCKind.tk_ulong); 343 if (seq) 344 m_currentProperty.value_type = orb.create_sequence_tc(0, ulongTC); 345 else 346 m_currentProperty.value_type = ulongTC; 347 m_lex.nextToken(); 348 } 349 else 350 throw new ParserException( 351 "only short and long can be unsigned", m_lex.getLine()); 352 break; 353 354 case Lex.CHAR: 355 TypeCode charTC = orb.get_primitive_tc(TCKind.tk_char); 356 if (seq) 357 m_currentProperty.value_type = orb.create_sequence_tc(0, charTC); 358 else 359 m_currentProperty.value_type = charTC; 360 m_lex.nextToken(); 361 break; 362 363 case Lex.BOOLEAN: 364 TypeCode booleanTC = orb.get_primitive_tc(TCKind.tk_boolean); 365 if (seq) 366 m_currentProperty.value_type = orb.create_sequence_tc(0, booleanTC); 367 else 368 m_currentProperty.value_type = booleanTC; 369 m_lex.nextToken(); 370 break; 371 372 case Lex.SHORT: 373 TypeCode shortTC = orb.get_primitive_tc(TCKind.tk_short); 374 if (seq) 375 m_currentProperty.value_type = orb.create_sequence_tc(0, shortTC); 376 else 377 m_currentProperty.value_type = shortTC; 378 m_lex.nextToken(); 379 break; 380 381 case Lex.LONG: 382 TypeCode longTC = orb.get_primitive_tc(TCKind.tk_long); 383 if (seq) 384 m_currentProperty.value_type = orb.create_sequence_tc(0, longTC); 385 else 386 m_currentProperty.value_type = longTC; 387 m_lex.nextToken(); 388 break; 389 390 case Lex.FLOAT: 391 TypeCode floatTC = orb.get_primitive_tc(TCKind.tk_float); 392 if (seq) 393 m_currentProperty.value_type = orb.create_sequence_tc(0, floatTC); 394 else 395 m_currentProperty.value_type = floatTC; 396 m_lex.nextToken(); 397 break; 398 399 case Lex.DOUBLE: 400 TypeCode doubleTC = orb.get_primitive_tc(TCKind.tk_double); 401 if (seq) 402 m_currentProperty.value_type = orb.create_sequence_tc(0, doubleTC); 403 else 404 m_currentProperty.value_type = doubleTC; 405 m_lex.nextToken(); 406 break; 407 408 case Lex.STRING: 409 TypeCode stringTC = orb.get_primitive_tc(TCKind.tk_string); 410 if (seq) 411 m_currentProperty.value_type = orb.create_sequence_tc(0, stringTC); 412 else 413 m_currentProperty.value_type = stringTC; 414 m_lex.nextToken(); 415 break; 416 417 default: 418 throw new ParserException( 419 "unknown/unsupported IDL type '" + m_lex.getLexeme() + "'", 420 m_lex.getLine()); 421 } } 423 424 425 535 } 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | Popular Tags |