-
Notifications
You must be signed in to change notification settings - Fork 9
/
parsers.js
155 lines (124 loc) · 5.08 KB
/
parsers.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var parsers = function () {
"use strict";
var _this = {};
function parseRegex(regex, content, fn) {
var ret = regex.exec(content);
if (ret) {
fn(Array.prototype.slice.call(ret, 1));
return true;
}
}
_this.readFileFromInput = function (element, callback) {
var file = element.files[0];
if (file) {
// create a new file reader
var reader = new FileReader();
// the FileReader is asynchronous, so we set a callback function.
// when the reader reads
reader.onload = function (e) {
callback(e.target.result);
};
// actually read the file
reader.readAsText(file);
}
};
_this.parseLEF = function (content) {
var retValue = {
cells: {}
};
var reg = /MACRO\s+(\S+)((?:.|\r?\n)+)END \1/g;
while (parseRegex(reg, content, function (m) {
var cell = retValue.cells[m[0]] = {};
parseRegex(/SIZE\s+(\S+)\s+BY\s+(\S+)/g, m[1], function (m) {
cell.w = +m[0];
cell.h = +m[1];
});
})) { }
return retValue;
}
_this.parseDEF = function (content) {
var retValue = {
die: { x1: 0, y1: 0, x2: 0, y2: 0 },
cells: [],
pins: [],
nets: []
};
// DIEAREA
parseRegex(/DIEAREA\s+\(\s+(\S+)\s+(\S+)\s+\)\s+\(\s+(\S+)\s+(\S+)\s+\)\s+;/g, content, function (m) {
retValue.die = { x1: +m[0], y1: +m[1], x2: +m[2], y2: +m[3] };
});
// DIEAREA
// COMPONENTS
parseRegex(/COMPONENTS.+;((?:\r?\n|.)+)END COMPONENTS/gm, content, function (matches) {
var cells = retValue.cells;
var data = {};
var stop = false;
var component = /\-\s+(\S+)\s+(\S+)[^\(]+\(\s+(\S+)\s+(\S+)/g;
for (; !stop;) {
stop = !parseRegex(component, matches[0], function (m) {
var cell = { name: m[0], type: m[1], x: +m[2], y: +m[3] };
(data[cell.y] = (data[cell.y] || [])).push(cell);
});
}
// sort descendingly; biggest y value is actually smallest
// on drawing. (mirrored).
Object.keys(data).sort(function (a, b) {
return (+b - +a);
}).forEach(function (val) {
cells.push(data[val]);
});
});
// COMPONENTS
// PINS
parseRegex(/PINS.+;((?:\r?\n|.)+)END PINS/g, content, function (matches) {
var pins = retValue.pins;
var component = /- .+ NET (\S+)\r?\n \+ LAYER (\S+) \( (\S+) (\S+) \) \( (\S+) (\S+) \)\r?\n \+ (?:PLACED|FIXED) \( (\S+) (\S+) \) (\w)/g;
while (parseRegex(component, matches[0], function (m) {
pins.push({ name: m[0], layer: m[1], x1: +m[2], y1: +m[3], x2: +m[4], y2: +m[5], x: +m[6], y: +m[7], ori: m[8] });
}));
});
// PINS
// NETS
parseRegex(/^NETS.+;((?:.|\r?\n)+)END NETS$/gm, content, function (matches) {
matches = matches[0].split(/\r?\n/);
var nets = retValue.nets;
for (var i = 0; i < matches.length; ++i) {
parseRegex(/-\s+(\S+)/g, matches[i], function (n) {
var net = { name: n[0], connections: [], routes: [] };
var connections = net.connections;
var routes = net.routes;
for (; i < matches.length; ++i) {
parseRegex(/(metal\d)/, matches[i], function (m) {
var route = { layer: m[0], coords: [] };
var coords = route.coords;
var parser = /\(\s+(-?(?:\*|\d+))\s+(-?(?:\*|\d+))\s+\)\s+(\w+)?/g;
while (parseRegex(parser, matches[i], function (m) {
var x = +m[0], y = +m[1];
if (m[0] === '*')
x = coords[coords.length - 1].x;
if (m[1] === '*')
y = coords[coords.length - 1].y;
coords.push({ x: x, y: y });
route.via = m[2];
}));
routes.push(route);
});
parseRegex(/\(\s+(\S+)\s+(\S+)\s+\)/g, matches[i], function (m) {
if (m[1] === 'CLK')
net.CLOCK = true;
connections.push({ name: m[0], pin: m[1] });
});
if (/\;/.test(matches[i]))
break;
}
if (/clk/i.test(net.name))
net.CLOCK = true;
nets.push(net);
});
}
});
// NETS
return retValue;
};
return _this;
}();