예제 #1
0
        public void CollectIndex(ABnfNodeElement node)
        {
            if (node.GetNodeType() == "Id")
            {
                var value = node.GetElementText();
                HashSet <ABnfNodeElement> set;
                if (!m_index.TryGetValue(value, out set))
                {
                    set = new HashSet <ABnfNodeElement>();
                    m_index.Add(value, set);
                }
                set.Add(node as ABnfNodeElement);
            }

            foreach (var child in node.GetChilds())
            {
                if (child is ABnfNodeElement)
                {
                    CollectIndex(child as ABnfNodeElement);
                }
            }
        }
예제 #2
0
        private bool CheckAutoPair(int offset, char left_pair, string right_pair)
        {
            if (m_indent_root == null)
            {
                var file = new UIABnfFile(m_full_path, m_abnf_ui, m_view.TextBuffer.CurrentSnapshot.GetText());
                m_indent_root = m_abnf_ui.Analysis(file);
            }

            if (m_indent_root == null)
            {
                return(false);
            }

            // 获取元素
            var element = m_indent_root.GetException(offset - 1);

            if (element == null)
            {
                return(false);
            }

            // 如果是错误元素,那么就以目标元素来判定
            if (element is ABnfErrorElement)
            {
                var error_element = element as ABnfErrorElement;
                if (error_element.GetTargetElement() is ABnfRegexElement)
                {
                    return((error_element.GetTargetElement() as ABnfRegexElement).IsMatch(left_pair + right_pair));
                }
                return(false);
            }

            // 如果是正则表达式,那么就直接匹配
            if (element is ABnfRegexElement)
            {
                return((element as ABnfRegexElement).IsMatch(left_pair + right_pair));
            }

            // 获取类型
            ABnfNodeElement node = element as ABnfNodeElement;

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

            // 查找规则
            var rule = m_abnf.GetRule(node.GetNodeType());

            foreach (var node_list in rule.node.node_list)
            {
                // 如果还有一个子规则,那么就跳过
                if (node_list.Count <= 1)
                {
                    continue;
                }

                // 如果找到规则并且和left_pair一致,那么就找到left_pair对应的规则
                int index = -1;
                for (int i = 0; i < node_list.Count; ++i)
                {
                    var node_token = node_list[i];
                    if (node_token.value == null)
                    {
                        continue;
                    }

                    if (node_token.value.type == ABnfRuleTokenType.TT_STRING)
                    {
                        if (node_token.value.value.Length == 1 &&
                            node_token.value.value[0] == left_pair)
                        {
                            index = i;
                            break;
                        }
                    }
                }
                // 如果没有找到就跳过当前规则组
                if (index == -1)
                {
                    continue;
                }
                // 从后面往前找,找到与right_pair配对的子规则,如果有,那么就返回true
                for (int i = node_list.Count - 1; i >= index + 1; --i)
                {
                    var node_token = node_list[i];
                    if (node_token.value == null)
                    {
                        continue;
                    }

                    if (node_token.value.type == ABnfRuleTokenType.TT_STRING)
                    {
                        if (node_token.value.value == right_pair)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }