public static void ExtractQuoteString(LexerCursor cursor)
        {
            var closed = false;

            while (cursor.TryMatch("QUOTE:START", "\""))
            {
                cursor.PassThrough("ESCAPE", @"\\.");

                if (cursor.TryMatch("QUOTE:END", @""""))
                {
                    closed = true;
                    break;
                }

                cursor.PassThrough("TEXT_RUN", @"[^\\\""]+");
                // LOOP EXIT
                if (cursor.EOF)
                {
                    break;
                }
            }

            if (closed == false)
            {
                cursor.FakeMatch("QUOTE:END", @"""");
            }
        }
Example #2
0
        public static LexerCursor ParseQuery(string input)
        {
            var cursor = new LexerCursor {
                Input    = input,
                MaxStack = 256
            };

            var lastPosition = -1;

            while (cursor.Peek("$", @".", false))
            {
                // LOOP EXIT
                if (cursor.EOF)
                {
                    break;
                }

                if (ExtractContent(cursor))
                {
                    continue;
                }
                // ignore closing parentheses if not in the context of group
                cursor.Ignore(@"\)");
                var closuresRequired = 0;
                while (!cursor.EOF)
                {
                    lastPosition = GobbleUnsupported(lastPosition, cursor);
                    if (!cursor.TryPush(KeywordParenthesesOpen, @"\(", ref closuresRequired))
                    {
                        break;
                    }


                    if (ExtractContent(cursor))
                    {
                        continue;
                    }

                    var exitPop = cursor.TryPop(KeywordParenthesesClose, @"\)", ref closuresRequired);

                    if (exitPop)
                    {
                        break;
                    }
                }

                while (closuresRequired > 0)
                {
                    closuresRequired--;
                    cursor.FakeMatch(KeywordParenthesesClose, @")", pop: true);
                }
            }

            return(cursor);
        }
Example #3
0
 private static int GobbleUnsupported(int lastPosition, LexerCursor cursor)
 {
     if (lastPosition != cursor.Position)
     {
         lastPosition = cursor.Position;
     }
     else
     {
         cursor.TryMatch(null, ".");
     }
     return(lastPosition);
 }
Example #4
0
        public static void ExtractQuoteString(LexerCursor cursor)
        {
            bool?closed = null;

            while (!cursor.EOF)
            {
                if (cursor.Peek(null, "\""))
                {
                    if (null == closed)
                    {
                        closed = false;
                        cursor.TryMatch(KeywordQuoteContent, "\"");
                    }
                    else if (cursor.TryMatch(KeywordQuoteContent, @"([""](\~\d{1,4}(\z|(?=\s)))?)"))
                    {
                        closed = true;
                        break;
                    }
                }

                if (closed == null)
                {
                    break;
                }

                if (cursor.TryMatch(KeywordQuoteContent, @"\\[\""]"))
                {
                    continue;
                }

                // tag unexpected backslashes to be ignored
                if (cursor.TryMatch(null, @"(\\$|\\.)"))
                {
                    continue;
                }
                if (cursor.TryMatch(KeywordQuoteContent, @"[^\\\""]+"))
                {
                    continue;
                }
                // LOOP EXIT
                if (cursor.EOF)
                {
                    break;
                }
            }

            if (closed == false)
            {
                cursor.FakeMatch(KeywordQuoteContent, @"""", pop: true);
            }
        }
        static string SanitizePhase(LexerCursor inputCursor)
        {
            var elements = new List <LexCapture>();

            RemoveDisallowedRepeats(inputCursor, elements);


            var scopebuilder = new ScopeBuilder();

            StructureSyntax(elements, scopebuilder);
            scopebuilder.LastestScopeSanitizer.Sanitize();
            var ret = scopebuilder.LastestScopeSanitizer.ToString().Trim();

            return(string.IsNullOrWhiteSpace(ret) ? "*" : ret);
        }
Example #6
0
        private static bool ExtractContent(LexerCursor cursor)
        {
            cursor.PassThrough(KeywordWhiteSpace, @"\s+");
            ExtractQuoteString(cursor);

            if (cursor.TryMatch(KeywordOperator, "\\+") ||
                cursor.TryMatch(KeywordOperator, "\\-") ||
                cursor.TryMatch(KeywordOperator, "\\!") ||
                cursor.TryMatch(KeywordOperator, "NOT[\\s$]"))
            {
                return(true);
            }
            if (ExtractOperator(cursor) || ExtractSearchTerm(cursor))
            {
                return(true);
            }

            return(false);
        }
        private static void RemoveDisallowedRepeats(LexerCursor inputCursor, List <LexCapture> elements)
        {
            LexCapture lastOperator = null;

            foreach (var element in inputCursor.GetVitalCapture())
            {
                if (element.Type.Name == null)
                {
                    continue;
                }

                element.Value = (element.Value ?? "").Trim();
                if (string.IsNullOrWhiteSpace(element.Value))
                {
                    element.Value = " ";
                }

                if (IsDisallowedRepeats(elements, lastOperator, element))
                {
                    continue;
                }

                elements.Add(element);
                switch (element.Type.Name)
                {
                case LuceneQueryParser.KeywordParenthesesClose:
                case LuceneQueryParser.KeywordParenthesesOpen:
                case LuceneQueryParser.KeywordQuoteContent:
                case LuceneQueryParser.KeywordOperand:
                    lastOperator = null;
                    break;

                case LuceneQueryParser.KeywordOperator:
                    lastOperator = element;
                    break;

                // ReSharper disable once RedundantEmptySwitchSection
                default:
                    break;
                }
            }
        }
Example #8
0
 private static bool ExtractSearchTerm(LexerCursor cursor)
 {
     return(cursor.TryMatch(KeywordOperand, @"(?<var>[\w\.\*\?]+)(\~0*\.\d{1,4}(\z|(?=\s)))?"));
 }
Example #9
0
 private static bool ExtractOperator(LexerCursor cursor)
 {
     return(cursor.TryMatch(KeywordOperator, @"(\bOR\b|\bAND\b)"));
 }
Example #10
0
        public static LexerCursor ParseEquation(string input)
        {
            var cursor = new LexerCursor {
                Input = input
            };

            while (cursor.Peek("$", @".", false))
            {
                // LOOP EXIT
                if (cursor.EOF)
                {
                    break;
                }

                cursor.Ignore(@"\s+");
                ExtractNumber(cursor);
                while (ExtractVariable(cursor))
                {
                    cursor.Ignore(@"\s+");
                    cursor.PassThrough("MEMBER", @"\.");
                    // LOOP EXIT
                    if (cursor.EOF)
                    {
                        break;
                    }
                }

                cursor.Ignore(@"\s+");
                ExtractQuoteString(cursor);

                cursor.Ignore(@"\s+");
                var parenthDepthCounter = 0;
                while (!cursor.EOF)
                {
                    cursor.TryPush("(", ref parenthDepthCounter);
                    cursor.Ignore(@"\s+");
                    if (cursor.TryMatch(","))
                    {
                        continue;
                    }

                    if (cursor.TryMatch("++"))
                    {
                        continue;
                    }
                    if (cursor.TryMatch("--"))
                    {
                        continue;
                    }
                    if (ExtractVariable(cursor))
                    {
                        continue;
                    }
                    if (ExtractNumber(cursor))
                    {
                        continue;
                    }


                    if (cursor.TryMatch("*"))
                    {
                        continue;
                    }
                    if (cursor.TryMatch("\\"))
                    {
                        continue;
                    }
                    if (cursor.TryMatch("%"))
                    {
                        continue;
                    }


                    if (cursor.TryMatch("+"))
                    {
                        continue;
                    }
                    if (cursor.TryMatch("-"))
                    {
                        continue;
                    }

                    if (cursor.TryMatch("==="))
                    {
                        continue;
                    }

                    if (cursor.TryMatch("<="))
                    {
                        continue;
                    }
                    if (cursor.TryMatch(">="))
                    {
                        continue;
                    }
                    if (cursor.TryMatch("<"))
                    {
                        continue;
                    }
                    if (cursor.TryMatch(">"))
                    {
                        continue;
                    }
                    if (cursor.TryMatch("=="))
                    {
                        continue;
                    }

                    if (cursor.TryPop(")", ref parenthDepthCounter))
                    {
                        break;
                    }

                    cursor.PassThrough(".");
                }
            }

            return(cursor);
        }
Example #11
0
 private static bool ExtractNumber(LexerCursor cursor)
 {
     return(cursor.TryMatch("NUMBER", @"\d+(\.\d+)?"));
 }
Example #12
0
 private static bool ExtractVariable(LexerCursor cursor)
 {
     return(cursor.TryMatch("ARG", @"\w([\d\w])*"));
 }