Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / jsbuilder / src / Parser.old.js
1 Parser = {
2     isBuild: function(builds) {
3         return builds.split('|').indexOf(this.build) != -1;
4     },
5
6     parse: function(file, build) {
7         var line,
8             trimmed,
9             o = this.output = [];
10
11         this.build = build;
12
13         file = new Stream(file);
14         while (!file.eof) {
15             line = file.readLine();
16             trimmed = line.trim();
17             if (this.isStatement(trimmed)) {
18                 this.handleStatement(this.parseStatement(trimmed), file);
19             }
20             else {
21                 this.output.push(line);
22                 this.checkExtraComma();
23             }
24         }
25         file.close();
26         return this.output.join('\n');
27     },
28
29     checkExtraComma: function() {
30         var output = this.output,
31             ln = output.length - 1,
32             line = output[ln],
33             trimmed = line.trim(),
34             prevLine;
35
36         if (trimmed[0] == '}') {
37             while (output[--ln].trim() == '') {
38                 output.splice(ln, 1);
39             }
40             prevLine = output[ln];
41             if (prevLine.trim().slice( - 1) == ',') {
42                 output[ln] = prevLine.slice(0, prevLine.lastIndexOf(','));
43             }
44         }
45     },
46
47     isStatement: function(line) {
48         return line.substr(0, 3) == '//[' && line.substr( - 1) == ']';
49     },
50
51     handleStatement: function(statement, file) {
52         switch (statement.type) {
53             case 'if':
54             case 'elseif':
55                 this.handleIf(file, statement.condition);
56                 break;
57
58             case 'else':
59                 this.handleElse(file);
60                 break;
61         }
62     },
63
64     parseStatement: function(statement) {
65         var parts = statement.substring(3, statement.length - 1).split(' ');
66         return {
67             type: parts[0],
68             condition: parts[1]
69         };
70     },
71
72     handleIf: function(file, condition) {
73         if (this.isBuild(condition)) {
74             var next = this.getNextStatement(file);
75             this.output.push(next.buffer);
76             this.toEndIf(file, next);
77         }
78         else {
79             this.handleStatement(this.getNextStatement(file), file);
80         }
81     },
82
83     handleElse: function(file) {
84         var next = this.toEndIf(file);
85         this.output.push(next.buffer);
86     },
87
88     toEndIf: function(file, next) {
89         next = next || this.getNextStatement(file);
90         while (next && next.type != 'endif') {
91             next = this.getNextStatement(file);
92         }
93         return next;
94     },
95
96     getNextStatement: function(file) {
97         var buffer = [],
98             line,
99             trimmed,
100             ret;
101
102         while (!file.eof) {
103             line = file.readLine();
104             trimmed = line.trim();
105             if (!this.isStatement(trimmed)) {
106                 buffer.push(line);
107             }
108             else {
109                 ret = this.parseStatement(trimmed);
110                 ret.buffer = buffer.join('\n');
111                 return ret;
112             }
113         }
114         return null;
115     }
116 };