mozilla/pdf.js · error · Error

Invalid token in func declaration.

Error message

Invalid token in func declaration.

What it means

Thrown by Parser.parseFuncDecl() when, immediately after the 'func' keyword, the next token is not an identifier (TOKEN.identifier). FormCalc function grammar is 'func Identifier ParamList do ExprList endfunc', so 'func' must be followed by the function name first.

Source

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

      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);
    }

    const identifier = tok.value;
    const params = this.parseParamList();

    tok = this.lexer.next();
    if (tok.id !== TOKEN.do) {
      throw new Error(Errors.func);
    }

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

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

    return [null, new FuncDecl(identifier, params, body)];

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Place a valid identifier (the function name) immediately after 'func'.
  2. Confirm the name is not a FormCalc keyword or builtin that the lexer would not classify as a plain identifier.
  3. Re-author the function declaration in a FormCalc tool and re-serialize the XFA packet.
  4. Catch the parser error at XFA binding and skip the malformed function.

Example fix

// before
func (a) do a+1 endfunc
// after
func inc(a) do a+1 endfunc
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

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

Prevention

When it happens

Trigger: A FormCalc script like 'func () do ... endfunc' or 'func 42 (...) do ...' where the function name identifier is missing. Reached while parsing an XFA form script that declares a function.

Common situations: Typo dropping the function name; using a reserved word as a function name; truncated/corrupted script bytes after 'func'; generator emitting 'func' then '(' directly.

Understand the failure class

Related errors


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