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