1 /* 2 * Copyright (c) 1998, 2016, 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 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("moduleID", new ModuleTypeNode()); 84 kindMap.put("value", new ValueTypeNode()); 85 kindMap.put("byte", new ByteTypeNode()); 86 kindMap.put("location", new LocationTypeNode()); 87 kindMap.put("tagged-object", new TaggedObjectTypeNode()); 88 kindMap.put("referenceTypeID", new ReferenceIDTypeNode()); 89 kindMap.put("typed-sequence", new ArrayRegionTypeNode()); 90 kindMap.put("untagged-value", new UntaggedValueTypeNode()); 91 } 92 93 RootNode items() throws IOException { 94 List<Node> list = new ArrayList<Node>(); 95 96 while (izer.nextToken() != StreamTokenizer.TT_EOF) { 97 izer.pushBack(); 98 list.add(item()); 99 } 100 RootNode node = new RootNode(); 101 node.set("Root", list, 1); 102 return node; 103 } 104 105 Node item() throws IOException { 106 switch (izer.nextToken()) { 107 case StreamTokenizer.TT_EOF: 108 error("Unexpect end-of-file"); 109 return null; 110 111 case StreamTokenizer.TT_WORD: { 112 String name = izer.sval; 113 if (izer.nextToken() == '=') { 114 int ntok = izer.nextToken(); 115 if (ntok == StreamTokenizer.TT_WORD) { 116 return new NameValueNode(name, izer.sval); 117 } else if (ntok == '\'') { 118 return new NameValueNode(name, izer.sval.charAt(0)); 119 } else { 120 error("Expected value after: " + name + " ="); 121 return null; 122 } 123 } else { 124 izer.pushBack(); 125 return new NameNode(name); 126 } 127 } 128 129 case '"': 130 return new CommentNode(izer.sval); 131 132 case '(': { 133 if (izer.nextToken() == StreamTokenizer.TT_WORD) { 134 String kind = izer.sval; 135 List<Node> list = new ArrayList<Node>(); 136 137 while (izer.nextToken() != ')') { 138 izer.pushBack(); 139 list.add(item()); 140 } 141 Node proto = kindMap.get(kind); 142 if (proto == null) { 143 error("Invalid kind: " + kind); 144 return null; 145 } else { 146 try { 147 Node node = (Node)proto.getClass().newInstance(); 148 node.set(kind, list, izer.lineno()); 149 return node; 150 } catch (InstantiationException exc) { 151 error(exc.toString()); 152 return null; 153 } catch (IllegalAccessException exc) { 154 error(exc.toString()); 155 return null; 156 } 157 } 158 } else { 159 error("Expected kind identifier, got " + izer.ttype + 160 " : " + izer.sval); 161 return null; 162 } 163 } 164 165 default: 166 error("Unexpected character: '" + (char)izer.ttype + "'"); 167 return null; 168 } 169 } 170 171 void error(String errmsg) { 172 System.err.println(Main.specSource + ":" + izer.lineno() + 173 ": " + errmsg); 174 throw new RuntimeException("Error: " + errmsg); 175 } 176 }