001: /*
002: * Copyright 1998-1999 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: abstract class Node {
032:
033: String kind;
034: List<Node> components;
035: int lineno;
036: List<String> commentList = new ArrayList<String>();
037: Node parent = null;
038: Context context;
039:
040: static final int maxStructIndent = 5;
041: static int structIndent = 0; // horrible hack
042:
043: abstract void document(PrintWriter writer);
044:
045: void set(String kind, List<Node> components, int lineno) {
046: this .kind = kind;
047: this .components = components;
048: this .lineno = lineno;
049: }
050:
051: void parentAndExtractComments() {
052: for (Iterator it = components.iterator(); it.hasNext();) {
053: Node node = (Node) it.next();
054: if (node instanceof CommentNode) {
055: it.remove();
056: commentList.add(((CommentNode) node).text());
057: } else {
058: node.parent = this ;
059: node.parentAndExtractComments();
060: }
061: }
062: }
063:
064: void prune() {
065: for (Iterator it = components.iterator(); it.hasNext();) {
066: Node node = (Node) it.next();
067: node.prune();
068: }
069: }
070:
071: void constrain(Context ctx) {
072: context = ctx;
073: for (Iterator it = components.iterator(); it.hasNext();) {
074: Node node = (Node) it.next();
075: constrainComponent(ctx, node);
076: }
077: }
078:
079: void constrainComponent(Context ctx, Node node) {
080: node.constrain(ctx);
081: }
082:
083: void indent(PrintWriter writer, int depth) {
084: for (int i = depth; i > 0; --i) {
085: writer.print(" ");
086: }
087: }
088:
089: void documentIndex(PrintWriter writer) {
090: }
091:
092: void docRowStart(PrintWriter writer) {
093: writer.println("<tr>");
094: if (structIndent > 0) {
095: writer.println("<td colspan=" + structIndent + ">");
096: }
097: }
098:
099: String comment() {
100: StringBuffer comment = new StringBuffer();
101: for (String st : commentList) {
102: comment.append(st);
103: }
104: return comment.toString();
105: }
106:
107: void genJavaComment(PrintWriter writer, int depth) {
108: if (commentList.size() > 0) {
109: indent(writer, depth);
110: writer.println("/**");
111: for (Iterator it = commentList.iterator(); it.hasNext();) {
112: indent(writer, depth);
113: writer.println(" * " + (String) it.next());
114: }
115: indent(writer, depth);
116: writer.println(" */");
117: }
118: }
119:
120: String javaType() {
121: return "-- WRONG ---";
122: }
123:
124: void genJava(PrintWriter writer, int depth) {
125: for (Iterator it = components.iterator(); it.hasNext();) {
126: Node node = (Node) it.next();
127: node.genJava(writer, depth);
128: }
129: }
130:
131: void genCInclude(PrintWriter writer) {
132: for (Iterator it = components.iterator(); it.hasNext();) {
133: Node node = (Node) it.next();
134: node.genCInclude(writer);
135: }
136: }
137:
138: String debugValue(String label) {
139: return label;
140: }
141:
142: void genJavaDebugWrite(PrintWriter writer, int depth,
143: String writeLabel) {
144: genJavaDebugWrite(writer, depth, writeLabel,
145: debugValue(writeLabel));
146: }
147:
148: void genJavaDebugWrite(PrintWriter writer, int depth,
149: String writeLabel, String displayValue) {
150: if (!Main.genDebug) {
151: return;
152: }
153: indent(writer, depth);
154: writer
155: .println("if ((ps.vm.traceFlags & VirtualMachineImpl.TRACE_SENDS) != 0) {");
156: indent(writer, depth + 1);
157: writer.print("ps.vm.printTrace(\"Sending: ");
158: indent(writer, depth); // this is inside the quotes
159: writer.print(writeLabel + "(" + javaType() + "): \" + ");
160: writer.println(displayValue + ");");
161: indent(writer, depth);
162: writer.println("}");
163: }
164:
165: public void genJavaRead(PrintWriter writer, int depth,
166: String readLabel) {
167: error("Internal - Should not call Node.genJavaRead()");
168: }
169:
170: void genJavaDebugRead(PrintWriter writer, int depth,
171: String readLabel, String displayValue) {
172: if (!Main.genDebug) {
173: return;
174: }
175: indent(writer, depth);
176: writer.println("if (vm.traceReceives) {");
177: indent(writer, depth + 1);
178: writer.print("vm.printReceiveTrace(" + depth + ", \"");
179: writer.print(readLabel + "(" + javaType() + "): \" + ");
180: writer.println(displayValue + ");");
181: indent(writer, depth);
182: writer.println("}");
183: }
184:
185: void genJavaPreDef(PrintWriter writer, int depth) {
186: for (Iterator it = components.iterator(); it.hasNext();) {
187: Node node = (Node) it.next();
188: node.genJavaPreDef(writer, depth);
189: }
190: }
191:
192: void error(String errmsg) {
193: System.err.println();
194: System.err.println(Main.specSource + ":" + lineno + ": " + kind
195: + " - " + errmsg);
196: System.err.println();
197: System.exit(1);
198: }
199: }
|