001: /*
002: * Copyright 1998-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.jdwpgen;
027:
028: import java.util.*;
029: import java.io.*;
030:
031: class Parse {
032:
033: final StreamTokenizer izer;
034: final Map<String, Node> kindMap = new HashMap<String, Node>();
035:
036: Parse(Reader reader) {
037: izer = new StreamTokenizer(new BufferedReader(reader));
038: izer.resetSyntax();
039: izer.slashStarComments(true);
040: izer.slashSlashComments(true);
041: izer.wordChars((int) 'a', (int) 'z');
042: izer.wordChars((int) 'A', (int) 'Z');
043: izer.wordChars((int) '0', (int) '9');
044: izer.wordChars((int) '_', (int) '_');
045: izer.wordChars((int) '-', (int) '-');
046: izer.wordChars((int) '.', (int) '.');
047: izer.whitespaceChars(0, 32);
048: izer.quoteChar('"');
049: izer.quoteChar('\'');
050:
051: kindMap.put("CommandSet", new CommandSetNode());
052: kindMap.put("Command", new CommandNode());
053: kindMap.put("Out", new OutNode());
054: kindMap.put("Reply", new ReplyNode());
055: kindMap.put("ErrorSet", new ErrorSetNode());
056: kindMap.put("Error", new ErrorNode());
057: kindMap.put("Event", new EventNode());
058: kindMap.put("Repeat", new RepeatNode());
059: kindMap.put("Group", new GroupNode());
060: kindMap.put("Select", new SelectNode());
061: kindMap.put("Alt", new AltNode());
062: kindMap.put("ConstantSet", new ConstantSetNode());
063: kindMap.put("Constant", new ConstantNode());
064: kindMap.put("int", new IntTypeNode());
065: kindMap.put("long", new LongTypeNode());
066: kindMap.put("boolean", new BooleanTypeNode());
067: kindMap.put("object", new ObjectTypeNode());
068: kindMap.put("threadObject", new ThreadObjectTypeNode());
069: kindMap.put("threadGroupObject",
070: new ThreadGroupObjectTypeNode());
071: kindMap.put("arrayObject", new ArrayObjectTypeNode());
072: kindMap.put("stringObject", new StringObjectTypeNode());
073: kindMap.put("classLoaderObject",
074: new ClassLoaderObjectTypeNode());
075: kindMap.put("classObject", new ClassObjectTypeNode());
076: kindMap.put("referenceType", new ReferenceTypeNode());
077: kindMap.put("referenceTypeID", new ReferenceIDTypeNode());
078: kindMap.put("classType", new ClassTypeNode());
079: kindMap.put("interfaceType", new InterfaceTypeNode());
080: kindMap.put("arrayType", new ArrayTypeNode());
081: kindMap.put("method", new MethodTypeNode());
082: kindMap.put("field", new FieldTypeNode());
083: kindMap.put("frame", new FrameTypeNode());
084: kindMap.put("string", new StringTypeNode());
085: kindMap.put("value", new ValueTypeNode());
086: kindMap.put("byte", new ByteTypeNode());
087: kindMap.put("location", new LocationTypeNode());
088: kindMap.put("tagged-object", new TaggedObjectTypeNode());
089: kindMap.put("referenceTypeID", new ReferenceIDTypeNode());
090: kindMap.put("typed-sequence", new ArrayRegionTypeNode());
091: kindMap.put("untagged-value", new UntaggedValueTypeNode());
092: }
093:
094: RootNode items() throws IOException {
095: List<Node> list = new ArrayList<Node>();
096:
097: while (izer.nextToken() != StreamTokenizer.TT_EOF) {
098: izer.pushBack();
099: list.add(item());
100: }
101: RootNode node = new RootNode();
102: node.set("Root", list, 1);
103: return node;
104: }
105:
106: Node item() throws IOException {
107: switch (izer.nextToken()) {
108: case StreamTokenizer.TT_EOF:
109: error("Unexpect end-of-file");
110: return null;
111:
112: case StreamTokenizer.TT_WORD: {
113: String name = izer.sval;
114: if (izer.nextToken() == '=') {
115: int ntok = izer.nextToken();
116: if (ntok == StreamTokenizer.TT_WORD) {
117: return new NameValueNode(name, izer.sval);
118: } else if (ntok == '\'') {
119: return new NameValueNode(name, izer.sval.charAt(0));
120: } else {
121: error("Expected value after: " + name + " =");
122: return null;
123: }
124: } else {
125: izer.pushBack();
126: return new NameNode(name);
127: }
128: }
129:
130: case '"':
131: return new CommentNode(izer.sval);
132:
133: case '(': {
134: if (izer.nextToken() == StreamTokenizer.TT_WORD) {
135: String kind = izer.sval;
136: List<Node> list = new ArrayList<Node>();
137:
138: while (izer.nextToken() != ')') {
139: izer.pushBack();
140: list.add(item());
141: }
142: Node proto = kindMap.get(kind);
143: if (proto == null) {
144: error("Invalid kind: " + kind);
145: return null;
146: } else {
147: try {
148: Node node = (Node) proto.getClass()
149: .newInstance();
150: node.set(kind, list, izer.lineno());
151: return node;
152: } catch (Exception exc) {
153: error(exc.toString());
154: }
155: }
156: } else {
157: error("Expected kind identifier, got " + izer.ttype
158: + " : " + izer.sval);
159: return null;
160: }
161: }
162:
163: default:
164: error("Unexpected character: '" + (char) izer.ttype + "'");
165: return null;
166: }
167: }
168:
169: void error(String errmsg) {
170: System.err.println(Main.specSource + ":" + izer.lineno() + ": "
171: + errmsg);
172: System.exit(1);
173: }
174: }
|