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

            xshd.AcceptElements(rnev);
            // Assign MainRuleSet so that references can be resolved
            foreach (var element in xshd.Elements)
            {
                if (element is XshdRuleSet xrs && xrs.Name == null)
                {
                    if (MainRuleSet != null)
                    {
                        throw Error(element, "Duplicate main RuleSet. There must be only one nameless RuleSet!");
                    }
                    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));

            foreach (var p in xshd.Elements.OfType <XshdProperty>())
            {
                _propDict.Add(p.Name, p.Value);
            }
        }
Esempio n. 2
0
        //internal static XmlSchemaSet LoadSchemaSet(XmlReader schemaInput)
        //{
        //    XmlSchemaSet schemaSet = new XmlSchemaSet();
        //    schemaSet.Add(null, schemaInput);
        //    schemaSet.ValidationEventHandler += delegate (object sender, ValidationEventArgs args)
        //    {
        //        throw new HighlightingDefinitionInvalidException(args.Message);
        //    };
        //    return schemaSet;
        //}
        #endregion

        #region Load Highlighting from XSHD
        /// <summary>
        /// Creates a highlighting definition from the XSHD file.
        /// </summary>
        public static IHighlightingDefinition Load(XshdSyntaxDefinition syntaxDefinition, IHighlightingDefinitionReferenceResolver resolver)
        {
            if (syntaxDefinition == null)
            {
                throw new ArgumentNullException(nameof(syntaxDefinition));
            }
            return(new XmlHighlightingDefinition(syntaxDefinition, resolver));
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        private static XshdSyntaxDefinition ParseDefinition(XmlReader reader)
        {
            Debug.Assert(reader.LocalName == "SyntaxDefinition");
            var def = new XshdSyntaxDefinition {
                Name = reader.GetAttribute("name")
            };
            var extensions = reader.GetAttribute("extensions");

            if (extensions != null)
            {
                def.Extensions.AddRange(extensions.Split(';'));
            }
            ParseElements(def.Elements, reader);
            Debug.Assert(reader.NodeType == XmlNodeType.EndElement);
            Debug.Assert(reader.LocalName == "SyntaxDefinition");
            return(def);
        }
Esempio n. 5
0
        /// <summary>
        /// Writes the specified syntax definition.
        /// </summary>
        public void WriteDefinition(XshdSyntaxDefinition definition)
        {
            if (definition == null)
            {
                throw new ArgumentNullException(nameof(definition));
            }
            _writer.WriteStartElement("SyntaxDefinition", Namespace);
            if (definition.Name != null)
            {
                _writer.WriteAttributeString("name", definition.Name);
            }
            if (definition.Extensions != null)
            {
                _writer.WriteAttributeString("extensions", string.Join(";", definition.Extensions.ToArray()));
            }

            definition.AcceptElements(this);

            _writer.WriteEndElement();
        }