1/* 2 * Copyright (C) 2010 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.clearsilver.jsilver.syntax; 18 19import com.google.clearsilver.jsilver.syntax.analysis.DepthFirstAdapter; 20import com.google.clearsilver.jsilver.syntax.node.AAddExpression; 21import com.google.clearsilver.jsilver.syntax.node.ADecimalExpression; 22import com.google.clearsilver.jsilver.syntax.node.ADivideExpression; 23import com.google.clearsilver.jsilver.syntax.node.AEqExpression; 24import com.google.clearsilver.jsilver.syntax.node.AFunctionExpression; 25import com.google.clearsilver.jsilver.syntax.node.AHexExpression; 26import com.google.clearsilver.jsilver.syntax.node.AModuloExpression; 27import com.google.clearsilver.jsilver.syntax.node.AMultiplyExpression; 28import com.google.clearsilver.jsilver.syntax.node.ANameVariable; 29import com.google.clearsilver.jsilver.syntax.node.ANeExpression; 30import com.google.clearsilver.jsilver.syntax.node.ANegativeExpression; 31import com.google.clearsilver.jsilver.syntax.node.ANumericAddExpression; 32import com.google.clearsilver.jsilver.syntax.node.ANumericEqExpression; 33import com.google.clearsilver.jsilver.syntax.node.ANumericExpression; 34import com.google.clearsilver.jsilver.syntax.node.ANumericNeExpression; 35import com.google.clearsilver.jsilver.syntax.node.ASubtractExpression; 36import com.google.clearsilver.jsilver.syntax.node.PExpression; 37import com.google.clearsilver.jsilver.syntax.node.PVariable; 38 39/** 40 * AST visitor to add numeric expressions to the syntax tree. 41 * 42 * <p> 43 * There are three types of expression we need to process; addition, equality and inequality. By 44 * default these are treated as string expressions unless one of the operands is numeric, in which 45 * case the original expression is replaced with its numeric equivalent. This behavior seems to 46 * exactly match Clearsilver's type inference system. 47 * 48 * <p> 49 * Note how we preprocess our node before testing to see is it should be replaced. This is very 50 * important because it means that type inference is propagated correctly along compound 51 * expressions. Consider the expression: 52 * 53 * <pre>#a + b + c</pre> 54 * 55 * which is parsed (left-to-right) as: 56 * 57 * <pre>(#a + b) + c</pre> 58 * 59 * When we process the left-hand-side sub-expression {@code #a + b} it is turned into a numeric 60 * addition (due to the forced numeric value on the left). Then when we process the main expression 61 * we propagate the numeric type into it. 62 * 63 * <p> 64 * This matches Clearsilver behavior but means that the expressions: 65 * 66 * <pre>#a + b + c</pre> 67 * 68 * and 69 * 70 * <pre>c + b + #a</pre> 71 * 72 * produce different results (the {@code c + b} subexpression in the latter is evaluated as string 73 * concatenation and not numeric addition). 74 */ 75public class TypeResolver extends DepthFirstAdapter { 76 77 @Override 78 public void caseAAddExpression(AAddExpression node) { 79 super.caseAAddExpression(node); 80 PExpression lhs = node.getLeft(); 81 PExpression rhs = node.getRight(); 82 if (isNumeric(lhs) || isNumeric(rhs)) { 83 node.replaceBy(new ANumericAddExpression(lhs, rhs)); 84 } 85 } 86 87 @Override 88 public void caseAEqExpression(AEqExpression node) { 89 super.caseAEqExpression(node); 90 PExpression lhs = node.getLeft(); 91 PExpression rhs = node.getRight(); 92 if (isNumeric(lhs) || isNumeric(rhs)) { 93 node.replaceBy(new ANumericEqExpression(lhs, rhs)); 94 } 95 } 96 97 @Override 98 public void caseANeExpression(ANeExpression node) { 99 super.caseANeExpression(node); 100 PExpression lhs = node.getLeft(); 101 PExpression rhs = node.getRight(); 102 if (isNumeric(lhs) || isNumeric(rhs)) { 103 node.replaceBy(new ANumericNeExpression(lhs, rhs)); 104 } 105 } 106 107 /** 108 * Determines whether the given (sub)expression is numeric, which in turn means that its parent 109 * expression should be treated as numeric if possible. 110 */ 111 static boolean isNumeric(PExpression node) { 112 return node instanceof ANumericExpression // forced numeric (#a) 113 || node instanceof ANumericAddExpression // numeric addition (a + b) 114 || node instanceof ASubtractExpression // subtraction (a - b) 115 || node instanceof AMultiplyExpression // multiplication (a * b) 116 || node instanceof ADivideExpression // division (a / b) 117 || node instanceof AModuloExpression // modulu (x % b) 118 || node instanceof ADecimalExpression // literal decimal (213) 119 || node instanceof AHexExpression // literal hex (0xabc or 0XABC) 120 || node instanceof ANegativeExpression // negative expression (-a) 121 || isNumericFunction(node); // numeric function (subcount) 122 } 123 124 /** 125 * Determine if the given expression represents a numeric function. 126 */ 127 static boolean isNumericFunction(PExpression node) { 128 if (!(node instanceof AFunctionExpression)) { 129 return false; 130 } 131 PVariable functionName = ((AFunctionExpression) node).getName(); 132 if (functionName instanceof ANameVariable) { 133 String name = ((ANameVariable) functionName).getWord().getText(); 134 if ("max".equals(name) || "min".equals(name) || "abs".equals(name) || "subcount".equals(name)) { 135 return true; 136 } 137 } 138 return false; 139 } 140} 141