Parser.old.js
2.99 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
Parser = {
isBuild: function(builds) {
return builds.split('|').indexOf(this.build) != -1;
},
parse: function(file, build) {
var line,
trimmed,
o = this.output = [];
this.build = build;
file = new Stream(file);
while (!file.eof) {
line = file.readLine();
trimmed = line.trim();
if (this.isStatement(trimmed)) {
this.handleStatement(this.parseStatement(trimmed), file);
}
else {
this.output.push(line);
this.checkExtraComma();
}
}
file.close();
return this.output.join('\n');
},
checkExtraComma: function() {
var output = this.output,
ln = output.length - 1,
line = output[ln],
trimmed = line.trim(),
prevLine;
if (trimmed[0] == '}') {
while (output[--ln].trim() == '') {
output.splice(ln, 1);
}
prevLine = output[ln];
if (prevLine.trim().slice( - 1) == ',') {
output[ln] = prevLine.slice(0, prevLine.lastIndexOf(','));
}
}
},
isStatement: function(line) {
return line.substr(0, 3) == '//[' && line.substr( - 1) == ']';
},
handleStatement: function(statement, file) {
switch (statement.type) {
case 'if':
case 'elseif':
this.handleIf(file, statement.condition);
break;
case 'else':
this.handleElse(file);
break;
}
},
parseStatement: function(statement) {
var parts = statement.substring(3, statement.length - 1).split(' ');
return {
type: parts[0],
condition: parts[1]
};
},
handleIf: function(file, condition) {
if (this.isBuild(condition)) {
var next = this.getNextStatement(file);
this.output.push(next.buffer);
this.toEndIf(file, next);
}
else {
this.handleStatement(this.getNextStatement(file), file);
}
},
handleElse: function(file) {
var next = this.toEndIf(file);
this.output.push(next.buffer);
},
toEndIf: function(file, next) {
next = next || this.getNextStatement(file);
while (next && next.type != 'endif') {
next = this.getNextStatement(file);
}
return next;
},
getNextStatement: function(file) {
var buffer = [],
line,
trimmed,
ret;
while (!file.eof) {
line = file.readLine();
trimmed = line.trim();
if (!this.isStatement(trimmed)) {
buffer.push(line);
}
else {
ret = this.parseStatement(trimmed);
ret.buffer = buffer.join('\n');
return ret;
}
}
return null;
}
};