KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > lang > SyntaxForm


1 package kawa.lang;
2 import gnu.expr.*;
3 import gnu.mapping.Symbol;
4 import java.io.*;
5
6 /** A "syntatic closure" - a syntax form with its compilation environment. */
7
8 public class SyntaxForm implements Externalizable
9 {
10   public Object JavaDoc form;
11
12   public TemplateScope scope;
13
14   // PairPosition pos;
15

16   /*DEBUGGING:
17   static int counter;
18   int id = ++counter;
19   */

20
21   private SyntaxForm ()
22   {
23   }
24
25   public static SyntaxForm make (Object JavaDoc form, TemplateScope scope)
26   {
27     SyntaxForm sf = new SyntaxForm();
28     sf.form = form;
29     sf.scope = scope;
30     return sf;
31   }
32
33   public String JavaDoc toString ()
34   {
35     return "#<syntax "
36       + form
37       ///* DEBUGGING:
38
// + " #" id +
39
+ " in #"+scope.id
40       //*/
41
+">";
42   }
43
44   /** Make a SyntaxForm object with the same contextual information as this.
45    * @param form which used for the new syntax value.
46    * Corresponds to the <code>datum-&gt;syntax-object</code> function.
47    */

48   public SyntaxForm fromDatum (Object JavaDoc form)
49   {
50     SyntaxForm sf = new SyntaxForm();
51     sf.form = form;
52     sf.scope = this.scope;
53     return sf;
54   }
55
56   /** Create a syntax object with specified form, and given syntatic context.
57    * Used to implement datum->syntax-object in the syntax-case API.
58    * @param template If this is a SyntaxForm, use its scope;
59    * otherwise use the current Compilation's current scope.
60    * (This means just returning the form as-is.)
61    * @param form The value (S-expression form) to use.
62    */

63   public static Object JavaDoc makeWithTemplate (Object JavaDoc template, Object JavaDoc form)
64   {
65     if (form instanceof SyntaxForm)
66       return (SyntaxForm) form;
67     if (template instanceof SyntaxForm)
68       {
69     SyntaxForm sform = (SyntaxForm) template;
70     if (form == sform.form)
71       return sform;
72     return sform.fromDatum(form);
73       }
74     return form;
75   }
76
77   public SyntaxForm fromDatumIfNeeded (Object JavaDoc form)
78   {
79     if (form == this.form)
80       return this;
81     else if (form instanceof SyntaxForm)
82       return (SyntaxForm) form;
83     else
84       return fromDatum(form);
85   }
86
87   public static Expression rewrite (Object JavaDoc x)
88   {
89     Translator tr = (Translator) Compilation.getCurrent();
90     return tr.rewrite(x);
91   }
92
93   public static Expression rewriteBody (Object JavaDoc x)
94   {
95     Translator tr = (Translator) Compilation.getCurrent();
96     return tr.rewrite_body(x);
97   }
98
99   public boolean isIdentifier ()
100   {
101     return form instanceof String JavaDoc || form instanceof Symbol;
102   }
103
104   public static boolean freeIdentifierEquals (SyntaxForm id1, SyntaxForm id2)
105   {
106     Translator tr = (Translator) Compilation.getCurrent();
107     return tr.lexical.lookup(id1.form, -1) == tr.lexical.lookup(id2.form, -1);
108   }
109
110   public void writeExternal(ObjectOutput out) throws IOException
111   {
112     out.writeObject(form);
113     out.writeObject(scope);
114   }
115
116   public void readExternal(ObjectInput in)
117     throws IOException, ClassNotFoundException JavaDoc
118   {
119     form = in.readObject();
120     scope = (TemplateScope) in.readObject();
121   }
122 }
123
Popular Tags