001: /**
002: * Parser support for literals
003: */package org.openlaszlo.sc.parser;
004:
005: public class ASTLiteral extends SimpleNode {
006:
007: private Object mValue = null;
008:
009: public ASTLiteral(int id) {
010: super (id);
011: }
012:
013: public ASTLiteral(Parser p, int id) {
014: super (p, id);
015: }
016:
017: public static Node jjtCreate(int id) {
018: return new ASTLiteral(id);
019: }
020:
021: public static Node jjtCreate(Parser p, int id) {
022: return new ASTLiteral(p, id);
023: }
024:
025: // Added
026: public ASTLiteral(Object value) {
027: mValue = value;
028: }
029:
030: public Object getValue() {
031: return mValue;
032: }
033:
034: static final int hexval(char c) {
035: switch (c) {
036: case '0':
037: return 0;
038: case '1':
039: return 1;
040: case '2':
041: return 2;
042: case '3':
043: return 3;
044: case '4':
045: return 4;
046: case '5':
047: return 5;
048: case '6':
049: return 6;
050: case '7':
051: return 7;
052: case '8':
053: return 8;
054: case '9':
055: return 9;
056:
057: case 'a':
058: case 'A':
059: return 10;
060: case 'b':
061: case 'B':
062: return 11;
063: case 'c':
064: case 'C':
065: return 12;
066: case 'd':
067: case 'D':
068: return 13;
069: case 'e':
070: case 'E':
071: return 14;
072: case 'f':
073: case 'F':
074: return 15;
075: }
076:
077: throw new RuntimeException("Illegal hex or unicode constant");
078: // Should never come here
079: }
080:
081: static final int octval(char c) {
082: switch (c) {
083: case '0':
084: return 0;
085: case '1':
086: return 1;
087: case '2':
088: return 2;
089: case '3':
090: return 3;
091: case '4':
092: return 4;
093: case '5':
094: return 5;
095: case '6':
096: return 6;
097: case '7':
098: return 7;
099: }
100:
101: throw new RuntimeException("Illegal octal constant");
102: // Should never come here
103: }
104:
105: public void setStringValue(String image) {
106: int l = image.length();
107: StringBuffer sb = new StringBuffer(l);
108: for (int i = 0; i < l; i++) {
109: char c = image.charAt(i);
110: if ((c == '\\') && (i + 1 < l)) {
111: i++;
112: c = image.charAt(i);
113: if (c == 'n')
114: c = '\n';
115: else if (c == 'b')
116: c = '\b';
117: else if (c == 'f')
118: c = '\f';
119: else if (c == 'r')
120: c = '\r';
121: else if (c == 't')
122: c = '\t';
123: else if (c == 'v')
124: c = '\u000B';
125: else if (c == 'x') {
126: c = (char) (hexval(image.charAt(i + 1)) << 4 | hexval(image
127: .charAt(i + 2)));
128: i += 2;
129: } else if (c == 'u') {
130: c = (char) (hexval(image.charAt(i + 1)) << 12
131: | hexval(image.charAt(i + 2)) << 8
132: | hexval(image.charAt(i + 3)) << 4 | hexval(image
133: .charAt(i + 4)));
134: i += 4;
135: } else if (c >= '0' && c <= '7') {
136: // Accepts anything from 0 - 377
137: c = (char) (octval(image.charAt(i)));
138: i++;
139: for (int j = i + ((int) c <= 3 ? 2 : 1); (i < j)
140: && (i < l) && (image.charAt(i) >= '0')
141: && (image.charAt(i) <= '7'); i++) {
142: c = (char) ((c << 3) | octval(image.charAt(i)));
143: }
144: }
145: }
146: sb.append(c);
147: }
148: mValue = sb.toString();
149: }
150:
151: public void setRegexpValue(String image) {
152: mValue = "" + image;
153: }
154:
155: public void setDecimalValue(String image) {
156: try {
157: mValue = new Long(Long.parseLong(image));
158: } catch (NumberFormatException e) {
159: mValue = new Double(image);
160: }
161: }
162:
163: public void setOctalValue(String image) {
164: try {
165: String imageWithout0 = image.substring(1);
166: mValue = new Long(Long.parseLong(imageWithout0, 8));
167: } catch (NumberFormatException e) {
168: mValue = new Double(image);
169: }
170: }
171:
172: public void setHexValue(String image) {
173: try {
174: String imageWithout0x = image.substring(2);
175: mValue = new Long(Long.parseLong(imageWithout0x, 16));
176: } catch (NumberFormatException e) {
177: mValue = new Double(image);
178: }
179: }
180:
181: public void setFloatingPointValue(String image) {
182: mValue = new Double(image);
183: }
184:
185: public void setBooleanValue(boolean value) {
186: mValue = new Boolean(value);
187: }
188:
189: public void setNullValue() {
190: mValue = null;
191: }
192:
193: public String toString() {
194: if (mValue == null) {
195: return "null";
196: }
197: return "Literal(" + mValue.toString() + ")";
198: }
199:
200: }
201:
202: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
203: * Copyright 2001-2006 Laszlo Systems, Inc. All Rights Reserved. *
204: * Use is subject to license terms. *
205: * J_LZ_COPYRIGHT_END *********************************************************/
|