mozilla/pdf.js · error · Error

Invalid token if ... endif declaration.

Error message

Invalid token if ... endif declaration.

What it means

Thrown by Parser.parseIf() when, immediately after the 'if' keyword, the next token is not '(' (TOKEN.leftParen). FormCalc if syntax is 'if '(' SimpleExpression ')' then ExprList ... endif', so the condition must be parenthesized.

Source

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

        throw new Error(Errors.func);
      }
      tok = this.lexer.next();
    }
  }

  parseSimpleExpr(tok = null) {
    return new SimpleExprParser(this.lexer).parse(tok);
  }

  parseIf() {
    // 'if' '(' SimpleExpression ')' then ExpressionList
    // ('elseif' '(' SimpleExpression ')' then ExpressionList )*
    // ('else' ExpressionList)?
    // 'endif'.
    let elseIfClause = [];
    let tok = this.lexer.next();
    if (tok.id !== TOKEN.leftParen) {
      throw new Error(Errors.if);
    }

    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) {

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Wrap the condition in parentheses: 'if (cond) then ... endif'.
  2. Do not substitute braces or bare expressions for the parenthesized condition.
  3. Re-emit the script from a FormCalc authoring tool.
  4. Lint every 'if'/'elseif' to confirm '(' follows.

Example fix

// before
if x > 0 then 1 endif
// after
if (x > 0) then 1 endif
Defensive patterns

Strategy: validation

Validate before calling

// Each 'if' / 'elseif' must be followed by '('.
function ifsOpenParen(src) {
  return !/\b(if|elseif)\b(?!\s*\()/.test(src);
}

Try / catch

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

Prevention

When it happens

Trigger: A FormCalc 'if x then ... endif' or 'if {x} then ...' where the opening parenthesis after 'if' is missing. Reached while parsing an XFA form script containing an if-statement.

Common situations: Author used brace-free or brace-style conditions (FormCalc mandates parentheses); typo; generator omitting '('; corrupted script bytes after 'if'.

Understand the failure class

Related errors


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