01: package gnu.kawa.lispexpr;
02:
03: import kawa.lang.*;
04: import gnu.expr.*;
05: import gnu.mapping.*;
06: import gnu.lists.*;
07: import gnu.bytecode.ClassType;
08: import gnu.kawa.xml.XmlNamespace;
09:
10: public class DefineNamespace extends Syntax {
11: private boolean makePrivate;
12: private boolean makeXML;
13:
14: public static final DefineNamespace define_namespace = new DefineNamespace();
15: public static final DefineNamespace define_private_namespace = new DefineNamespace();
16: public static final DefineNamespace define_xml_namespace = new DefineNamespace();
17: static {
18: define_namespace.setName("define-namespace");
19: define_private_namespace.setName("define-private-namespace");
20: define_private_namespace.makePrivate = true;
21: define_xml_namespace.setName("define-xml-namespace");
22: define_xml_namespace.makeXML = true;
23: }
24:
25: public boolean scanForDefinitions(Pair st, java.util.Vector forms,
26: ScopeExp defs, Translator tr) {
27: Pair p1, p2;
28: if (!(st.cdr instanceof Pair)
29: || !((p1 = (Pair) st.cdr).car instanceof String)
30: || !(p1.cdr instanceof Pair)
31: || (p2 = (Pair) p1.cdr).cdr != LList.Empty) {
32: tr.error('e', "invalid syntax for define-namespace");
33: return false;
34: }
35: String name = (String) p1.car;
36: Declaration decl = defs.getDefine(name, 'w', tr);
37: tr.push(decl);
38: decl.setFlag(Declaration.IS_CONSTANT
39: | Declaration.IS_NAMESPACE_PREFIX);
40: if (makePrivate) {
41: decl.setFlag(Declaration.PRIVATE_SPECIFIED);
42: decl.setPrivate(true);
43: } else if (defs instanceof ModuleExp)
44: decl.setCanRead(true);
45: Translator.setLine(decl, p1);
46: Expression value;
47: String literal = null;
48: if (p2.car instanceof FString) {
49: literal = p2.car.toString();
50: Namespace namespace;
51: if (literal.startsWith("class:")) {
52: String cname = literal.substring(6);
53: namespace = ClassNamespace.getInstance(literal,
54: ClassType.make(cname));
55: decl.setType(ClassType
56: .make("gnu.kawa.lispexpr.ClassNamespace"));
57: } else if (makeXML) {
58: namespace = XmlNamespace.getInstance(name, literal);
59: decl.setType(ClassType
60: .make("gnu.kawa.xml.XmlNamespace"));
61: } else {
62: namespace = Namespace.getInstance(literal);
63: decl.setType(ClassType.make("gnu.mapping.Namespace"));
64: }
65: value = new QuoteExp(namespace);
66: decl.setFlag(Declaration.TYPE_SPECIFIED);
67: } else
68: value = tr.rewrite_car(p2, false);
69: decl.noteValue(value);
70: forms.addElement(SetExp.makeDefinition(decl, value));
71: return true;
72: }
73:
74: public Expression rewriteForm(Pair form, Translator tr) {
75: return tr
76: .syntaxError("define-namespace is only allowed in a <body>");
77: }
78:
79: public static final String XML_NAMESPACE_MAGIC = "&xml&";
80: }
|