mozilla/pdf.js · error · Error

Invalid elseif declaration.

Error message

Invalid elseif declaration.

What it means

Thrown by Parser.parseIf() inside an 'elseif' branch when, immediately after 'elseif', the next token is not '(' (TOKEN.leftParen). Each elseif clause repeats the 'elseif '(' SimpleExpression ')' then ExprList' shape, so the condition must be parenthesized.

Source

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

    const [tok1, condition] = this.parseSimpleExpr();

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

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

    const [tok2, thenClause] = this.parseExprList();
    tok = tok2 || this.lexer.next();

    while (tok.id === TOKEN.elseif) {
      tok = this.lexer.next();
      if (tok.id !== TOKEN.leftParen) {
        throw new Error(Errors.elseif);
      }

      const [tok3, elseIfCondition] = this.parseSimpleExpr();

      tok = tok3 || this.lexer.next();
      if (tok.id !== TOKEN.rightParen) {
        throw new Error(Errors.elseif);
      }

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

      const [tok4, elseIfThenClause] = this.parseExprList();
      elseIfClause.push(new ElseIfDecl(elseIfCondition, elseIfThenClause));

      tok = tok4 || this.lexer.next();

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Parenthesize every elseif condition: 'elseif (cond) then ...'.
  2. Treat elseif exactly like if syntactically.
  3. Re-author the if/elseif chain in a FormCalc tool.
  4. Lint elseif clauses for a following '('.

Example fix

// before
elseif x > 1 then 2
// after
elseif (x > 1) then 2
Defensive patterns

Strategy: validation

Validate before calling

// Reuse the if-open-paren check; elseif must also be followed by '('.
function elseifsOpenParen(src) {
  return !/\belseif\b(?!\s*\()/.test(src);
}

Try / catch

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

Prevention

When it happens

Trigger: A FormCalc 'elseif x then ...' where the opening parenthesis after 'elseif' is missing. Reached while parsing the elseif chain of an XFA if-statement.

Common situations: Author forgot parens on an elseif condition; typo; generator emitting elseif without '('; corrupted script bytes.

Related errors


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