{"record":{"id":"635d12b0c0a17f03","repo":"iOfficeAI/OfficeCLI","slug":"formula-nesting-exceeds-the-maximum-supported-dept","errorCode":null,"errorMessage":"Formula nesting exceeds the maximum supported depth (256). See https://katex.org/docs/supported.html for supported syntax.","messagePattern":"Formula nesting exceeds the maximum supported depth \\(256\\)\\. See https://katex\\.org/docs/supported\\.html for supported syntax\\.","errorType":"exception","errorClass":"FormulaParseException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/Formula/FormulaParser.cs","lineNumber":1102,"sourceCode":"                    if (text.Length > 0)\n                        tokens.Add(new Token(TokenType.Text, text));\n                    break;\n            }\n        }\n\n        return tokens;\n    }\n\n    private static bool IsSpecialChar(char c) => c is '_' or '^' or '{' or '}' or '[' or ']' or '\\\\' or '&';\n\n    // ==================== Parser ====================\n\n    private static List<OpenXmlElement> ParseGroup(List<Token> tokens, ref int pos, bool insideBraces)\n    {\n        if (++_groupDepth > DocumentLimits.MaxRecursionDepth)\n        {\n            _groupDepth--;\n            throw new FormulaParseException(\n                $\"Formula nesting exceeds the maximum supported depth ({DocumentLimits.MaxRecursionDepth}). {KatexDocsHint}\");\n        }\n        try\n        {\n        var elements = new List<OpenXmlElement>();\n\n        while (pos < tokens.Count)\n        {\n            var token = tokens[pos];\n\n            if (token.Type == TokenType.RBrace)\n            {\n                if (insideBraces) { pos++; break; }\n                pos++;\n                continue;\n            }\n\n            if (token.Type == TokenType.Text)","sourceCodeStart":1084,"sourceCodeEnd":1120,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/Formula/FormulaParser.cs#L1084-L1120","documentation":"Thrown by FormulaParser.ParseGroup when brace nesting depth exceeds 256 (DocumentLimits.MaxRecursionDepth). The depth guard protects the recursive descent parser from stack exhaustion on pathologically nested groups and mirrors the document-tree walker cap. The message includes the KaTeX docs hint.","triggerScenarios":"Calling Parse with a formula containing more than 256 levels of nested braces/groups. Each entered group increments _groupDepth; crossing 256 throws.","commonSituations":"A programmatically generated deeply-nested formula (e.g. nested fractions/matrix builders); malformed input with runaway brace nesting; a macro expansion that recurses too far.","solutions":["Reduce the nesting depth of the formula below 256 levels.","Flatten generated structures (avoid recursively wrapping each element in a group).","Cap your generator's recursion and error out before producing such a formula."],"exampleFix":"// before\nvar f = \"{\" + string.Concat(Enumerable.Repeat(\"{\", 300)) + \"x\" + string.Concat(Enumerable.Repeat(\"}\", 300)) + \"}\";\nparser.Parse(f);\n// after\nparser.Parse(@\"\\frac{a}{b}\"); // shallow nesting","handlingStrategy":"validation","validationCode":"static int GroupDepth(string formula)\n{\n    int depth = 0, max = 0; bool esc = false;\n    foreach (var c in formula)\n    {\n        if (esc) { esc = false; continue; }\n        if (c == '\\\\') esc = true;\n        else if (c == '{') { depth++; max = Math.Max(max, depth); }\n        else if (c == '}') depth--;\n    }\n    return max;\n}","typeGuard":"static bool IsWithinGroupDepth(string formula, int limit = 256) => GroupDepth(formula) <= limit;","tryCatchPattern":"try { parser.Parse(formula); }\ncatch (FormulaParseException ex) when (ex.Message.Contains(\"maximum supported depth\"))\n{ /* flatten the formula */ }","preventionTips":["Cap generators that wrap each element in a group.","Validate nesting depth before parsing programmatically built formulas."],"tags":["formula","katex","recursion-limit","parsing"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}