object IXshdVisitor.VisitKeywords(XshdKeywords keywords) { writer.WriteStartElement("Keywords", Namespace); WriteColorReference(keywords.ColorReference); foreach (string word in keywords.Words) { writer.WriteElementString("Word", Namespace, word); } writer.WriteEndElement(); return null; }
XshdRuleSet ImportRuleSet(XmlElement element) { XshdRuleSet ruleSet = new XshdRuleSet(); ruleSet.Name = element.GetAttributeOrNull("name"); if (element.HasAttribute("escapecharacter")) { ruleSetEscapeCharacter = element.GetAttribute("escapecharacter")[0]; } else { ruleSetEscapeCharacter = '\0'; } if (element.HasAttribute("reference")) { ruleSet.Elements.Add( new XshdImport { RuleSetReference = new XshdReference<XshdRuleSet>( element.GetAttribute("reference"), string.Empty ) }); } ruleSet.IgnoreCase = element.GetBoolAttribute("ignorecase"); foreach (XmlElement el in element.GetElementsByTagName("KeyWords")) { XshdKeywords keywords = new XshdKeywords(); keywords.ColorReference = GetColorReference(el); // we have to handle old syntax highlighting definitions that contain // empty keywords or empty keyword groups foreach (XmlElement node in el.GetElementsByTagName("Key")) { string word = node.GetAttribute("word"); if (!string.IsNullOrEmpty(word)) keywords.Words.Add(word); } if (keywords.Words.Count > 0) { ruleSet.Elements.Add(keywords); } } foreach (XmlElement el in element.GetElementsByTagName("Span")) { ruleSet.Elements.Add(ImportSpan(el)); } foreach (XmlElement el in element.GetElementsByTagName("MarkPrevious")) { ruleSet.Elements.Add(ImportMarkPrevNext(el, false)); } foreach (XmlElement el in element.GetElementsByTagName("MarkFollowing")) { ruleSet.Elements.Add(ImportMarkPrevNext(el, true)); } return ruleSet; }
public object VisitKeywords(XshdKeywords keywords) { if (keywords.Words.Count == 0) return Error(keywords, "Keyword group must not be empty."); foreach (string keyword in keywords.Words) { if (string.IsNullOrEmpty(keyword)) throw Error(keywords, "Cannot use empty string as keyword"); } StringBuilder keyWordRegex = new StringBuilder(); // We can use "\b" only where the keyword starts/ends with a letter or digit, otherwise we don't // highlight correctly. (example: ILAsm-Mode.xshd with ".maxstack" keyword) if (keywords.Words.All(IsSimpleWord)) { keyWordRegex.Append(@"\b(?>"); // (?> = atomic group // atomic groups increase matching performance, but we // must ensure that the keywords are sorted correctly. // "\b(?>in|int)\b" does not match "int" because the atomic group captures "in". // To solve this, we are sorting the keywords by descending length. int i = 0; foreach (string keyword in keywords.Words.OrderByDescending(w=>w.Length)) { if (i++ > 0) keyWordRegex.Append('|'); keyWordRegex.Append(Regex.Escape(keyword)); } keyWordRegex.Append(@")\b"); } else { keyWordRegex.Append('('); int i = 0; foreach (string keyword in keywords.Words) { if (i++ > 0) keyWordRegex.Append('|'); if (char.IsLetterOrDigit(keyword[0])) keyWordRegex.Append(@"\b"); keyWordRegex.Append(Regex.Escape(keyword)); if (char.IsLetterOrDigit(keyword[keyword.Length - 1])) keyWordRegex.Append(@"\b"); } keyWordRegex.Append(')'); } return new HighlightingRule { Color = GetColor(keywords, keywords.ColorReference), Regex = CreateRegex(keywords, keyWordRegex.ToString(), XshdRegexType.Default) }; }
static XshdKeywords ParseKeywords(XmlReader reader) { XshdKeywords keywords = new XshdKeywords(); SetPosition(keywords, reader); keywords.ColorReference = ParseColorReference(reader); reader.Read(); while (reader.NodeType != XmlNodeType.EndElement) { Debug.Assert(reader.NodeType == XmlNodeType.Element); keywords.Words.Add(reader.ReadElementString()); } return keywords; }
public object VisitKeywords(XshdKeywords keywords) { return keywords.ColorReference.AcceptVisitor(this); }