tryStmt()

in lib/parser.js [2124:2181]


  tryStmt() {
    const begin = this.getIndex();
    this.match(Tag.TRY);
    let tryStmts = this.blockStmts();
    const catchBlocks = [];
    let catchStmts = null;
    let id = null;
    let finallyStmts = null;
    let end;
    if (this.look.tag === Tag.CATCH || this.look.tag === Tag.FINALLY) {
      while(this.look.tag === Tag.CATCH) {
        this.move();
        this.match('(');
        const catchId = this.look;
        this.match(Tag.ID);
        if(this.is(')')) {
          // compatible with v1
          // only can be last catch block
          this.move();
          id = catchId;
          catchStmts = this.blockStmts();
          catchBlocks.push({
            id: catchId,
            catchStmts,
          });
          end = catchStmts.tokenRange[1];
          break;
        }
        this.match(':');
        catchId.type = this.baseType();
        this.match(')');
        catchStmts = this.blockStmts();
        catchBlocks.push({
          id: catchId,
          catchStmts,
        });
        end = catchStmts.tokenRange[1];
      }

      if (this.look.tag === Tag.FINALLY) {
        this.move();
        finallyStmts = this.blockStmts();
        end = finallyStmts.tokenRange[1];
      }
    } else {
      this.error('"try" expect "catch" or "finally"');
    }

    return {
      type: 'try',
      tryBlock: tryStmts,
      catchId: id,
      catchBlock: catchStmts,
      catchBlocks: catchBlocks,
      finallyBlock: finallyStmts,
      tokenRange: [begin, end]
    };
  }