KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > lang > Syntax


1 package kawa.lang;
2 import gnu.mapping.*;
3 import gnu.expr.*;
4 import gnu.lists.*;
5 import gnu.text.Printable;
6
7 /**
8  * Abstract class for "syntax" objects.
9  * Builtins and macros are instances of this class.
10  * @author Per Bothner
11  */

12
13 abstract public class Syntax implements Printable, Named
14 {
15   Object JavaDoc name;
16
17   public final String JavaDoc getName()
18   {
19     return name == null ? null
20       : name instanceof Symbol ? ((Symbol) name).getName()
21       : name.toString();
22   }
23   public Object JavaDoc getSymbol() { return name; }
24
25   public void setName (Object JavaDoc name) { this.name = name; }
26   public void setName (String JavaDoc name) { this.name = name; }
27
28   public Syntax ()
29   {
30   }
31
32   public Syntax (Object JavaDoc name)
33   {
34     setName(name);
35   }
36
37   /**
38    * Re-write an expression that is an "application" of this Syntax object.
39    * @param obj the arguments to this "application" (i.e. the cdr of
40    * the macro/builtin invokation)
41    * @param tr the Translator that provides context
42    * @return the re-written expression
43    */

44
45   public Expression rewrite (Object JavaDoc obj, Translator tr)
46   {
47     throw new InternalError JavaDoc("rewrite method not defined");
48   }
49
50   public Expression rewriteForm (Object JavaDoc form, Translator tr)
51   {
52     if (form instanceof Pair)
53       return rewriteForm((Pair) form, tr);
54     else
55       return tr.syntaxError("non-list form for "+this);
56   }
57
58   public Expression rewriteForm (Pair form, Translator tr)
59   {
60     return rewrite(form.cdr, tr);
61   }
62
63   public void scanForm (Pair st, ScopeExp defs, Translator tr)
64   {
65     boolean ok = scanForDefinitions(st, tr.formStack, defs, tr);
66     if (! ok)
67       tr.formStack.add(new ErrorExp("syntax error expanding "+this));
68   }
69
70   /** Check if a statement is a definition, for initial pass.
71    * Semi-deprecated - should convert calls to use scanForm.
72    * @param st the statement to check
73    * @param forms where to append the (possibly-modified) statement
74    * @param defs where to add Declarations for found definitions
75    * @param tr the compilation state
76    * @return true on success
77    */

78   public boolean scanForDefinitions (Pair st, java.util.Vector JavaDoc forms,
79                                     ScopeExp defs, Translator tr)
80   {
81     forms.addElement(st);
82     return true;
83   }
84
85   public void print (Consumer out)
86   {
87     out.write("#<syntax ");
88     String JavaDoc name = this.getName();
89     out.write(name == null ? "<unnamed>" : name);
90     out.write('>');
91   }
92 }
93
Popular Tags