KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > client > typemgr > Parser


1 // Copyright (C) 1998-1999
2
// Object Oriented Concepts, Inc.
3

4 // **********************************************************************
5
//
6
// Copyright (c) 1997
7
// Mark Spruiell (mark@intellisoft.com)
8
//
9
// See the COPYING file for more information
10
//
11
// **********************************************************************
12

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 JavaDoc m_currentIdent;
24   private String JavaDoc m_currentScopedName;
25   private String JavaDoc m_name;
26   private String JavaDoc m_interface;
27   private PropStruct m_currentProperty;
28   private PropStruct[] m_properties;
29   private String JavaDoc[] 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 JavaDoc getName()
60   {
61     return m_name;
62   }
63
64
65   public String JavaDoc getInterface()
66   {
67     return m_interface;
68   }
69
70
71   public PropStruct[] getProperties()
72   {
73     return m_properties;
74   }
75
76
77   public String JavaDoc[] 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         // process the base service types
142

143       while (m_lex.getToken() == Lex.COMMA) {
144         m_lex.nextToken();
145         parseScopedName();
146         superTypes.addElement(m_currentScopedName);
147       }
148     }
149
150       // save our list of supertypes
151
m_superTypes = new String JavaDoc[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 JavaDoc name = new StringBuffer JavaDoc();
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         // don't want two DOUBLECOLONs in a row
177
if (t == Lex.DOUBLECOLON && lastToken == Lex.DOUBLECOLON)
178         throw new ParserException(
179           "malformed scoped identifier", m_lex.getLine());
180     }
181
182       // check for a malformed identifier; see if we've gotten
183
// and IDENT, and make sure the name didn't end with "::"
184
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         // save current property
213
props.addElement(new PropStruct(m_currentProperty.name,
214         m_currentProperty.value_type, m_currentProperty.mode));
215
216       t = m_lex.getToken();
217     }
218
219       // save our list of supertypes
220
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     } // switch (t)
422
}
423
424
425   /*********************** comment out this line to enable main()
426
427   public static void main(String[] args)
428   {
429     try {
430       Parser parser = new Parser();
431       parser.parse(new InputStreamReader(System.in));
432       System.out.println("Parse OK");
433       System.out.println("Service name: " + parser.getName());
434       System.out.println("Interface : " + parser.getInterface());
435       System.out.print("Super types : ");
436       String[] superTypes = parser.getSuperTypes();
437       for (int i = 0; i < superTypes.length; i++)
438         System.out.print(superTypes[i] + " ");
439       System.out.println();
440
441       PropStruct[] props = parser.getProperties();
442       System.out.println();
443       for (int i = 0; i < props.length; i++) {
444         System.out.println("Property: " + props[i].name);
445         System.out.print("Mode : ");
446         switch (props[i].mode.value()) {
447           case PropertyMode._PROP_NORMAL:
448             System.out.println("Normal");
449             break;
450           case PropertyMode._PROP_READONLY:
451             System.out.println("Read-only");
452             break;
453           case PropertyMode._PROP_MANDATORY:
454             System.out.println("Mandatory");
455             break;
456           case PropertyMode._PROP_MANDATORY_READONLY:
457             System.out.println("Mandatory Read-only");
458             break;
459         }
460
461         System.out.println("Type : " +
462           convertType(props[i].value_type));
463         System.out.println();
464       }
465     }
466     catch (ParserException e) {
467       System.out.println("Error (" + e.getLine() + ") : " + e.getMessage());
468     }
469   }
470
471
472   protected static String convertType(TypeCode tc)
473   {
474     String result = null;
475
476     TCKind kind = tc.kind();
477     if (kind == TCKind.tk_sequence) {
478       try {
479         TypeCode elemTC = tc.content_type();
480         kind = elemTC.kind();
481         result = "sequence<" + convertKind(kind) + ">";
482       }
483       catch (org.omg.CORBA.TypeCodePackage.BadKind e) {
484         throw new RuntimeException();
485       }
486     }
487     else
488       result = convertKind(kind);
489
490     return result;
491   }
492
493
494   protected static String convertKind(TCKind kind)
495   {
496     String result = "unknown";
497
498     switch (kind.value()) {
499       case TCKind._tk_null:
500         result = "other";
501         break;
502       case TCKind._tk_boolean:
503         result = "boolean";
504         break;
505       case TCKind._tk_short:
506         result = "short";
507         break;
508       case TCKind._tk_ushort:
509         result = "unsigned short";
510         break;
511       case TCKind._tk_long:
512         result = "long";
513         break;
514       case TCKind._tk_ulong:
515         result = "unsigned long";
516         break;
517       case TCKind._tk_float:
518         result = "float";
519         break;
520       case TCKind._tk_double:
521         result = "double";
522         break;
523       case TCKind._tk_char:
524         result = "char";
525         break;
526       case TCKind._tk_string:
527         result = "string";
528         break;
529     }
530
531     return result;
532   }
533
534   /*********************** comment out this line to enable main() */

535 }
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
Popular Tags