001: /*
002: *
003: *
004: * Copyright 1990-2007 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 makedep;
028:
029: import java.io.*;
030: import java.util.*;
031:
032: public class MacroDefinitions {
033: private Hashtable macros;
034:
035: public MacroDefinitions() {
036: macros = new Hashtable();
037: }
038:
039: private String lookup(String name) throws NoSuchElementException {
040: String s = (String) macros.get(name);
041: if (s != null) {
042: return s;
043: }
044: throw new NoSuchElementException(name);
045: }
046:
047: public void addMacro(String name, String contents) {
048: //System.out.println("adding macro: "+ name + " = \"" + contents + "\"");
049: macros.put(name, contents);
050: }
051:
052: private boolean lineIsEmpty(String s) {
053: for (int i = 0; i < s.length(); i++) {
054: if (!Character.isWhitespace(s.charAt(i))) {
055: return false;
056: }
057: }
058: return true;
059: }
060:
061: public void readFrom(String fileName, boolean missingOk)
062: throws FileNotFoundException, FileFormatException,
063: IOException {
064: BufferedReader reader = null;
065: try {
066: reader = new BufferedReader(new FileReader(fileName));
067: } catch (FileNotFoundException e) {
068: if (missingOk) {
069: return;
070: } else {
071: throw (e);
072: }
073: }
074: String line;
075: do {
076: line = reader.readLine();
077: if (line != null) {
078: if ((!line.startsWith("//")) && (!lineIsEmpty(line))) {
079: int nameBegin = -1;
080: int nameEnd = -1;
081: boolean gotEquals = false;
082: int contentsBegin = -1;
083: int contentsEnd = -1;
084:
085: int i = 0;
086: // Scan forward for beginning of name
087: while (i < line.length()) {
088: if (!Character.isWhitespace(line.charAt(i))) {
089: break;
090: }
091: i++;
092: }
093: nameBegin = i;
094:
095: // Scan forward for end of name
096: while (i < line.length()) {
097: if (Character.isWhitespace(line.charAt(i))) {
098: break;
099: }
100: if (line.charAt(i) == '=') {
101: break;
102: }
103: i++;
104: }
105: nameEnd = i;
106:
107: // Scan forward for equals sign
108: while (i < line.length()) {
109: if (line.charAt(i) == '=') {
110: gotEquals = true;
111: break;
112: }
113: i++;
114: }
115:
116: // Scan forward for start of contents
117: i++;
118: while (i < line.length()) {
119: if (!Character.isWhitespace(line.charAt(i))) {
120: break;
121: }
122: i++;
123: }
124: contentsBegin = i;
125:
126: // Scan *backward* for end of contents
127: i = line.length() - 1;
128: while (i >= 0) {
129: if (!Character.isWhitespace(line.charAt(i))) {
130: break;
131: }
132: i--;
133: }
134: contentsEnd = i + 1;
135:
136: String name = null, contents = null;
137: if (nameBegin < nameEnd) {
138: name = line.substring(nameBegin, nameEnd);
139: }
140: if (gotEquals) {
141: if (contentsBegin < contentsEnd) {
142: contents = line.substring(contentsBegin,
143: contentsEnd);
144: } else {
145: contents = "";
146: }
147: }
148:
149: if (name == null || contents == null) {
150: throw new FileFormatException(
151: "Expected \"macroname = value\", "
152: + "but found: " + line);
153: }
154:
155: addMacro(name, contents);
156: }
157: }
158: } while (line != null);
159:
160: reader.close();
161:
162: /*
163: * iarch and carch are defined only for a few platforms that
164: * have different interpreter and compiler CPU architectures
165: * (such as AOT-enabled ROM generator). On most platforms,
166: * iarch and carch are not defined and should have the same
167: * value as arch.
168: */
169: String arch = lookup("arch");
170: String iarch = getMacroContent("iarch");
171: String carch = getMacroContent("carch");
172: if (iarch == null || iarch.equals("")) {
173: addMacro("iarch", arch);
174: }
175: if (carch == null || carch.equals("")) {
176: addMacro("carch", arch);
177: }
178: }
179:
180: /** Throws IllegalArgumentException if passed token is illegally
181: formatted */
182: public String expand(String token) throws IllegalArgumentException {
183: // the token may contain one or more <macroName>'s
184:
185: String out = "";
186:
187: // emacs lingo
188: int mark = 0;
189: int point = 0;
190:
191: int len = token.length();
192:
193: if (len == 0)
194: return out;
195:
196: do {
197: // Scan "point" forward until hitting either the end of
198: // the string or the beginning of a macro
199: if (token.charAt(point) == '<') {
200: // Append (point - mark) to out
201: if ((point - mark) != 0) {
202: out += token.substring(mark, point);
203: }
204: mark = point + 1;
205: // Scan forward from point for right bracket
206: point++;
207: while ((point < len) && (token.charAt(point) != '>')) {
208: point++;
209: }
210: if (point == len) {
211: throw new IllegalArgumentException(
212: "Could not find right angle-bracket in token "
213: + token);
214: }
215: String name = token.substring(mark, point);
216: if (name == null) {
217: throw new IllegalArgumentException(
218: "Empty macro in token " + token);
219: }
220: try {
221: String contents = lookup(name);
222: out += contents;
223: point++;
224: mark = point;
225: } catch (NoSuchElementException e) {
226: throw new IllegalArgumentException("Unknown macro "
227: + name + " in token " + token);
228: }
229: } else {
230: point++;
231: }
232: } while (point != len);
233:
234: if (mark != point) {
235: out += token.substring(mark, point);
236: }
237:
238: return out;
239: }
240:
241: public MacroDefinitions copy() {
242: MacroDefinitions ret = new MacroDefinitions();
243: for (Enumeration e = macros.keys(); e.hasMoreElements();) {
244: String name = (String) e.nextElement();
245: String value = (String) macros.get(name);
246: ret.macros.put(name, value);
247: }
248: return ret;
249: }
250:
251: public void setAllMacroBodiesTo(String s) {
252: for (Enumeration e = macros.keys(); e.hasMoreElements();) {
253: String name = (String) e.nextElement();
254: macros.put(name, s);
255: }
256: }
257:
258: public String getMacroContent(String name) {
259: try {
260: return lookup(name);
261: } catch (NoSuchElementException e) {
262: return null;
263: }
264: }
265:
266: private void error(String text) throws FileFormatException {
267: throw new FileFormatException(
268: "Expected \"macroname = value\", but found: " + text);
269: }
270: }
|