コード例 #1
0
 /// <summary>
 /// Adds the given query (new format) to the list of queries to be
 /// evaluated.
 /// </summary>
 /// <param name="id">
 /// The unique id of the query
 /// </param>
 /// <param name="query">
 /// The query function to be evaluated
 /// </param>
 public void AddQuery(string id, Query query)
 {
     if (QueryMap.ContainsKey(id))
     {
         throw new Exception("A query with id '" + id + "' already exists");
     }
     QueryMap.Add(id, query);
 }
コード例 #2
0
        public void ValueQuery(string name, double value, bool OverWriteIfExistent = false)
        {
            Query query = (app, t) => value;

            if (QueryMap.ContainsKey(name))
            {
                if (!OverWriteIfExistent)
                {
                    throw new Exception("A query with id '" + name + "' already exists");
                }
                else
                {
                    QueryMap[name] = query;
                }
            }
            else
            {
                QueryMap.Add(name, query);
            }
        }
コード例 #3
0
ファイル: QueryParser.cs プロジェクト: wittyansh/nledger
        public ExprOp ParseQueryExpr(QueryLexerTokenKind tokContext, bool subExpression = false)
        {
            ExprOp limiter = null;

            ExprOp next;

            while ((next = ParseOrExpr(tokContext)) != null)
            {
                if (limiter == null)
                {
                    limiter = next;
                }
                else
                {
                    ExprOp prev = limiter;
                    limiter       = new ExprOp(OpKindEnum.O_OR);
                    limiter.Left  = prev;
                    limiter.Right = next;
                }
            }

            if (!subExpression)
            {
                if (limiter != null)
                {
                    QueryMap.Add(QueryKindEnum.QUERY_LIMIT, new Predicate(limiter, WhatToKeep).PrintToStr());
                }

                QueryLexerToken tok = Lexer.PeekToken(tokContext);
                while (tok.Kind != QueryLexerTokenKind.END_REACHED)
                {
                    switch (tok.Kind)
                    {
                    case QueryLexerTokenKind.TOK_SHOW:
                    case QueryLexerTokenKind.TOK_ONLY:
                    case QueryLexerTokenKind.TOK_BOLD:
                    {
                        Lexer.NextToken(tokContext);

                        QueryKindEnum kind;
                        switch (tok.Kind)
                        {
                        case QueryLexerTokenKind.TOK_SHOW:
                            kind = QueryKindEnum.QUERY_SHOW;
                            break;

                        case QueryLexerTokenKind.TOK_ONLY:
                            kind = QueryKindEnum.QUERY_ONLY;
                            break;

                        case QueryLexerTokenKind.TOK_BOLD:
                            kind = QueryKindEnum.QUERY_BOLD;
                            break;

                        default:
                            throw new InvalidOperationException("To avoid using unassigned QueryKindEnum kind...");
                        }

                        ExprOp node = next = null;
                        while ((next = ParseOrExpr(tokContext)) != null)
                        {
                            if (node == null)
                            {
                                node = next;
                            }
                            else
                            {
                                ExprOp prev = node;
                                node       = new ExprOp(OpKindEnum.O_OR);
                                node.Left  = prev;
                                node.Right = next;
                            }
                        }

                        if (node != null)
                        {
                            QueryMap.Add(kind, new Predicate(node, WhatToKeep).PrintToStr());
                        }

                        break;
                    }

                    case QueryLexerTokenKind.TOK_FOR:
                    case QueryLexerTokenKind.TOK_SINCE:
                    case QueryLexerTokenKind.TOK_UNTIL:
                    {
                        tok = Lexer.NextToken(tokContext);

                        string forString = string.Empty;

                        if (tok.Kind == QueryLexerTokenKind.TOK_SINCE)
                        {
                            forString = "since";
                        }
                        else if (tok.Kind == QueryLexerTokenKind.TOK_UNTIL)
                        {
                            forString = "until";
                        }

                        Lexer.ConsumeNextArg = true;
                        tok = Lexer.PeekToken(tokContext);

                        while (tok.Kind != QueryLexerTokenKind.END_REACHED)
                        {
                            tok = Lexer.NextToken(tokContext);
                            if (tok.Kind != QueryLexerTokenKind.TERM)
                            {
                                throw new InvalidOperationException();
                            }

                            if (tok.Value == "show" || tok.Value == "bold" || tok.Value == "for" || tok.Value == "since" || tok.Value == "until")
                            {
                                Lexer.SetPrev();
                                Lexer.ConsumeNextArg = false;
                                break;
                            }

                            if (!String.IsNullOrEmpty(forString))
                            {
                                forString += " ";
                            }
                            forString += tok.Value;

                            Lexer.ConsumeNextArg = true;
                            tok = Lexer.PeekToken(tokContext);
                        }

                        if (!String.IsNullOrEmpty(forString))
                        {
                            QueryMap.Add(QueryKindEnum.QUERY_FOR, forString);
                        }
                        break;
                    }

                    default:
                        goto done;
                    }

                    tok = Lexer.PeekToken(tokContext);
                }

                done :;
            }

            return(limiter);
        }