/// <summary> /// Evaluation: Executes the binary operation. /// </summary> /// <param name="context"></param> /// <returns></returns> public object Evaluate(IEvaluationScope context) { var lhs = leftExpression.Evaluate(context); var rhs = rightExpression.Evaluate(context); return(operation.Operation(lhs, rhs)); }
/// <summary> /// Scans an assembly for e.g. <see cref="CQLGlobalFunction"/> to extend the scope with global functions and variables. /// </summary> /// <param name="this"></param> /// <param name="assembly"></param> public static void AddFromScan(this IEvaluationScope @this, Assembly assembly) { foreach (var type in assembly.GetTypes()) { @this.AddTypeScan(type); } }
/// <summary> /// Evaluates the THIS expression and applies the evaluated indices as an array access. /// </summary> /// <param name="context"></param> /// <returns></returns> public object Evaluate(IEvaluationScope context) { var @this = ThisExpression.Evaluate(context); var indices = Indices.Select(i => i.Evaluate(context)).ToArray(); return(indexer.Get(@this, indices)); }
/// <summary> /// Evaluation of expression: reads the value of the variable from the given context. /// </summary> /// <param name="context">The context.</param> /// <returns></returns> /// <exception cref="LocateableException">Unknown field!</exception> public object Evaluate(IEvaluationScope context) { if (!context.TryGetVariable(Identifier, out IVariableDefinition nameable)) { throw new LocateableException(Location, "Unknown field!"); } return(nameable.Value); }
/// <summary> /// Scans a type and its nested types for e.g. <see cref="CQLGlobalFunction"/> to extend the scope with global functions and variables. /// </summary> /// <param name="this"></param> /// <param name="type"></param> public static void AddFromScan(this IEvaluationScope @this, Type type) { var types = new[] { type }.Concat(type.GetNestedTypes()); foreach (var tpe in types) { @this.AddTypeScan(tpe); } }
/// <summary> /// Evaluates the value of this array expression. /// </summary> /// <param name="context"></param> /// <returns></returns> public object Evaluate(IEvaluationScope context) { var values = Elements.Select(elem => elem.Evaluate(context)).ToArray(); var result = (Array)Activator.CreateInstance(SemanticType, values.Length); for (var index = 0; index < values.Length; index++) { result.SetValue(values[index], index); } return(result); }
/// <summary> /// Checks type for e.g. <see cref="CQLGlobalFunction"/> to extend the scope with global functions and variables. /// </summary> /// <param name="this"></param> /// <param name="type"></param> public static void AddTypeScan(this IEvaluationScope @this, Type type) { var functions = type.GetMethods(BindingFlags.Static | BindingFlags.Public) .Select(f => new { func = f, attr = f.GetCustomAttributes <CQLGlobalFunction>().FirstOrDefault() }) .Where(f => f.attr != null) .ToArray(); foreach (var function in functions) { @this.DefineNativeGlobalFunction(function.attr.Name, function.func); } }
/// <summary> /// Evaluation... does only execute the branch with the corressponding condition /// </summary> /// <param name="context"></param> /// <returns></returns> public object Evaluate(IEvaluationScope context) { var condition = Condition.Evaluate(context); if ((bool)condition == true) { return(Then.Evaluate(context)); } else { return(Else.Evaluate(context)); } }
/// <summary> /// Converts a concrete evaluation scope into a abstract validation scope. /// </summary> /// <param name="this"></param> /// <returns></returns> public static IValidationScope ToValidationScope(this IEvaluationScope @this) { if (@this == null) { return(null); } var result = new ValidationScope(@this.TypeSystem, @this.Parent.ToValidationScope()); foreach (var elem in @this) { result.DefineVariable(elem.Name, elem.Value?.GetType() ?? typeof(object)); } return(result); }
/// <summary> /// Evaluates the THIS expression first. If the result is a function closure, the closure will be invoked with the evaluated parameters. /// </summary> /// <param name="context"></param> /// <returns></returns> public object Evaluate(IEvaluationScope context) { var @this = this.ThisExpression.Evaluate(context); if (@this is IMemberFunctionClosure) { return(((IMemberFunctionClosure)@this).Invoke(Parameters.Select(p => p.Evaluate(context)).ToArray())); } if (@this is IGlobalFunctionClosure) { return(((IGlobalFunctionClosure)@this).Invoke(Parameters.Select(p => p.Evaluate(context)).ToArray())); } throw new LocateableException(Location, "Closure expected!"); }
public static void SetupFixture(TestContext testContext) { var typeSystemBuilder = new TypeSystemBuilder(); typeSystemBuilder.AddType <A>("A", "stuff").AddForeignProperty(IdDelimiter.Dot, "b", a => a.b); typeSystemBuilder.AddType <B>("B", "stuff 2").AddForeignProperty(IdDelimiter.Dot, "c", a => a.c); context = new EvaluationScope(typeSystemBuilder.Build()); context.DefineVariable("a", new A() { b = new B() { c = 1 } }); }
/// <summary> /// Parses AND validates a query string. /// </summary> /// <param name="text"></param> /// <param name="context"></param> /// <param name="errorListener"></param> /// <returns></returns> public static Query Parse(string text, IEvaluationScope context, IErrorListener errorListener = null) { try { var query = ParseForSyntaxOnly(text); return(query.Validate(context.ToValidationScope())); } catch (LocateableException ex) { if (errorListener != null) { errorListener.TriggerError(ex); return(null); } throw ex; } }
public TextBox() { InitializeComponent(); SetupErrorVisualization(); SetupSyntaxHighlighting(); SetupPasting(); var typeSystemBuilder = new TypeSystemBuilder(); var typeSystem = typeSystemBuilder.Build(); nullContext = new EvaluationScope(typeSystem); textEditor.TextArea.TextEntered += TextArea_TextEntered; textEditor.TextArea.PreviewKeyDown += TextArea_PreviewKeyDown; textEditor_TextChanged(this, null); }
/// <summary> /// Evaluates a user query string with a given context and an optional error listener. /// If no listener is given, this method will throw exceptions instead. /// Do not use this method if you want to evaluate a query for multiple subjects. /// Use <see cref="Parse(string, IEvaluationScope, IErrorListener)"/> instead in combination /// with <see cref="Query.Evaluate(IEvaluationScope)"/>. /// </summary> /// <typeparam name="TSubject"></typeparam> /// <param name="text"></param> /// <param name="subject"></param> /// <param name="context"></param> /// <param name="errorListener"></param> /// <returns></returns> public static bool?Evaluate <TSubject>(string text, TSubject subject, IEvaluationScope context, IErrorListener errorListener = null) { try { context.DefineThis(subject); var query = Parse(text, context, errorListener); return(query.Evaluate(context)); } catch (LocateableException ex) { if (errorListener != null) { errorListener.TriggerError(ex); return(null); } throw ex; } }
/// <summary> /// Creates an auto completion suggester from the evaluation scope. /// </summary> /// <param name="context"></param> public AutoCompletionSuggester(IEvaluationScope context) { this.context = context; this.ruleNames = CQLParser.ruleNames; this.vocabulary = CQLParser.DefaultVocabulary; this.atn = CQLParser._ATN; suggestionsByTokenType = new Dictionary <int, IEnumerable <Token> >(); for (var index = 0; index < CQLLexer.DefaultVocabulary.MaxTokenType; index++) { var name = CQLLexer.DefaultVocabulary.GetDisplayName(index); if (name == null || name.Contains("LITERAL")) { continue; } if (name.StartsWith("'") && name.EndsWith("'")) { name = name.Substring(1, name.Length - 2); } suggestionsByTokenType[index] = new[] { new Token(name, "") }; } }
/// <summary> /// Evaluation. /// </summary> /// <param name="context"></param> /// <returns></returns> public object Evaluate(IEvaluationScope context) { var @this = ThisExpression.Evaluate(context); return(validatedProperty.Get(@this)); }
/// <summary> /// Evaluates literal to its value. /// </summary> /// <param name="context"></param> /// <returns></returns> public object Evaluate(IEvaluationScope context) { return(Value); }
/// <summary> /// Creates an empty evaluation scope. /// </summary> /// <param name="system"></param> /// <param name="parent"></param> public EvaluationScope(ITypeSystem system, IEvaluationScope parent = null) { this.system = system; Parent = parent; }
/// <summary> /// Evaluation... /// </summary> /// <param name="context"></param> /// <returns></returns> public object Evaluate(IEvaluationScope context) { return(Expression.Evaluate(context)); }
/// <summary> /// Trys to complete the user input by a given context. /// </summary> /// <param name="textUntilCaret"></param> /// <param name="context"></param> /// <returns></returns> public static IEnumerable <Suggestion> AutoComplete(string textUntilCaret, IEvaluationScope context) { var suggester = new AutoCompletionSuggester(context); return(suggester.GetSuggestions(textUntilCaret)); }
/// <summary> /// Defines a global function by its <see cref="MethodInfo"/>. /// </summary> /// <param name="this"></param> /// <param name="name"></param> /// <param name="info"></param> public static void DefineNativeGlobalFunction(this IEvaluationScope @this, string name, MethodInfo info) { var function = NativeGlobalFunctionExtensions.CreateByMethodInfo(info); addGlobalFunction(@this, function.GetType(), name, function); }
/// <summary> /// Evaluates the query. /// </summary> /// <param name="subject"></param> /// <returns></returns> public bool Evaluate(IEvaluationScope subject) { return((bool)Expression.Evaluate(subject)); }
/// <summary> /// Evaluation: Casts the input value. /// </summary> /// <param name="context"></param> /// <returns></returns> public object Evaluate(IEvaluationScope context) { var operand = Expression.Evaluate(context); return(rule.Cast(operand)); }
/// <summary> /// Define THIS. /// </summary> /// <param name="this"></param> /// <param name="value"></param> /// <returns></returns> public static IVariableDefinition DefineThis(this IEvaluationScope @this, object value) { return(@this.DefineVariable(ThisName, value)); }
/// <summary> /// Lookup THIS /// </summary> /// <param name="this"></param> /// <param name="variable"></param> /// <returns></returns> public static bool TryGetThis(this IEvaluationScope @this, out IVariableDefinition variable) { return(@this.TryGetVariable(ThisName, out variable)); }
/// <summary> /// Evaluation. Nothing to do. /// </summary> /// <param name="context"></param> /// <returns></returns> public object Evaluate(IEvaluationScope context) { return(null); }
/// <summary> /// Evaluation... /// </summary> /// <param name="context"></param> /// <returns></returns> public object Evaluate(IEvaluationScope context) { return(new TypeSystem.Implementation.TypeSystem.Empty()); }