Exemple #1
0
        /// <summary>
        /// Retrives the related items for a token from the remaining items list.
        /// </summary>
        /// <param name="matched">
        /// The <see cref="Token"/> the items we are looking for are related to.
        /// </param>
        /// <param name="remainingItems">
        /// A <see cref="TokenSequence"/> containing the yet to be matched items.
        /// </param>
        /// <param name="position">
        /// A <see cref="ExpressionItemPosition"/> the position of the related item.
        /// </param>
        /// <returns>
        /// A <see cref="TokenSequence"/> containing the items related to the
        /// matched item found in the given position.
        /// </returns>
        protected TokenSequence GetRelatedItems(Token matched,
                                                TokenSequence remainingItems,
                                                ExpressionItemPosition position)
        {
            TokenSequence sequence = new TokenSequence();

            string remainingItemsString = remainingItems.ToString();

            int i = 0;

            while (i < remainingItems.Count)
            {
                Token checkedItem = remainingItems[i];

                if (CheckTokenInRelatedSequence(matched, checkedItem, position))
                {
                    sequence.Append(checkedItem);
                    remainingItems.RemoveAt(i);
                }
                else if (!SpecialPosition(matched, checkedItem))
                {
                    LogSentInvoker("Encontrado {0}, cancelando la creación de la secuencia de items «{1}» {2}",
                                   checkedItem.Text,
                                   position,
                                   matched.Type);
                    break;
                }
                else
                {
                    i++;
                }
            }

            LogSentInvoker("Extraida la secuencia ({0}) en posicion «{1}» de entre los elementos de ({2})",
                           sequence,
                           position,
                           remainingItemsString);

            return(sequence);
        }
Exemple #2
0
        /// <summary>
        /// Tries to match the expetect token with one from the given sequence.
        /// </summary>
        /// <param name="sequence">
        /// A <see cref="TokenSequence"/> containing the items not yet matched.
        /// </param>
        /// <param name="output">
        /// The output in a <see cref="System.String"/>.
        /// </param>
        /// <returns>
        /// A <see cref="System.Boolean"/> that tells if the matching process
        /// was successful.
        /// </returns>
        protected override bool MatchSequence(ref TokenSequence sequence,
                                              out string output)
        {
            output = "";
            int idx = 0;

            // We tell the controller we are trying to match this token.
            TokenMatchingInvoker(this.TokenType);

            if (forceTokenSearch)
            {
                idx = sequence.SearchToken(this.tokenType);
                LogSentInvoker("Forzada búsqueda de {0}, posición {1}",
                               this.tokenType,
                               idx);
            }


            // By default, we say we had a success
            bool  res     = true;
            Token matched = null;



            if (idx == -1)
            {
                LogSentInvoker("El item esperado {0} no fue encontrado.", this.tokenType);
                res = !IsCompulsory;
            }
            else
            {
                bool different;

                // If the token type is a literal, we compare with the text
                // instead of the type.
                if (this.tokenType.StartsWith("'") &&
                    this.tokenType.EndsWith("'"))
                {
                    string expectedText = tokenType.Substring(1,
                                                              this.tokenType.Length - 2);
                    different = expectedText != sequence[idx].Text;
                }
                else
                {
                    different = tokenType != sequence[idx].Type;
                }

                if (different)
                {
                    LogSentInvoker("El item esperado {0} no fue encontrado.", this.tokenType);
                    res = !IsCompulsory;
                }
                else
                {
                    matched = sequence.RemoveAt(idx);
                    if (this.relatedItems.Count == 0)
                    {
                        output = String.Format(formatString, matched.Text);
                    }
                    else
                    {
                        res = MatchRelatedItems(matched, ref sequence, out output);
                        if (!res)
                        {
                            matched = null;
                        }

                        RelatedSequenceSetInvoker(sequence);
                    }
                }
            }



            // We tell the controller we finished matching the token.
            TokenMatchingFinishedInvoker(matched, this.tokenType);

            return(res);
        }