• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package build.tools.jdwpgen;
27 
28 import java.util.*;
29 import java.io.*;
30 
31 class Parse {
32 
33     final StreamTokenizer izer;
34     final Map<String, Node> kindMap = new HashMap<String, Node>();
35 
Parse(Reader reader)36     Parse(Reader reader) {
37         izer = new StreamTokenizer(new BufferedReader(reader));
38         izer.resetSyntax();
39         izer.slashStarComments(true);
40         izer.slashSlashComments(true);
41         izer.wordChars((int)'a', (int)'z');
42         izer.wordChars((int)'A', (int)'Z');
43         izer.wordChars((int)'0', (int)'9');
44         izer.wordChars((int)'_', (int)'_');
45         izer.wordChars((int)'-', (int)'-');
46         izer.wordChars((int)'.', (int)'.');
47         izer.whitespaceChars(0, 32);
48         izer.quoteChar('"');
49         izer.quoteChar('\'');
50 
51         kindMap.put("CommandSet", new CommandSetNode());
52         kindMap.put("Command", new CommandNode());
53         kindMap.put("Out", new OutNode());
54         kindMap.put("Reply", new ReplyNode());
55         kindMap.put("ErrorSet", new ErrorSetNode());
56         kindMap.put("Error", new ErrorNode());
57         kindMap.put("Event", new EventNode());
58         kindMap.put("Repeat", new RepeatNode());
59         kindMap.put("Group", new GroupNode());
60         kindMap.put("Select", new SelectNode());
61         kindMap.put("Alt", new AltNode());
62         kindMap.put("ConstantSet", new ConstantSetNode());
63         kindMap.put("Constant", new ConstantNode());
64         kindMap.put("int", new IntTypeNode());
65         kindMap.put("long", new LongTypeNode());
66         kindMap.put("boolean", new BooleanTypeNode());
67         kindMap.put("object", new ObjectTypeNode());
68         kindMap.put("threadObject", new ThreadObjectTypeNode());
69         kindMap.put("threadGroupObject", new ThreadGroupObjectTypeNode());
70         kindMap.put("arrayObject", new ArrayObjectTypeNode());
71         kindMap.put("stringObject", new StringObjectTypeNode());
72         kindMap.put("classLoaderObject", new ClassLoaderObjectTypeNode());
73         kindMap.put("classObject", new ClassObjectTypeNode());
74         kindMap.put("referenceType", new ReferenceTypeNode());
75         kindMap.put("referenceTypeID", new ReferenceIDTypeNode());
76         kindMap.put("classType", new ClassTypeNode());
77         kindMap.put("interfaceType", new InterfaceTypeNode());
78         kindMap.put("arrayType", new ArrayTypeNode());
79         kindMap.put("method", new MethodTypeNode());
80         kindMap.put("field", new FieldTypeNode());
81         kindMap.put("frame", new FrameTypeNode());
82         kindMap.put("string", new StringTypeNode());
83         kindMap.put("value", new ValueTypeNode());
84         kindMap.put("byte", new ByteTypeNode());
85         kindMap.put("location", new LocationTypeNode());
86         kindMap.put("tagged-object", new TaggedObjectTypeNode());
87         kindMap.put("referenceTypeID", new ReferenceIDTypeNode());
88         kindMap.put("typed-sequence", new ArrayRegionTypeNode());
89         kindMap.put("untagged-value", new UntaggedValueTypeNode());
90     }
91 
items()92     RootNode items() throws IOException {
93         List<Node> list = new ArrayList<Node>();
94 
95         while (izer.nextToken() != StreamTokenizer.TT_EOF) {
96             izer.pushBack();
97             list.add(item());
98         }
99         RootNode node =  new RootNode();
100         node.set("Root", list, 1);
101         return node;
102     }
103 
item()104     Node item() throws IOException {
105         switch (izer.nextToken()) {
106             case StreamTokenizer.TT_EOF:
107                 error("Unexpect end-of-file");
108                 return null;
109 
110             case StreamTokenizer.TT_WORD: {
111                 String name = izer.sval;
112                 if (izer.nextToken() == '=') {
113                     int ntok = izer.nextToken();
114                     if (ntok == StreamTokenizer.TT_WORD) {
115                         return new NameValueNode(name, izer.sval);
116                     } else if (ntok == '\'') {
117                         return new NameValueNode(name, izer.sval.charAt(0));
118                     } else {
119                         error("Expected value after: " + name + " =");
120                         return null;
121                     }
122                 } else {
123                     izer.pushBack();
124                     return new NameNode(name);
125                 }
126             }
127 
128             case '"':
129                 return new CommentNode(izer.sval);
130 
131             case '(': {
132                 if (izer.nextToken() == StreamTokenizer.TT_WORD) {
133                     String kind = izer.sval;
134                     List<Node> list = new ArrayList<Node>();
135 
136                     while (izer.nextToken() != ')') {
137                         izer.pushBack();
138                         list.add(item());
139                     }
140                     Node proto = kindMap.get(kind);
141                     if (proto == null) {
142                         error("Invalid kind: " + kind);
143                         return null;
144                     } else {
145                         try {
146                             Node node = (Node)proto.getClass().newInstance();
147                             node.set(kind, list, izer.lineno());
148                             return node;
149                         } catch (InstantiationException exc) {
150                             error(exc.toString());
151                             return null;
152                         } catch (IllegalAccessException exc) {
153                             error(exc.toString());
154                             return null;
155                         }
156                     }
157                 } else {
158                     error("Expected kind identifier, got " + izer.ttype +
159                           " : " + izer.sval);
160                     return null;
161                 }
162             }
163 
164             default:
165                 error("Unexpected character: '" + (char)izer.ttype + "'");
166                 return null;
167         }
168     }
169 
error(String errmsg)170     void error(String errmsg) {
171         System.err.println(Main.specSource + ":" + izer.lineno() +
172                            ": " + errmsg);
173         throw new RuntimeException("Error: " + errmsg);
174     }
175 }
176