mozilla/pdf.js · error · Error
Invalid token in Form code
Error message
Invalid token in Form code
What it means
Thrown by Parser.parse() (top-level entry of the FormCalc parser) when the expression list was consumed but the lexer has not reached EOF. Any leftover, non-whitespace token after a complete declaration/expression list means the source is not a well-formed FormCalc program. Unlike the named Errors.* messages, this one is an inline literal string.
Source
Thrown at src/core/xfa/formcalc_parser.js:989
return { special: "break" };
}
}
class ContinueDecl extends Leaf {
dump() {
return { special: "continue" };
}
}
class Parser {
constructor(code) {
this.lexer = new Lexer(code);
}
parse() {
const [tok, decls] = this.parseExprList();
if (tok.id !== TOKEN.eof) {
throw new Error("Invalid token in Form code");
}
return decls;
}
parseExprList() {
const expressions = [];
let tok = null,
expr;
while (true) {
[tok, expr] = this.parseExpr(tok);
if (!expr) {
return [tok, new ExprList(expressions)];
}
expressions.push(expr);
}
}
parseExpr(tok) {View on GitHub (pinned to 5903d58d58)
Solutions
- Locate the byte offset of the unexpected token (the lexer tracks position) and remove or restructure the trailing tokens so the program ends cleanly at EOF.
- Ensure each top-level declaration (var/func/if/for/while/foreach/block) is complete and that no statement spills past its terminator.
- Re-serialize the XFA form script through a validated FormCalc authoring/export tool.
- Isolate the script string and run it through a FormCalc lint/parse pass before binding it to the XFA template.
Example fix
// before (two top-level forms jammed together) var a = 1 var b = 2 // after var a = 1 var b = 2
Defensive patterns
Strategy: validation
Validate before calling
// Cheap pre-check: the script should parse to exactly one expression list
// ending at EOF. Wrap the parser and treat any throw as 'do not bind'.
function isParseableFormCalc(src) {
try { new FormCalcParser(src).parse(); return true; }
catch { return false; }
} Try / catch
try {
const decls = new Parser(src).parse();
bindDecls(xfaNode, decls);
} catch (e) {
if (e instanceof Error && /Invalid token in Form code/.test(e.message)) {
console.warn("FormCalc program has trailing/invalid tokens; skipping", e.message);
return;
}
throw e;
} Prevention
- Run FormCalc scripts through a parse/lint pass before binding.
- Ensure each top-level construct is complete and nothing trails after the last terminator.
- Re-serialize XFA packets with a validated tool.
When it happens
Trigger: A FormCalc script where valid declarations are followed by stray tokens (e.g. an orphan keyword, a second top-level construct the grammar does not allow, or trailing characters after 'endfunc'/'endif'). Reached when PDF.js parses an XFA form's 'script' or 'calc' content.
Common situations: Two statements concatenated without a legal separator; a trailing fragment left after deleting a declaration mid-edit; a generator that appends extra tokens; truncated or concatenated script content in a damaged XFA packet.
Understand the failure class
- Authentication and authorization failures — expired tokens, bad credentials, and missing scopes.
Related errors
- Invalid token in index.
- 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/6fe18a259713592b.
Report an issue: GitHub.