Beispiel #1
0
        /// <summary>
        /// Matches a given token sequence into one or more Tokens.
        /// </summary>
        /// <param name="sequence">
        /// The token sequence (possibly) containing tokens.
        /// </param>
        private void MatchTokens(SequenceNode node)
        {
            TokenSequence discarded = new TokenSequence();

            TokenSequence accepted      = new TokenSequence();
            SequenceNode  acceptedNode  = new SequenceNode(accepted, view);
            SequenceNode  discardedNode = new SequenceNode(discarded, view);
            TokenSequence sequence      = node.Sequence;

            node.AddChildSequence(acceptedNode);

            foreach (Token t in sequence)
            {
                accepted.Append(t);
            }

            NodeBeingProcessedInvoker(node);
            SuspendByStep();

            bool  found      = false;
            Token foundToken = null;

            bool discardedNodeAdded = false;

            MessageLogSentInvoker("===== Tratando la secuencia {0} =====",
                                  node.NodeName);

            while (accepted.Count > 0 && !found)
            {
                foreach (LexicalRule rule in lexicalRules)
                {
                    found = rule.Match(accepted, out foundToken);

                    MessageLogSentInvoker("¿La regla «{0}» acepta la secuencia ({1})?: {2}",
                                          rule.Name,
                                          accepted.ToString(),
                                          found?"Sí":"No");

                    SequenceBeingMatchedInvoker(foundToken, rule, found);
                    SuspendByStep();

                    if (found)
                    {
                        // We search no more.
                        break;
                    }
                    Thread.Sleep(50);
                }

                // We check if a token was found
                if (!found)
                {
                    // We remove the token from the input sequence and add it
                    // at the beggining of the discarded set.
                    int lastIndex = accepted.Count - 1;
                    if (!discardedNodeAdded)
                    {
                        // If we haven't done so, we add the discarded sequence.
                        node.AddChildSequence(discardedNode);
                        discardedNodeAdded = true;
                    }

                    this.MatchingFailedInvoker();

                    MessageLogSentInvoker("Se elimina el último símbolo de la secuencia {0} para seguir probando.",
                                          accepted.ToString());

                    discarded.Prepend(accepted[lastIndex]);

                    accepted.RemoveAt(lastIndex);
                }
                else
                {
                    // We found a token, so we stop searching and add
                    // the token to the result.
                    acceptedNode.FoundToken = foundToken;
                }
            }



            if (found && discarded.Count > 0)
            {
                MessageLogSentInvoker("Se tratará la secuencia de símbolos descartados.");
                // We follow the recursive path.
                MatchTokens(discardedNode);
            }
            else if (found && discarded.Count == 0)
            {
                // Only one token was found, we assimilate the acceptedNode
                // with its parent.
                node.FoundToken = foundToken;
                node.RemoveSequenceChildren();
            }
            else
            {
                // If nothing was found, we remove the children.
                node.RemoveSequenceChildren();

                MessageLogSentInvoker("No se pudo reconocer la secuencia {0}.",
                                      node.Sequence.ToString());
            }

            StepDoneInvoker();
            SuspendByNode();
        }