mozilla/pdf.js · error · Error

Invalid token in var declaration.

Error message

Invalid token in var declaration.

What it means

Thrown by Parser.parseVarDecl() when, immediately after the 'var' keyword, the next token is not an identifier (TOKEN.identifier). FormCalc's var declaration grammar is 'var Identifier ('=' SimpleExpression)?', so 'var' must be directly followed by a variable name.

Source

Thrown at src/core/xfa/formcalc_parser.js:1065

    return parser.parse(tok);
  }

  parseBlock() {
    const [tok1, body] = this.parseExprList();

    const tok = tok1 || this.lexer.next();
    if (tok.id !== TOKEN.end) {
      throw new Error(Errors.block);
    }

    return [null, new BlockDecl(body)];
  }

  parseVarDecl() {
    // 'var' Identifier ('=' SimpleExpression)?
    let tok = this.lexer.next();
    if (tok.id !== TOKEN.identifier) {
      throw new Error(Errors.var);
    }

    const identifier = tok.value;

    tok = this.lexer.next();
    if (tok.id !== TOKEN.assign) {
      return [tok, new VarDecl(identifier, null)];
    }

    const [tok1, expr] = this.parseSimpleExpr();
    return [tok1, new VarDecl(identifier, expr)];
  }

  parseFuncDecl() {
    // 'func' Identifier ParameterList 'do' ExpressionList 'endfunc'.
    let tok = this.lexer.next();
    if (tok.id !== TOKEN.identifier) {
      throw new Error(Errors.func);

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Add a legal identifier name directly after 'var' (FormCalc identifiers are letter-led, alphanumeric/underscore).
  2. Ensure the name is not a reserved FormCalc keyword that the lexer would tokenize differently.
  3. Re-emit the script from the form design tool to fix the declaration.
  4. Lint the script string for every 'var' occurrence before binding.

Example fix

// before
var = 5
// after
var x = 5
Defensive patterns

Strategy: validation

Validate before calling

// Reject 'var' not followed by an identifier.
function validVarDecls(src) {
  return !/\bvar\b(?!\s+[A-Za-z_][A-Za-z0-9_]*)/.test(src);
}

Try / catch

try {
  bindFormCalcScript(xfaNode, src);
} catch (e) {
  if (e instanceof Error && /var declaration/.test(e.message)) {
    console.warn("Malformed FormCalc var declaration; skipping", e.message);
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: A FormCalc statement like 'var = 5' or 'var 123' where 'var' is not followed by an identifier token. Reached during XFA form script parsing when a 'var' declaration is malformed.

Common situations: Typo omitting the variable name; using a reserved keyword where a name is expected; corrupted script bytes dropping the identifier; generator emitting 'var' then a non-name literal.

Understand the failure class

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/f13e3635ca14528c. Report an issue: GitHub.