public override IElementValue Apply(IElementValue[] parms) { if (parms == null || parms.Length == 0 || parms.Length > 2) { throw new ELOperationException("Invalid number of parameter for function JsStringEscapeOperator"); } IElementValue o1 = parms[0]; bool o2 = false; if (parms.Length == 2) { if (parms[1].Type == TokenType.BooleanLiteral) { o2 = (bool)parms[1].Value; } } if (o2) { return(new ElementLiteral(TokenType.StringLiteral, o1.ToString().Replace("\\", "\\\\").Replace("'", "\\'"))); } else { return(new ElementLiteral(TokenType.StringLiteral, o1.ToString().Replace("\\", "\\\\").Replace("\"", "\\\""))); } }
public override IElementValue Apply(IElementValue left, IElementValue right) { TokenType leftType = left.Type; TokenType rightType = right.Type; switch (leftType) { case TokenType.BooleanLiteral: break; default: throw new ELOperationException("Invalid operator type Only Decimal Integer or Strings are allowed"); } switch (rightType) { case TokenType.BooleanLiteral: break; default: throw new ELOperationException("Invalid operator type Only Booleans are allowed"); } if (((bool)left.Value) || ((bool)right.Value)) { return(ElementLiteral.TrueInstance); } return(ElementLiteral.FalseInstance); }
internal ResolvedExpression(IElementValue elementValue) : this() { switch (elementValue.Type) { case TokenType.BooleanLiteral: ResolvedType = booleanType; break; case TokenType.DecimalLiteral: ResolvedType = doubleType; break; case TokenType.IntegerLiteral: ResolvedType = intType; break; case TokenType.Object: ResolvedType = objectType; break; case TokenType.StringLiteral: ResolvedType = stringType; break; default: ResolvedType = null; break; } ResolvedValue = elementValue.Value; EvalException = null; }
public override IElementValue Apply(IElementValue left, IElementValue right) { TokenType leftType = left.Type; TokenType rightType = right.Type; switch (leftType) { case TokenType.BooleanLiteral: break; default: throw new ELOperationException("And", "Boolean"); } switch (rightType) { case TokenType.BooleanLiteral: break; default: throw new ELOperationException("And", "Boolean"); } if (((bool)left.Value) && ((bool)right.Value)) { return(ElementLiteral.TrueInstance); } return(ElementLiteral.FalseInstance); }
public override IElementValue Apply(IElementValue left, IElementValue right) { TokenType leftType = left.Type; TokenType rightType = right.Type; switch (leftType) { case TokenType.DecimalLiteral: case TokenType.IntegerLiteral: case TokenType.StringLiteral: case TokenType.BooleanLiteral: break; default: throw new ELOperationException("Invalid operator type Only Decimal Integer or Strings are allowed"); } switch (rightType) { case TokenType.DecimalLiteral: case TokenType.IntegerLiteral: case TokenType.StringLiteral: case TokenType.BooleanLiteral: break; default: throw new ELOperationException("Invalid operator type Only Decimal Integer or Strings are allowed"); } if (leftType == TokenType.StringLiteral || rightType == TokenType.StringLiteral) { string leftString = left.ToString(); string rightString = right.ToString(); if (string.Compare(leftString, rightString, StringComparison.Ordinal) >= 0) { return(ElementLiteral.TrueInstance); } else { return(ElementLiteral.FalseInstance); } } if (leftType == TokenType.BooleanLiteral || rightType == TokenType.BooleanLiteral) { throw new ELOperationException("Invalid operator can not compare booleans"); } double leftValue = Convert.ToDouble(left.Value); double rightValue = Convert.ToDouble(right.Value); if (leftValue >= rightValue) { return(ElementLiteral.TrueInstance); } return(ElementLiteral.FalseInstance); }
public override IElementValue Apply(IElementValue[] parms) { if (parms == null || parms.Length == 0) { throw new ELOperationException("Invalid number of parameter for function ToBoolean"); } IElementValue variable = parms[0]; return(Operator.ToBoolean(variable)); }
static internal IElementValue ToNumber(IElementValue element) { if (element.Value == null) { return(new ElementLiteral(TokenType.IntegerLiteral, 0)); } switch (element.Type) { case TokenType.DecimalLiteral: case TokenType.IntegerLiteral: return(element); case TokenType.BooleanLiteral: if (((bool)element.Value) == true) { return(new ElementLiteral(TokenType.IntegerLiteral, 1)); } else { return(new ElementLiteral(TokenType.IntegerLiteral, 0)); } case TokenType.StringLiteral: if ("Infinity".Equals(element.Value)) { return(ElementLiteral.InfinityInstance); } else if ("-Infinity".Equals(element.Value)) { return(ElementLiteral.NegativeInfinityInstance); } Match match = numberRegexp.Match((string)element.Value); if (!match.Success) { throw new ELOperationException(string.Format("Can not convert [{0}] to number", element.Value)); } double doubleValue = Double.Parse(match.Value, System.Globalization.NumberStyles.Float); if (doubleValue == Math.Floor(doubleValue)) { return(new ElementLiteral(TokenType.IntegerLiteral, (int)doubleValue)); } else { return(new ElementLiteral(TokenType.DecimalLiteral, doubleValue)); } default: throw new ELOperationException("Cannot convert to number objects or arrays."); } }
public override IElementValue Apply(IElementValue[] parms) { if (parms == null || parms.Length == 0) { throw new ELOperationException("Invalid number of parameter for function ToString"); } IElementValue variable = parms[0]; return(new ElementLiteral(TokenType.StringLiteral, variable.ToString())); }
public override IElementValue Apply(IElementValue[] parms) { if (parms == null || parms.Length == 0) { throw new ELOperationException("Invalid number of parameter for function UrlDecode"); } IElementValue variable = parms[0]; string strVal = variable.ToString(); return(new ElementLiteral(TokenType.StringLiteral, HttpUtility.UrlDecode(strVal))); }
public override IElementValue Apply(IElementValue left, IElementValue right) { double leftValue = Convert.ToDouble(Operator.ToNumber(left).Value); double rightValue = Convert.ToDouble(Operator.ToNumber(right).Value); if (double.IsInfinity(leftValue) && double.IsInfinity(rightValue)) { throw new ELOperationException("Cannot divide Infinity values."); } if (rightValue == 0.0d) { if (leftValue > 0.0d) { return(new ElementLiteral(TokenType.DecimalLiteral, double.PositiveInfinity)); } else if (leftValue < 0.0d) { return(new ElementLiteral(TokenType.DecimalLiteral, double.NegativeInfinity)); } else { throw new ELOperationException("Cannot divide 0/0"); } } else if (double.IsInfinity(rightValue)) { return(new ElementLiteral(TokenType.IntegerLiteral, 0)); } else if (double.IsInfinity(leftValue)) { if (rightValue > 0) { return(left); } else { return(new ElementLiteral(TokenType.DecimalLiteral, -leftValue)); } } double returnValue = leftValue / rightValue; if (returnValue == Math.Floor(returnValue)) { return(new ElementLiteral(TokenType.IntegerLiteral, Convert.ToInt32(returnValue))); } else { return(new ElementLiteral(TokenType.DecimalLiteral, returnValue)); } }
public override IElementValue Apply(IElementValue operatorElement) { switch (operatorElement.Type) { case TokenType.BooleanLiteral: if ((bool)operatorElement.Value == true) { return(ElementLiteral.FalseInstance); } return(ElementLiteral.TrueInstance); default: throw new ELOperationException("Invalid Element only Booleans are allowed"); } }
public override IElementValue Apply(IElementValue left, IElementValue right) { double leftValue = Convert.ToDouble(Operator.ToNumber(left).Value); double rightValue = Convert.ToDouble(Operator.ToNumber(right).Value); if ((double.IsInfinity(leftValue) && rightValue == 0d) || (double.IsInfinity(rightValue) && leftValue == 0d)) { throw new ELOperationException("Cannot multipy (+/-)Infinity by 0"); } double result = leftValue * rightValue; return(Operator.ToNumeric(result)); }
public override IElementValue Apply(IElementValue left, IElementValue right) { if (left is ElementLiteral && right is ElementLiteral) { if (((ElementLiteral)left).Equals((ElementLiteral)right)) { return(ElementLiteral.FalseInstance); } } else if (left is ElementObject && right is ElementObject) { if (((ElementObject)left).Equals((ElementObject)right)) { return(ElementLiteral.FalseInstance); } } return(ElementLiteral.TrueInstance); }
public override IElementValue Apply(IElementValue[] parms) { if (parms == null || parms.Length == 0) { throw new ELOperationException("Invalid number of parameter for function Empty"); } IElementValue operatorElement = parms[0]; switch (operatorElement.Type) { case TokenType.DecimalLiteral: case TokenType.IntegerLiteral: case TokenType.BooleanLiteral: return(ElementLiteral.FalseInstance); case TokenType.StringLiteral: if (string.IsNullOrEmpty(operatorElement.ToString())) { return(ElementLiteral.TrueInstance); } return(ElementLiteral.FalseInstance); case TokenType.Object: if (operatorElement.Value == null) { return(ElementLiteral.TrueInstance); } if (operatorElement.Value is ICollection) { if (((ICollection)operatorElement.Value).Count == 0) { return(ElementLiteral.TrueInstance); } } return(ElementLiteral.FalseInstance); default: throw new ELOperationException("Invalid Element"); } }
static internal IElementValue ToBoolean(IElementValue element) { if (element.Value == null) { return(ElementLiteral.falseInstance); } switch (element.Type) { case TokenType.BooleanLiteral: return(element); case TokenType.DecimalLiteral: case TokenType.IntegerLiteral: double doubleVal = Convert.ToDouble(element.Value); if (doubleVal == 0) { return(ElementLiteral.falseInstance); } else { return(ElementLiteral.trueInstance); } case TokenType.StringLiteral: if (element.Value.Equals("true")) { return(ElementLiteral.trueInstance); } else if (element.Value.Equals("false")) { return(ElementLiteral.falseInstance); } else { throw new ELOperationException(string.Format("Cannot convert string [] to boolean", element.Value)); } default: throw new ELOperationException("Cannot convert objects or arrays to boolean"); } }
public static ResolvedExpression ResolveExpression(string expression, IObjectResolver dc) { if (string.IsNullOrEmpty(expression)) { return(new ResolvedExpression()); } try{ string variableExpression = GetVariableExpression(expression); IList <Element> parseData = Parser.Parse(variableExpression); IElementValue response = EvaluateExpression(parseData, dc); return(new ResolvedExpression(response)); } catch (ELException exception) { dc.ELErrors.Add(exception); return(new ResolvedExpression(exception)); } catch (Exception e) { dc.ELErrors.Add(new ELException("Unexpected error in expression language")); return(new ResolvedExpression()); } }
public override IElementValue Apply(IElementValue left, IElementValue right) { double leftValue = Convert.ToDouble(Operator.ToNumber(left).Value); double rightValue = Convert.ToDouble(Operator.ToNumber(right).Value); if (double.IsPositiveInfinity(leftValue) && double.IsNegativeInfinity(rightValue)) { return(left); } else if (double.IsNegativeInfinity(leftValue) && double.IsPositiveInfinity(rightValue)) { return(left); } else if (double.IsNegativeInfinity(leftValue) && double.IsNegativeInfinity(rightValue)) { throw new ELOperationException("Cannot substract negative and negative infinity values"); } else if (double.IsPositiveInfinity(leftValue) && double.IsPositiveInfinity(rightValue)) { throw new ELOperationException("Cannot substract positive and positive infinity values"); } else if (double.IsInfinity(leftValue)) { return(left); } else if (double.IsPositiveInfinity(rightValue)) { return(new ElementLiteral(TokenType.DecimalLiteral, double.NegativeInfinity)); } else if (double.IsNegativeInfinity(rightValue)) { return(new ElementLiteral(TokenType.DecimalLiteral, double.PositiveInfinity)); } double result = leftValue - rightValue; return(Operator.ToNumeric(result)); }
public override IElementValue Apply(IElementValue left, IElementValue right) { double leftValue = Convert.ToDouble(Operator.ToNumber(left).Value); double rightValue = Convert.ToDouble(Operator.ToNumber(right).Value); if (rightValue == 0.0d) { throw new ELOperationException("Modulo Right operator cannot be 0"); } else if (double.IsInfinity(leftValue)) { throw new ELOperationException("Modulo Left operator cannot be Infinity nor -Infinity"); } else if (double.IsInfinity(rightValue)) { if (Math.Floor(leftValue) == leftValue) { return(new ElementLiteral(TokenType.IntegerLiteral, Convert.ToInt32(leftValue))); } else { return(new ElementLiteral(TokenType.DecimalLiteral, leftValue)); } } double returnValue = leftValue % rightValue; if (returnValue == Math.Floor(returnValue)) { return(new ElementLiteral(TokenType.IntegerLiteral, Convert.ToInt32(returnValue))); } else { return(new ElementLiteral(TokenType.DecimalLiteral, returnValue)); } }
public override IElementValue Apply(IElementValue[] parms) { if (parms == null || parms.Length == 0) { throw new ELOperationException("Invalid number of parameter for function HtmlEncode"); } IElementValue variable = parms[0]; string strVal; if (variable.Type == TokenType.Object) { if (variable.Value == null) { strVal = "null"; } else { throw new ELOperationException("Cannot convert objects or arrays to string"); } } strVal = variable.ToString(); return(new ElementLiteral(TokenType.StringLiteral, HttpUtility.HtmlEncode(strVal))); }
private static IElementValue EvaluateExpression(IList <Element> data, IObjectResolver dc) { if (data.Count == 0) { return(null); } Stack <IElementValue> output = new Stack <IElementValue>(); for (int j = 0; j < data.Count; j++) { Element element = data[j]; switch (element.Type) { case TokenType.BinaryOperator: ElementBinaryOperator binaryElement = (ElementBinaryOperator)element; BinaryOperator binaryOperator = (BinaryOperator)binaryElement.Operator; IElementValue b2 = output.Pop(); IElementValue b1 = output.Pop(); IElementValue binaryOutput = binaryOperator.Apply(b1, b2); output.Push(binaryOutput); break; case TokenType.UnitaryOperator: ElementUnitaryOperator unitaryElement = (ElementUnitaryOperator)element; UnitaryOperator unitaryOperator = (UnitaryOperator)unitaryElement.Operator; IElementValue u1 = output.Pop(); IElementValue unitaryOutput = unitaryOperator.Apply(u1); output.Push(unitaryOutput); break; case TokenType.Function: ElementFunction functionElement = (ElementFunction)element; FunctionOperator functionOperator = (FunctionOperator)functionElement.Operator; IElementValue[] paramVals = new IElementValue[functionElement.Parameters.Count]; for (int paramIndex = 0; paramIndex < functionElement.Parameters.Count; paramIndex++) { paramVals[paramIndex] = EvaluateExpression(functionElement.Parameters[paramIndex], dc); } IElementValue functionOutput = functionOperator.Apply(paramVals); output.Push(functionOutput); break; case TokenType.DecimalLiteral: case TokenType.IntegerLiteral: case TokenType.StringLiteral: case TokenType.BooleanLiteral: case TokenType.Selector: output.Push((IElementValue)element); break; case TokenType.OpenParenthesis: int parenthesis = 1; IList <Element> parenthesisElements = new List <Element>(); for (j = j + 1; j < data.Count; j++) { Element parenthesisElement = data[j]; if (parenthesisElement.Type == TokenType.OpenParenthesis) { parenthesis++; } else if (parenthesisElement.Type == TokenType.CloseParenthesis) { parenthesis--; if (parenthesis == 0) { IElementValue parenthesisOutput = EvaluateExpression(parenthesisElements, dc); output.Push(parenthesisOutput); break; } } else { parenthesisElements.Add(parenthesisElement); } } break; case TokenType.TernaryIf: IElementValue ternaryIfValue = output.Pop(); if (ternaryIfValue.Type != TokenType.BooleanLiteral) { throw new ELException("Condition is not boolean."); } bool condition = (bool)ternaryIfValue.Value; IList <Element> ternaryElements = new List <Element>(); bool elseReached = false; for (j = j + 1; j < data.Count; j++) { Element conditionValue = data[j]; if (conditionValue.Type == TokenType.TernaryElse) { elseReached = true; } if (condition == true) { if (elseReached == true) { j = data.Count; break; } ternaryElements.Add(conditionValue); } else { if (elseReached == true) { ternaryElements.Add(conditionValue); } } } IElementValue ternaryOutput = EvaluateExpression(ternaryElements, dc); output.Push(ternaryOutput); break; case TokenType.Variable: StringBuilder variableStr = new StringBuilder(); string expression; expression = ((ElementVariable)element).Expression; variableStr.Append(expression); bool variableBoundryHit = false; bool end = false; while (j + 1 < data.Count && end == false) { switch (data[j + 1].Type) { case TokenType.Variable: if (!variableBoundryHit) { end = true; break; } j++; variableStr.Append(((ElementVariable)data[j]).Expression); variableBoundryHit = false; break; case TokenType.OpenBracket: j++; int brackets = 1; variableBoundryHit = true; IList <Element> bracketElements = new List <Element>(); for (j = j + 1; j < data.Count; j++) { Element bracketElement = data[j]; if (bracketElement.Type == TokenType.OpenBracket) { brackets++; } else if (bracketElement.Type == TokenType.CloseBracket) { brackets--; if (brackets == 0) { IElementValue bracketOutput = EvaluateExpression(bracketElements, dc); if (bracketOutput.Type != TokenType.IntegerLiteral && bracketOutput.Type != TokenType.Selector) { throw new ELException("Integers and Selectors can only be used as brackets indexes."); } variableStr.Append("[").Append(bracketOutput.Value).Append("]"); break; } } else { bracketElements.Add(bracketElement); } } break; case TokenType.Dot: j++; variableBoundryHit = true; variableStr.Append("."); break; default: end = true; break; } } object value = dc.GetVariableObject(variableStr.ToString()); IElementValue elementValue; if (value is string) { elementValue = new ElementLiteral(TokenType.StringLiteral, value); } else if (value is bool) { elementValue = new ElementLiteral(TokenType.BooleanLiteral, value); } else if (value is int) { elementValue = new ElementLiteral(TokenType.IntegerLiteral, value); } else if (value is double || value is float || value is decimal) { elementValue = new ElementLiteral(TokenType.DecimalLiteral, Convert.ToDouble(value)); } else { elementValue = new ElementLiteral(TokenType.Object, value); } output.Push(elementValue); break; } } if (output.Count != 1) { throw new ELException("Error evaluating expression"); } return((IElementValue)output.Pop()); }
public override IElementValue Apply(IElementValue left, IElementValue right) { TokenType leftType = left.Type; TokenType rightType = right.Type; //We validate that are accepted types if (left.Value == null && rightType == TokenType.StringLiteral) { return(new ElementLiteral(TokenType.StringLiteral, "null" + right.ToString())); } else if (right.Value == null && leftType == TokenType.StringLiteral) { return(new ElementLiteral(TokenType.StringLiteral, left.ToString() + "null")); } if ( (leftType == TokenType.BooleanLiteral && rightType == TokenType.StringLiteral) || (leftType == TokenType.StringLiteral && rightType == TokenType.BooleanLiteral) ) { return(new ElementLiteral(TokenType.StringLiteral, left.ToString() + right.ToString())); } //If either is a string we concatenate the values if ((right.Type == TokenType.Object && right.Value != null) || (left.Type == TokenType.Object && left.Value != null)) { throw new ELOperationException("Cannot add not null objects or arrays."); } if (leftType == TokenType.StringLiteral || rightType == TokenType.StringLiteral) { return(new ElementLiteral(TokenType.StringLiteral, string.Format("{0}{1}", left.ToString(), right.ToString()))); } IElementValue leftVal = Operator.ToNumber(left); IElementValue rightVal = Operator.ToNumber(right); double leftValue = Convert.ToDouble(leftVal.Value); double rightValue = Convert.ToDouble(rightVal.Value); if (double.IsPositiveInfinity(leftValue) && double.IsPositiveInfinity(rightValue)) { return(leftVal); } else if (double.IsNegativeInfinity(leftValue) && double.IsNegativeInfinity(rightValue)) { return(leftVal); } else if ((double.IsNegativeInfinity(leftValue) && double.IsPositiveInfinity(rightValue)) || (double.IsPositiveInfinity(leftValue) && double.IsNegativeInfinity(rightValue))) { throw new ELOperationException("Cannot add positive and negative infinity values"); } else if (double.IsInfinity(leftValue)) { return(leftVal); } else if (double.IsInfinity(rightValue)) { return(rightVal); } double result = leftValue + rightValue; return(ToNumeric(result)); }
public abstract IElementValue Apply(IElementValue variable);
public override IElementValue Apply(IElementValue operatorElement) { double val = Convert.ToDouble(Operator.ToNumber(operatorElement).Value); return(Operator.ToNumeric(-val)); }
public abstract IElementValue Apply(IElementValue left, IElementValue right);