001: /*
002: * $Id: NamespaceStatement.java,v 1.5 2002/09/16 08:05:06 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.script.statements;
011:
012: import anvil.Location;
013: import anvil.codec.ConstantPool;
014: import anvil.codec.Code;
015: import anvil.codec.ClassRoom;
016: import anvil.codec.Field;
017: import anvil.codec.Method;
018: import anvil.doc.Doc;
019: import anvil.doc.DocParser;
020: import anvil.ErrorListener;
021: import anvil.parser.Tag;
022: import anvil.script.Type;
023: import anvil.script.Scope;
024: import anvil.script.Module;
025: import anvil.script.compiler.ByteCompiler;
026: import anvil.script.CompilableFunction;
027: import anvil.script.expression.Expression;
028: import anvil.script.parser.TemplateParser;
029: import anvil.java.util.Hashlist;
030: import java.io.IOException;
031: import java.util.Enumeration;
032:
033: /**
034: * class NamespaceStatement
035: *
036: * @author: Jani Lehtimäki
037: */
038: public class NamespaceStatement extends DefinitionStatement implements
039: Scope {
040:
041: protected int _poplevel = 0;
042:
043: public NamespaceStatement(Location location,
044: DefinitionStatement parent) {
045: super (parent, location);
046: }
047:
048: public NamespaceStatement(Location location,
049: DefinitionStatement parent, String name, String document) {
050: super (parent, location, name, DocParser.parseNamespace(name,
051: document));
052: setName(name);
053: }
054:
055: public int typeOf() {
056: return Statement.ST_NAMESPACE;
057: }
058:
059: public String name() {
060: return "namespace";
061: }
062:
063: public void setName(String name) {
064: _name = name;
065: _descriptor = _parent.getDescriptor() + "$n_" + _name;
066: }
067:
068: public void setPopLevel(int level) {
069: _poplevel = level;
070: }
071:
072: public int getType() {
073: return NAMESPACE;
074: }
075:
076: public VariableStatement declare(Location location, String name,
077: Expression expr, String document, boolean statik) {
078: VariableStatement var = new StaticVariableStatement(location,
079: this , name, expr, document);
080: declare(var);
081: return var;
082: }
083:
084: public Type lookupDeclaration(String name) {
085: Type type = (Type) _types.get(name);
086: if (type != null) {
087: return type;
088: }
089: return super .lookupDeclaration(name);
090: }
091:
092: public void parse(TemplateParser parser, Tag tag) {
093: super .parse(parser, tag);
094: }
095:
096: public void onCharacters(TemplateParser parser, String cdata) {
097: }
098:
099: public boolean onTag(TemplateParser parser, int type, Tag tag) {
100: switch (type) {
101: case ST_TAG:
102: break;
103:
104: case ST_IMPORT:
105: onImport(parser, tag);
106: break;
107:
108: case ST_VAR:
109: onVar(parser, tag);
110: break;
111:
112: case ST_CONST:
113: onConst(parser, tag);
114: break;
115:
116: case ST_FUNCTION:
117: onFunction(parser, type, tag);
118: break;
119:
120: case ST_CLASS:
121: onClass(parser, type, tag);
122: break;
123:
124: case ST_NAMESPACE:
125: onNamespace(parser, type, tag);
126: break;
127:
128: case ST_ENDNAMESPACE:
129: while (_poplevel-- > 0) {
130: parser.pop();
131: }
132: break;
133:
134: default:
135: return false;
136:
137: }
138: return true;
139: }
140:
141: public void check(ErrorListener context) {
142: super .check(context);
143: }
144:
145: public void compile(ByteCompiler context) {
146: ClassRoom clazz = context.getClassRoom().createClass(
147: getDescriptor(), "n_" + _name);
148: clazz.setAccessFlags(context.ACC_PUBLIC);
149: ConstantPool pool = clazz.getPool();
150: context.pushClass(clazz);
151: Field typefield1 = clazz.createField("_class",
152: "Lanvil/script/compiler/CompiledNamespace;",
153: Code.ACC_PUBLIC | Code.ACC_STATIC);
154: Field typefield2 = clazz.createField("_type",
155: "Lanvil/core/Any;", Code.ACC_PUBLIC | Code.ACC_STATIC);
156: clazz.setSuperClassname(context.TYPE_OBJECT);
157: clazz.setAccessFlags(Code.ACC_SUPER | Code.ACC_PUBLIC);
158:
159: compileMembers(context, _types.size(), _types.elements());
160:
161: Code code = clazz.getStatic().getCode();
162: context.pushCode(code);
163: //code.println("INTERFACE-START:"+getDescriptor());
164: code.getstatic(pool.addFieldRef(_parent.getDescriptor(),
165: "_members", "[Ljava/lang/Object;"));
166: code.pop();
167: //code.println("INTERFACE-END:"+getDescriptor());
168: code.vreturn();
169: context.popCode();
170:
171: super.compile(context);
172:
173: context.popClass();
174: }
175:
176: }
|