public XmlHighlightingDefinition(XshdSyntaxDefinition xshd, IHighlightingDefinitionReferenceResolver resolver)
        {
            this.Name = xshd.Name;
            // Create HighlightingRuleSet instances
            var rnev = new RegisterNamedElementsVisitor(this);

            xshd.AcceptElements(rnev);
            // Assign MainRuleSet so that references can be resolved
            foreach (XshdElement element in xshd.Elements)
            {
                XshdRuleSet xrs = element as XshdRuleSet;
                if (xrs != null && xrs.Name == null)
                {
                    if (MainRuleSet != null)
                    {
                        throw Error(element, "Duplicate main RuleSet. There must be only one nameless RuleSet!");
                    }
                    else
                    {
                        MainRuleSet = rnev.ruleSets[xrs];
                    }
                }
            }
            if (MainRuleSet == null)
            {
                throw new HighlightingDefinitionInvalidException("Could not find main RuleSet.");
            }
            // Translate elements within the rulesets (resolving references and processing imports)
            xshd.AcceptElements(new TranslateElementVisitor(this, rnev.ruleSets, resolver));
        }
Esempio n. 2
0
        static XshdRuleSet ParseRuleSet(XmlReader reader)
        {
            XshdRuleSet ruleSet = new XshdRuleSet();

            SetPosition(ruleSet, reader);
            ruleSet.Name       = reader.GetAttribute("name");
            ruleSet.IgnoreCase = reader.GetBoolAttribute("ignoreCase");

            CheckElementName(reader, ruleSet.Name);
            ParseElements(ruleSet.Elements, reader);
            return(ruleSet);
        }
            public object VisitRuleSet(XshdRuleSet ruleSet)
            {
                HighlightingRuleSet rs = ruleSetDict[ruleSet];

                if (processedRuleSets.Contains(ruleSet))
                {
                    return(rs);
                }
                if (!processingStartedRuleSets.Add(ruleSet))
                {
                    throw Error(ruleSet, "RuleSet cannot be processed because it contains cyclic <Import>");
                }

                bool oldIgnoreCase = ignoreCase;

                if (ruleSet.IgnoreCase != null)
                {
                    ignoreCase = ruleSet.IgnoreCase.Value;
                }

                rs.Name = ruleSet.Name;

                foreach (XshdElement element in ruleSet.Elements)
                {
                    object o = element.AcceptVisitor(this);
                    HighlightingRuleSet elementRuleSet = o as HighlightingRuleSet;
                    if (elementRuleSet != null)
                    {
                        Merge(rs, elementRuleSet);
                    }
                    else
                    {
                        HighlightingSpan span = o as HighlightingSpan;
                        if (span != null)
                        {
                            rs.Spans.Add(span);
                        }
                        else
                        {
                            HighlightingRule elementRule = o as HighlightingRule;
                            if (elementRule != null)
                            {
                                rs.Rules.Add(elementRule);
                            }
                        }
                    }
                }

                ignoreCase = oldIgnoreCase;
                processedRuleSets.Add(ruleSet);

                return(rs);
            }
Esempio n. 4
0
        XshdSyntaxDefinition ParseDefinition(XmlElement syntaxDefinition)
        {
            XshdSyntaxDefinition def = new XshdSyntaxDefinition();

            def.Name = syntaxDefinition.GetAttributeOrNull("name");
            if (syntaxDefinition.HasAttribute("extensions"))
            {
                def.Extensions.AddRange(syntaxDefinition.GetAttribute("extensions").Split(';', '|'));
            }

            XshdRuleSet mainRuleSetElement = null;

            foreach (XmlElement element in syntaxDefinition.GetElementsByTagName("RuleSet"))
            {
                XshdRuleSet ruleSet = ImportRuleSet(element);
                def.Elements.Add(ruleSet);
                if (ruleSet.Name == null)
                {
                    mainRuleSetElement = ruleSet;
                }

                if (syntaxDefinition["Digits"] != null)
                {
                    // create digit highlighting rule

                    const string optionalExponent = @"([eE][+-]?[0-9]+)?";
                    const string floatingPoint    = @"\.[0-9]+";
                    ruleSet.Elements.Add(
                        new XshdRule {
                        ColorReference = GetColorReference(syntaxDefinition["Digits"]),
                        RegexType      = XshdRegexType.IgnorePatternWhitespace,
                        Regex          = @"\b0[xX][0-9a-fA-F]+"
                                         + @"|\b"
                                         + @"(\d+(" + floatingPoint + ")?"
                                         + @"|" + floatingPoint + ")"
                                         + optionalExponent
                    });
                }
            }

            if (syntaxDefinition.HasAttribute("extends") && mainRuleSetElement != null)
            {
                // convert 'extends="HTML"' to '<Import ruleSet="HTML/" />' in main rule set.
                mainRuleSetElement.Elements.Add(
                    new XshdImport {
                    RuleSetReference = new XshdReference <XshdRuleSet>(
                        syntaxDefinition.GetAttribute("extends"), string.Empty
                        )
                });
            }
            return(def);
        }
        object IXshdVisitor.VisitRuleSet(XshdRuleSet ruleSet)
        {
            writer.WriteStartElement("RuleSet", Namespace);

            if (ruleSet.Name != null)
            {
                writer.WriteAttributeString("name", ruleSet.Name);
            }
            WriteBoolAttribute("ignoreCase", ruleSet.IgnoreCase);

            ruleSet.AcceptElements(this);

            writer.WriteEndElement();
            return(null);
        }
            public object VisitRuleSet(XshdRuleSet ruleSet)
            {
                HighlightingRuleSet hrs = new HighlightingRuleSet();

                ruleSets.Add(ruleSet, hrs);
                if (ruleSet.Name != null)
                {
                    if (ruleSet.Name.Length == 0)
                    {
                        throw Error(ruleSet, "Name must not be the empty string");
                    }
                    if (def.ruleSetDict.ContainsKey(ruleSet.Name))
                    {
                        throw Error(ruleSet, "Duplicate rule set name '" + ruleSet.Name + "'.");
                    }

                    def.ruleSetDict.Add(ruleSet.Name, hrs);
                }
                ruleSet.AcceptElements(this);
                return(null);
            }
Esempio n. 7
0
        XshdSpan ImportSpan(XmlElement element)
        {
            XshdSpan span = new XshdSpan();

            if (element.HasAttribute("rule"))
            {
                span.RuleSetReference = new XshdReference <XshdRuleSet>(null, element.GetAttribute("rule"));
            }
            char escapeCharacter = ruleSetEscapeCharacter;

            if (element.HasAttribute("escapecharacter"))
            {
                escapeCharacter = element.GetAttribute("escapecharacter")[0];
            }
            span.Multiline = !(element.GetBoolAttribute("stopateol") ?? false);

            span.SpanColorReference = GetColorReference(element);

            span.BeginRegexType = XshdRegexType.IgnorePatternWhitespace;
            span.BeginRegex     = ImportRegex(element["Begin"].InnerText,
                                              element["Begin"].GetBoolAttribute("singleword") ?? false,
                                              element["Begin"].GetBoolAttribute("startofline"));
            span.BeginColorReference = GetColorReference(element["Begin"]);

            string endElementText = string.Empty;

            if (element["End"] != null)
            {
                span.EndRegexType = XshdRegexType.IgnorePatternWhitespace;
                endElementText    = element["End"].InnerText;
                span.EndRegex     = ImportRegex(endElementText,
                                                element["End"].GetBoolAttribute("singleword") ?? false,
                                                null);
                span.EndColorReference = GetColorReference(element["End"]);
            }

            if (escapeCharacter != '\0')
            {
                XshdRuleSet ruleSet = new XshdRuleSet();
                if (endElementText.Length == 1 && endElementText[0] == escapeCharacter)
                {
                    // ""-style escape
                    ruleSet.Elements.Add(new XshdSpan {
                        BeginRegex = Regex.Escape(endElementText + endElementText),
                        EndRegex   = ""
                    });
                }
                else
                {
                    // \"-style escape
                    ruleSet.Elements.Add(new XshdSpan {
                        BeginRegex = Regex.Escape(escapeCharacter.ToString()),
                        EndRegex   = "."
                    });
                }
                if (span.RuleSetReference.ReferencedElement != null)
                {
                    ruleSet.Elements.Add(new XshdImport {
                        RuleSetReference = span.RuleSetReference
                    });
                }
                span.RuleSetReference = new XshdReference <XshdRuleSet>(ruleSet);
            }
            return(span);
        }
Esempio n. 8
0
        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);
        }