private void Visit(ParserRuleContext parserRuleContext) { if (IsUnaryResultContext(parserRuleContext)) { VisitUnaryResultContext(parserRuleContext); } else if (parserRuleContext is VBAParser.LExprContext lExpr) { Visit(lExpr); } else if (parserRuleContext is VBAParser.LiteralExprContext litExpr) { Visit(litExpr); } else if (parserRuleContext is VBAParser.CaseClauseContext || parserRuleContext is VBAParser.RangeClauseContext) { VisitImpl(parserRuleContext); StoreVisitResult(parserRuleContext, _inspValueFactory.Create(parserRuleContext.GetText())); } else if (IsBinaryMathContext(parserRuleContext) || IsBinaryLogicalContext(parserRuleContext)) { VisitBinaryOpEvaluationContext(parserRuleContext); } else if (IsUnaryLogicalContext(parserRuleContext) || IsUnaryMathContext(parserRuleContext)) { VisitUnaryOpEvaluationContext(parserRuleContext); } }
public string InferAsTypeName(VBAParser.LiteralExprContext literalExprContext) { var asTypeName = _parseTreeValueFactory?.Create(literalExprContext?.GetText()).ValueType ?? Tokens.Variant; return(_literalResultModifiers.TryGetValue(asTypeName, out var modifiedAsTypeName) ? modifiedAsTypeName : asTypeName); }
private T ConvertToContainedGeneric <K>(K value) { var parseTreeValue = _valueFactory.Create(value.ToString(), TypeName); if (_valueConverter(parseTreeValue, out T tValue)) { return(tValue); } throw new ArgumentException($"Unable to convert {value.ToString()} to {typeof(T).ToString()}"); }
CreateLogicPair(IParseTreeValue value, string opSymbol, IParseTreeValueFactory factory) { var operands = value.Token.Split(new string[] { opSymbol }, StringSplitOptions.None); if (operands.Count() == 2) { var lhs = factory.Create(operands[0].Trim()); var rhs = factory.Create(operands[1].Trim()); if (opSymbol.Equals(Tokens.Like)) { rhs = factory.CreateDeclaredType($"\"{rhs.Token}\"", Tokens.String); } return(lhs, rhs); } if (operands.Count() == 1) { var lhs = factory.Create(operands[0].Trim()); return(lhs, null); } return(null, null); }
public IRangeClauseFilter Create(string typeName, IParseTreeValueFactory valueFactory) { if (valueFactory is null) { throw new ArgumentNullException(); } if (!(IntegralNumberExtents.Keys.Contains(typeName) || typeName.Equals(Tokens.Double) || typeName.Equals(Tokens.Single) || typeName.Equals(Tokens.Currency) || typeName.Equals(Tokens.Boolean) || typeName.Equals(Tokens.String))) { throw new ArgumentException($"Unsupported TypeName ({typeName})"); } if (IntegralNumberExtents.Keys.Contains(typeName)) { var integerTypeFilter = new RangeClauseFilter <long>(typeName, valueFactory, this, ParseTreeValue.TryConvertValue); var minExtent = valueFactory.Create(IntegralNumberExtents[typeName].Item1.ToString(), typeName); var maxExtent = valueFactory.Create(IntegralNumberExtents[typeName].Item2.ToString(), typeName); integerTypeFilter.AddExtents(minExtent, maxExtent); return(integerTypeFilter); } else if (typeName.Equals(Tokens.Double) || typeName.Equals(Tokens.Single)) { var doubleTypeFilter = new RangeClauseFilter <double>(typeName, valueFactory, this, ParseTreeValue.TryConvertValue); if (typeName.Equals(Tokens.Single)) { var minExtent = valueFactory.Create(CompareExtents.SINGLEMIN.ToString(), typeName); var maxExtent = valueFactory.Create(CompareExtents.SINGLEMAX.ToString(), typeName); doubleTypeFilter.AddExtents(minExtent, maxExtent); } return(doubleTypeFilter); } else if (typeName.Equals(Tokens.Boolean)) { var boolTypeFilter = new RangeClauseFilter <bool>(typeName, valueFactory, this, ParseTreeValue.TryConvertValue); return(boolTypeFilter); } else if (typeName.Equals(Tokens.Currency)) { var decimalTypeFilter = new RangeClauseFilter <decimal>(typeName, valueFactory, this, ParseTreeValue.TryConvertValue); var minExtent = valueFactory.Create(CompareExtents.CURRENCYMIN.ToString(), typeName); var maxExtent = valueFactory.Create(CompareExtents.CURRENCYMAX.ToString(), typeName); decimalTypeFilter.AddExtents(minExtent, maxExtent); return(decimalTypeFilter); } var filter = new RangeClauseFilter <string>(typeName, valueFactory, this, ParseTreeValue.TryConvertValue); return(filter); }
public IParseTreeValue Evaluate(IParseTreeValue LHS, IParseTreeValue RHS, string opSymbol) { var isMathOp = MathOpsBinary.ContainsKey(opSymbol); var isLogicOp = LogicOpsBinary.ContainsKey(opSymbol); Debug.Assert(isMathOp || isLogicOp); var opResultTypeName = isMathOp ? DetermineMathResultType(LHS, RHS) : Tokens.Boolean; var operands = PrepareOperands(new string[] { LHS.ValueText, RHS.ValueText }); if (operands.Count == 2) { if (isMathOp) { var mathResult = MathOpsBinary[opSymbol](operands[0], operands[1]); return(_valueFactory.Create(mathResult.ToString(), opResultTypeName)); } var logicResult = LogicOpsBinary[opSymbol](operands[0], operands[1]); return(_valueFactory.Create(logicResult.ToString(), opResultTypeName)); } return(_valueFactory.Create($"{LHS.ValueText} {opSymbol} {RHS.ValueText}", opResultTypeName)); }
private IParseTreeValue EvaluateRelationalOp(string opSymbol, IParseTreeValue LHS, IParseTreeValue RHS) { var opProvider = new OperatorTypesProvider((LHS.ValueType, RHS.ValueType), opSymbol); if (!(LHS.ParsesToConstantValue && RHS.ParsesToConstantValue)) { //special case of resolve-able expression with variable LHS if (opSymbol.Equals(Tokens.Like) && RHS.Token.Equals($"\"*\"")) { return(_valueFactory.Create(true)); } //Unable to resolve to a value, return an expression if (opProvider.OperatorDeclaredType.Equals(string.Empty)) { return(_valueFactory.CreateExpression($"{LHS.Token} {opSymbol} {RHS.Token}", Tokens.Variant)); } return(_valueFactory.CreateExpression($"{LHS.Token} {opSymbol} {RHS.Token}", opProvider.OperatorDeclaredType)); } if (opSymbol.Equals(RelationalOperators.EQ)) { if (opProvider.OperatorEffectiveType.Equals(Tokens.Boolean)) { return(_valueFactory.Create(Compare(LHS, RHS, (bool a, bool b) => { return a == b; }))); } var result = IsStringCompare(LHS, RHS) ? Compare(LHS, RHS, (string a, string b) => { return(AreEqual(a, b)); }) : Compare(LHS, RHS, (decimal a, decimal b) => { return(a == b); }, (double a, double b) => { return(a == b); }); return(_valueFactory.Create(result.Equals(Tokens.True))); } if (opSymbol.Equals(RelationalOperators.NEQ)) { if (opProvider.OperatorEffectiveType.Equals(Tokens.Boolean)) { return(_valueFactory.Create(Compare(LHS, RHS, (bool a, bool b) => { return a == true && b == false; }))); } var result = IsStringCompare(LHS, RHS) ? Compare(LHS, RHS, (string a, string b) => { return(!AreEqual(a, b)); }) : Compare(LHS, RHS, (decimal a, decimal b) => { return(a != b); }, (double a, double b) => { return(a != b); }); return(_valueFactory.Create(result.Equals(Tokens.True))); } if (opSymbol.Equals(RelationalOperators.LT)) { if (opProvider.OperatorEffectiveType.Equals(Tokens.Boolean)) { return(_valueFactory.Create(Compare(LHS, RHS, (bool a, bool b) => { return (a == true && b == false); }))); } var result = IsStringCompare(LHS, RHS) ? Compare(LHS, RHS, (string a, string b) => { return(IsLessThan(a, b)); }) : Compare(LHS, RHS, (decimal a, decimal b) => { return(a < b); }, (double a, double b) => { return(a < b); }); return(_valueFactory.Create(result.Equals(Tokens.True))); } if (opSymbol.Equals(RelationalOperators.LTE) || opSymbol.Equals(RelationalOperators.LTE2)) { if (opProvider.OperatorEffectiveType.Equals(Tokens.Boolean)) { return(_valueFactory.Create(Compare(LHS, RHS, (bool a, bool b) => { return (a == true && b == false) || a == b; }))); } var result = IsStringCompare(LHS, RHS) ? Compare(LHS, RHS, (string a, string b) => { return(IsLessThan(a, b) || AreEqual(a, b)); }) : Compare(LHS, RHS, (decimal a, decimal b) => { return(a <= b); }, (double a, double b) => { return(a <= b); }); return(_valueFactory.Create(result.Equals(Tokens.True))); } if (opSymbol.Equals(RelationalOperators.GT)) { if (opProvider.OperatorEffectiveType.Equals(Tokens.Boolean)) { return(_valueFactory.Create(Compare(LHS, RHS, (bool a, bool b) => { return a == false && b == true || a == b; }))); } var result = IsStringCompare(LHS, RHS) ? Compare(LHS, RHS, (string a, string b) => { return(IsGreaterThan(a, b)); }) : Compare(LHS, RHS, (decimal a, decimal b) => { return(a > b); }, (double a, double b) => { return(a > b); }); return(_valueFactory.Create(result.Equals(Tokens.True))); } if (opSymbol.Equals(RelationalOperators.GTE) || opSymbol.Equals(RelationalOperators.GTE2)) { if (opProvider.OperatorEffectiveType.Equals(Tokens.Boolean)) { return(_valueFactory.Create(Compare(LHS, RHS, (bool a, bool b) => { return a == false && b == true || a == b; }))); } var result = IsStringCompare(LHS, RHS) ? Compare(LHS, RHS, (string a, string b) => { return(IsGreaterThan(a, b) || AreEqual(a, b)); }) : Compare(LHS, RHS, (decimal a, decimal b) => { return(a >= b); }, (double a, double b) => { return(a >= b); }); return(_valueFactory.Create(result.Equals(Tokens.True))); } if (opSymbol.Equals(RelationalOperators.LIKE)) { if (RHS.Token.Equals("*")) { return(_valueFactory.Create(true)); } if (LHS.ParsesToConstantValue && RHS.ParsesToConstantValue) { var matches = Like(LHS.Token, RHS.Token); return(_valueFactory.Create(matches)); } } return(_valueFactory.CreateExpression($"{LHS.Token} {opSymbol} {RHS.Token}", opProvider.OperatorDeclaredType)); }