Ejemplo n.º 1
0
        private void CollectHighlight(ABnfGuess target_guess, ABnfElement element, List <ALanguageHighlightWordInfo> list)
        {
            if (element is ALittleScriptPropertyValueCustomTypeElement ||
                element is ALittleScriptVarAssignNameDecElement)
            {
                if (element.GetElementText() != m_key)
                {
                    return;
                }

                var error = element.GuessType(out ABnfGuess guess);
                if (error != null)
                {
                    return;
                }
                if (guess.GetValue() == target_guess.GetValue())
                {
                    var info = new ALanguageHighlightWordInfo();
                    info.start = element.GetStart();
                    info.end   = element.GetEnd();
                    list.Add(info);
                }
                return;
            }

            if (element is ABnfNodeElement)
            {
                var childs = (element as ABnfNodeElement).GetChilds();
                foreach (var child in childs)
                {
                    CollectHighlight(target_guess, child, list);
                }
            }
        }
Ejemplo n.º 2
0
        // 拾取高亮
        public void PeekHighlightWord(int offset, long version)
        {
            ABnfElement element = GetException(offset);

            if (element == null)
            {
                return;
            }
            if (element is ABnfErrorElement)
            {
                return;
            }

            var    target = element;
            string value  = element.GetElementText();

            if (!m_left_pairs.ContainsKey(value) && !m_right_pairs.ContainsKey(value))
            {
                target = null;
            }

            ABnfNodeElement node = element as ABnfNodeElement;

            if (node == null)
            {
                node = element.GetParent();
            }
            if (node == null)
            {
                return;
            }

            if (node.GetReference().PeekHighlightWord())
            {
                target = node;
            }

            if (target == null)
            {
                return;
            }

            var list = new List <ALanguageHighlightWordInfo>();

            QueryHighlightWordTag(element, list);
            Application.Current.Dispatcher.Invoke(() =>
            {
                if (m_view.Properties.TryGetProperty(nameof(ALanguageHighlightWordTagger), out ALanguageHighlightWordTagger tagger))
                {
                    tagger.Refresh(version, list);
                }
            });
        }
        // 添加新元素
        public void AddNameDec(ABnfElement dec)
        {
            // 获取名称
            string name = dec.GetElementText();

            // 计算类型
            ALittleScriptUtility.ABnfElementType type;
            if (dec is ALittleScriptClassNameDecElement)
            {
                type = ALittleScriptUtility.ABnfElementType.CLASS_NAME;
            }
            else if (dec is ALittleScriptEnumNameDecElement)
            {
                type = ALittleScriptUtility.ABnfElementType.ENUM_NAME;
            }
            else if (dec is ALittleScriptStructNameDecElement)
            {
                type = ALittleScriptUtility.ABnfElementType.STRUCT_NAME;
            }
            else if (dec is ALittleScriptVarAssignNameDecElement)
            {
                type = ALittleScriptUtility.ABnfElementType.INSTANCE_NAME;
            }
            else if (dec is ALittleScriptMethodNameDecElement)
            {
                type = ALittleScriptUtility.ABnfElementType.GLOBAL_METHOD;
            }
            else if (dec is ALittleScriptUsingNameDecElement)
            {
                type = ALittleScriptUtility.ABnfElementType.USING_NAME;
            }
            else
            {
                return;
            }

            // 添加到映射表
            if (!m_element_map.TryGetValue(type, out Dictionary <string, HashSet <ABnfElement> > map))
            {
                map = new Dictionary <string, HashSet <ABnfElement> >();
                m_element_map.Add(type, map);
            }
            if (!map.TryGetValue(name, out HashSet <ABnfElement> set))
            {
                set = new HashSet <ABnfElement>();
                map.Add(name, set);
            }
            if (!set.Contains(dec))
            {
                set.Add(dec);
            }
        }
        // 移除元素
        public void RemoveNameDec(ABnfElement dec)
        {
            // 获取名称
            string name = dec.GetElementText();

            // 计算类型
            ALittleScriptUtility.ABnfElementType type;
            if (dec is ALittleScriptClassNameDecElement)
            {
                type = ALittleScriptUtility.ABnfElementType.CLASS_NAME;
            }
            else if (dec is ALittleScriptEnumNameDecElement)
            {
                type = ALittleScriptUtility.ABnfElementType.ENUM_NAME;
            }
            else if (dec is ALittleScriptStructNameDecElement)
            {
                type = ALittleScriptUtility.ABnfElementType.STRUCT_NAME;
            }
            else if (dec is ALittleScriptVarAssignNameDecElement)
            {
                type = ALittleScriptUtility.ABnfElementType.INSTANCE_NAME;
            }
            else if (dec is ALittleScriptMethodNameDecElement)
            {
                type = ALittleScriptUtility.ABnfElementType.GLOBAL_METHOD;
            }
            else if (dec is ALittleScriptUsingNameDecElement)
            {
                type = ALittleScriptUtility.ABnfElementType.USING_NAME;
            }
            else
            {
                return;
            }

            if (!m_element_map.TryGetValue(type, out Dictionary <string, HashSet <ABnfElement> > map))
            {
                return;
            }
            if (!map.TryGetValue(name, out HashSet <ABnfElement> set))
            {
                return;
            }
            set.Remove(dec);
            if (set.Count == 0)
            {
                map.Remove(name);
            }
        }
Ejemplo n.º 5
0
        // 收集规则ID
        public void CollectRule()
        {
            foreach (var element in m_root.GetChilds())
            {
                if (element.GetNodeType() != "Expression")
                {
                    continue;
                }


                var         node  = element as ABnfNodeElement;
                ABnfElement id    = null;
                ABnfElement value = null;
                foreach (var child in node.GetChilds())
                {
                    if (child.GetNodeType() == "Id")
                    {
                        id = child;
                    }
                    else if (child.GetNodeType() == "Node")
                    {
                        value = child;
                    }
                }
                if (id == null)
                {
                    continue;
                }
                if (value == null)
                {
                    continue;
                }
                string id_value = id.GetElementText();

                HashSet <ABnfElement> rule_set;
                if (!m_rule.TryGetValue(id_value, out rule_set))
                {
                    rule_set = new HashSet <ABnfElement>();
                    m_rule.Add(id_value, rule_set);
                }
                rule_set.Add(element);
            }
        }
Ejemplo n.º 6
0
        public override string QueryClassificationTag(out bool blur)
        {
            blur = false;

            var type = m_element.GetNodeType();

            if (type == "Id")
            {
                var text = m_element.GetElementText();
                if (text == "Root" || text == "LineComment" || text == "BlockComment")
                {
                    return("ABnfKeyWord");
                }
                else
                {
                    return("ABnfId");
                }
            }
            else if (type == "LineComment" || type == "BlockComment" || type == "Key" || type == "String" || type == "Regex")
            {
                return("ABnf" + type);
            }
            return(null);
        }
Ejemplo n.º 7
0
        private void CollectCompile(ABnfElement element, CollectCompileInfo info, bool multi)
        {
            if (element.GetNodeType() == "List")
            {
                var node = element as ABnfNodeElement;
                if (node == null)
                {
                    return;
                }

                var leaf_or_group = new CollectCompileInfo();
                leaf_or_group.CopyFrom(info);

                List <CollectCompileInfo> option_list = new List <CollectCompileInfo>();
                option_list.Add(leaf_or_group);
                foreach (var child in node.GetChilds())
                {
                    if (child.GetNodeType() == "Option")
                    {
                        CollectCompileInfo option = new CollectCompileInfo();
                        option.CopyFrom(info);
                        CollectCompile(child, option, multi);
                        option_list.Add(option);
                    }
                    else
                    {
                        CollectCompile(child, leaf_or_group, multi);
                    }
                }

                foreach (var option in option_list)
                {
                    if (option.has_string > info.has_string)
                    {
                        info.has_string = option.has_string;
                    }
                    if (option.has_key > info.has_key)
                    {
                        info.has_key = option.has_key;
                    }
                    if (option.has_regex > info.has_regex)
                    {
                        info.has_regex = option.has_regex;
                    }

                    foreach (var pair in option.id_map)
                    {
                        if (info.id_map.TryGetValue(pair.Key, out int value))
                        {
                            if (pair.Value > value)
                            {
                                info.id_map.Remove(pair.Key);
                                info.id_map.Add(pair.Key, pair.Value);
                            }
                        }
                        else
                        {
                            info.id_map.Add(pair.Key, pair.Value);
                        }
                    }
                }
            }
            else if (element.GetNodeType() == "Option" ||
                     element.GetNodeType() == "Node")
            {
                var node = element as ABnfNodeElement;
                if (node == null)
                {
                    return;
                }

                foreach (var child in node.GetChilds())
                {
                    CollectCompile(child, info, multi);
                }
            }
            else if (element.GetNodeType() == "Group" ||
                     element.GetNodeType() == "Leaf")
            {
                var node = element as ABnfNodeElement;
                if (node == null)
                {
                    return;
                }

                if (multi == false)
                {
                    ABnfElement tail_node = null;
                    foreach (var child in node.GetChilds())
                    {
                        if (child.GetNodeType() == "NodeTail")
                        {
                            tail_node = child;
                        }
                    }

                    if (tail_node != null)
                    {
                        var tail_value = tail_node.GetElementText();
                        multi = tail_value.StartsWith("+") || tail_value.StartsWith("*");
                    }
                }

                foreach (var child in node.GetChilds())
                {
                    CollectCompile(child, info, multi);
                }
            }
            else if (element.GetNodeType() == "Id")
            {
                int cur_count = 0;
                if (!info.id_map.TryGetValue(element.GetElementText(), out cur_count))
                {
                    cur_count = 0;
                }
                info.id_map[element.GetElementText()] = cur_count + (multi ? 2 : 1);
                return;
            }
            else if (element.GetNodeType() == "String")
            {
                info.has_string = info.has_string + (multi ? 2 : 1);
            }
            else if (element.GetNodeType() == "Key")
            {
                info.has_key = info.has_key + (multi ? 2 : 1);
            }
            else if (element.GetNodeType() == "Regex")
            {
                info.has_regex = info.has_regex + (multi ? 2 : 1);
            }
        }
Ejemplo n.º 8
0
        private ABnfGuessError CheckElementError(ABnfElement element)
        {
            if (element.GetNodeType() == "List")
            {
                var node = element as ABnfNodeElement;
                if (node == null)
                {
                    return(null);
                }

                bool has_child = false;
                foreach (var child in node.GetChilds())
                {
                    if (child.GetNodeType() == "Option")
                    {
                        var error = CheckElementError(child);
                        if (error != null)
                        {
                            return(error);
                        }
                    }
                    else
                    {
                        var error = CheckElementError(child);
                        if (error == null)
                        {
                            has_child = true;
                            break;
                        }
                    }
                }
                if (!has_child)
                {
                    return(new ABnfGuessError(element, "该规则不能保证一定会有子节点"));
                }
            }
            else if (element.GetNodeType() == "Option" ||
                     element.GetNodeType() == "Node")
            {
                var node = element as ABnfNodeElement;
                if (node == null)
                {
                    return(null);
                }

                foreach (var child in node.GetChilds())
                {
                    var error = CheckElementError(child);
                    if (error != null)
                    {
                        return(error);
                    }
                }
            }
            else if (element.GetNodeType() == "Group" ||
                     element.GetNodeType() == "Leaf")
            {
                var node = element as ABnfNodeElement;
                if (node == null)
                {
                    return(null);
                }

                ABnfElement tail_node = null;
                foreach (var child in node.GetChilds())
                {
                    if (child.GetNodeType() == "NodeTail")
                    {
                        tail_node = child;
                    }
                }

                if (tail_node != null && (tail_node.GetElementText() == "*" | tail_node.GetElementText() == "?"))
                {
                    return(new ABnfGuessError(element, "该规则不能保证一定会有子节点"));
                }

                foreach (var child in node.GetChilds())
                {
                    var error = CheckElementError(child);
                    if (error != null)
                    {
                        return(error);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 9
0
        // 高亮标签
        public void QueryHighlightWordTag(ABnfElement element, List <ALanguageHighlightWordInfo> info_list)
        {
            // 找到对应的配对
            string value = element.GetElementText();

            if (m_left_pairs.TryGetValue(value, out string right_pair))
            {
                var parent = element.GetParent();
                if (parent == null)
                {
                    return;
                }

                // 找到所在的位置
                var childs = parent.GetChilds();
                int index  = childs.IndexOf(element);
                if (index < 0)
                {
                    return;
                }

                // 往后找到对应的匹配
                for (int i = index + 1; i < childs.Count; ++i)
                {
                    if (childs[i].GetElementText() == right_pair)
                    {
                        var info = new ALanguageHighlightWordInfo();
                        info.start = element.GetStart();
                        info.end   = element.GetEnd();
                        info_list.Add(info);

                        info       = new ALanguageHighlightWordInfo();
                        info.start = childs[i].GetStart();
                        info.end   = childs[i].GetEnd();
                        info_list.Add(info);

                        break;
                    }
                }
                return;
            }

            // 找到对应的配对
            if (m_right_pairs.TryGetValue(value, out string left_pair))
            {
                var parent = element.GetParent();
                if (parent == null)
                {
                    return;
                }

                // 找到所在的位置
                var childs = parent.GetChilds();
                int index  = childs.IndexOf(element);
                if (index < 0)
                {
                    return;
                }

                // 往前找到对应的匹配
                for (int i = index - 1; i >= 0; --i)
                {
                    if (childs[i].GetElementText() == left_pair)
                    {
                        var info = new ALanguageHighlightWordInfo();
                        info.start = element.GetStart();
                        info.end   = element.GetEnd();
                        info_list.Add(info);

                        info       = new ALanguageHighlightWordInfo();
                        info.start = childs[i].GetStart();
                        info.end   = childs[i].GetEnd();
                        info_list.Add(info);

                        break;
                    }
                }
                return;
            }

            ABnfNodeElement node = element as ABnfNodeElement;

            if (node == null)
            {
                node = element.GetParent();
            }
            if (node == null)
            {
                return;
            }

            // 找到高亮配对
            node.GetReference().QueryHighlightWordTag(info_list);
        }
        public override int GetDesiredIndentation(int offset, ABnfElement select)
        {
            ABnfElement parent = m_element.GetParent();

            if (parent == null)
            {
                if (m_desire_indent >= 0)
                {
                    return(m_desire_indent);
                }
                m_desire_indent = 0;
                return(m_desire_indent);
            }

            if (m_element is ALittleScriptClassBodyDecElement ||
                m_element is ALittleScriptStructBodyDecElement ||
                m_element is ALittleScriptEnumBodyDecElement ||
                m_element is ALittleScriptMethodBodyDecElement ||
                m_element is ALittleScriptForBodyElement ||
                m_element is ALittleScriptWhileBodyElement ||
                m_element is ALittleScriptDoWhileBodyElement ||
                m_element is ALittleScriptIfBodyElement ||
                m_element is ALittleScriptElseIfBodyElement ||
                m_element is ALittleScriptElseBodyElement ||
                m_element is ALittleScriptWrapExprElement)
            {
                if (m_desire_indent < 0)
                {
                    m_desire_indent = parent.GetReference().GetDesiredIndentation(offset, null);
                }
                if (select is ABnfStringElement && select.GetElementText() == "{")
                {
                    return(m_desire_indent);
                }
                int find = m_element.FindForwardFirstEnterAndHaveNotSpaceOrTab();
                if (find < 0 || offset <= find || offset > m_element.GetEnd())
                {
                    return(m_desire_indent + ALanguageSmartIndentProvider.s_indent_size);
                }
                return(m_desire_indent);
            }
            else if (m_element is ALittleScriptMethodParamDecElement ||
                     m_element is ALittleScriptOpNewListStatElement)
            {
                if (m_desire_indent >= 0)
                {
                    return(m_desire_indent);
                }

                var element = m_element as ABnfNodeElement;
                var childs  = element.GetChilds();
                if (childs.Count > 0)
                {
                    m_desire_indent = childs[0].GetStartIndent();
                    return(m_desire_indent);
                }
            }
            else if (m_element is ALittleScriptOp8Element ||
                     m_element is ALittleScriptOp7Element ||
                     m_element is ALittleScriptOp6Element ||
                     m_element is ALittleScriptOp5Element ||
                     m_element is ALittleScriptOp4Element ||
                     m_element is ALittleScriptOp3Element ||
                     m_element is ALittleScriptOp2Element)
            {
                if (m_desire_indent >= 0)
                {
                    return(m_desire_indent);
                }

                m_desire_indent = parent.GetReference().GetDesiredIndentation(offset, null) + ALanguageSmartIndentProvider.s_indent_size;
                return(m_desire_indent);
            }

            if (m_desire_indent >= 0)
            {
                return(m_desire_indent);
            }
            m_desire_indent = parent.GetReference().GetDesiredIndentation(offset, null);
            return(m_desire_indent);
        }