b653c46bd1eea397d5a7529bd0ad35acb192260e
[org.ibex.core.git] / src / org / xwt / util / Preprocessor.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt.util;
3
4 import java.util.*;
5 import java.io.*;
6
7 /**
8  *   A VERY crude, inefficient Java preprocessor
9  *
10  *   //#define FOO bar baz       -- replace all instances of token FOO with "bar baz"
11  *   //#replace foo/bar baz/bop  -- DUPLICATE everything between here and //#end,
12  *                                  replacing foo with bar and baz with bop in the *second* copy
13  *
14  *   Replacements are done on a token basis.  Tokens are defined as a
15  *   sequence of characters which all belong to a single class.  The
16  *   two character classes are:
17  *
18  *     - [a-zA-Z0-9_]
19  *     - all other non-whitespace characters
20  */
21 public class Preprocessor {
22
23     static Hashtable replace = new Hashtable();
24     static Hashtable savereplace = replace;
25     static Vector sinceLastRepeat = null;
26
27     public static void main(String[] args) throws IOException {
28         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
29         String s = null;
30
31         while((s = br.readLine()) != null) {
32             if (sinceLastRepeat != null) sinceLastRepeat.addElement(s);
33             String trimmed = s.trim();
34             if (trimmed.startsWith("//#define ")) {
35                 trimmed = trimmed.substring(10).trim();
36                 String key = trimmed.substring(0, trimmed.indexOf(' '));
37                 String val = trimmed.substring(trimmed.indexOf(' ')).trim();
38                 replace.put(key, val);
39                 
40             } else if (trimmed.startsWith("//#repeat ")) {
41                 StringTokenizer st = new StringTokenizer(trimmed.substring(9), " ");
42                 savereplace = replace;
43                 replace = (Hashtable)replace.clone();
44                 while (st.hasMoreTokens()) {
45                     String tok = st.nextToken().trim();
46                     String key = tok.substring(0, tok.indexOf('/'));
47                     String val = tok.substring(tok.indexOf('/') + 1);
48                     replace.put(key, val);
49                 }
50                 sinceLastRepeat = new Vector();
51
52             } else if (trimmed.startsWith("//#end")) {
53                 replace = savereplace;
54                 System.out.println();
55                 for(int i=0; i<sinceLastRepeat.size(); i++) processLine((String)sinceLastRepeat.elementAt(i));
56                 sinceLastRepeat = null;
57
58             } else {
59                 processLine(s);
60             }
61            
62         }
63
64     }
65
66     static void processLine(String s) throws IOException {
67         for(int i=0; i<s.length(); i++) {
68             char c = s.charAt(i);
69             if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_') {
70                 System.out.print(c);
71                 continue;
72             }
73             int j;
74             for(j = i; j < s.length(); j++) {
75                 c = s.charAt(j);
76                 if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_') break;
77             }
78             String tok = s.substring(i, j);
79             String val = (String)replace.get(tok);
80             if (val != null) System.out.print(val);
81             else System.out.print(tok);
82             i = j - 1;
83         }
84         System.out.println();
85     }
86 }
87