Example #1
0
        /// <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));
        }
Example #2
0
 /// <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);
     }
 }
Example #3
0
        /// <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));
        }
Example #4
0
        /// <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);
        }
Example #5
0
        /// <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);
            }
        }
Example #6
0
        /// <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);
        }
Example #7
0
        /// <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);
            }
        }
Example #8
0
        /// <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));
            }
        }
Example #9
0
        /// <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);
        }
Example #10
0
        /// <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!");
        }
Example #11
0
        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
                }
            });
        }
Example #12
0
 /// <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;
     }
 }
Example #13
0
        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);
        }
Example #14
0
 /// <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;
     }
 }
Example #15
0
        /// <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, "") };
            }
        }
Example #16
0
        /// <summary>
        /// Evaluation.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public object Evaluate(IEvaluationScope context)
        {
            var @this = ThisExpression.Evaluate(context);

            return(validatedProperty.Get(@this));
        }
Example #17
0
 /// <summary>
 /// Evaluates literal to its value.
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public object Evaluate(IEvaluationScope context)
 {
     return(Value);
 }
Example #18
0
 /// <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;
 }
Example #19
0
 /// <summary>
 /// Evaluation...
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public object Evaluate(IEvaluationScope context)
 {
     return(Expression.Evaluate(context));
 }
Example #20
0
        /// <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));
        }
Example #21
0
        /// <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);
        }
Example #22
0
 /// <summary>
 /// Evaluates the query.
 /// </summary>
 /// <param name="subject"></param>
 /// <returns></returns>
 public bool Evaluate(IEvaluationScope subject)
 {
     return((bool)Expression.Evaluate(subject));
 }
Example #23
0
        /// <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));
        }
Example #24
0
 /// <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));
 }
Example #25
0
 /// <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));
 }
Example #26
0
 /// <summary>
 /// Evaluation. Nothing to do.
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public object Evaluate(IEvaluationScope context)
 {
     return(null);
 }
Example #27
0
 /// <summary>
 /// Evaluation...
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public object Evaluate(IEvaluationScope context)
 {
     return(new TypeSystem.Implementation.TypeSystem.Empty());
 }