You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LexicalUnitImpl.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. * Copyright 2000-2013 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. /*
  17. * Copyright (c) 1999 World Wide Web Consortium
  18. * (Massachusetts Institute of Technology, Institut National de Recherche
  19. * en Informatique et en Automatique, Keio University).
  20. * All Rights Reserved. http://www.w3.org/Consortium/Legal/
  21. *
  22. * $Id: LexicalUnitImpl.java,v 1.3 2000/02/15 02:08:19 plehegar Exp $
  23. */
  24. package com.vaadin.sass.internal.parser;
  25. import java.io.Serializable;
  26. import org.w3c.css.sac.LexicalUnit;
  27. import com.vaadin.sass.internal.expression.exception.IncompatibleUnitsException;
  28. import com.vaadin.sass.internal.util.ColorUtil;
  29. import com.vaadin.sass.internal.util.DeepCopy;
  30. /**
  31. * @version $Revision: 1.3 $
  32. * @author Philippe Le Hegaret
  33. *
  34. * @modified Sebastian Nyholm @ Vaadin Ltd
  35. */
  36. public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit,
  37. Serializable {
  38. private static final long serialVersionUID = -6649833716809789399L;
  39. LexicalUnitImpl prev;
  40. LexicalUnitImpl next;
  41. short type;
  42. int line;
  43. int column;
  44. int i;
  45. float f;
  46. short dimension;
  47. String sdimension;
  48. String s;
  49. String fname;
  50. LexicalUnitImpl params;
  51. LexicalUnitImpl(short type, int line, int column, LexicalUnitImpl p) {
  52. if (p != null) {
  53. prev = p;
  54. p.next = this;
  55. }
  56. this.line = line;
  57. this.column = column - 1;
  58. this.type = type;
  59. }
  60. LexicalUnitImpl(int line, int column, LexicalUnitImpl previous, int i) {
  61. this(SAC_INTEGER, line, column, previous);
  62. this.i = i;
  63. f = i;
  64. }
  65. LexicalUnitImpl(int line, int column, LexicalUnitImpl previous,
  66. short dimension, String sdimension, float f) {
  67. this(dimension, line, column, previous);
  68. this.f = f;
  69. i = (int) f;
  70. this.dimension = dimension;
  71. this.sdimension = sdimension;
  72. }
  73. LexicalUnitImpl(int line, int column, LexicalUnitImpl previous, short type,
  74. String s) {
  75. this(type, line, column, previous);
  76. this.s = s;
  77. }
  78. LexicalUnitImpl(short type, int line, int column, LexicalUnitImpl previous,
  79. String fname, LexicalUnitImpl params) {
  80. this(type, line, column, previous);
  81. this.fname = fname;
  82. this.params = params;
  83. }
  84. public int getLineNumber() {
  85. return line;
  86. }
  87. public int getColumnNumber() {
  88. return column;
  89. }
  90. @Override
  91. public short getLexicalUnitType() {
  92. return type;
  93. }
  94. public void setLexicalUnitType(short type) {
  95. this.type = type;
  96. }
  97. public void getLexicalUnitType(short type) {
  98. this.type = type;
  99. }
  100. @Override
  101. public LexicalUnitImpl getNextLexicalUnit() {
  102. return next;
  103. }
  104. public void setNextLexicalUnit(LexicalUnitImpl n) {
  105. next = n;
  106. }
  107. @Override
  108. public LexicalUnitImpl getPreviousLexicalUnit() {
  109. return prev;
  110. }
  111. public void setPrevLexicalUnit(LexicalUnitImpl n) {
  112. prev = n;
  113. }
  114. @Override
  115. public int getIntegerValue() {
  116. return i;
  117. }
  118. void setIntegerValue(int i) {
  119. this.i = i;
  120. f = i;
  121. }
  122. @Override
  123. public float getFloatValue() {
  124. return f;
  125. }
  126. public void setFloatValue(float f) {
  127. this.f = f;
  128. i = (int) f;
  129. }
  130. @Override
  131. public String getDimensionUnitText() {
  132. switch (type) {
  133. case SAC_PERCENTAGE:
  134. return "%";
  135. case SAC_EM:
  136. return "em";
  137. case SCSSLexicalUnit.SAC_LEM:
  138. return "lem";
  139. case SCSSLexicalUnit.SAC_REM:
  140. return "rem";
  141. case SAC_EX:
  142. return "ex";
  143. case SAC_PIXEL:
  144. return "px";
  145. case SAC_CENTIMETER:
  146. return "cm";
  147. case SAC_MILLIMETER:
  148. return "mm";
  149. case SAC_INCH:
  150. return "in";
  151. case SAC_POINT:
  152. return "pt";
  153. case SAC_PICA:
  154. return "pc";
  155. case SAC_DEGREE:
  156. return "deg";
  157. case SAC_RADIAN:
  158. return "rad";
  159. case SAC_GRADIAN:
  160. return "grad";
  161. case SAC_MILLISECOND:
  162. return "ms";
  163. case SAC_SECOND:
  164. return "s";
  165. case SAC_HERTZ:
  166. return "Hz";
  167. case SAC_KILOHERTZ:
  168. return "kHz";
  169. case SAC_DIMENSION:
  170. return sdimension;
  171. default:
  172. throw new IllegalStateException("invalid dimension " + type);
  173. }
  174. }
  175. @Override
  176. public String getStringValue() {
  177. return s;
  178. }
  179. public void setStringValue(String str) {
  180. s = str;
  181. }
  182. @Override
  183. public String getFunctionName() {
  184. return fname;
  185. }
  186. @Override
  187. public LexicalUnitImpl getParameters() {
  188. return params;
  189. }
  190. @Override
  191. public LexicalUnitImpl getSubValues() {
  192. return params;
  193. }
  194. @Override
  195. public String toString() {
  196. short type = getLexicalUnitType();
  197. String text = null;
  198. switch (type) {
  199. case SCSS_VARIABLE:
  200. text = "$" + s;
  201. break;
  202. case LexicalUnit.SAC_OPERATOR_COMMA:
  203. text = ",";
  204. break;
  205. case LexicalUnit.SAC_OPERATOR_PLUS:
  206. text = "+";
  207. break;
  208. case LexicalUnit.SAC_OPERATOR_MINUS:
  209. text = "-";
  210. break;
  211. case LexicalUnit.SAC_OPERATOR_MULTIPLY:
  212. text = "*";
  213. break;
  214. case LexicalUnit.SAC_OPERATOR_SLASH:
  215. text = "/";
  216. break;
  217. case LexicalUnit.SAC_OPERATOR_MOD:
  218. text = "%";
  219. break;
  220. case LexicalUnit.SAC_OPERATOR_EXP:
  221. text = "^";
  222. break;
  223. case LexicalUnit.SAC_OPERATOR_LT:
  224. text = "<";
  225. break;
  226. case LexicalUnit.SAC_OPERATOR_GT:
  227. text = ">";
  228. break;
  229. case LexicalUnit.SAC_OPERATOR_LE:
  230. text = "<=";
  231. break;
  232. case LexicalUnit.SAC_OPERATOR_GE:
  233. text = "=>";
  234. break;
  235. case LexicalUnit.SAC_OPERATOR_TILDE:
  236. text = "~";
  237. break;
  238. case LexicalUnit.SAC_INHERIT:
  239. text = "inherit";
  240. break;
  241. case LexicalUnit.SAC_INTEGER:
  242. text = Integer.toString(getIntegerValue(), 10);
  243. break;
  244. case LexicalUnit.SAC_REAL:
  245. text = getFloatValue() + "";
  246. break;
  247. case LexicalUnit.SAC_EM:
  248. case SCSSLexicalUnit.SAC_LEM:
  249. case SCSSLexicalUnit.SAC_REM:
  250. case LexicalUnit.SAC_EX:
  251. case LexicalUnit.SAC_PIXEL:
  252. case LexicalUnit.SAC_INCH:
  253. case LexicalUnit.SAC_CENTIMETER:
  254. case LexicalUnit.SAC_MILLIMETER:
  255. case LexicalUnit.SAC_POINT:
  256. case LexicalUnit.SAC_PICA:
  257. case LexicalUnit.SAC_PERCENTAGE:
  258. case LexicalUnit.SAC_DEGREE:
  259. case LexicalUnit.SAC_GRADIAN:
  260. case LexicalUnit.SAC_RADIAN:
  261. case LexicalUnit.SAC_MILLISECOND:
  262. case LexicalUnit.SAC_SECOND:
  263. case LexicalUnit.SAC_HERTZ:
  264. case LexicalUnit.SAC_KILOHERTZ:
  265. case LexicalUnit.SAC_DIMENSION:
  266. float f = getFloatValue();
  267. int i = (int) f;
  268. if ((i) == f) {
  269. text = i + getDimensionUnitText();
  270. } else {
  271. text = f + getDimensionUnitText();
  272. }
  273. break;
  274. case LexicalUnit.SAC_URI:
  275. text = "url(" + getStringValue() + ")";
  276. break;
  277. case LexicalUnit.SAC_RGBCOLOR:
  278. case LexicalUnit.SAC_COUNTER_FUNCTION:
  279. case LexicalUnit.SAC_COUNTERS_FUNCTION:
  280. case LexicalUnit.SAC_RECT_FUNCTION:
  281. case LexicalUnit.SAC_FUNCTION:
  282. String funcName = getFunctionName();
  283. LexicalUnitImpl firstParam = getParameters();
  284. if ("round".equals(funcName)) {
  285. firstParam
  286. .setFloatValue(Math.round(firstParam.getFloatValue()));
  287. text = firstParam.toString();
  288. } else if ("ceil".equals(funcName)) {
  289. firstParam.setFloatValue((float) Math.ceil(firstParam
  290. .getFloatValue()));
  291. text = firstParam.toString();
  292. } else if ("floor".equals(funcName)) {
  293. firstParam.setFloatValue((float) Math.floor(firstParam
  294. .getFloatValue()));
  295. text = firstParam.toString();
  296. } else if ("abs".equals(funcName)) {
  297. firstParam.setFloatValue(Math.abs(firstParam.getFloatValue()));
  298. text = firstParam.toString();
  299. } else if ("darken".equals(funcName)) {
  300. LexicalUnitImpl dark = ColorUtil.darken(this);
  301. text = dark.toString();
  302. } else if ("lighten".equals(funcName)) {
  303. text = ColorUtil.lighten(this).toString();
  304. } else {
  305. text = getFunctionName() + "(" + getParameters() + ")";
  306. }
  307. break;
  308. case LexicalUnit.SAC_IDENT:
  309. text = getStringValue();
  310. break;
  311. case LexicalUnit.SAC_STRING_VALUE:
  312. // @@SEEME. not exact
  313. text = "\"" + getStringValue() + "\"";
  314. break;
  315. case LexicalUnit.SAC_ATTR:
  316. text = "attr(" + getStringValue() + ")";
  317. break;
  318. case LexicalUnit.SAC_UNICODERANGE:
  319. text = "@@TODO";
  320. break;
  321. case LexicalUnit.SAC_SUB_EXPRESSION:
  322. text = getSubValues().toString();
  323. break;
  324. default:
  325. text = "@unknown";
  326. break;
  327. }
  328. if (getNextLexicalUnit() != null) {
  329. if (getNextLexicalUnit().getLexicalUnitType() == SAC_OPERATOR_COMMA) {
  330. return text + getNextLexicalUnit();
  331. }
  332. return text + ' ' + getNextLexicalUnit();
  333. } else {
  334. return text;
  335. }
  336. }
  337. @Override
  338. public LexicalUnitImpl divide(LexicalUnitImpl denominator) {
  339. if (denominator.getLexicalUnitType() != SAC_INTEGER
  340. && denominator.getLexicalUnitType() != SAC_REAL
  341. && getLexicalUnitType() != denominator.getLexicalUnitType()) {
  342. throw new IncompatibleUnitsException(toString());
  343. }
  344. setFloatValue(getFloatValue() / denominator.getFloatValue());
  345. if (getLexicalUnitType() == denominator.getLexicalUnitType()) {
  346. setLexicalUnitType(SAC_REAL);
  347. }
  348. setNextLexicalUnit(denominator.getNextLexicalUnit());
  349. return this;
  350. }
  351. @Override
  352. public LexicalUnitImpl add(LexicalUnitImpl another) {
  353. checkAndSetUnit(another);
  354. setFloatValue(getFloatValue() + another.getFloatValue());
  355. return this;
  356. }
  357. @Override
  358. public LexicalUnitImpl minus(LexicalUnitImpl another) {
  359. checkAndSetUnit(another);
  360. setFloatValue(getFloatValue() - another.getFloatValue());
  361. return this;
  362. }
  363. @Override
  364. public LexicalUnitImpl multiply(LexicalUnitImpl another) {
  365. checkAndSetUnit(another);
  366. setFloatValue(getFloatValue() * another.getIntegerValue());
  367. return this;
  368. }
  369. protected void checkAndSetUnit(LexicalUnitImpl another) {
  370. if (getLexicalUnitType() != SAC_INTEGER
  371. && getLexicalUnitType() != SAC_REAL
  372. && another.getLexicalUnitType() != SAC_INTEGER
  373. && another.getLexicalUnitType() != SAC_REAL
  374. && getLexicalUnitType() != another.getLexicalUnitType()) {
  375. throw new IncompatibleUnitsException(toString());
  376. }
  377. if (another.getLexicalUnitType() != SAC_INTEGER
  378. && another.getLexicalUnitType() != SAC_REAL) {
  379. setLexicalUnitType(another.getLexicalUnitType());
  380. }
  381. setNextLexicalUnit(another.getNextLexicalUnit());
  382. }
  383. @Override
  384. public LexicalUnitImpl modulo(LexicalUnitImpl another) {
  385. if (getLexicalUnitType() != another.getLexicalUnitType()) {
  386. throw new IncompatibleUnitsException(toString());
  387. }
  388. setIntegerValue(getIntegerValue() % another.getIntegerValue());
  389. setNextLexicalUnit(another.getNextLexicalUnit());
  390. return this;
  391. }
  392. public void replaceValue(LexicalUnitImpl another) {
  393. // shouldn't modify 'another' directly, should only modify its copy.
  394. LexicalUnitImpl deepCopyAnother = (LexicalUnitImpl) DeepCopy
  395. .copy(another);
  396. type = deepCopyAnother.getLexicalUnitType();
  397. i = deepCopyAnother.getIntegerValue();
  398. f = deepCopyAnother.getFloatValue();
  399. s = deepCopyAnother.getStringValue();
  400. fname = deepCopyAnother.getFunctionName();
  401. prev = deepCopyAnother.getPreviousLexicalUnit();
  402. dimension = deepCopyAnother.getDimension();
  403. sdimension = deepCopyAnother.getSdimension();
  404. params = deepCopyAnother.getParameters();
  405. LexicalUnitImpl finalNextInAnother = deepCopyAnother;
  406. while (finalNextInAnother.getNextLexicalUnit() != null) {
  407. finalNextInAnother = finalNextInAnother.getNextLexicalUnit();
  408. }
  409. finalNextInAnother.setNextLexicalUnit(next);
  410. next = deepCopyAnother.next;
  411. }
  412. public void setParameters(LexicalUnitImpl params) {
  413. this.params = params;
  414. }
  415. public short getDimension() {
  416. return dimension;
  417. }
  418. public String getSdimension() {
  419. return sdimension;
  420. }
  421. // here some useful function for creation
  422. public static LexicalUnitImpl createVariable(int line, int column,
  423. LexicalUnitImpl previous, String name) {
  424. return new LexicalUnitImpl(line, column, previous, SCSS_VARIABLE, name);
  425. }
  426. public static LexicalUnitImpl createNumber(int line, int column,
  427. LexicalUnitImpl previous, float v) {
  428. int i = (int) v;
  429. if (v == i) {
  430. return new LexicalUnitImpl(line, column, previous, i);
  431. } else {
  432. return new LexicalUnitImpl(line, column, previous, SAC_REAL, "", v);
  433. }
  434. }
  435. public static LexicalUnitImpl createInteger(int line, int column,
  436. LexicalUnitImpl previous, int i) {
  437. return new LexicalUnitImpl(line, column, previous, i);
  438. }
  439. public static LexicalUnitImpl createPercentage(int line, int column,
  440. LexicalUnitImpl previous, float v) {
  441. return new LexicalUnitImpl(line, column, previous, SAC_PERCENTAGE,
  442. null, v);
  443. }
  444. static LexicalUnitImpl createEMS(int line, int column,
  445. LexicalUnitImpl previous, float v) {
  446. return new LexicalUnitImpl(line, column, previous, SAC_EM, null, v);
  447. }
  448. static LexicalUnitImpl createLEM(int line, int column,
  449. LexicalUnitImpl previous, float v) {
  450. return new LexicalUnitImpl(line, column, previous,
  451. SCSSLexicalUnit.SAC_LEM, null, v);
  452. }
  453. static LexicalUnitImpl createREM(int line, int column,
  454. LexicalUnitImpl previous, float v) {
  455. return new LexicalUnitImpl(line, column, previous,
  456. SCSSLexicalUnit.SAC_REM, null, v);
  457. }
  458. static LexicalUnitImpl createEXS(int line, int column,
  459. LexicalUnitImpl previous, float v) {
  460. return new LexicalUnitImpl(line, column, previous, SAC_EX, null, v);
  461. }
  462. public static LexicalUnitImpl createPX(int line, int column,
  463. LexicalUnitImpl previous, float v) {
  464. return new LexicalUnitImpl(line, column, previous, SAC_PIXEL, null, v);
  465. }
  466. public static LexicalUnitImpl createCM(int line, int column,
  467. LexicalUnitImpl previous, float v) {
  468. return new LexicalUnitImpl(line, column, previous, SAC_CENTIMETER,
  469. null, v);
  470. }
  471. static LexicalUnitImpl createMM(int line, int column,
  472. LexicalUnitImpl previous, float v) {
  473. return new LexicalUnitImpl(line, column, previous, SAC_MILLIMETER,
  474. null, v);
  475. }
  476. static LexicalUnitImpl createIN(int line, int column,
  477. LexicalUnitImpl previous, float v) {
  478. return new LexicalUnitImpl(line, column, previous, SAC_INCH, null, v);
  479. }
  480. static LexicalUnitImpl createPT(int line, int column,
  481. LexicalUnitImpl previous, float v) {
  482. return new LexicalUnitImpl(line, column, previous, SAC_POINT, null, v);
  483. }
  484. static LexicalUnitImpl createPC(int line, int column,
  485. LexicalUnitImpl previous, float v) {
  486. return new LexicalUnitImpl(line, column, previous, SAC_PICA, null, v);
  487. }
  488. static LexicalUnitImpl createDEG(int line, int column,
  489. LexicalUnitImpl previous, float v) {
  490. return new LexicalUnitImpl(line, column, previous, SAC_DEGREE, null, v);
  491. }
  492. static LexicalUnitImpl createRAD(int line, int column,
  493. LexicalUnitImpl previous, float v) {
  494. return new LexicalUnitImpl(line, column, previous, SAC_RADIAN, null, v);
  495. }
  496. static LexicalUnitImpl createGRAD(int line, int column,
  497. LexicalUnitImpl previous, float v) {
  498. return new LexicalUnitImpl(line, column, previous, SAC_GRADIAN, null, v);
  499. }
  500. static LexicalUnitImpl createMS(int line, int column,
  501. LexicalUnitImpl previous, float v) {
  502. if (v < 0) {
  503. throw new ParseException("Time values may not be negative");
  504. }
  505. return new LexicalUnitImpl(line, column, previous, SAC_MILLISECOND,
  506. null, v);
  507. }
  508. static LexicalUnitImpl createS(int line, int column,
  509. LexicalUnitImpl previous, float v) {
  510. if (v < 0) {
  511. throw new ParseException("Time values may not be negative");
  512. }
  513. return new LexicalUnitImpl(line, column, previous, SAC_SECOND, null, v);
  514. }
  515. static LexicalUnitImpl createHZ(int line, int column,
  516. LexicalUnitImpl previous, float v) {
  517. if (v < 0) {
  518. throw new ParseException("Frequency values may not be negative");
  519. }
  520. return new LexicalUnitImpl(line, column, previous, SAC_HERTZ, null, v);
  521. }
  522. static LexicalUnitImpl createKHZ(int line, int column,
  523. LexicalUnitImpl previous, float v) {
  524. if (v < 0) {
  525. throw new ParseException("Frequency values may not be negative");
  526. }
  527. return new LexicalUnitImpl(line, column, previous, SAC_KILOHERTZ, null,
  528. v);
  529. }
  530. static LexicalUnitImpl createDimen(int line, int column,
  531. LexicalUnitImpl previous, float v, String s) {
  532. return new LexicalUnitImpl(line, column, previous, SAC_DIMENSION, s, v);
  533. }
  534. static LexicalUnitImpl createInherit(int line, int column,
  535. LexicalUnitImpl previous) {
  536. return new LexicalUnitImpl(line, column, previous, SAC_INHERIT,
  537. "inherit");
  538. }
  539. public static LexicalUnitImpl createIdent(int line, int column,
  540. LexicalUnitImpl previous, String s) {
  541. return new LexicalUnitImpl(line, column, previous, SAC_IDENT, s);
  542. }
  543. public static LexicalUnitImpl createString(String s) {
  544. return new LexicalUnitImpl(0, 0, null, SAC_STRING_VALUE, s);
  545. }
  546. static LexicalUnitImpl createString(int line, int column,
  547. LexicalUnitImpl previous, String s) {
  548. return new LexicalUnitImpl(line, column, previous, SAC_STRING_VALUE, s);
  549. }
  550. static LexicalUnitImpl createURL(int line, int column,
  551. LexicalUnitImpl previous, String s) {
  552. return new LexicalUnitImpl(line, column, previous, SAC_URI, s);
  553. }
  554. static LexicalUnitImpl createAttr(int line, int column,
  555. LexicalUnitImpl previous, String s) {
  556. return new LexicalUnitImpl(line, column, previous, SAC_ATTR, s);
  557. }
  558. static LexicalUnitImpl createCounter(int line, int column,
  559. LexicalUnitImpl previous, LexicalUnit params) {
  560. return new LexicalUnitImpl(SAC_COUNTER_FUNCTION, line, column,
  561. previous, "counter", (LexicalUnitImpl) params);
  562. }
  563. public static LexicalUnitImpl createCounters(int line, int column,
  564. LexicalUnitImpl previous, LexicalUnit params) {
  565. return new LexicalUnitImpl(SAC_COUNTERS_FUNCTION, line, column,
  566. previous, "counters", (LexicalUnitImpl) params);
  567. }
  568. public static LexicalUnitImpl createRGBColor(int line, int column,
  569. LexicalUnitImpl previous, LexicalUnit params) {
  570. return new LexicalUnitImpl(SAC_RGBCOLOR, line, column, previous, "rgb",
  571. (LexicalUnitImpl) params);
  572. }
  573. public static LexicalUnitImpl createRect(int line, int column,
  574. LexicalUnitImpl previous, LexicalUnit params) {
  575. return new LexicalUnitImpl(SAC_RECT_FUNCTION, line, column, previous,
  576. "rect", (LexicalUnitImpl) params);
  577. }
  578. public static LexicalUnitImpl createFunction(int line, int column,
  579. LexicalUnitImpl previous, String fname, LexicalUnit params) {
  580. return new LexicalUnitImpl(SAC_FUNCTION, line, column, previous, fname,
  581. (LexicalUnitImpl) params);
  582. }
  583. public static LexicalUnitImpl createUnicodeRange(int line, int column,
  584. LexicalUnit previous, LexicalUnit params) {
  585. // @@ return new LexicalUnitImpl(line, column, previous, null,
  586. // SAC_UNICODERANGE, params);
  587. return null;
  588. }
  589. public static LexicalUnitImpl createComma(int line, int column,
  590. LexicalUnitImpl previous) {
  591. return new LexicalUnitImpl(SAC_OPERATOR_COMMA, line, column, previous);
  592. }
  593. public static LexicalUnitImpl createSlash(int line, int column,
  594. LexicalUnitImpl previous) {
  595. return new LexicalUnitImpl(SAC_OPERATOR_SLASH, line, column, previous);
  596. }
  597. public static LexicalUnitImpl createAdd(int line, int column,
  598. LexicalUnitImpl previous) {
  599. return new LexicalUnitImpl(SAC_OPERATOR_PLUS, line, column, previous);
  600. }
  601. public static LexicalUnitImpl createMinus(int line, int column,
  602. LexicalUnitImpl previous) {
  603. return new LexicalUnitImpl(SAC_OPERATOR_MINUS, line, column, previous);
  604. }
  605. public static LexicalUnitImpl createMultiply(int line, int column,
  606. LexicalUnitImpl previous) {
  607. return new LexicalUnitImpl(SAC_OPERATOR_MULTIPLY, line, column,
  608. previous);
  609. }
  610. public static LexicalUnitImpl createModulo(int line, int column,
  611. LexicalUnitImpl previous) {
  612. return new LexicalUnitImpl(SAC_OPERATOR_MOD, line, column, previous);
  613. }
  614. public static LexicalUnitImpl createLeftParenthesis(int line, int column,
  615. LexicalUnitImpl previous) {
  616. return new LexicalUnitImpl(SCSS_OPERATOR_LEFT_PAREN, line, column,
  617. previous);
  618. }
  619. public static LexicalUnitImpl createRightParenthesis(int line, int column,
  620. LexicalUnitImpl previous) {
  621. return new LexicalUnitImpl(SCSS_OPERATOR_LEFT_PAREN, line, column,
  622. previous);
  623. }
  624. @Override
  625. public LexicalUnitImpl clone() {
  626. LexicalUnitImpl cloned = new LexicalUnitImpl(type, line, column, prev);
  627. cloned.replaceValue(this);
  628. return cloned;
  629. }
  630. /**
  631. * Tries to return the value for this {@link LexicalUnitImpl} without
  632. * considering any related units.
  633. *
  634. * @return
  635. */
  636. public Object getValue() {
  637. if (s != null) {
  638. return s;
  639. } else if (i != -1) {
  640. return i;
  641. } else if (f != -1) {
  642. return f;
  643. } else {
  644. return null;
  645. }
  646. }
  647. public void setFunctionName(String functionName) {
  648. fname = functionName;
  649. }
  650. public static LexicalUnitImpl createIdent(String s) {
  651. return new LexicalUnitImpl(0, 0, null, SAC_IDENT, s);
  652. }
  653. public static void replaceValues(LexicalUnitImpl unit,
  654. LexicalUnitImpl replaceWith) {
  655. unit.setLexicalUnitType(replaceWith.getLexicalUnitType());
  656. unit.setStringValue(replaceWith.getStringValue());
  657. unit.setFloatValue(replaceWith.getFloatValue());
  658. unit.setIntegerValue(replaceWith.getIntegerValue());
  659. unit.setFunctionName(replaceWith.getFunctionName());
  660. if (replaceWith.getParameters() != null) {
  661. unit.setParameters(replaceWith.getParameters());
  662. }
  663. }
  664. }