-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseFile.js
108 lines (95 loc) · 2.79 KB
/
parseFile.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
/* eslint-disable no-console */
const fs = require('fs');
let current_indent = '';
module.exports = function (path, indent) {
const content = fs.readFileSync(path).toString();
const lines = content.replace(/\r\n/g, '\n').split('\n');
current_indent = '';
for (let i = 0; i < lines.length;i += 1) {
let line = lines[i].trim();
if (line.startsWith('//'))
addLine(line);
if (line.startsWith('typedef struct')) {
const variables = [];
let type_name = '';
while (!type_name) {
i += 1;
line = lines[i].trim();
if (line === '{') continue;
if (line === '') continue;
if (line.startsWith('//')) {
variables.push({
type: '',
name: '',
comment: line,
});
continue;
}
if (line.startsWith('}')) {
type_name = line.replace('}', '').replace(';', '').trim();
break;
}
const variable = {
type: '',
name: '',
comment: '',
};
const commentIdx = line.indexOf('//');
if (commentIdx !== -1) {
variable.comment = line.slice(commentIdx);
line = line.slice(0, commentIdx);
}
line = line.trim();
const lineSplit = line.split(' ');
let isArray = false;
variable.name = lineSplit.pop().replace(';', '');
const square_bracket_idx = variable.name.indexOf('[');
if (square_bracket_idx !== -1) {
isArray = true;
variable.name = variable.name.slice(0, square_bracket_idx);
}
variable.type = getType(lineSplit, isArray);
variables.push(variable);
}
addLine(`type ${type_name} = {`);
addIndent(indent);
for (const variable of variables) {
if (!variable.type)
addLine(variable.comment);
else
addLine(`${variable.name}: ${variable.type}; ${variable.comment}`);
}
subIndent(indent);
addLine('}');
addLine('');
}
}
};
function addLine (line) {
console.log(current_indent + line);
}
const numType = [ 'int', 'short', 'long', 'double', 'float', 'uint8', 'int8', 'uint16', 'int16', 'uint32', 'int32', 'fp32', 'fp64' ];
const charType = [ 'char' ];
function getType (lineSplit, isArray) {
for (let i = 0;i < lineSplit.length;i += 1) {
const str = lineSplit[i];
if (str === 'unsigned') continue;
if (str === 'signed') continue;
if (numType.includes(str)) {
if (isArray)
return 'number[]';
return 'number';
}
if (charType.includes(str))
return 'string';
if (isArray)
return str + '[]';
return str;
}
}
function addIndent (indent) {
current_indent += indent;
}
function subIndent (indent) {
current_indent = current_indent.replace(indent, '');
}