mozilla/pdf.js · error · Error
Invalid token in index.
Error message
Invalid token in index.
What it means
Thrown by SimpleExprParser.parseIndex() in the XFA FormCalc parser when an array index uses the wildcard form '[*]' but the '*' is not immediately followed by a closing ']'. FormCalc uses '[*]' to denote AstEveryOccurence (all occurrences of a SOM node); any token other than ']' after the '*' is rejected. The error message comes from Errors.index.
Source
Thrown at src/core/xfa/formcalc_parser.js:493
const [tok, param] = parser.parse();
if (param) {
params.push(param);
}
if (tok.id === TOKEN.rightParen) {
return params;
} else if (tok.id !== TOKEN.comma) {
throw new Error(Errors.params);
}
parser.reset();
}
}
static parseIndex(lexer) {
let tok = lexer.next();
if (tok.id === TOKEN.times) {
tok = lexer.next();
if (tok.id !== TOKEN.rightBracket) {
throw new Error(Errors.index);
}
return new AstEveryOccurence();
}
const [token, expr] = new SimpleExprParser(lexer).parse(tok);
if (token.id !== TOKEN.rightBracket) {
throw new Error(Errors.index);
}
return expr;
}
pushOperator(op) {
this.flushWithOperator(op);
this.operators.push(op);
this.last = OPERATOR;
}
pushOperand(op) {
this.operands.push(op);View on GitHub (pinned to 5903d58d58)
Solutions
- Inspect the offending FormCalc script in the XFA packet (the SOM reference with '[*') and ensure every '[*]' is written exactly as bracket-star-bracket.
- Regenerate or repair the XFA form source (XDP/PDF) with a conformant FormCalc authoring tool rather than hand-editing the script bytes.
- If you do not control the PDF, catch the parser error at the XFA-binding layer and skip the malformed script so the rest of the document still renders.
- Verify the FormCalc token stream with a standalone FormCalc lexer to confirm the '*' is immediately followed by ']' in the source text.
Example fix
// before (FormCalc source inside XFA) var x = Subform[* +1]; // after var x = Subform[*];
Defensive patterns
Strategy: validation
Validate before calling
// Validate a FormCalc wildcard index before binding the XFA script.
// Rejects '[*]' patterns not written as exactly [*].
function hasMalformedWildcard(script) {
// matches '[*' not immediately followed by ']'
return /\[\*(?!\])/.test(script);
}
if (hasMalformedWildcard(formCalcSrc)) {
// skip binding this script; log the offset
} Try / catch
// Wrap XFA script evaluation; FormCalc parse errors are plain Error.
try {
bindFormCalcScript(xfaNode, src);
} catch (e) {
if (e instanceof Error && /Invalid token in index\./.test(e.message)) {
console.warn("Skipping malformed FormCalc index script", e.message);
return;
}
throw e;
} Prevention
- Author FormCalc wildcard subscripts only as '[*]' in a FormCalc-aware tool.
- Lint SOM reference expressions before binding them to XFA nodes.
- Never hand-edit the raw script bytes of an XFA packet.
When it happens
Trigger: A FormCalc index expression like 'foo[* +' or 'foo[* x]' — i.e., the lexer emitted TOKEN.times for the '*' but the next token is not TOKEN.rightBracket. Reached when PDF.js evaluates an XFA form's script (calc/event/initialize) containing such a wildcard reference.
Common situations: Loading an XFA-enabled PDF whose embedded FormCalc script has a typo in a wildcard subscript; a hand-edited or generator-emitted XFA packet with malformed '*'; a corrupted/truncated form script where bytes were dropped inside '[*]'.
Understand the failure class
- Authentication and authorization failures — expired tokens, bad credentials, and missing scopes.
Related errors
- Invalid token in Form code
- Invalid token in do ... end declaration.
- Invalid token in var declaration.
- Invalid token in func declaration.
- Invalid token if ... endif declaration.
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/de5b9418acc27029.
Report an issue: GitHub.