/// <summary> /// Parses the where clauses and metadata in a text wildcard to extract nested wildcards (helper) /// </summary> /// <param name="w">The string containing nested TextWildcards to parse.</param> private void ParseNestedWildcardsHelper(ref string s) { int cc = 0; TextWildcard inner; s = s ?? String.Empty; StringBuilder sb = new StringBuilder(s.Length); do { // Read the string from cc till the next open brace (wildcard delimiter). while ((cc < s.Length) && (s[cc] != '{')) { sb.Append(s[cc++]); } // Otherwise, extract the nested text wildcard inner = TextWildcard.XtractWildcard(s, ref cc); // If the extraction failed, continue if (inner == null) { continue; } // Replace the wildcard entry with its unique keycode sb.AppendFormat("{{{0}}}", inner.Keycode); // Add the text wildcard to the reference lists // When a wildcard is added, all nested wildcards are also processed AddWildcard(inner); } while(cc < s.Length); s = sb.ToString(); }
private List <string> GetValidReplacements(ProductionRule productionRule) { List <string> vr = new List <string>(productionRule.Replacements.Count); for (int i = 0; i < productionRule.Replacements.Count; ++i) { string replacement = (productionRule.Replacements[i] ?? String.Empty).Trim(); for (int cc = 0; cc < replacement.Length; ++cc) { cc = replacement.IndexOf('{', cc); if (cc == -1) { break; } int bcc = cc; TextWildcard w = TextWildcard.XtractWildcard(replacement, ref cc); if (w.Name != "void") { continue; } replacement = replacement.Remove(bcc) + replacement.Substring(cc); cc = bcc; } if (String.IsNullOrEmpty(replacement)) { continue; } vr.Add(replacement); } return(vr); }
/// <summary> /// Extracts the metadata strings from a regular expression match object that /// contains the wildcard /// </summary> /// <param name="m">The regular expression match object that contains the wildcard.</param> private string[] FetchMetadata(TextWildcard w) { if ((w == null) || String.IsNullOrEmpty(w.Metadata)) { return(null); } ReplaceNestedWildcards(w); return(w.Metadata.Split(new string[] { "\r", "\n", @"\\", @"\\r", @"\\n" }, StringSplitOptions.None)); }
/// <summary> /// Converts the wildcard contained within the provided regular expression /// match object into a Token object. /// </summary> /// <param name="m">The regular expression match object that contains the wildcard.</param> private Token TokenizeTextWildcard(TextWildcard w) { return(new Token( w.Value, new NamedTaskElement(String.Format("{{{0}{1}}}", w.Keycode, w.Obfuscated ? "?" : "")), // The wildcard has not been added (will be the next one) so this counter needs to be incremented new string[] { (currentWildcardIx + 1).ToString() } )); }
/// <summary> /// Converts the literal string to the left of the provided wildcard match /// into a Token object. /// </summary> /// <param name="taskPrototype">The task prototype string.</param> /// <param name="cc">Read header for the task prototupe string pointing to /// the start of the literal string</param> /// <param name="m">The regular expression match object that contains the next /// wildcard.</param> private Token TokenizeLeftLiteralString(string taskPrototype, ref int cc, TextWildcard w) { if (cc > w.Index) { return(null); } string ss = taskPrototype.Substring(cc, w.Index - cc); cc = w.Index + w.Value.Length; return(String.IsNullOrEmpty(ss) ? null : new Token(ss)); }
/* * private void SRGSExpandWhereWildcard(TextWildcard w) * { * string keyword = ComputeKeyword(w); * string uri = "#_"; * SRGSWriteRuleRef(uri); * } */ private void SRGSExpandWildcard(string s, ref int cc) { string uri = "#_"; TextWildcard w = TextWildcard.XtractWildcard(s, ref cc); /* * if (!String.IsNullOrEmpty(w.Where)) * { * SRGSExpandWhereWildcard(w); * return; * } */ string keyword = ComputeKeyword(w); switch (keyword) { case "category": uri += "categories"; break; case "pron": uri += "pronobjs"; break; case "gesture": case "name": case "female": case "male": case "location": case "beacon": case "placement": case "room": case "object": case "aobject": case "kobject": case "sobject": case "pronobj": case "pronsub": uri += w.Name + "s"; break; case "question": writer.WriteString("question"); return; case "void": return; default: return; } SRGSWriteRuleRef(uri); }
/// <summary> /// Replaces all wildcards contained within the given wildcard metadata. /// </summary> /// <returns>The input string with all wildcards replaced.</returns> /// <param name="s">The input string</param> public void ReplaceNestedWildcards(TextWildcard textWildcard) { string s = textWildcard.Metadata; if (ReplaceNestedWildcardsHelper(ref s)) { textWildcard.Metadata = s; } s = textWildcard.Where; if (ReplaceNestedWildcardsHelper(ref s)) { textWildcard.Where = s; } }
private string ComputeKeyword(TextWildcard w) { if (w.Type == null) { return(w.Name); } switch (w.Name) { case "location": switch (w.Type) { case "beacon": case "placement": case "room": return(w.Type); } break; case "name": switch (w.Type) { case "male": case "female": return(w.Type); } break; case "object": switch (w.Type) { case "aobject": case "kobject": case "special": return(w.Type[0].ToString() + w.Name); } break; case "pron": switch (w.Type) { case "obj": return("pronobj"); case "sub": return("pronsub"); } break; } return(w.Name); }
/// <summary> /// Parses the where clauses and metadata in a text wildcard to extract nested wildcards. /// </summary> /// <param name="w">The TextWildcard to parse.</param> private void ParseNestedWildcards(TextWildcard textWildcard) { string s; if (!String.IsNullOrEmpty(s = textWildcard.Metadata)) { ParseNestedWildcardsHelper(ref s); textWildcard.Metadata = s; } if (!String.IsNullOrEmpty(s = textWildcard.Where)) { ParseNestedWildcardsHelper(ref s); textWildcard.Where = s; } }
private bool ReplaceNestedWildcardsHelper(ref string s) { int cc = 0; TextWildcard inner; if (String.IsNullOrEmpty(s)) { return(false); } StringBuilder sb = new StringBuilder(s.Length); do { // Fetch the string from cc till the next open brace (wildcard delimiter). while ((cc < s.Length) && (s[cc] != '{')) { sb.Append(s[cc++]); } // If the end of the string has been reached, quit if (cc >= s.Length) { break; } // Otherwise, extract the text wildcard inner = TextWildcard.XtractWildcard(s, ref cc); // If the extraction failed, continue if (inner == null) { continue; } // Otherwise, if the wildcard has where clauses or metadata, they need to be replaced first. if (!String.IsNullOrEmpty(inner.Metadata) || !String.IsNullOrEmpty(inner.Where)) { ReplaceNestedWildcards(inner); } // After replacing nested wildcards in where clauses and metadata, the inner wildcard value is replaced if (!wildcards.ContainsKey(inner.Keycode) || (wildcards[inner.Keycode] == null)) { return(false); } sb.Append(wildcards[inner.Keycode].Replacement.Name); } while(cc < s.Length); // Finally, the metadata of the wildcard is updated. s = sb.ToString(); return(true); }
/// <summary> /// Safely adds a wildcard to the wildcards and wildcardsByKeycode storages /// </summary> /// <param name="w">The wildcard to add.</param> private void AddWildcard(TextWildcard w) { if ((w == null) || !w.Success) { return; } if (!wildcards.ContainsKey(w.Keycode)) { wildcards.Add(w.Keycode, new Wildcard(w)); } else { wildcards[w.Keycode].Add(w); } textWildcards.Add(w); ++this.currentWildcardIx; ParseNestedWildcards(w); }
public static TextWildcard XtractWildcard(string s, ref int cc) { if ((cc >= s.Length) || (s[cc] != '{')) { return(null); } // Create Wildcard and set index TextWildcard wildcard = new TextWildcard(); wildcard.index = cc; // Read wildcard name ++cc; wildcard.Name = ReadWildcardName(s, ref cc); if (String.IsNullOrEmpty(wildcard.Name)) { return(null); } // Read obfuscator wildcard.obfuscated = Scanner.ReadChar('?', s, ref cc); // Read wildcard type wildcard.Type = ReadWildcardType(s, ref cc); // Read wildcard id wildcard.Id = ReadWildcardId(s, ref cc); // Read wildcard where clauses (query) wildcard.Where = ReadWhereClauses(s, ref cc); // Read wildcard metadata wildcard.metadata = ReadWildcardMetadata(s, ref cc); // Set wildcard value if (cc < s.Length) { ++cc; } wildcard.value = s.Substring(wildcard.index, cc - wildcard.index); return(wildcard); }
/// <summary> /// Parses the given string, splitting it into tokens whic are added to the provided token list. /// All found wildcards are added to the wildcard lists but no replacements are made. /// </summary> /// <param name="s">The string to parse.</param> /// <param name="cc">The read header where the parse must start</param> /// <param name="tokens">The list where found tokens will be added</param> private void ParseString(string s, ref int cc, List <Token> tokens) { int bcc = 0; Token token; TextWildcard tWildcard; do { // Update read header backup bcc = cc; // Read the string from cc till the next open brace (wildcard delimiter). while ((cc < s.Length) && (s [cc] != '{')) { ++cc; } // Wildcard found. Extract the string to the left string left = s.Substring(bcc, cc - bcc); // Store the string at the left of the wildcard as token tokens.Add(new Token(left)); // If the end of the string has been reached, quit if (cc >= s.Length) { break; } // Otherwise, extract the text wildcard tWildcard = TextWildcard.XtractWildcard(s, ref cc); // If the extraction failed, continue if (tWildcard == null) { continue; } // Convert the text wildcard into a token token = TokenizeTextWildcard(tWildcard); // Add the text wildcard to the reference lists // When a wildcard is added, all nested wildcards are also processed AddWildcard(tWildcard); // Add the token tokens.Add(token); } while(cc < s.Length); }
public void FindWildcards(string s) { int cc = 0; this.textWildcards.Clear(); this.wildcards.Clear(); TextWildcard w; while (cc < s.Length) { if (s[cc] == '{') { w = TextWildcard.XtractWildcard(s, ref cc); if (w == null) { continue; } AddWildcard(w); } ++cc; } }