public override bool TryGetTokenFromInput(ITextCursor textCursor, out string token)
        {
            var tokenText = textCursor.All();

            var match = TryParse(tokenText, out token) &&
                        Expression != null &&
                        Expression.IsMatch(tokenText);

            return(match);
        }
        public override bool TryGetTokenFromInput(ITextCursor textCursor, out string token)
        {
            var match = false;

            token = null;

            if (ValidValues != null &&
                ValidValues.Length > 0)
            {
                string matchText = null;

                var queryText = string.Empty;

                while (string.IsNullOrEmpty(matchText) &&
                       ValidValues.Any(v => v.Length >= queryText.Length))
                {
                    var tokenPeek = textCursor.Next();

                    if (string.IsNullOrWhiteSpace(tokenPeek))
                    {
                        break;
                    }

                    queryText = textCursor.RightToLeftParsing
                        ? $"{tokenPeek} {queryText}"
                        : $"{queryText} {tokenPeek}";

                    matchText = TryGetMatchText(ValidValues, queryText.Trim());
                }

                if (!string.IsNullOrEmpty(matchText))
                {
                    token = matchText;
                    match = true;
                }
            }
            else
            {
                var queryText = textCursor.All();

                if (!string.IsNullOrEmpty(queryText))
                {
                    token = queryText;
                    match = true;
                }
            }

            return(match);
        }
Example #3
0
        public override bool TryGetTokenFromInput(ITextCursor textCursor, out string token)
        {
            var match = false;

            token = null;

            if (ValidValues != null &&
                ValidValues.Length > 0)
            {
                string matchText = null;

                var queryText = string.Empty;

                while (true)
                {
                    var tokenPeek = textCursor.Peek();

                    if (string.IsNullOrWhiteSpace(tokenPeek))
                    {
                        break;
                    }

                    if (textCursor.RightToLeftParsing)
                    {
                        queryText = $"{tokenPeek} {queryText}";
                    }
                    else
                    {
                        queryText = $"{queryText} {tokenPeek}";
                    }

                    queryText = queryText.Trim();

                    string[] matchTexts;

                    if (textCursor.RightToLeftParsing)
                    {
                        matchTexts = ValidValues
                                     .Where(t => t.EndsWith(queryText, StringComparison.InvariantCultureIgnoreCase))
                                     .OrderByDescending(t => t.Length)
                                     .ToArray();
                    }
                    else
                    {
                        matchTexts = ValidValues
                                     .Where(t => t.StartsWith(queryText, StringComparison.InvariantCultureIgnoreCase))
                                     .OrderByDescending(t => t.Length)
                                     .ToArray();
                    }

                    if (matchTexts.Length >= 1)
                    {
                        // Avança o cursor
                        textCursor.Next();

                        if (matchTexts.Any(t => t.Equals(queryText, StringComparison.InvariantCultureIgnoreCase)))
                        {
                            matchText = queryText;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                if (!string.IsNullOrEmpty(matchText))
                {
                    token = matchText;
                    match = true;
                }
            }
            else
            {
                var queryText = textCursor.All();

                if (!string.IsNullOrEmpty(queryText))
                {
                    token = queryText;
                    match = true;
                }
            }

            return(match);
        }
Example #4
0
        public bool TryParse(ITextCursor textCursor, Syntax syntax, IRequestContext context, out Expression expression)
        {
            var queryMatch = true;
            var tokens = new Token[syntax.TokenTypes.Length];
            var leftPos = 0;
            var rightPos = syntax.TokenTypes.Length - 1;

            while (leftPos <= rightPos)
            {
                var pos = textCursor.RightToLeftParsing ? rightPos : leftPos;
                var tokenType = syntax.TokenTypes[pos];

                if (tokenType != null)
                {
                    textCursor.SavePosition();

                    object queryToken;
                    if (tokenType.TryGetTokenFromInput(textCursor, out queryToken))
                    {
                        tokens[pos] = new Token(queryToken, TokenSource.Input, tokenType);
                    }
                    else if (context != null &&
                             tokenType.TryGetTokenFromContext(context, out queryToken))
                    {
                        textCursor.RollbackPosition();
                        tokens[pos] = new Token(queryToken, TokenSource.Context, tokenType);
                    }
                    else if (!tokenType.IsOptional)
                    {
                        queryMatch = false;
                        break;
                    }
                    else
                    {
                        textCursor.RollbackPosition();
                    }
                }

                if (textCursor.RightToLeftParsing)
                {
                    rightPos--;
                }
                else
                {
                    leftPos++;
                }

                if (tokenType != null &&
                    tokenType.InvertParsing)
                {
                    textCursor.InvertParsing();
                }
            }

            var remainingText = textCursor.All();

            if (queryMatch &&
                (!syntax.PerfectMatchOnly || string.IsNullOrEmpty(remainingText)))
            {
                expression = new Expression(tokens, syntax, context, remainingText);
            }
            else
            {
                queryMatch = false;
                expression = null;
            }

            return queryMatch;
        }
Example #5
0
        public bool TryParse(ITextCursor textCursor, Syntax syntax, IRequestContext context, out Expression expression)
        {
            var queryMatch = true;
            var tokens     = new Token[syntax.TokenTypes.Length];
            var leftPos    = 0;
            var rightPos   = syntax.TokenTypes.Length - 1;

            while (leftPos <= rightPos)
            {
                var pos       = textCursor.RightToLeftParsing ? rightPos : leftPos;
                var tokenType = syntax.TokenTypes[pos];

                if (tokenType != null)
                {
                    textCursor.SavePosition();

                    object queryToken;
                    if (tokenType.TryGetTokenFromInput(textCursor, out queryToken))
                    {
                        tokens[pos] = new Token(queryToken, TokenSource.Input, tokenType);
                    }
                    else if (context != null &&
                             tokenType.TryGetTokenFromContext(context, out queryToken))
                    {
                        textCursor.RollbackPosition();
                        tokens[pos] = new Token(queryToken, TokenSource.Context, tokenType);
                    }
                    else if (!tokenType.IsOptional)
                    {
                        queryMatch = false;
                        break;
                    }
                    else
                    {
                        textCursor.RollbackPosition();
                    }
                }

                if (textCursor.RightToLeftParsing)
                {
                    rightPos--;
                }
                else
                {
                    leftPos++;
                }

                if (tokenType != null &&
                    tokenType.InvertParsing)
                {
                    textCursor.InvertParsing();
                }
            }

            var remainingText = textCursor.All();

            if (queryMatch &&
                (!syntax.PerfectMatchOnly || string.IsNullOrEmpty(remainingText)))
            {
                expression = new Expression(tokens, syntax, context, remainingText);
            }
            else
            {
                queryMatch = false;
                expression = null;
            }

            return(queryMatch);
        }