Пример #1
0
        private static Color?ParseColorWithDefs(string raw, Dictionary <string, Color> colorByName)
        {
            Color color;

            if (colorByName.TryGetValue(raw, out color))
            {
                return(color);
            }
            return(HighlighterUtil.ParseColor(raw));
        }
Пример #2
0
        public void ParseXml(IEnumerable <XmlDocument> xmls)
        {
            Reset();

            Dictionary <string, Color> defColors = new Dictionary <string, Color>();
            Dictionary <string, Color> colors    = new Dictionary <string, Color>();
            Dictionary <string, int>   widths    = new Dictionary <string, int>();

            foreach (XmlDocument xml in xmls)
            {
                foreach (XmlNode node in xml.ChildNodes)
                {
                    XmlElement root = node as XmlElement;
                    if (root == null || root.Name != "scheme")
                    {
                        continue;
                    }
                    foreach (XmlNode nodeI in root.ChildNodes)
                    {
                        XmlElement elementI = nodeI as XmlElement;
                        if (elementI == null)
                        {
                            continue;
                        }
                        if (elementI.Name == "defColor")
                        {
                            string name  = elementI.GetAttribute("name");
                            string value = elementI.GetAttribute("value");
                            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
                            {
                                Color?color = HighlighterUtil.ParseColor(value);
                                if (color != null)
                                {
                                    defColors[name] = color.Value;
                                }
                            }
                        }
                        else if (elementI.Name == "style")
                        {
                            Ds        ds    = Ds.GetByName(elementI.GetAttribute("name"));
                            TextStyle style = new TextStyle();
                            Color?    color = ParseColorWithDefs(elementI.GetAttribute("color"), defColors);
                            if (color != null)
                            {
                                style.brush = new SolidBrush(color.Value);
                            }
                            style.Italic    = elementI.GetAttribute("italic") == "true";
                            style.Bold      = elementI.GetAttribute("bold") == "true";
                            style.Underline = elementI.GetAttribute("underline") == "true";
                            style.Strikeout = elementI.GetAttribute("strikeout") == "true";
                            this[ds]        = style;
                        }
                        else if (elementI.Name == "color")
                        {
                            string name  = elementI.GetAttribute("name");
                            string value = elementI.GetAttribute("value");
                            string width = elementI.GetAttribute("width");
                            if (!string.IsNullOrEmpty(name))
                            {
                                if (!string.IsNullOrEmpty(value))
                                {
                                    Color?color = ParseColorWithDefs(value, defColors);
                                    if (color != null)
                                    {
                                        colors[name] = color.Value;
                                    }
                                }
                                if (!string.IsNullOrEmpty(width))
                                {
                                    int intValue;
                                    if (int.TryParse(width, out intValue))
                                    {
                                        widths[name] = intValue;
                                    }
                                }
                            }
                        }
                        else if (elementI.Name == "width")
                        {
                            string name  = elementI.GetAttribute("name");
                            string value = elementI.GetAttribute("value");
                            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
                            {
                                int intValue;
                                if (int.TryParse(value, out intValue))
                                {
                                    widths[name] = intValue;
                                }
                            }
                        }
                    }
                    break;
                }
            }

            SetColor(ref bgColor, "bg", colors);
            SetColor(ref fgColor, "fg", colors);
            SetColor(ref lineBgColor, "lineBg", colors);
            SetColor(ref lineNumberBgColor, "lineNumberBg", colors);
            SetColor(ref lineNumberFgColor, "lineNumberFg", colors);
            SetColor(ref selectionBrushColor, "selectionBrush", colors);
            SetColor(ref selectionPenColor, "selectionPen", colors);
            SetColor(ref matchBrushColor, "matchBrush", colors);
            SetColor(ref markPenColor, "markPen", colors);
            SetColor(ref mainCaretColor, "mainCaret", colors);
            SetColor(ref caretColor, "caret", colors);
            SetColor(ref printMarginColor, "printMargin", colors);
            SetWidth(ref mainCaretWidth, "mainCaret", widths);
            SetWidth(ref caretWidth, "caret", widths);

            SetColor(ref separatorColor, "separator", colors);
            SetColor(ref selectedSeparatorColor, "selectedSeparator", colors);

            SetColor(ref splitterBgColor, "splitterBg", colors);
            SetColor(ref splitterLineColor, "splitterLine", colors);
            SetColor(ref scrollBgColor, "scrollBg", colors);
            SetColor(ref scrollThumbColor, "scrollThumb", colors);
            SetColor(ref scrollThumbHoverColor, "scrollThumbHover", colors);
            SetColor(ref scrollArrowColor, "scrollArrow", colors);
            SetColor(ref scrollArrowHoverColor, "scrollArrowHover", colors);

            Tabs_ParseXml(colors);

            Update();
        }
Пример #3
0
        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;
        }
Пример #4
0
 private Rules.Rule ParseRule(Raw.Rule rawRule, List <Rules.RegExpr> regExprRules, StyleData parentStyleData)
 {
     Rules.Rule commonRule = null;
     if (rawRule.type == "keyword")
     {
         string[] list;
         string   key = rawRule.String.ToLowerInvariant();
         textListOf.TryGetValue(key, out list);
         if (list != null)
         {
             bool casesensitive = GetBool(rawRule.general.keywordsCasesensitive, true);
             Rules.KeywordData cachedData;
             keywordDataOf.TryGetValue(key, out cachedData);
             Rules.KeywordData data = null;
             for (Rules.KeywordData dataI = cachedData; dataI != null; dataI = dataI.next)
             {
                 if (dataI.casesensitive == casesensitive)
                 {
                     data = dataI;
                     break;
                 }
             }
             if (data == null)
             {
                 data = new Rules.KeywordData(list, casesensitive, cachedData);
                 keywordDataOf[key] = data;
             }
             commonRule = new Rules.Keyword(
                 data,
                 rawRule.general.keywordsWeakDeliminator ?? "",
                 rawRule.general.keywordsAdditionalDeliminator ?? "");
         }
     }
     else if (rawRule.type == "DetectChar")
     {
         Rules.DetectChar rule = new Rules.DetectChar();
         rule.char0 = CharOf(rawRule.char0);
         commonRule = rule;
     }
     else if (rawRule.type == "Detect2Chars")
     {
         Rules.Detect2Chars rule = new Rules.Detect2Chars();
         rule.char0 = CharOf(rawRule.char0);
         rule.char1 = CharOf(rawRule.char1);
         commonRule = rule;
     }
     else if (rawRule.type == "AnyChar")
     {
         Rules.AnyChar rule = new Rules.AnyChar();
         rule.chars = rawRule.String;
         commonRule = rule;
     }
     else if (rawRule.type == "StringDetect")
     {
         Rules.StringDetect rule = new Rules.StringDetect();
         rule.insensitive = GetBool(rawRule.insensitive);
         rule.text        = rawRule.String;
         commonRule       = rule;
     }
     else if (rawRule.type == "WordDetect")
     {
         commonRule = new Rules.WordDetect(rawRule.String, GetBool(rawRule.insensitive));
     }
     else if (rawRule.type == "RegExpr")
     {
         Rules.RegExpr rule  = new Rules.RegExpr();
         string        regex = rawRule.String;
         rule.regex = new Regex(
             HighlighterUtil.FixRegexUnicodeChars(GetBool(rawRule.minimal) ? HighlighterUtil.LazyOfRegex(regex) : regex),
             (GetBool(rawRule.insensitive) ? RegexOptions.IgnoreCase : RegexOptions.None)
             );
         commonRule = rule;
         regExprRules.Add(rule);
     }
     else if (rawRule.type == "Int")
     {
         commonRule = new Rules.Int();
     }
     else if (rawRule.type == "Float")
     {
         commonRule = new Rules.Float();
     }
     else if (rawRule.type == "HlCOct")
     {
         commonRule = new Rules.HlCOct();
     }
     else if (rawRule.type == "HlCHex")
     {
         commonRule = new Rules.HlCHex();
     }
     else if (rawRule.type == "RangeDetect")
     {
         Rules.RangeDetect rule = new Rules.RangeDetect();
         rule.char0 = CharOf(rawRule.char0);
         rule.char1 = CharOf(rawRule.char1);
         commonRule = rule;
     }
     else if (rawRule.type == "DetectSpaces")
     {
         commonRule = new Rules.DetectSpaces();
     }
     else if (rawRule.type == "DetectIdentifier")
     {
         commonRule = new Rules.DetectIdentifier();
     }
     else if (rawRule.type == "HlCStringChar")
     {
         commonRule = new Rules.HlCStringChar();
     }
     else if (rawRule.type == "HlCChar")
     {
         commonRule = new Rules.HlCChar();
     }
     else if (rawRule.type == "LineContinue")
     {
         commonRule = new Rules.LineContinue();
     }
     if (commonRule != null)
     {
         {
             StyleData styleData = null;
             string    attribute = rawRule.attribute;
             if (attribute != null)
             {
                 styleDataOf.TryGetValue(attribute.ToLowerInvariant(), out styleData);
             }
             if (styleData == null)
             {
                 styleData = parentStyleData ?? defaultStyleData;
             }
             commonRule.attribute = styleData;
         }
         commonRule.lookAhead = GetBool(rawRule.lookAhead);
         int column = GetInt(rawRule.column, -1);
         if (GetBool(rawRule.firstNonSpace))
         {
             commonRule.column = 0;
         }
         else
         {
             commonRule.column = column;
         }
         commonRule.context = GetSwitchInfo(!string.IsNullOrEmpty(rawRule.context) ? rawRule.context : "#stay");
         List <Rules.Rule> childs = null;
         foreach (Raw.Rule childI in rawRule.childs)
         {
             Rules.Rule child = ParseRule(childI, regExprRules, commonRule.attribute);
             if (child != null)
             {
                 if (childs == null)
                 {
                     childs = new List <Rules.Rule>();
                 }
                 childs.Add(child);
             }
         }
         if (childs != null)
         {
             commonRule.childs = childs.ToArray();
         }
     }
     return(commonRule);
 }