Ejemplo n.º 1
0
        private static bool TryResolveRecursive(RuleEntry entry, int depth, Dictionary <string, string> constants, out string output, bool log)
        {
            if (log)
            {
                logSb.AppendLine();
                logSb.Append(depth.ToStringCached() + " ");
                for (int i = 0; i < depth; i++)
                {
                    logSb.Append("   ");
                }
                logSb.Append(entry + " ");
            }
            loopCount++;
            if (loopCount > 1000)
            {
                Log.Error("Hit loops limit resolving grammar.");
                output = "HIT_LOOPS_LIMIT";
                if (log)
                {
                    logSb.Append("UNRESOLVABLE: Hit loops limit");
                }
                return(false);
            }
            if (depth > 50)
            {
                Log.Error("Grammar recurred too deep while resolving keyword (>" + 50 + " deep)");
                output = "DEPTH_LIMIT_REACHED";
                if (log)
                {
                    logSb.Append("UNRESOLVABLE: Depth limit reached");
                }
                return(false);
            }
            string text = entry.rule.Generate();
            bool   flag = false;
            int    num  = -1;

            for (int j = 0; j < text.Length; j++)
            {
                char c = text[j];
                if (c == '[')
                {
                    num = j;
                }
                if (c == ']')
                {
                    if (num == -1)
                    {
                        Log.Error("Could not resolve rule " + text + ": mismatched brackets.");
                        output = "MISMATCHED_BRACKETS";
                        if (log)
                        {
                            logSb.Append("UNRESOLVABLE: Mismatched brackets");
                        }
                        flag = true;
                    }
                    else
                    {
                        string text2 = text.Substring(num + 1, j - num - 1);
                        while (true)
                        {
                            RuleEntry ruleEntry = RandomPossiblyResolvableEntry(text2, constants);
                            if (ruleEntry == null)
                            {
                                entry.MarkKnownUnresolvable();
                                output = "CANNOT_RESOLVE_SUBSYMBOL:" + text2;
                                if (log)
                                {
                                    logSb.Append("UNRESOLVABLE: Cannot resolve subsymbol '" + text2 + "'");
                                }
                                flag = true;
                                break;
                            }
                            ruleEntry.uses++;
                            if (TryResolveRecursive(ruleEntry, depth + 1, constants, out string output2, log))
                            {
                                text = text.Substring(0, num) + output2 + text.Substring(j + 1);
                                j    = num;
                                break;
                            }
                            ruleEntry.MarkKnownUnresolvable();
                        }
                    }
                }
            }
            output = text;
            return(!flag);
        }
Ejemplo n.º 2
0
        private static bool TryResolveRecursive(RuleEntry entry, int depth, Dictionary <string, string> constants, out string output, bool log, List <string> extraTags, List <string> resolvedTags)
        {
            string text = "";

            for (int i = 0; i < depth; i++)
            {
                text += "  ";
            }

            if (log && depth > 0)
            {
                GrammarResolver.logSbTrace.AppendLine();
                GrammarResolver.logSbTrace.Append(depth.ToStringCached().PadRight(3));
                GrammarResolver.logSbTrace.Append(text + entry);
            }

            text += "     ";
            GrammarResolver.loopCount++;
            if (GrammarResolver.loopCount > 1000)
            {
                Log.Error("Hit loops limit resolving grammar.");
                output = "HIT_LOOPS_LIMIT";
                if (log)
                {
                    GrammarResolver.logSbTrace.Append("\n" + text + "UNRESOLVABLE: Hit loops limit");
                }

                return(false);
            }

            if (depth > 50)
            {
                Log.Error("Grammar recurred too deep while resolving keyword (>" + 50 + " deep)");
                output = "DEPTH_LIMIT_REACHED";
                if (log)
                {
                    GrammarResolver.logSbTrace.Append("\n" + text + "UNRESOLVABLE: Depth limit reached");
                }

                return(false);
            }

            string text2 = entry.rule.Generate();
            bool   flag  = false;
            int    num   = -1;

            for (int j = 0; j < text2.Length; j++)
            {
                char num2 = text2[j];
                if (num2 == '[')
                {
                    num = j;
                }

                if (num2 != ']')
                {
                    continue;
                }

                if (num == -1)
                {
                    Log.Error("Could not resolve rule because of mismatched brackets: " + text2);
                    output = "MISMATCHED_BRACKETS";
                    if (log)
                    {
                        GrammarResolver.logSbTrace.Append("\n" + text + "UNRESOLVABLE: Mismatched brackets");
                    }

                    flag = true;
                    continue;
                }

                string text3 = text2.Substring(num + 1, j - num - 1);
                while (true)
                {
                    RuleEntry ruleEntry = RandomPossiblyResolvableEntry(text3, constants, extraTags, resolvedTags);
                    if (ruleEntry == null)
                    {
                        entry.MarkKnownUnresolvable();
                        output = "CANNOT_RESOLVE_SUBSYMBOL:" + text3;
                        if (log)
                        {
                            GrammarResolver.logSbTrace.Append("\n" + text + text3 + " → UNRESOLVABLE");
                        }

                        flag = true;
                        break;
                    }

                    ruleEntry.uses++;
                    List <string> list = resolvedTags.ToList();
                    if (TryResolveRecursive(ruleEntry, depth + 1, constants, out string output2, log, extraTags, list))
                    {
                        text2 = text2.Substring(0, num) + output2 + text2.Substring(j + 1);
                        j     = num;
                        resolvedTags.Clear();
                        resolvedTags.AddRange(list);
                        if (!ruleEntry.rule.tag.NullOrEmpty() && !resolvedTags.Contains(ruleEntry.rule.tag))
                        {
                            resolvedTags.Add(ruleEntry.rule.tag);
                        }

                        break;
                    }

                    ruleEntry.MarkKnownUnresolvable();
                }
            }

            output = text2;
            return(!flag);
        }