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.

ArithmeticExpressionEvaluator.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. package com.vaadin.sass.internal.expression;
  17. import static com.vaadin.sass.internal.parser.SCSSLexicalUnit.SCSS_VARIABLE;
  18. import java.util.Stack;
  19. import com.vaadin.sass.internal.expression.exception.ArithmeticException;
  20. import com.vaadin.sass.internal.parser.LexicalUnitImpl;
  21. import com.vaadin.sass.internal.parser.SCSSLexicalUnit;
  22. public class ArithmeticExpressionEvaluator {
  23. private static ArithmeticExpressionEvaluator instance;
  24. public static ArithmeticExpressionEvaluator get() {
  25. if (instance == null) {
  26. instance = new ArithmeticExpressionEvaluator();
  27. }
  28. return instance;
  29. }
  30. private void createNewOperand(BinaryOperator operator,
  31. Stack<Object> operands) {
  32. Object rightOperand = operands.pop();
  33. operands.push(new BinaryExpression(operands.pop(), operator,
  34. rightOperand));
  35. }
  36. public boolean containsArithmeticalOperator(LexicalUnitImpl term) {
  37. LexicalUnitImpl current = term;
  38. while (current != null) {
  39. for (BinaryOperator operator : BinaryOperator.values()) {
  40. /*
  41. * '/' is treated as an arithmetical operator when one of its
  42. * operands is Variable, or there is another binary operator.
  43. * Otherwise, '/' is treated as a CSS operator.
  44. */
  45. if (current.getLexicalUnitType() == operator.type) {
  46. if (current.getLexicalUnitType() != BinaryOperator.DIV.type) {
  47. return true;
  48. } else {
  49. if (current.getPreviousLexicalUnit()
  50. .getLexicalUnitType() == SCSS_VARIABLE
  51. || current.getNextLexicalUnit()
  52. .getLexicalUnitType() == SCSS_VARIABLE) {
  53. return true;
  54. }
  55. }
  56. }
  57. }
  58. current = current.getNextLexicalUnit();
  59. }
  60. return false;
  61. }
  62. private Object createExpression(LexicalUnitImpl term) {
  63. LexicalUnitImpl current = term;
  64. boolean afterOperand = false;
  65. Stack<Object> operands = new Stack<Object>();
  66. Stack<Object> operators = new Stack<Object>();
  67. inputTermLoop: while (current != null) {
  68. if (afterOperand) {
  69. if (current.getLexicalUnitType() == SCSSLexicalUnit.SCSS_OPERATOR_RIGHT_PAREN) {
  70. Object operator = null;
  71. while (!operators.isEmpty()
  72. && ((operator = operators.pop()) != Parentheses.LEFT)) {
  73. createNewOperand((BinaryOperator) operator, operands);
  74. }
  75. current = current.getNextLexicalUnit();
  76. continue;
  77. }
  78. afterOperand = false;
  79. for (BinaryOperator operator : BinaryOperator.values()) {
  80. if (current.getLexicalUnitType() == operator.type) {
  81. while (!operators.isEmpty()
  82. && (operators.peek() != Parentheses.LEFT)
  83. && (((BinaryOperator) operators.peek()).precedence >= operator.precedence)) {
  84. createNewOperand((BinaryOperator) operators.pop(),
  85. operands);
  86. }
  87. operators.push(operator);
  88. current = current.getNextLexicalUnit();
  89. continue inputTermLoop;
  90. }
  91. }
  92. throw new ArithmeticException();
  93. }
  94. if (current.getLexicalUnitType() == SCSSLexicalUnit.SCSS_OPERATOR_LEFT_PAREN) {
  95. operators.push(Parentheses.LEFT);
  96. current = current.getNextLexicalUnit();
  97. continue;
  98. }
  99. afterOperand = true;
  100. operands.push(current);
  101. current = current.getNextLexicalUnit();
  102. }
  103. while (!operators.isEmpty()) {
  104. Object operator = operators.pop();
  105. if (operator == Parentheses.LEFT) {
  106. throw new ArithmeticException("Unexpected \"(\" found");
  107. }
  108. createNewOperand((BinaryOperator) operator, operands);
  109. }
  110. Object expression = operands.pop();
  111. if (!operands.isEmpty()) {
  112. LexicalUnitImpl operand = (LexicalUnitImpl) operands.peek();
  113. throw new ArithmeticException("Unexpected operand "
  114. + operand.toString() + " found");
  115. }
  116. return expression;
  117. }
  118. public LexicalUnitImpl evaluate(LexicalUnitImpl term) {
  119. Object result = ArithmeticExpressionEvaluator.get().createExpression(
  120. term);
  121. if (result instanceof BinaryExpression) {
  122. return ((BinaryExpression) result).eval();
  123. }
  124. return term;
  125. }
  126. }