Example #1
0
 public override ABnfReference CreateReference(ABnfElement element)
 {
     if (element.GetNodeType() == "Id")
     {
         return(new ABnfIdReference(element));
     }
     else if (element.GetNodeType() == "Regex")
     {
         return(new ABnfRegexReference(element));
     }
     else if (element.GetNodeType() == "Key")
     {
         return(new ABnfKeyReference(element));
     }
     else if (element.GetNodeType() == "String")
     {
         return(new ABnfStringReference(element));
     }
     else if (element.GetNodeType() == "Node")
     {
         return(new ABnfNodeReference(element));
     }
     return(new ABnfCommonReference(element));
 }
        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);
        }
Example #3
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);
            }
        }
        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);
        }