KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > lang > AutoloadSyntax


1 package kawa.lang;
2 import gnu.mapping.*;
3 import gnu.expr.*;
4 import gnu.lists.*;
5 import java.io.*;
6
7 /**
8  * Implement autoloading of Syntax (including macros).
9  * A named class is loaded, and apply requests are forwarded to it.
10  * @author Per Bothner
11  */

12
13 public class AutoloadSyntax extends Syntax implements Externalizable
14 {
15   /** The name of the class that defines the macro/builtin.
16    * It must be the name of a class in the CLASSPATH (for example:
17    * "kawa.standard.list"). Either the class must extend Syntax
18    * (and have a default constructor), or the class must be a ModuleMody,
19    * (whose apply0 () is expected to define the Syntax in the
20    * global environment. */

21   String JavaDoc className;
22
23   Environment env;
24
25   /** The loaded syntax, or null if it has not yet been loaded. */
26   Syntax loaded;
27
28   public AutoloadSyntax ()
29   {
30   }
31
32   public AutoloadSyntax (String JavaDoc name, String JavaDoc className)
33   {
34     super(name);
35     this.className = className;
36   }
37
38   public AutoloadSyntax (String JavaDoc name, String JavaDoc className, Environment env)
39   {
40     super(name);
41     this.className = className;
42     this.env = env;
43   }
44
45   public void print(java.io.PrintWriter JavaDoc ps)
46   {
47     ps.print(toString());
48   }
49
50   public String JavaDoc toString ()
51   {
52     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(100);
53     sbuf.append("#<syntax ");
54     if (getName() != null)
55       {
56     sbuf.append(getName());
57     sbuf.append(' ');
58       }
59     if (loaded != null)
60       sbuf.append("autoloaded>");
61     else
62       {
63     sbuf.append("autoload ");
64     sbuf.append(className);
65     sbuf.append(">");
66       }
67     return sbuf.toString();
68   }
69
70   private void throw_error (String JavaDoc prefix)
71   {
72     throw new GenericError (prefix + className
73                 + " while autoloading "
74                 + (getName() == null ? "" : getName().toString()));
75   }
76
77   /** Load the class named in className. */
78   void load ()
79   {
80     String JavaDoc name = this.getName();
81     try
82       {
83     Object JavaDoc value = Class.forName (className).newInstance ();
84         if (value instanceof Syntax)
85       {
86         loaded = (Syntax) value;
87         if (name != null && loaded.getName() == null)
88           loaded.setName(name);
89       }
90     else
91       throw_error ("failed to autoload valid syntax object ");
92       }
93     catch (ClassNotFoundException JavaDoc ex)
94       { throw_error ("failed to find class "); }
95     catch (InstantiationException JavaDoc ex)
96       { throw_error ("failed to instantiate class "); }
97     catch (IllegalAccessException JavaDoc ex)
98       { throw_error ("illegal access in class "); }
99     catch (UnboundLocationException e)
100       { throw_error ("missing symbol '" + e.getMessage () + "' "); }
101     catch (WrongArguments ex)
102       { throw_error ("type error"); }
103   }
104
105   public void scanForm (Pair st, ScopeExp defs, Translator tr)
106   {
107     if (loaded == null)
108       {
109     try
110       {
111         load ();
112       }
113     catch (RuntimeException JavaDoc e)
114       {
115         tr.syntaxError(e.getMessage ());
116             return;
117       }
118       }
119     loaded.scanForm(st, defs, tr);
120   }
121
122   public Expression rewriteForm (Pair form, Translator tr)
123   {
124     if (loaded == null)
125       {
126     try
127       {
128         load ();
129       }
130     catch (GenericError e)
131       {
132         return tr.syntaxError (e.getMessage ());
133       }
134     catch (WrongType e)
135       {
136         return tr.syntaxError (e.getMessage ());
137       }
138       }
139     Syntax saveSyntax = tr.currentSyntax;
140     tr.currentSyntax = loaded;
141     try
142       {
143     return loaded.rewriteForm(form, tr);
144       }
145     finally
146       {
147     tr.currentSyntax = saveSyntax;
148       }
149   }
150
151   public void writeExternal(ObjectOutput out) throws IOException
152   {
153     out.writeObject(getName());
154     out.writeObject(className);
155   }
156
157   public void readExternal(ObjectInput in)
158     throws IOException, ClassNotFoundException JavaDoc
159   {
160     setName((String JavaDoc) in.readObject());
161     className = (String JavaDoc) in.readObject();
162   }
163 }
164
Popular Tags