ifStmt()

in lib/parser.js [2054:2096]


  ifStmt() {
    const begin = this.getIndex();
    this.match(Tag.IF);
    this.match('(');
    var condition = this.expr();
    this.match(')');
    var stmts = this.blockStmts();
    var end = stmts.tokenRange[1];

    var elseStmts;
    var elseIfs = [];
    while (this.look.tag === Tag.ELSE) {
      this.move();
      if (this.look.tag === Tag.IF) {
        this.move();
        this.match('(');
        let condition = this.expr();
        this.match(')');
        let stmts = this.blockStmts();
        end = stmts.tokenRange[1];
        elseIfs.push({
          type: 'elseif',
          condition: condition,
          stmts: stmts
        });
      } else if (this.look.tag === '{') {
        elseStmts = this.blockStmts();
        end = elseStmts.tokenRange[1];
        break;
      } else {
        this.error('expect "if" or "{"');
      }
    }

    return {
      type: 'if',
      condition: condition,
      stmts: stmts,
      elseIfs: elseIfs,
      elseStmts: elseStmts,
      tokenRange: [begin, end]
    };
  }