-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (97 loc) · 3.08 KB
/
index.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
const fs = require("fs/promises");
const rec = require('iconv-lite');
async function readFile_win1251(filename) {
let raw = await fs.readFile(filename);
let reconverted = rec.encode(rec.decode(raw, 'win1251'), 'utf8').toString();
return reconverted;
}
if(!String.prototype.replaceAll) {
String.prototype.replaceAll = function(fromstr,tostr) {
var str = this;
while(str.indexOf(fromstr) != -1) {
str = str.replace(fromstr,tostr);
}
return str;
};
} // fucking node.js v14.x
exports.parse = async function(schm, prettyPrint) {
var scheme = {};
var regex = /Add\((.+),(\d+),(\d+),(\d+)\)/;
var regexPoint = /Point\((.+)\)/;
var regexLink = /link\((.+),(\d+):(.+),\[(.+)\]\)/;
let file = await readFile_win1251(schm);
let lines = file.split("\r\n");
let pack = lines.splice(0,1)[0];
let ver = lines.splice(0,1)[0];
scheme.pack = pack.substring(5,pack.length - 1);
scheme.hiasmVersion = ver.substring(4,ver.length - 1);
scheme.elements = [];
let curelem = null;
let containerelem = [];
lines.forEach((lineraw,index)=>{
var line = lineraw.trimStart();
if(line.startsWith('Add')) {
let match = regex.exec(line);
if(match) {
curelem = {
name: match[1],
id: parseInt(match[2]),
x: parseInt(match[3]),
y: parseInt(match[4]),
props: {},
points: [],
links: []
}
} else throw new TypeError("Line starts with Add, yet no match found for line #"+index+".");
} else if(line == '}') {
if(curelem != null) {
if(lines[index+1]) {
if(lines[index+1].trimStart() == 'BEGIN_SDK') {
containerelem.push(curelem);
containerelem[containerelem.length-1].elements = [];
} else if(containerelem.length > 0) {
containerelem[containerelem.length-1].elements.push(curelem);
curelem = null;
} else {
scheme.elements.push(curelem);
curelem = null;
}
} else {
scheme.elements.push(curelem);
curelem = null;
}
} else throw new TypeError('Found "}" on line #'+index+', but no element is being processed.');
} else if(line == 'END_SDK') {
if(containerelem.length == 1) {
scheme.elements.push(containerelem[0]);
containerelem = [];
} else if(containerelem.length > 1) {
containerelem[containerelem.length-2].elements.push(containerelem.pop());
} else {
throw new TypeError("Found a END_SDK, yet no container to be seen.");
}
} else if(curelem != null && line != '{') {
let el = line;
let prop = el.split("=");
let point = regexPoint.exec(el);
let link = regexLink.exec(el);
if(point) {
curelem.points.push(point[1]);
} else if(link) {
let coordsbefore = link[4].toString().replaceAll('(','[').replaceAll(')','],');
let coords = JSON.parse('['+coordsbefore.substring(0,coordsbefore.length - 1)+']');
curelem.links.push({
point: link[1],
to: {
id: link[2],
name: link[3],
},
coords
});
} else if(prop) {
curelem.props[prop[0]] = prop[1];
}
}
});
return JSON.stringify(scheme,null,prettyPrint?' ':null);
};