Beispiel #1
0
        /// <summary>
        /// Adds a child to the node.
        /// </summary>
        /// <param name="childNode">
        /// A <see cref="SequenceNode"/>
        /// </param>
        public void AddChildSequence(SequenceNode childNode)
        {
            AddChildSequenceArgs _args = new AddChildSequenceArgs(childNode);

            Application.Invoke(this,
                               _args,
                               delegate(object sender, EventArgs args)
            {
                AddChildSequenceArgs a = args as AddChildSequenceArgs;

                a.ChildNode.NodeName =
                    String.Format("{0}.{1}", this.NodeName, this.ChildCount + 1);

                this.AddChild(a.ChildNode);

                if (widget != null)
                {
                    // We expand the node
                    widget.ExpandAll();
                    widget.ColumnsAutosize();
                }
            });
        }
		public SequenceAddedArgs(SequenceNode sequence)
		{
			this.sequence = sequence;
		}
		public AddChildSequenceArgs(SequenceNode childNode)
		{
			this.childNode= childNode;
		}
		/// <summary>
		/// Adds a child to the node.
		/// </summary>
		/// <param name="childNode">
		/// A <see cref="SequenceNode"/>
		/// </param>
		public void AddChildSequence(SequenceNode childNode)
		{
			AddChildSequenceArgs _args = new AddChildSequenceArgs(childNode);
			Application.Invoke(this, 
			                   _args,
			                   delegate(object sender, EventArgs args)
			{
				AddChildSequenceArgs a = args as AddChildSequenceArgs;
			
				a.ChildNode.NodeName = 
					String.Format("{0}.{1}",this.NodeName,this.ChildCount+1);
				
				this.AddChild(a.ChildNode);
				
				if(widget!=null)
				{
					// We expand the node			
					widget.ExpandAll();
					widget.ColumnsAutosize();
				}
				
			});
		}
		private void SequenceAddedInvoker(SequenceNode sequence)
		{
			if(SequenceAdded !=null)
				SequenceAdded(this, new SequenceAddedArgs(sequence));
		}
		/// <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();
		}
		/// <summary>
		/// Processes the tokens given as an input to the controller.
		/// </summary>
		/// <returns>
		/// The list of sequences of contiguous tokens.
		/// </returns>
		private List<SequenceNode> GetTokenSequences()
		{
			List<SequenceNode> tokenSequences = new List<SequenceNode>();
		
			SequenceNode node = null;
			Token lastToken = null;
			Token currentToken = null;
			// All the tokens must be in one sequence.
			while(this.tokens.Count > 0)
			{	
				TokenSequence foundSequence = null;
				
				currentToken = tokens[0];
				
				NodeBeingProcessedInvoker(null);					
				
				if(tokenSequences.Count ==0)
					TokenCheckedInvoker(null, currentToken);
				
				foreach (SequenceNode storedNode in tokenSequences) 
				{					
					// We search the stored sequences so we may find one that
					// has the same baseline as the one being checked.
					storedNode.Select();
					
					this.MessageLogSentInvoker("Comprobando si «{0}» puede formar parte de la secuencia {1}",
					                           currentToken.Text,
					                           storedNode.NodeName);
					
					lastToken = storedNode.Sequence.Last;
					
					TokenCheckedInvoker(new TokenSequence(storedNode.Sequence),
					                    currentToken);
					if(tokenSequences.Count>1)
						SuspendByStep();
					
					if(currentToken.CloseFollows(lastToken))
					{
						foundSequence = storedNode.Sequence;
						break;
					}
					
					Thread.Sleep(50);
					
				}
				
				
				
				if(foundSequence == null)
				{
					// If the symbols aren't contiguous, a new sequence has
					// commenced.
					foundSequence = new TokenSequence();	
					node = new SequenceNode(foundSequence, view);
					tokenSequences.Add(node);
					node.NodeName = tokenSequences.Count.ToString();
					SequenceAddedInvoker(node);
					MessageLogSentInvoker("===== Secuencia {0} añadida =====", 
					                      node.NodeName);
				}
				
				
				
				// We add the token to the current sequence, and remove it
				// from the inital token list.
				foundSequence.Append(currentToken);
				MessageLogSentInvoker("Símbolo «{0}» añadido a la secuencia {1}", 
				                      currentToken.Text,
				                      node.NodeName);	
				
				StepDoneInvoker();				
				SuspendByNode();
				
				
				tokens.RemoveAt(0);
			}
			
			return tokenSequences;
			
		}
		/// <summary>
		/// Gets the tokens of a given node.
		/// </summary>
		/// <param name="node">
		/// A <see cref="SequencedNode"/>
		/// </param>
		/// <returns>
		/// A <see cref="List`1"/>
		/// </returns>
		private List<Token> GetTokens(SequenceNode node)
		{
			List<Token> res = new List<Token>();
			if(node.ChildCount ==0)
			{
				// It should have a token;
				res.Add(node.FoundToken);
			}
			else
			{
				for(int i = 0; i< node.ChildCount; i++)
				{
					res.AddRange(GetTokens(node[i] as SequenceNode));
				}
			}
			
			return res;
		}
		/// <summary>
		/// Sets a <see cref="SequenceNode"/> that will be analyzed lexically.
		/// </summary>
		/// <param name="node">
		/// A <see cref="SequenceNode"/>
		/// </param>
		public void SetSequenceForTokenizing(SequenceNode node)
		{
			sequenceForTokenizing = node;
		}
		private void OnSequencesNVButtonPress(object sender,
		                                           ButtonPressEventArgs args)
		{
			if(processFinished && args.Event.Button == 3)
			{
				TreePath path = new TreePath();
                // Obtenemos el treepath con las coordenadas del cursor.
                sequencesNV.GetPathAtPos(System.Convert.ToInt16 (args.Event.X), 
				                      System.Convert.ToInt16 (args.Event.Y),				              
				                      out path);
                
				if( path != null)
				{
					// We try only if a node was found.			
					SequenceNode node =
						(SequenceNode)(sequencesModel.GetNode(path));	
					
					
					selectedNode = node;										
					sequenceNodeMenu.Popup();	
				}
			}
		}
		/// <summary>
		/// Checks a node for errors.
		/// </summary>
		/// <param name="node">
		/// The node to be checked.
		/// </param>
		/// <returns>
		/// A list with the errors causes.
		/// </returns>
		private List<string> CheckNodeErrors(SequenceNode node)
		{
			List<string> res = new List<string>();
			if(node.ChildCount > 0)
			{
				// If the node has children, we check them.
				for(int i=0; i<node.ChildCount; i++)
				{
					res.AddRange(CheckNodeErrors(node[i] as SequenceNode));
				}
			}
			else
			{
				if(node.FoundToken == null)
				{
					res.Add(String.Format("· La secuencia {0} no encajó con ninguna regla léxica definida.",
					                      node.NodeName));
				}
			}
			
			return res;
		}
Beispiel #12
0
 public AddChildSequenceArgs(SequenceNode childNode)
 {
     this.childNode = childNode;
 }