001: /**
002: * Parser encapsulation of modifiers for classes, functions
003: * (private/public/protected/internal/static/final/dynamic/override)
004: */package org.openlaszlo.sc.parser;
005:
006: public class ASTModifiedDefinition extends SimpleNode {
007:
008: public static final String DEFAULT_ACCESS = "<default>";
009: public static final String PUBLIC_ACCESS = "public";
010: public static final String PROTECTED_ACCESS = "protected";
011: public static final String INTERNAL_ACCESS = "internal";
012: public static final String PRIVATE_ACCESS = "private";
013:
014: private String access = DEFAULT_ACCESS;
015: private boolean isStatic = false;
016: private boolean isFinal = false;
017: private boolean isDynamic = false;
018: private boolean isOverride = false;
019: private String namespace = null;
020:
021: private Token t;
022:
023: public ASTModifiedDefinition(int id) {
024: super (id);
025: }
026:
027: public ASTModifiedDefinition(Parser p, int id) {
028: super (p, id);
029: }
030:
031: public static Node jjtCreate(int id) {
032: return new ASTModifiedDefinition(id);
033: }
034:
035: public static Node jjtCreate(Parser p, int id) {
036: return new ASTModifiedDefinition(p, id);
037: }
038:
039: // Token is used for error reporting
040: public ASTModifiedDefinition setToken(Token t) {
041: this .t = t;
042: return this ;
043: }
044:
045: public void setNamespace(String value) {
046: if (access != DEFAULT_ACCESS) {
047: throw new ParseException(t, "cannot use namespace \""
048: + value + "\" with visibility \"" + access + "\"");
049: }
050: if (namespace != null) {
051: throw new ParseException(t,
052: "cannot set namespace twice: \"" + namespace
053: + "\" and \"" + value + "\"");
054: }
055: this .namespace = value;
056: }
057:
058: public void setAccess(String value) {
059: if (access != DEFAULT_ACCESS && value != access) {
060: throw new ParseException(t, "access level changed from \""
061: + access + "\" to \"" + value + "\"");
062: }
063: if (namespace != null) {
064: throw new ParseException(t, "cannot use namespace \""
065: + namespace + "\" with visibility \"" + value
066: + "\"");
067: }
068: this .access = value;
069: }
070:
071: public String getAccess() {
072: return this .access;
073: }
074:
075: public void setStatic(boolean value) {
076: isStatic = true;
077: }
078:
079: public boolean isStatic() {
080: return isStatic;
081: }
082:
083: public void setFinal(boolean value) {
084: isFinal = true;
085: }
086:
087: public boolean isFinal() {
088: return isFinal;
089: }
090:
091: public void setDynamic(boolean value) {
092: isDynamic = true;
093: }
094:
095: public boolean isDynamic() {
096: return isDynamic;
097: }
098:
099: public void setOverride(boolean value) {
100: isOverride = true;
101: }
102:
103: public boolean isOverride() {
104: return isOverride;
105: }
106:
107: private void verifyVariable(SimpleNode subnode) {
108: if (isOverride)
109: throw new ParseException(
110: "cannot use override on variable: " + subnode);
111: if (isDynamic)
112: throw new ParseException("cannot use dynamic on variable: "
113: + subnode);
114: }
115:
116: private void verifyFunction(SimpleNode subnode) {
117: if (isDynamic)
118: throw new ParseException("cannot use dynamic on variable: "
119: + subnode);
120: }
121:
122: private void verifyClass(SimpleNode subnode) {
123: if (isOverride)
124: throw new ParseException("cannot use override on class: "
125: + subnode);
126: }
127:
128: public void verifyTopLevel(SimpleNode subnode) {
129: if (subnode instanceof ASTVariableStatement)
130: verifyVariable(subnode);
131: else if (subnode instanceof ASTFunctionDeclaration)
132: verifyFunction(subnode);
133: else if (subnode instanceof ASTClassDefinition)
134: verifyClass(subnode);
135: else
136: throw new ParseException("unexpected type at top level: "
137: + subnode);
138: }
139:
140: public void verifyClassLevel(SimpleNode subnode) {
141: if (subnode instanceof ASTVariableStatement)
142: verifyVariable(subnode);
143: else if (subnode instanceof ASTFunctionDeclaration)
144: verifyFunction(subnode);
145: else if (subnode instanceof ASTClassDefinition)
146: throw new ParseException("inner classes not allowed: "
147: + subnode);
148: else
149: throw new ParseException("unexpected type at class level: "
150: + subnode);
151: }
152:
153: public String toJavascriptString() {
154: String str = namespace;
155: if (namespace == null) {
156: if (access == DEFAULT_ACCESS)
157: str = "";
158: else
159: str = access;
160: }
161: if (isStatic)
162: str += " static";
163: if (isDynamic)
164: str += " dynamic";
165: if (isFinal)
166: str += " final";
167: if (isOverride)
168: str += " override";
169:
170: // might be a leading blank if no namespace
171: if (str.length() > 0 && str.charAt(0) == ' ')
172: str = str.substring(1);
173:
174: return str;
175: }
176:
177: public String toString() {
178: String str = toJavascriptString();
179: // For clarity, mark the namespace as such,
180: // it always appears first
181: if (namespace != null)
182: str = "namespace=" + str;
183: return "ModifiedDefinition(" + str + ")";
184: }
185:
186: /** Accept the visitor */
187: /* TODO: [2007-11-27 dda] Needed when VISITOR=true
188: public Object jjtAccept(ParserVisitor visitor, Object data) {
189: return visitor.visit(this, data);
190: }
191: */
192:
193: }
194:
195: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
196: * Copyright 2007 Laszlo Systems, Inc. All Rights Reserved. *
197: * Use is subject to license terms. *
198: * J_LZ_COPYRIGHT_END *********************************************************/
|