Exemple #1
0
        public static void InlineIncludeRules(Raw raw, HighlighterSet highlighterSet)
        {
            if (raw == null)
            {
                return;
            }
            Collector collector = new Collector(highlighterSet);

            foreach (RawList list in raw.lists)
            {
                if (list.name != null)
                {
                    collector.listByName[list.name.ToLowerInvariant()] = list;
                }
            }
            foreach (Context context in raw.contexts)
            {
                if (context.name != null)
                {
                    collector.contextByName[context.name.ToLowerInvariant()] = context;
                }
            }
            foreach (ItemData itemData in raw.itemDatas)
            {
                if (itemData.name != null)
                {
                    collector.itemDataByName[itemData.name.ToLowerInvariant()] = itemData;
                }
            }

            foreach (Context context in raw.contexts)
            {
                AddExternalContext(collector, ref context.lineEndContext);
                AddExternalContext(collector, ref context.fallthroughContext);
                AddRulesExternalContext(collector, context.rules);
            }

            foreach (Context context in raw.contexts)
            {
                RecursiveInlineIncludeRules(context, collector);
            }

            raw.lists.AddRange(collector.additionLists);
            raw.contexts.AddRange(collector.additionContexts);
            raw.itemDatas.AddRange(collector.additionItemDatas);
        }
Exemple #2
0
        private static void AddExternalContext(Collector collector, ref string name)
        {
            if (!string.IsNullOrEmpty(name) && name.Length > 2 && name[0] == '#' && name[1] == '#')
            {
                string key = name.Substring(2).ToLowerInvariant();
                if (!collector.externalContextAdded.ContainsKey(key))
                {
                    collector.externalContextAdded[key] = true;

                    Raw externalRaw = collector.highlighterSet.GetRaw(key);
                    if (externalRaw != null && externalRaw.contexts.Count > 0)
                    {
                        name = externalRaw.contexts[0].name;
                        foreach (RawList list in externalRaw.lists)
                        {
                            string nameI = list.name.ToLowerInvariant();
                            if (!collector.listByName.ContainsKey(nameI))
                            {
                                collector.listByName[nameI] = list;
                                collector.additionLists.Add(list);
                            }
                        }
                        foreach (Context context in externalRaw.contexts)
                        {
                            string nameI = context.name.ToLowerInvariant();
                            if (!collector.contextByName.ContainsKey(nameI))
                            {
                                collector.contextByName[nameI] = context;
                                collector.inlined[context]     = true;
                                collector.additionContexts.Add(context);
                            }
                        }
                        foreach (ItemData itemData in externalRaw.itemDatas)
                        {
                            string nameI = itemData.name.ToLowerInvariant();
                            if (!collector.itemDataByName.ContainsKey(nameI))
                            {
                                collector.itemDataByName[nameI] = itemData;
                                collector.additionItemDatas.Add(itemData);
                            }
                        }
                    }
                }
            }
        }
        public Highlighter GetHighlighter(string type)
        {
            type = type.ToLowerInvariant();
            Highlighter highlighter;

            highlighterBy.TryGetValue(type, out highlighter);
            if (highlighter == null)
            {
                Raw raw = GetRaw(type);
                if (raw != null)
                {
                    highlighter         = new Highlighter(raw);
                    highlighter.type    = type;
                    highlighterBy[type] = highlighter;
                }
            }
            return(highlighter);
        }
Exemple #4
0
        public static Raw Parse(XmlDocument xml)
        {
            if (xml == null)
            {
                return(null);
            }
            Raw raw = new Raw();

            foreach (XmlNode nodeI in xml.ChildNodes)
            {
                XmlElement elementI = nodeI as XmlElement;
                if (elementI != null && elementI.Name == "language")
                {
                    foreach (XmlNode nodeJ in elementI.ChildNodes)
                    {
                        XmlElement elementJ = nodeJ as XmlElement;
                        if (elementJ != null)
                        {
                            if (elementJ.Name == "highlighting")
                            {
                                foreach (XmlNode nodeK in elementJ.ChildNodes)
                                {
                                    XmlElement elementK = nodeK as XmlElement;
                                    if (elementK != null)
                                    {
                                        if (elementK.Name == "list")
                                        {
                                            RawList list = new RawList();
                                            list.name = elementK.GetAttribute("name");
                                            foreach (XmlNode nodeL in elementK.ChildNodes)
                                            {
                                                XmlElement elementL = nodeL as XmlElement;
                                                if (elementL != null && elementL.Name == "item")
                                                {
                                                    string innerText = elementL.InnerText.Trim();
                                                    if (!string.IsNullOrEmpty(innerText))
                                                    {
                                                        list.items.Add(innerText);
                                                    }
                                                }
                                            }
                                            raw.lists.Add(list);
                                        }
                                        else if (elementK.Name == "contexts")
                                        {
                                            foreach (XmlNode nodeL in elementK.ChildNodes)
                                            {
                                                XmlElement elementL = nodeL as XmlElement;
                                                if (elementL != null && elementL.Name == "context")
                                                {
                                                    Context context = new Context();
                                                    context.name               = elementL.GetAttribute("name");
                                                    context.attribute          = elementL.GetAttribute("attribute");
                                                    context.lineEndContext     = elementL.GetAttribute("lineEndContext");
                                                    context.fallthrough        = elementL.GetAttribute("fallthrough");
                                                    context.fallthroughContext = elementL.GetAttribute("fallthroughContext");
                                                    ParseRules(context.rules, elementL, raw.general);
                                                    raw.contexts.Add(context);
                                                }
                                            }
                                        }
                                        else if (elementK.Name == "itemDatas")
                                        {
                                            foreach (XmlNode nodeL in elementK.ChildNodes)
                                            {
                                                XmlElement elementL = nodeL as XmlElement;
                                                if (elementL != null && elementL.Name == "itemData")
                                                {
                                                    ItemData itemData = new ItemData();
                                                    itemData.name        = elementL.GetAttribute("name");
                                                    itemData.defStyleNum = elementL.GetAttribute("defStyleNum");
                                                    itemData.color       = elementL.GetAttribute("color");
                                                    itemData.italic      = elementL.GetAttribute("italic");
                                                    itemData.bold        = elementL.GetAttribute("bold");
                                                    itemData.underline   = elementL.GetAttribute("underline");
                                                    itemData.strikeout   = elementL.GetAttribute("strikeout");
                                                    raw.itemDatas.Add(itemData);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else if (elementJ.Name == "general")
                            {
                                foreach (XmlNode nodeK in elementJ.ChildNodes)
                                {
                                    XmlElement elementK = nodeK as XmlElement;
                                    if (elementK != null)
                                    {
                                        if (elementK.Name == "keywords")
                                        {
                                            raw.general.keywordsCasesensitive         = elementK.GetAttribute("casesensitive");
                                            raw.general.keywordsWeakDeliminator       = elementK.GetAttribute("weakDeliminator");
                                            raw.general.keywordsAdditionalDeliminator = elementK.GetAttribute("additionalDeliminator");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(raw);
        }
        public Highlighter(Raw raw)
        {
            styleDataOf      = new Dictionary <string, StyleData>();
            textListOf       = new Dictionary <string, string[]>();
            keywordDataOf    = new Dictionary <string, Rules.KeywordData>();
            customStyleDatas = new List <StyleData>();
            foreach (Raw.RawList listI in raw.lists)
            {
                textListOf[listI.name.ToLowerInvariant()] = listI.items.ToArray();
            }
            {
                bool first = true;
                foreach (Raw.ItemData itemDataI in raw.itemDatas)
                {
                    StyleData data = new StyleData();
                    data.ds   = Ds.GetByName(itemDataI.defStyleNum);
                    data.name = itemDataI.name;

                    data.color     = HighlighterUtil.ParseColor(itemDataI.color);
                    data.italic    = GetVariantBool(itemDataI.italic);
                    data.bold      = GetVariantBool(itemDataI.bold);
                    data.underline = GetVariantBool(itemDataI.underline);
                    data.strikeout = GetVariantBool(itemDataI.strikeout);
                    if (data.color == null && data.italic == null && data.bold == null && data.underline == null && data.strikeout == null)
                    {
                        data.index = data.ds.index;
                    }
                    else
                    {
                        data.index = (short)(Ds.all.Count + customStyleDatas.Count);
                        customStyleDatas.Add(data);
                    }

                    styleDataOf[data.name.ToLowerInvariant()] = data;
                    if (first)
                    {
                        first            = false;
                        defaultStyleData = data;
                    }
                }
            }

            List <Rules.Context> contextList = new List <Rules.Context>();

            contextOf = new Dictionary <string, Rules.Context>();
            foreach (Raw.Context contextI in raw.contexts)
            {
                string        name    = contextI.name;
                Rules.Context context = new Rules.Context();
                context.name = name;
                contextOf[name.ToLowerInvariant()] = context;
                contextList.Add(context);
            }
            List <Rules.RegExpr> regExprRules = new List <Rules.RegExpr>();

            foreach (Raw.Context contextI in raw.contexts)
            {
                Rules.Context context = contextOf[contextI.name.ToLowerInvariant()];
                {
                    StyleData styleData = null;
                    string    attribute = contextI.attribute;
                    if (attribute != null)
                    {
                        styleDataOf.TryGetValue(attribute.ToLowerInvariant(), out styleData);
                    }
                    if (styleData == null)
                    {
                        styleData = defaultStyleData;
                    }
                    context.attribute = styleData;
                }
                StyleData currentAttribute = context.attribute;
                context.lineEndContext     = GetSwitchInfo(contextI.lineEndContext);
                context.fallthrough        = GetBool(contextI.fallthrough);
                context.fallthroughContext = GetSwitchInfo(contextI.fallthroughContext);

                List <Rules.Rule> contextRules = new List <Rules.Rule>();
                foreach (Raw.Rule ruleI in contextI.rules)
                {
                    Rules.Rule rule = ParseRule(ruleI, regExprRules, context.attribute);
                    if (rule != null)
                    {
                        contextRules.Add(rule);
                    }
                }
                context.childs = contextRules.ToArray();
            }
            awakePositions = new int[regExprRules.Count];
            for (int i = 0; i < regExprRules.Count; i++)
            {
                Rules.RegExpr rule = regExprRules[i];
                rule.awakePositions = awakePositions;
                rule.awakeIndex     = i;
            }
            contexts    = contextList.ToArray();
            styleDataOf = null;
            contextOf   = null;
            textListOf  = null;
        }