PageRenderTime 53ms CodeModel.GetById 24ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XML/sidekick/ecmascript/parser/ASTLiteral.java

#
Java | 364 lines | 284 code | 50 blank | 30 comment | 71 complexity | f4dcd29c9860d7caab188330367abf73 MD5 | raw file
  1/* Generated By:JJTree: Do not edit this line. ASTLiteral.java */
  2
  3package sidekick.ecmascript.parser;
  4
  5import sidekick.ecmascript.parser.EcmaScript;
  6import sidekick.ecmascript.parser.EcmaScriptVisitor;
  7import sidekick.ecmascript.parser.ParseException;
  8import sidekick.ecmascript.parser.Token;
  9
 10public class ASTLiteral extends SimpleNode {
 11
 12    static public final Object NULL = new Object();
 13
 14    static public final Object REGEX = new Object();
 15
 16    static public final Object HTML = new Object();
 17
 18    protected String tokenImage;
 19
 20    protected Object value;
 21
 22    public ASTLiteral(int id) {
 23        super(id);
 24    }
 25
 26    public ASTLiteral(EcmaScript p, int id) {
 27        super(p, id);
 28    }
 29
 30    /** Accept the visitor. * */
 31    @Override
 32    public Object jjtAccept(EcmaScriptVisitor visitor, Object data) {
 33        return visitor.visit(this, data);
 34    }
 35
 36    public void setValue(Object value) {
 37        this.value = value;
 38    }
 39
 40    public Object getValue() {
 41        return value;
 42    }
 43
 44    public String getTokenImage() {
 45        return tokenImage;
 46    }
 47
 48    static public String unescapedString(String image) {
 49        image = image.substring(1, image.length() - 1);
 50
 51        int l = image.length();
 52        StringBuffer sb = new StringBuffer(l);
 53
 54        /*
 55         *
 56         * \XXX - The character specified by up to three octal digits XXX
 57         * between 0 and 377 ( example: \251 ) \xXX - The character specified by
 58         * the two hexadecimal digits XX between 00 and FF ( example: \xA9 ) \
 59         * uXXXX - The Unicode character specified by the four hexadecimal
 60         * digits XXXX ( example: \u00A9 )
 61         */
 62        for (int i = 0; i < l; i++) {
 63            char c = image.charAt(i);
 64            if ((c == '\\') && (i + 1 < l)) {
 65                i++;
 66                c = image.charAt(i);
 67                if (c == 'n') {
 68                    c = '\n';
 69                } else if (c == 'b') {
 70                    c = '\b';
 71                } else if (c == 'f') {
 72                    c = '\f';
 73                } else if (c == 'r') {
 74                    c = '\r';
 75                } else if (c == 't') {
 76                    c = '\t';
 77                } else if (c == 'x' && l == 4) {
 78                    c = (char) (hexval(image.charAt(i + 1)) << 4 | hexval(image
 79                            .charAt(i + 1)));
 80                    i += 2;
 81                } else if (c == 'u') {
 82                    c = (char) (hexval(image.charAt(i + 1)) << 12
 83                            | hexval(image.charAt(i + 2)) << 8
 84                            | hexval(image.charAt(i + 3)) << 4 | hexval(image
 85                            .charAt(i + 4)));
 86                    i += 4;
 87                } else if ((c >= '0') && (c <= '7') && l == 4) {
 88                    c = (char) (octval(image.charAt(i)));
 89                    if ((image.length() > i) && (image.charAt(i + 1) >= '0')
 90                            && (image.charAt(i + 1) <= '7')) {
 91                        i++;
 92                        c = (char) ((c << 4) | octval(image.charAt(i)));
 93                    }
 94                }
 95            }
 96            sb.append(c);
 97        }
 98        return sb.toString();
 99    }
100
101    static public String escapedString(String image, char quoteChar) {
102        int l = image.length();
103        StringBuffer sb = new StringBuffer(l);
104
105        for (int i = 0; i < l; i++) {
106            char c = image.charAt(i);
107
108            if (c == '\n') {
109                sb.append("\\n");
110            } else if (c == '\r') {
111                sb.append("\\r");
112            } else if (c == '\b') {
113                sb.append("\\b");
114            } else if (c == '\f') {
115                sb.append("\\f");
116            } else if (c == '\t') {
117                sb.append("\\t");
118            } else if (c == quoteChar) {
119                sb.append("\\");
120                sb.append(quoteChar);
121            } else {
122                // PENDING(uwe): unicode escapes, hex escapes etc
123                sb.append(c);
124            }
125        }
126        return sb.toString();
127    }
128
129    public void setStringValue(String image) throws ParseException {
130        tokenImage = image;
131
132        value = ASTLiteral.unescapedString(image);
133
134        /*
135         * if (!expectActionScript) { if (value.equals("class") ||
136         * value.equals("super")) { throw new ParseException( "reserved
137         * identifier class or super used"); } }
138         */
139    }
140
141    public void setUnquotedStringValue(String valueStr) {
142        value = valueStr;
143
144        // PENDING(uwe): clean up and other value types
145        // PENDING(uwe): serious shit here -> escaping/unescaping, quotes etc
146        Token token = getBeginToken();
147
148        char quoteChar = token.image.charAt(0);
149        if (quoteChar == '"') {
150            token.image = "\"" + ASTLiteral.escapedString(valueStr, quoteChar)
151                    + "\"";
152        } else {
153            token.image = "'" + ASTLiteral.escapedString(valueStr, quoteChar)
154                    + "'";
155        }
156    }
157
158    public void setRegexValue(String image) {
159        tokenImage = image;
160        value = ASTLiteral.REGEX;
161    }
162
163    public void setHtmlValue(String image) {
164        tokenImage = image;
165        value = ASTLiteral.HTML;
166    }
167
168    public void setDecimalValue(String image) {
169        tokenImage = image;
170
171        try {
172            value = new Long(image);
173        } catch (NumberFormatException e) {
174            // it's a floating point
175            value = new Double(image);
176        }
177    }
178
179    public void setHexValue(String image) {
180        tokenImage = image;
181
182        value = new Long(Long.parseLong(image.substring(2), 16));
183    }
184
185    public void setFloatingPointValue(String image) {
186        tokenImage = image;
187
188        value = new Double(image);
189    }
190
191    public void setBooleanValue(String image) {
192        tokenImage = image;
193
194        value = new Boolean(image);
195
196    }
197
198    public void setNullValue() {
199        value = ASTLiteral.NULL;
200    }
201
202    @Override
203    public String toString() {
204        boolean b = System.getProperty( "sidekick.ecmascript.general.allNodes", "false" ).startsWith("t");
205        if (b) {
206            return super.toString();
207        }
208
209        return "Literal[" + (value == null ? "" : value.toString()) + "]";
210    }
211
212    /**
213     * Overwrites <code>equals</code> from <code>Object</code>.
214     *
215     * @param obj
216     *            a object
217     * @return true if specified object equal to receiver
218     * @see java.lang.Object#equals(Object)
219     */
220    @Override
221    public boolean equals(Object obj) {
222        if ((obj != null) && (obj.getClass().equals(this.getClass()))) {
223            ASTLiteral other = (ASTLiteral) obj;
224            boolean result = true;
225
226            if (tokenImage != null) {
227                result = tokenImage.equals(other.tokenImage);
228            } else {
229                result = (other.tokenImage == null);
230            }
231
232            if (result) {
233                result = value.equals(other.value);
234            }
235
236            return result;
237        }
238
239        return false;
240    }
241
242    /**
243     * Overwrites <code>hashCode</code> from <code>Object</code>.
244     *
245     * @return hash code of receiver
246     * @see java.lang.Object#hashCode()
247     */
248    @Override
249    public int hashCode() {
250        int result = 17;
251
252        if (tokenImage != null) {
253            result = 37 * result + tokenImage.hashCode();
254        } else {
255            result = 37 * result;
256        }
257
258        if (value != null) {
259            result = 37 * result + value.hashCode();
260        } else {
261            result = 37 * result;
262        }
263
264        return result;
265    }
266
267    static final int hexval(char c) {
268        switch (c) {
269        case '0':
270            return 0;
271        case '1':
272            return 1;
273        case '2':
274            return 2;
275        case '3':
276            return 3;
277        case '4':
278            return 4;
279        case '5':
280            return 5;
281        case '6':
282            return 6;
283        case '7':
284            return 7;
285        case '8':
286            return 8;
287        case '9':
288            return 9;
289
290        case 'a':
291        case 'A':
292            return 10;
293        case 'b':
294        case 'B':
295            return 11;
296        case 'c':
297        case 'C':
298            return 12;
299        case 'd':
300        case 'D':
301            return 13;
302        case 'e':
303        case 'E':
304            return 14;
305        case 'f':
306        case 'F':
307            return 15;
308        }
309
310        throw new IllegalStateException("Illegal hex or unicode constant"); // Should
311        // never
312        // come
313        // here
314    }
315
316    static final int octval(char c) {
317        switch (c) {
318        case '0':
319            return 0;
320        case '1':
321            return 1;
322        case '2':
323            return 2;
324        case '3':
325            return 3;
326        case '4':
327            return 4;
328        case '5':
329            return 5;
330        case '6':
331            return 6;
332        case '7':
333            return 7;
334        case '8':
335            return 8;
336        case '9':
337            return 9;
338
339        case 'a':
340        case 'A':
341            return 10;
342        case 'b':
343        case 'B':
344            return 11;
345        case 'c':
346        case 'C':
347            return 12;
348        case 'd':
349        case 'D':
350            return 13;
351        case 'e':
352        case 'E':
353            return 14;
354        case 'f':
355        case 'F':
356            return 15;
357        }
358
359        throw new IllegalStateException("Illegal octal constant"); // Should
360        // never
361        // come here
362    }
363
364}