/// <summary> /// Creates the label for the sequence column when the sequence /// is modified. /// </summary> /// <param name="sender"> /// A <see cref="System.Object"/> /// </param> /// <param name="_args"> /// A <see cref="EventArgs"/> /// </param> private void OnSequenceChanged(object sender, EventArgs _args) { Application.Invoke(delegate(object resender, EventArgs args) { SequenceText = sequence.ToString(); if (SequenceText == "") { // If we have removed the label, we warn the user. SequenceText = "Error al reconocer"; } if (widget != null) { // This shouldn't be required. widget.Columns[1].QueueResize(); widget.QueueDraw(); widget.ColumnsAutosize(); } }); }
public override bool Match(ref TokenSequence sequence, out string output) { string sequenceOriginal = sequence.ToString(); MatchingInvoker(); List<string> outputList = new List<string>(); bool res; foreach (ExpressionItem item in items) { string expressionString; res = item.Match(ref sequence,out expressionString); if(!res) { output=""; LogSentInvoker("La expresión «" +this.Label +"» falló el reconocimiento, restaurando la secuencia (" +sequenceOriginal +")"); MatchingFinishedInvoker(output); return false; } outputList.Add(expressionString); } output = String.Format(formatString, outputList.ToArray()); LogSentInvoker("La expresion «" +this.Label +"» tuvo exito al reconocer, se produjo la salida «" +output +"»"); MatchingFinishedInvoker(output); return true; }
/// <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; }
/// <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> /// 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(); }