Blame view

js/lib/JSBuilder/src/Parser/Statement/If.js 1.34 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Parser.Statement.If = Ext.extend(Parser.Statement, {
    satisfied: false,

    evaluate: function() {
        var ret = true, n;

        for (n in this.properties) {
            if (!Parser.evaluate(n, this.properties[n])) {
                ret = false;
                break;
            }
        }

        return (this.isInverted ? !ret : ret);
    },

    parse: function(stream) {
        if (this.evaluate()) {
            this.satisfied = true;
        }

        Parser.Statement.If.superclass.parse.apply(this, arguments);

        if (!this.satisfied) {
            return '';
        }

        return this.buffer;
    },

    onSubStatement: function(statement, stream) {
        var parsed = statement.parse(stream),
            satisfied = false;

        if (statement.type === 'elseif') {
            if (!this.satisfied) {
                if (statement.evaluate()) {
                    this.satisfied = true;
                    satisfied = true;
                }
            }
        } else if (statement.type === 'else') {
            if (!this.satisfied) {
                this.satisfied = true;
                satisfied = true;
            }
        } else {
            this.pushBuffer(parsed);
            return;
        }

        if (satisfied) {
            this.resetBuffer();
            this.pushBuffer(parsed);
        }
    }
});