1 9 package org.w3c.flute.parser; 10 11 import org.w3c.css.sac.LexicalUnit; 12 13 17 class LexicalUnitImpl implements LexicalUnit { 18 19 LexicalUnit prev; 20 LexicalUnit next; 21 22 short type; 23 int line; 24 int column; 25 26 int i; 27 float f; 28 short dimension; 29 String sdimension; 30 String s; 31 String fname; 32 LexicalUnitImpl params; 33 34 LexicalUnitImpl(short type, int line, int column, LexicalUnitImpl p) { 35 if (p != null) { 36 prev = p; 37 p.next = this; 38 } 39 this.line = line; 40 this.column = column -1; 41 this.type = type; 42 } 43 44 LexicalUnitImpl(int line, int column, LexicalUnitImpl previous, int i) { 45 this(SAC_INTEGER, line, column, previous); 46 this.i = i; 47 } 48 49 LexicalUnitImpl(int line, int column, LexicalUnitImpl previous, 50 short dimension, String sdimension, float f) { 51 this(dimension, line, column, previous); 52 this.f = f; 53 this.dimension = dimension; 54 this.sdimension = sdimension; 55 } 56 57 LexicalUnitImpl(int line, int column, LexicalUnitImpl previous, 58 short type, String s) { 59 this(type, line, column, previous); 60 this.s = s; 61 } 62 63 LexicalUnitImpl(short type, int line, int column, 64 LexicalUnitImpl previous, String fname, 65 LexicalUnitImpl params) { 66 this(type, line, column, previous); 67 this.fname = fname; 68 this.params = params; 69 } 70 71 72 73 public int getLineNumber() { 74 return line; 75 } 76 77 public int getColumnNumber() { 78 return column; 79 } 80 81 public short getLexicalUnitType() { 82 return type; 83 } 84 85 public LexicalUnit getNextLexicalUnit() { 86 return next; 87 } 88 89 public LexicalUnit getPreviousLexicalUnit() { 90 return prev; 91 } 92 93 public int getIntegerValue() { 94 return i; 95 } 96 97 void setIntegerValue(int i) { 98 this.i = i; 99 } 100 101 public float getFloatValue() { 102 return f; 103 } 104 105 void setFloatValue(float f) { 106 this.f = f; 107 } 108 109 public String getDimensionUnitText() { 110 switch (type) { 111 case SAC_PERCENTAGE: 112 return "%"; 113 case SAC_EM: 114 return "em"; 115 case SAC_EX: 116 return "ex"; 117 case SAC_PIXEL: 118 return "px"; 119 case SAC_CENTIMETER: 120 return "cm"; 121 case SAC_MILLIMETER: 122 return "mm"; 123 case SAC_INCH: 124 return "in"; 125 case SAC_POINT: 126 return "pt"; 127 case SAC_PICA: 128 return "pc"; 129 case SAC_DEGREE: 130 return "deg"; 131 case SAC_RADIAN: 132 return "rad"; 133 case SAC_GRADIAN: 134 return "grad"; 135 case SAC_MILLISECOND: 136 return "ms"; 137 case SAC_SECOND: 138 return "s"; 139 case SAC_HERTZ: 140 return "Hz"; 141 case SAC_KILOHERTZ: 142 return "kHz"; 143 case SAC_DIMENSION: 144 return sdimension; 145 default: 146 throw new IllegalStateException ("invalid dimension " + type); 147 } 148 } 149 150 public String getStringValue() { 151 return s; 152 } 153 154 public String getFunctionName() { 155 return fname; 156 } 157 158 public org.w3c.css.sac.LexicalUnit getParameters() { 159 return params; 160 } 161 162 public org.w3c.css.sac.LexicalUnit getSubValues() { 163 return params; 164 } 165 166 public String toString() { 167 String text; 168 switch (type) { 169 case SAC_OPERATOR_COMMA: 170 text = ","; 171 break; 172 case SAC_OPERATOR_PLUS: 173 text = "+"; 174 break; 175 case SAC_OPERATOR_MINUS: 176 text = "-"; 177 break; 178 case SAC_OPERATOR_MULTIPLY: 179 text = "*"; 180 break; 181 case SAC_OPERATOR_SLASH: 182 text = "/"; 183 break; 184 case SAC_OPERATOR_MOD: 185 text = "%"; 186 break; 187 case SAC_OPERATOR_EXP: 188 text = "^"; 189 break; 190 case SAC_OPERATOR_LT: 191 text = "<"; 192 break; 193 case SAC_OPERATOR_GT: 194 text = ">"; 195 break; 196 case SAC_OPERATOR_LE: 197 text = "<="; 198 break; 199 case SAC_OPERATOR_GE: 200 text = "=>"; 201 break; 202 case SAC_OPERATOR_TILDE: 203 text = "~"; 204 break; 205 case SAC_INHERIT: 206 text = "inherit"; 207 break; 208 case SAC_INTEGER: 209 text = Integer.toString(i, 10); 210 break; 211 case SAC_REAL: 212 text = f + ""; 213 break; 214 case SAC_EM: 215 case SAC_EX: 216 case SAC_PIXEL: 217 case SAC_INCH: 218 case SAC_CENTIMETER: 219 case SAC_MILLIMETER: 220 case SAC_POINT: 221 case SAC_PICA: 222 case SAC_PERCENTAGE: 223 case SAC_DEGREE: 224 case SAC_GRADIAN: 225 case SAC_RADIAN: 226 case SAC_MILLISECOND: 227 case SAC_SECOND: 228 case SAC_HERTZ: 229 case SAC_KILOHERTZ: 230 case SAC_DIMENSION: 231 String fs = null; 232 int i = (int) f; 233 if (((float) i) == f) { 234 text = i + getDimensionUnitText(); 235 } else { 236 text = f + getDimensionUnitText(); 237 } 238 break; 239 case SAC_URI: 240 text = "uri(" + s + ")"; 241 break; 242 case SAC_COUNTER_FUNCTION: 243 case SAC_COUNTERS_FUNCTION: 244 case SAC_RGBCOLOR: 245 case SAC_RECT_FUNCTION: 246 case SAC_FUNCTION: 247 text = getFunctionName() + "(" + getParameters() + ")"; 248 break; 249 case SAC_IDENT: 250 text = getStringValue(); 251 break; 252 case SAC_STRING_VALUE: 253 text = "\"" + getStringValue() + "\""; 255 break; 256 case SAC_ATTR: 257 text = "attr(" + getStringValue() + ")"; 258 break; 259 case SAC_UNICODERANGE: 260 text = "@@TODO"; 261 break; 262 case SAC_SUB_EXPRESSION: 263 text = getSubValues().toString(); 264 break; 265 default: 266 text = "@unknown"; 267 break; 268 } 269 if (next != null) { 270 return text + ' ' + next; 271 } else { 272 return text; 273 } 274 } 275 276 static LexicalUnitImpl createNumber(int line, int column, 278 LexicalUnitImpl previous, float v) { 279 int i = (int) v; 280 if (v == ((float) i)) { 281 return new LexicalUnitImpl(line, column, previous, i); 282 } else { 283 return new LexicalUnitImpl(line, column, previous, SAC_REAL, "", v); 284 } 285 } 286 static LexicalUnitImpl createInteger(int line, int column, 287 LexicalUnitImpl previous, int i) { 288 return new LexicalUnitImpl(line, column, previous, i); 289 } 290 static LexicalUnitImpl createPercentage(int line, int column, 291 LexicalUnitImpl previous, float v) { 292 return new LexicalUnitImpl(line, column, previous, 293 SAC_PERCENTAGE, null, v); 294 } 295 static LexicalUnitImpl createEMS(int line, int column, 296 LexicalUnitImpl previous, float v) { 297 return new LexicalUnitImpl(line, column, previous, 298 SAC_EM, null, v); 299 } 300 static LexicalUnitImpl createEXS(int line, int column, 301 LexicalUnitImpl previous, float v) { 302 return new LexicalUnitImpl(line, column, previous, 303 SAC_EX, null, v); 304 } 305 static LexicalUnitImpl createPX(int line, int column, 306 LexicalUnitImpl previous, float v) { 307 return new LexicalUnitImpl(line, column, previous, SAC_PIXEL, 308 null, v); 309 } 310 static LexicalUnitImpl createCM(int line, int column, 311 LexicalUnitImpl previous, float v) { 312 return new LexicalUnitImpl(line, column, previous, 313 SAC_CENTIMETER, null, v); 314 } 315 static LexicalUnitImpl createMM(int line, int column, 316 LexicalUnitImpl previous, float v) { 317 return new LexicalUnitImpl(line, column, previous, 318 SAC_MILLIMETER, null, v); 319 } 320 static LexicalUnitImpl createIN(int line, int column, 321 LexicalUnitImpl previous, float v) { 322 return new LexicalUnitImpl(line, column, previous, SAC_INCH, 323 null, v); 324 } 325 static LexicalUnitImpl createPT(int line, int column, 326 LexicalUnitImpl previous, float v) { 327 return new LexicalUnitImpl(line, column, previous, SAC_POINT, 328 null, v); 329 } 330 static LexicalUnitImpl createPC(int line, int column, 331 LexicalUnitImpl previous, float v) { 332 return new LexicalUnitImpl(line, column, previous, SAC_PICA, 333 null, v); 334 } 335 static LexicalUnitImpl createDEG(int line, int column, 336 LexicalUnitImpl previous, float v) { 337 return new LexicalUnitImpl(line, column, previous, SAC_DEGREE, 338 null, v); 339 } 340 static LexicalUnitImpl createRAD(int line, int column, 341 LexicalUnitImpl previous, float v) { 342 return new LexicalUnitImpl(line, column, previous, SAC_RADIAN, 343 null, v); 344 } 345 static LexicalUnitImpl createGRAD(int line, int column, 346 LexicalUnitImpl previous, float v) { 347 return new LexicalUnitImpl(line, column, previous, SAC_GRADIAN, 348 null, v); 349 } 350 static LexicalUnitImpl createMS(int line, int column, 351 LexicalUnitImpl previous, float v) { 352 if (v < 0) { 353 throw new ParseException("Time values may not be negative"); 354 } 355 return new LexicalUnitImpl(line, column, previous, 356 SAC_MILLISECOND, null, v); 357 } 358 static LexicalUnitImpl createS(int line, int column, 359 LexicalUnitImpl previous, float v) { 360 if (v < 0) { 361 throw new ParseException("Time values may not be negative"); 362 } 363 return new LexicalUnitImpl(line, column, previous, SAC_SECOND, 364 null, v); 365 } 366 static LexicalUnitImpl createHZ(int line, int column, 367 LexicalUnitImpl previous, float v) { 368 if (v < 0) { 369 throw new ParseException("Frequency values may not be negative"); 370 } 371 return new LexicalUnitImpl(line, column, previous, SAC_HERTZ, 372 null, v); 373 } 374 static LexicalUnitImpl createKHZ(int line, int column, 375 LexicalUnitImpl previous, float v) { 376 if (v < 0) { 377 throw new ParseException("Frequency values may not be negative"); 378 } 379 return new LexicalUnitImpl(line, column, previous, 380 SAC_KILOHERTZ, null, v); 381 } 382 static LexicalUnitImpl createDimen(int line, int column, 383 LexicalUnitImpl previous, 384 float v, String s) { 385 return new LexicalUnitImpl(line, column, previous, 386 SAC_DIMENSION, s, v); 387 } 388 static LexicalUnitImpl createInherit(int line, int column, 389 LexicalUnitImpl previous) { 390 return new LexicalUnitImpl(line, column, previous, SAC_INHERIT, "inherit"); 391 } 392 static LexicalUnitImpl createIdent(int line, int column, 393 LexicalUnitImpl previous, String s) { 394 return new LexicalUnitImpl(line, column, previous, SAC_IDENT, s); 395 } 396 static LexicalUnitImpl createString(int line, int column, 397 LexicalUnitImpl previous, String s) { 398 return new LexicalUnitImpl(line, column, previous, 399 SAC_STRING_VALUE, s); 400 } 401 static LexicalUnitImpl createURL(int line, int column, 402 LexicalUnitImpl previous, String s) { 403 return new LexicalUnitImpl(line, column, previous, SAC_URI, s); 404 } 405 static LexicalUnitImpl createAttr(int line, int column, 406 LexicalUnitImpl previous, String s) { 407 return new LexicalUnitImpl(line, column, previous, SAC_ATTR, s); 408 } 409 static LexicalUnitImpl createCounter(int line, int column, 410 LexicalUnitImpl previous, 411 LexicalUnit params) { 412 return new LexicalUnitImpl(SAC_COUNTER_FUNCTION, line, 413 column, previous, "counter", 414 (LexicalUnitImpl) params); 415 } 416 static LexicalUnitImpl createCounters(int line, int column, 417 LexicalUnitImpl previous, 418 LexicalUnit params) { 419 return new LexicalUnitImpl(SAC_COUNTERS_FUNCTION, line, 420 column, previous, "counters", 421 (LexicalUnitImpl) params); 422 } 423 static LexicalUnitImpl createRGBColor(int line, int column, 424 LexicalUnitImpl previous, 425 LexicalUnit params) { 426 return new LexicalUnitImpl(SAC_RGBCOLOR, line, column, 427 previous, "color", 428 (LexicalUnitImpl) params); 429 } 430 static LexicalUnitImpl createRect(int line, int column, 431 LexicalUnitImpl previous, 432 LexicalUnit params) { 433 return new LexicalUnitImpl(SAC_RECT_FUNCTION, line, column, 434 previous, "rect", 435 (LexicalUnitImpl) params); 436 } 437 static LexicalUnitImpl createFunction(int line, int column, 438 LexicalUnitImpl previous, 439 String fname, 440 LexicalUnit params) { 441 return new LexicalUnitImpl(SAC_FUNCTION, line, column, previous, 442 fname, 443 (LexicalUnitImpl) params); 444 } 445 static LexicalUnitImpl createUnicodeRange(int line, int column, 446 LexicalUnit previous, 447 LexicalUnit params) { 448 return null; 450 } 451 static LexicalUnitImpl createComma(int line, int column, 452 LexicalUnitImpl previous) { 453 return new LexicalUnitImpl(SAC_OPERATOR_COMMA, line, column, previous); 454 } 455 static LexicalUnitImpl createSlash(int line, int column, 456 LexicalUnitImpl previous) { 457 return new LexicalUnitImpl(SAC_OPERATOR_SLASH, line, column, previous); 458 } 459 } 460 | Popular Tags |