iOfficeAI/OfficeCLI · error · FormulaParseException
Formula nesting exceeds the maximum supported depth (256). S
Error message
Formula nesting exceeds the maximum supported depth (256). See https://katex.org/docs/supported.html for supported syntax.
What it means
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.
Source
Thrown at src/officecli/Core/Formula/FormulaParser.cs:1102
if (text.Length > 0)
tokens.Add(new Token(TokenType.Text, text));
break;
}
}
return tokens;
}
private static bool IsSpecialChar(char c) => c is '_' or '^' or '{' or '}' or '[' or ']' or '\\' or '&';
// ==================== Parser ====================
private static List<OpenXmlElement> ParseGroup(List<Token> tokens, ref int pos, bool insideBraces)
{
if (++_groupDepth > DocumentLimits.MaxRecursionDepth)
{
_groupDepth--;
throw new FormulaParseException(
$"Formula nesting exceeds the maximum supported depth ({DocumentLimits.MaxRecursionDepth}). {KatexDocsHint}");
}
try
{
var elements = new List<OpenXmlElement>();
while (pos < tokens.Count)
{
var token = tokens[pos];
if (token.Type == TokenType.RBrace)
{
if (insideBraces) { pos++; break; }
pos++;
continue;
}
if (token.Type == TokenType.Text)View on GitHub (pinned to 1ced45e900)
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.
Example fix
// before
var f = "{" + string.Concat(Enumerable.Repeat("{", 300)) + "x" + string.Concat(Enumerable.Repeat("}", 300)) + "}";
parser.Parse(f);
// after
parser.Parse(@"\frac{a}{b}"); // shallow nesting Defensive patterns
Strategy: validation
Validate before calling
static int GroupDepth(string formula)
{
int depth = 0, max = 0; bool esc = false;
foreach (var c in formula)
{
if (esc) { esc = false; continue; }
if (c == '\\') esc = true;
else if (c == '{') { depth++; max = Math.Max(max, depth); }
else if (c == '}') depth--;
}
return max;
} Type guard
static bool IsWithinGroupDepth(string formula, int limit = 256) => GroupDepth(formula) <= limit;
Try / catch
try { parser.Parse(formula); }
catch (FormulaParseException ex) when (ex.Message.Contains("maximum supported depth"))
{ /* flatten the formula */ } Prevention
- Cap generators that wrap each element in a group.
- Validate nesting depth before parsing programmatically built formulas.
When it happens
Trigger: Calling Parse with a formula containing more than 256 levels of nested braces/groups. Each entered group increments _groupDepth; crossing 256 throws.
Common situations: 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.
Related errors
- Failed to parse formula: {ex.Message} See https://katex.org/
- Invalid color transform '{token}': value must be an integer.
- Invalid {paramName} value: '{raw}' (empty).
- Invalid {paramName} value: '{raw}'.
- Invalid array constant: '{badElem}'. Inline arrays {...} may
AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13).
Data as JSON: /api/errors/635d12b0c0a17f03.
Report an issue: GitHub.