private static void ProcessNear(Stack <string> stack, LexicalElement element, bool matchOrder)
        {
            var o2 = stack.Pop();
            var o1 = stack.Pop();

            if (IsComplexExpression(o1) || IsComplexExpression(o2))
            {
                throw new ParsingException("NEAR keyword only supports simple terms or prefix terms. The keyword does not allow complex expressions as operands", element, ErrorKind.NearKeywordOperandError);
            }

            var matchOrderString = matchOrder.ToString().ToUpper();
            var result           = $"(NEAR(({o1}, {o2}), 2000, {matchOrderString}))";

            stack.Push(result);
        }
Beispiel #2
0
        private static LexicalElement[] RemoveBeginningAndEndingOrphanAndKeyword(LexicalElement[] lexicalElements)
        {
            if (lexicalElements.Length == 1)
            {
                return(lexicalElements);
            }

            var hasBeginningOrphan = lexicalElements[0].Type == LexicalElementType.And;
            var hasEndingOrphan    = lexicalElements[lexicalElements.Length - 1].Type == LexicalElementType.And;

            if (!hasBeginningOrphan && !hasEndingOrphan)
            {
                return(lexicalElements);
            }

            if (hasBeginningOrphan && hasEndingOrphan && lexicalElements.Length == 2)
            {
                return(lexicalElements);
            }

            var startPosition = 0;
            var resultLength  = lexicalElements.Length;

            if (hasBeginningOrphan)
            {
                resultLength--;
                startPosition++;
            }

            if (hasEndingOrphan)
            {
                resultLength--;
            }

            var result = new LexicalElement[resultLength];

            for (var i = 0; i < result.Length; i++)
            {
                result[i] = lexicalElements[startPosition + i];
            }

            return(result);
        }
        private static LexicalElement[] MergeFollowingTerms(LexicalElement[] lexicalElements)
        {
            var result = new List <LexicalElement>(lexicalElements.Length);

            LexicalElement tempTerm = null;

            foreach (var lexicalElement in lexicalElements)
            {
                if (lexicalElement.Type == LexicalElementType.Term)
                {
                    if (tempTerm == null)
                    {
                        tempTerm = lexicalElement;
                    }
                    else
                    {
                        var newText = $"{tempTerm.Text} {lexicalElement.Text}";
                        tempTerm = new LexicalElement(LexicalElementType.Term, newText, tempTerm.Position);
                    }
                }
                else
                {
                    if (tempTerm != null)
                    {
                        result.Add(tempTerm);
                        tempTerm = null;
                    }

                    result.Add(lexicalElement);
                }
            }

            if (tempTerm != null)
            {
                result.Add(tempTerm);
            }

            return(result.ToArray());
        }
Beispiel #4
0
        private static LexicalElement[] FixApostrophesForUserFriendliness(LexicalElement[] lexicalElements)
        {
            var result = new List <LexicalElement>(lexicalElements.Length * 2);

            foreach (var lexicalElement in lexicalElements)
            {
                if (lexicalElement.Type == LexicalElementType.Term && (lexicalElement.Text.Contains("'") || lexicalElement.Text.Contains("’")))
                {
                    var version1 = new LexicalElement(LexicalElementType.Term, lexicalElement.Text.Replace("'", "’"), lexicalElement.Position);
                    result.Add(version1);
                    var version2 = new LexicalElement(LexicalElementType.Term, lexicalElement.Text.Replace("’", "'"), lexicalElement.Position);
                    result.Add(version2);
                    var orKeyword = new LexicalElement(LexicalElementType.Or, "OR", lexicalElement.Position);
                    result.Add(orKeyword);
                }
                else
                {
                    result.Add(lexicalElement);
                }
            }

            return(result.ToArray());
        }
        private static LexicalElement[] ToLexicalElements(Tuple <string, int>[] input)
        {
            var result = new LexicalElement[input.Length];
            var i      = 0;

            foreach (var tuple in input)
            {
                var type = GetLexicalElementType(tuple.Item1);
                var text = RemoveQuotes(tuple.Item1);
                // Adding 1 to the position let us switch from the 0-based character array index, to the 1-based column index of text editors.
                var position = tuple.Item2 + 1;

                var element = new LexicalElement(type, text, position);

                if (element.Type == LexicalElementType.Term && !IsAsteriskFreeOrAsteriskEnded(element.Text))
                {
                    throw new ParsingException("Lexical term element contains misplaced asterisk", element, ErrorKind.MisplacedAsteriskInTerm);
                }

                result[i++] = element;
            }

            return(result);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ParsingException"/> class.
 /// </summary>
 /// <param name="message">The message that explains the reason for this exception.</param>
 /// <param name="lexicalElement">The lexical element where the problem was detected.</param>
 /// <param name="errorKind">The error kind.</param>
 public ParsingException(string message, LexicalElement lexicalElement, ErrorKind errorKind)
     : base(message)
 {
     LexicalElement = lexicalElement;
     ErrorKind      = errorKind;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ParsingException"/> class.
 /// </summary>
 /// <param name="message">The message that explains the reason for this exception.</param>
 /// <param name="lexicalElement">The lexical element where the problem was detected.</param>
 public ParsingException(string message, LexicalElement lexicalElement)
     : base(message)
 {
     LexicalElement = lexicalElement;
     ErrorKind      = ErrorKind.UnknownError;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ParsingException"/> class.
 /// </summary>
 /// <param name="lexicalElement">The lexical element where the problem was detected.</param>
 public ParsingException(LexicalElement lexicalElement)
 {
     LexicalElement = lexicalElement;
     ErrorKind      = ErrorKind.UnknownError;
 }