/// <summary> /// Call point to start the processing. /// </summary> protected override void Process() { MessageLogSentInvoker("==========================================="); MessageLogSentInvoker(" Comenzando proceso de análisis sintáctico"); MessageLogSentInvoker("==========================================="); SyntacticalRule startRule = SyntacticalRulesLibrary.Instance.StartRule; OnMatcherMatching(this, new MatchingArgs(SyntacticalRulesLibrary.Instance.StartRule)); TokenSequence inputTokens = new TokenSequence(startTokens); parsingResult = startRule.Match(ref inputTokens, out output); if (inputTokens.Count > 0) { // Not all tokens were matched, so the parsing process // was unsuccessfull. parsingResult = false; } ProcessFinishedInvoker(); }
private bool ProcessExpression(SymbolTable symbolTable) { // TODO: If there are performance problems, we could find a faster way to detect expressions than doing a full parse var tokenSequence = new TokenSequence(XValue); if (tokenSequence.RequiresProcessing) { if (tokenSequence.RequiresTemplateArguments) { var templateInstance = TemplateInstance; if (templateInstance == null || templateInstance.Template == null) { return(false); } XValue = tokenSequence.Process(symbolTable, templateInstance.ArgumentDictionary); } else { XValue = tokenSequence.Process(symbolTable, null); } } return(true); }
public void RepeaterTestCase() { TokenSequence sequence = new TokenSequence(); Token t = new Token("NUMBER"); t.Text = "200.9"; sequence.Append(t); t = new Token("ADD"); t.Text = "+"; sequence.Append(t); t = new Token("NUMBER"); t.Text = "14"; sequence.Append(t); t = new Token("ADD"); t.Text = "+"; sequence.Append(t); t = new Token("NUMBER"); t.Text = "28"; sequence.Append(t); SyntacticalRule rule = new SyntacticalRule("formula"); SyntacticalExpression exp = new SyntacticalExpression(); exp.FormatString = "{0}{1}"; ExpressionTokenItem eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; exp.Items.Add(eti); ExpressionGroupItem group = new ExpressionGroupItem(); group.Modifier = ExpressionItemModifier.RepeatingNonCompulsory; eti = new ExpressionTokenItem(); eti.TokenType = "ADD"; group.ChildrenItems.Add(eti); eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; group.ChildrenItems.Add(eti); group.FormatString = " + {1}"; exp.Items.Add(group); rule.Expressions.Add(exp); string output; bool res = rule.Match(ref sequence, out output); Assert.IsTrue(res, "Matching wasn't succesfull"); Assert.AreEqual("200.9 + 14 + 28", output, "Output isn't correct.'"); }
private void TokenCheckedInvoker(TokenSequence lastSequence, Token currentToken) { if (TokenChecked != null) { TokenChecked(this, new TokenCheckedArgs(lastSequence, currentToken)); } }
private static string GenerateToken(this KernelCommand command) { var seed = command.Parent?.GetNextToken(); var sequence = new TokenSequence(seed); command.Properties.Add(TokenKey, sequence); return(sequence.Current); }
public void RepeaterTestCase() { TokenSequence sequence= new TokenSequence(); Token t = new Token("NUMBER"); t.Text="200.9"; sequence.Append(t); t = new Token("ADD"); t.Text="+"; sequence.Append(t); t = new Token("NUMBER"); t.Text="14"; sequence.Append(t); t = new Token("ADD"); t.Text="+"; sequence.Append(t); t = new Token("NUMBER"); t.Text="28"; sequence.Append(t); SyntacticalRule rule = new SyntacticalRule("formula"); SyntacticalExpression exp = new SyntacticalExpression(); exp.FormatString="{0}{1}"; ExpressionTokenItem eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; exp.Items.Add(eti); ExpressionGroupItem group = new ExpressionGroupItem(); group.Modifier = ExpressionItemModifier.RepeatingNonCompulsory; eti = new ExpressionTokenItem(); eti.TokenType = "ADD"; group.ChildrenItems.Add(eti); eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; group.ChildrenItems.Add(eti); group.FormatString=" + {1}"; exp.Items.Add(group); rule.Expressions.Add(exp); string output; bool res = rule.Match(ref sequence, out output); Assert.IsTrue(res, "Matching wasn't succesfull"); Assert.AreEqual("200.9 + 14 + 28", output, "Output isn't correct.'"); }
private static bool BindExpression(ParserContext context, XObject xmlObject, string xmlValue, PropertyInfo boundProperty) { var tokenSequence = new TokenSequence(xmlValue); if (tokenSequence.RequiresProcessing) { DelayedBind(context, xmlObject, xmlValue, boundProperty); return(true); } return(false); }
private Token TestToken(string text, LexicalRule rule) { TokenSequence tokens = new TokenSequence(); foreach (char c in text) { tokens.Append(new Token(c.ToString(), 0,0, new FloatBitmap(2,2))); } Token res; if(!rule.Match(tokens, out res)) return null; return res; }
private Token TestToken(string text, LexicalRule rule) { TokenSequence tokens = new TokenSequence(); foreach (char c in text) { tokens.Append(new Token(c.ToString(), 0, 0, new FloatBitmap(2, 2))); } Token res; if (!rule.Match(tokens, out res)) { return(null); } return(res); }
/// <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> /// Handles the start of processing of a new node. /// </summary> /// <param name="sender"> /// A <see cref="System.Object"/> /// </param> /// <param name="args"> /// A <see cref="EventArgs"/> /// </param> private void OnControllerTokenChecked(object sender, TokenCheckedArgs args) { Application.Invoke(sender, args, delegate(object resender, EventArgs _args) { TokenCheckedArgs a = _args as TokenCheckedArgs; if(!sequencingFinished) { currentToken = a.CurrentToken; FloatBitmap sequenceImage; if(a.LastSequence!= null) { TokenSequence joinSeq = new TokenSequence(a.LastSequence); lastToken = joinSeq.Last; joinSeq.Append(currentToken); Token joinedToken =Token.Join(joinSeq, ""); sequenceImage = joinedToken.Image; } else { sequenceImage = currentToken.Image; lastToken = null; } // We add a border to the orginal image. Gdk.Pixbuf sequencePixbuf = sequenceImage.CreatePixbuf(); Gdk.Pixbuf drawnImage = new Gdk.Pixbuf(sequencePixbuf.Colorspace,false, 8, sequencePixbuf.Width+10, sequencePixbuf.Height+10); drawnImage.Fill(0xFFFFFFFF); sequencePixbuf.CopyArea(0, 0, sequencePixbuf.Width, sequencePixbuf.Height, drawnImage, 5,5); if(lastToken!=null) { uint color; if(currentToken.CloseFollows(lastToken)) { color = 0x00FF00; sequencingStepResultLbl.Markup = String.Format("<b>Sí, el símbolo «{0}» se añadirá a la secuencia actual</b>", currentToken.Text); } else { color = 0xFF0000; sequencingStepResultLbl.Markup = String.Format("<b>No, «{0}» no puede ser considerado parte de la secuencia ({1})</b>", currentToken.Text, a.LastSequence.ToString()); } Gdk.Pixbuf markedImage = drawnImage.Copy(); // We paint the image of the color markedImage = markedImage.CompositeColorSimple(markedImage.Width, markedImage.Height, Gdk.InterpType.Nearest, 100, 1, color, color); // We are going to mark the image of the to symbols being considered // with their baselines. int min = int.MaxValue; foreach (Token t in a.LastSequence ) { if(t.Top < min) min =t.Top; } int offset = Math.Min(min, currentToken.Top); int lastBaseline = lastToken.Baseline - offset; int currentBaseline = currentToken.Baseline - offset; markedImage.CopyArea(0, lastBaseline, markedImage.Width, 5, drawnImage, 0,lastBaseline); markedImage.CopyArea(0, currentBaseline, markedImage.Width,5, drawnImage, 0,currentBaseline); } baselineImageArea.Image = drawnImage; } if(controller.StepMode == ControllerStepMode.StepByStep) tokenizingNextButtonsAlign.Sensitive = true; }); }
protected override bool MatchSequence(ref TokenSequence sequence, out string output) { output=""; List<string> res = new List<string>(); foreach (ExpressionItem item in childrenItems) { string auxOutput; if(item.Match(ref sequence, out auxOutput)) { res.Add(auxOutput); } else { return false; } } output = String.Format(FormatString, res.ToArray()); return true; }
protected override bool MatchSequence (ref TokenSequence sequence, out string output) { // The actual matching is done by the rule. SyntacticalRule ruleCalled = SyntacticalRulesLibrary.Instance[ruleName]; bool res = ruleCalled.Match(ref sequence, out output); if(res) { output = String.Format(formatString, output); } return res; }
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; }
public void SumAndMultTestCase() { TokenSequence sequence = new TokenSequence(); Token t = new Token("NUMBER"); t.Text = "200.9"; sequence.Append(t); t = new Token("ADD"); t.Text = "+"; sequence.Append(t); t = new Token("NUMBER"); t.Text = "28"; sequence.Append(t); t = new Token("MULT"); t.Text = "x"; sequence.Append(t); t = new Token("NUMBER"); t.Text = "14"; sequence.Append(t); SyntacticalRulesLibrary.Instance.ClearRules(); SyntacticalRule rule = new SyntacticalRule("multiplicacion"); SyntacticalRulesLibrary.Instance.AddRule(rule); SyntacticalExpression exp = new SyntacticalExpression(); exp.FormatString = "{0}{1}"; ExpressionTokenItem eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; exp.Items.Add(eti); ExpressionGroupItem group = new ExpressionGroupItem(); group.Modifier = ExpressionItemModifier.RepeatingNonCompulsory; eti = new ExpressionTokenItem(); eti.TokenType = "MULT"; group.ChildrenItems.Add(eti); eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; group.ChildrenItems.Add(eti); group.FormatString = " x {1}"; exp.Items.Add(group); rule.Expressions.Add(exp); rule = new SyntacticalRule("formula"); SyntacticalRulesLibrary.Instance.AddRule(rule); exp = new SyntacticalExpression(); exp.FormatString = "{0}{1}"; ExpressionRuleCallItem esi = new ExpressionRuleCallItem(); esi.RuleName = "multiplicacion"; exp.Items.Add(esi); group = new ExpressionGroupItem(); group.Modifier = ExpressionItemModifier.RepeatingNonCompulsory; eti = new ExpressionTokenItem(); eti.TokenType = "ADD"; group.ChildrenItems.Add(eti); esi = new ExpressionRuleCallItem(); esi.RuleName = "multiplicacion"; group.ChildrenItems.Add(esi); group.FormatString = " + {1}"; exp.Items.Add(group); rule.Expressions.Add(exp); string output; bool res = rule.Match(ref sequence, out output); Assert.IsTrue(res, "Matching wasn't succesfull"); Assert.AreEqual("200.9 + 28 x 14", output, "Output isn't correct.'"); }
/// <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; }
public SequenceSetArgs(TokenSequence newSequence) { this.newSequence = newSequence; }
private void SequenceRestoredInvoker(TokenSequence backupSequence) { if(SequenceRestored != null) { SequenceRestored(this, new SequenceSetArgs(backupSequence)); } }
/// <summary> /// Tries to math a <see cref="TokenSequence"/> with the rule's /// expressions. /// </summary> /// <param name="sequence"> /// The <see cref="TokenSequence"/> we want to match. /// </param> /// <param name="text"> /// The <see cref="System.String"/> output text produced by the /// expression that matched the input sequence. /// </param> /// <returns> /// <c>true</c> it the sequence was matched correctly, <c>false</c> /// if there were errors. /// </returns> public override bool Match(ref TokenSequence sequence, out string text) { TokenSequence backupSequence = new TokenSequence(sequence); this.LogSentInvoker("Se intentará aplicar la regla «{0}»", this.Label); foreach (SyntacticalExpression expression in expressions) { this.LogSentInvoker("Se intentará aplicar la expresión «" +expression.Label +"»"); string expressionRes; if(expression.Match(ref sequence, out expressionRes)) { // If the matching is successful, we consider // the output valid. text = expressionRes; return true; } else { sequence = new TokenSequence(backupSequence); SequenceRestoredInvoker(sequence); } } text =""; return false; }
private void RelatedSequenceSetInvoker(TokenSequence relatedSequence) { if(RelatedSequenceSet != null) { RelatedSequenceSet(this, new SequenceSetArgs(relatedSequence)); } }
private void TokenCheckedInvoker(TokenSequence lastSequence, Token currentToken) { if(TokenChecked !=null) { TokenChecked(this, new TokenCheckedArgs(lastSequence, currentToken)); } }
public abstract bool Match(ref TokenSequence sequence, out string text);
public void SumAndMultTestCase() { TokenSequence sequence= new TokenSequence(); Token t = new Token("NUMBER"); t.Text="200.9"; sequence.Append(t); t = new Token("ADD"); t.Text="+"; sequence.Append(t); t = new Token("NUMBER"); t.Text="28"; sequence.Append(t); t = new Token("MULT"); t.Text="x"; sequence.Append(t); t = new Token("NUMBER"); t.Text="14"; sequence.Append(t); SyntacticalRulesLibrary.Instance.ClearRules(); SyntacticalRule rule = new SyntacticalRule("multiplicacion"); SyntacticalRulesLibrary.Instance.AddRule(rule); SyntacticalExpression exp = new SyntacticalExpression(); exp.FormatString="{0}{1}"; ExpressionTokenItem eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; exp.Items.Add(eti); ExpressionGroupItem group = new ExpressionGroupItem(); group.Modifier = ExpressionItemModifier.RepeatingNonCompulsory; eti = new ExpressionTokenItem(); eti.TokenType = "MULT"; group.ChildrenItems.Add(eti); eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; group.ChildrenItems.Add(eti); group.FormatString=" x {1}"; exp.Items.Add(group); rule.Expressions.Add(exp); rule = new SyntacticalRule("formula"); SyntacticalRulesLibrary.Instance.AddRule(rule); exp = new SyntacticalExpression(); exp.FormatString="{0}{1}"; ExpressionRuleCallItem esi = new ExpressionRuleCallItem(); esi.RuleName = "multiplicacion"; exp.Items.Add(esi); group = new ExpressionGroupItem(); group.Modifier = ExpressionItemModifier.RepeatingNonCompulsory; eti = new ExpressionTokenItem(); eti.TokenType = "ADD"; group.ChildrenItems.Add(eti); esi = new ExpressionRuleCallItem(); esi.RuleName = "multiplicacion"; group.ChildrenItems.Add(esi); group.FormatString=" + {1}"; exp.Items.Add(group); rule.Expressions.Add(exp); string output; bool res = rule.Match(ref sequence, out output); Assert.IsTrue(res, "Matching wasn't succesfull"); Assert.AreEqual("200.9 + 28 x 14", output, "Output isn't correct.'"); }
/// <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> /// Call point to start the processing. /// </summary> protected override void Process () { MessageLogSentInvoker("==========================================="); MessageLogSentInvoker(" Comenzando proceso de análisis sintáctico"); MessageLogSentInvoker("==========================================="); SyntacticalRule startRule = SyntacticalRulesLibrary.Instance.StartRule; OnMatcherMatching(this, new MatchingArgs(SyntacticalRulesLibrary.Instance.StartRule)); TokenSequence inputTokens = new TokenSequence(startTokens); parsingResult = startRule.Match(ref inputTokens, out output); if(inputTokens.Count > 0) { // Not all tokens were matched, so the parsing process // was unsuccessfull. parsingResult = false; } ProcessFinishedInvoker(); }
/// <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> /// Handles the start of processing of a new node. /// </summary> /// <param name="sender"> /// A <see cref="System.Object"/> /// </param> /// <param name="args"> /// A <see cref="EventArgs"/> /// </param> private void OnControllerTokenChecked(object sender, TokenCheckedArgs args) { Application.Invoke(sender, args, delegate(object resender, EventArgs _args) { TokenCheckedArgs a = _args as TokenCheckedArgs; if (!sequencingFinished) { currentToken = a.CurrentToken; FloatBitmap sequenceImage; if (a.LastSequence != null) { TokenSequence joinSeq = new TokenSequence(a.LastSequence); lastToken = joinSeq.Last; joinSeq.Append(currentToken); Token joinedToken = Token.Join(joinSeq, ""); sequenceImage = joinedToken.Image; } else { sequenceImage = currentToken.Image; lastToken = null; } // We add a border to the orginal image. Gdk.Pixbuf sequencePixbuf = sequenceImage.CreatePixbuf(); Gdk.Pixbuf drawnImage = new Gdk.Pixbuf(sequencePixbuf.Colorspace, false, 8, sequencePixbuf.Width + 10, sequencePixbuf.Height + 10); drawnImage.Fill(0xFFFFFFFF); sequencePixbuf.CopyArea(0, 0, sequencePixbuf.Width, sequencePixbuf.Height, drawnImage, 5, 5); if (lastToken != null) { uint color; if (currentToken.CloseFollows(lastToken)) { color = 0x00FF00; sequencingStepResultLbl.Markup = String.Format("<b>Sí, el símbolo «{0}» se añadirá a la secuencia actual</b>", currentToken.Text); } else { color = 0xFF0000; sequencingStepResultLbl.Markup = String.Format("<b>No, «{0}» no puede ser considerado parte de la secuencia ({1})</b>", currentToken.Text, a.LastSequence.ToString()); } Gdk.Pixbuf markedImage = drawnImage.Copy(); // We paint the image of the color markedImage = markedImage.CompositeColorSimple(markedImage.Width, markedImage.Height, Gdk.InterpType.Nearest, 100, 1, color, color); // We are going to mark the image of the to symbols being considered // with their baselines. int min = int.MaxValue; foreach (Token t in a.LastSequence) { if (t.Top < min) { min = t.Top; } } int offset = Math.Min(min, currentToken.Top); int lastBaseline = lastToken.Baseline - offset; int currentBaseline = currentToken.Baseline - offset; markedImage.CopyArea(0, lastBaseline, markedImage.Width, 5, drawnImage, 0, lastBaseline); markedImage.CopyArea(0, currentBaseline, markedImage.Width, 5, drawnImage, 0, currentBaseline); } baselineImageArea.Image = drawnImage; } if (controller.StepMode == ControllerStepMode.StepByStep) { tokenizingNextButtonsAlign.Sensitive = true; } }); }
public SequenceNode(TokenSequence sequence, NodeView widget) { this.sequence = sequence; this.sequence.Changed += new EventHandler(OnSequenceChanged); this.widget = widget; }
/// <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; }
/// <summary> /// Tries to match the token's related items. /// </summary> /// <param name="matched"> /// The <see cref="Token"/> matched token. /// </param> /// <param name="sequence"> /// The remaining tokens. /// </param> /// <param name="output"> /// A <see cref="System.String"/> containing the output. /// </param> /// <returns> /// A <see cref="System.Boolean"/> indicating if the matching was /// successfull. /// </returns> private bool MatchRelatedItems(Token matched, ref TokenSequence sequence, out string output) { output = ""; // We return true unless we found a matching error in one of the // related items. bool res = true; // We have to create a list of outputs, so we can apply the // output format string later. List<string> outputs = new List<string>(); // We add the matched token own text as the first element outputs.Add(matched.Text); foreach(ExpressionItem relatedItem in this.relatedItems) { TokenSequence backupSequence = new TokenSequence(sequence); string relatedItemOutput; TokenSequence relatedRemnant = GetRelatedItems(matched,sequence,relatedItem.Position); LogSentInvoker("Reconociendo los items relacionados ({0}) de {1} con el elemento «{2}»", relatedRemnant, matched.Type, relatedItem.ToString()); RelatedSequenceSetInvoker(relatedRemnant); bool matchRes = relatedItem.Match(ref relatedRemnant, out relatedItemOutput); if(matchRes) { outputs.Add(relatedItemOutput); if(relatedRemnant.Count > 0) { // We weren't unable to match all the tokens, // so we have to restore partially. sequence = backupSequence; int i =0; while(i<sequence.Count) { if(!relatedRemnant.Sequence.Contains(sequence[i])) { sequence.Sequence.Remove(sequence[i]); } else { i++; } } } } else if(!relatedItem.IsCompulsory) { // We can fail, because then we may be removing tokens useful // to later rules; outputs.Add(""); sequence = backupSequence; } else { res = false; break; } } if(res) { output = String.Format(this.formatString, outputs.ToArray()); } return res; }
public TokenCheckedArgs (TokenSequence lastSequence, Token currentToken) : base () { this.lastSequence = lastSequence; this.currentToken= currentToken; }
/// <summary> /// Tries to match a given token sequence with this rule. /// </summary> /// <param name="tokens"> /// The tokens we try to group. /// </param> /// <param name="foundToken"> /// The token result of joining the sequence, has a meaning or not. /// </param> /// <returns> /// If a valid token was found. /// </returns> public bool Match(TokenSequence tokens, out Token foundToken) { // We form the expression to be matched. string text = ""; foreach(Token t in tokens) { if(!String.IsNullOrEmpty(t.Type)) { text += String.Format("%%{0}%%", t.Type); } else { text += t.Text; } } if(regularExpressions == null) { // We have to load the expressions. regularExpressions = new List<Regex>(); foreach(string pattern in lexicalExpressions) { // We add the modifiers to apply the regex to all the // text. Regex regex = new Regex(String.Format("^{0}$",pattern), RegexOptions.Compiled |RegexOptions.Singleline); regularExpressions.Add(regex); } } bool found = false; // Now we try to match a rule with the text. foreach(Regex expression in regularExpressions) { Match match = expression.Match(text); if(match.Success) { found = true; break; } } foundToken = Token.Join(tokens, this.ruleName); return found; }
public TokenCheckedArgs(TokenSequence lastSequence, Token currentToken) : base() { this.lastSequence = lastSequence; this.currentToken = currentToken; }