|
@@ -0,0 +1,141 @@
|
|
1
|
+#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+//
|
|
4
|
+// Formatter script for pins_MYPINS.h files
|
|
5
|
+//
|
|
6
|
+// Usage: mffmt [infile] [outfile]
|
|
7
|
+//
|
|
8
|
+// With no parameters convert STDIN to STDOUT
|
|
9
|
+//
|
|
10
|
+
|
|
11
|
+const fs = require("fs");
|
|
12
|
+
|
|
13
|
+// String lpad / rpad
|
|
14
|
+String.prototype.lpad = function(len, chr) {
|
|
15
|
+ if (!len) return this;
|
|
16
|
+ if (chr === undefined) chr = ' ';
|
|
17
|
+ var s = this+'', need = len - s.length;
|
|
18
|
+ if (need > 0) s = new Array(need+1).join(chr) + s;
|
|
19
|
+ return s;
|
|
20
|
+};
|
|
21
|
+
|
|
22
|
+String.prototype.rpad = function(len, chr) {
|
|
23
|
+ if (!len) return this;
|
|
24
|
+ if (chr === undefined) chr = ' ';
|
|
25
|
+ var s = this+'', need = len - s.length;
|
|
26
|
+ if (need > 0) s += new Array(need+1).join(chr);
|
|
27
|
+ return s;
|
|
28
|
+};
|
|
29
|
+
|
|
30
|
+const mpatt = [ '-?\\d+', 'P[A-I]\\d+', 'P\\d_\\d+' ],
|
|
31
|
+ definePatt = new RegExp(`^\\s*(//)?#define\\s+[A-Z_][A-Z0-9_]+\\s+(${mpatt[0]}|${mpatt[1]}|${mpatt[2]})\\s*(//.*)?$`, 'gm'),
|
|
32
|
+ ppad = [ 3, 4, 5 ],
|
|
33
|
+ col_comment = 50,
|
|
34
|
+ col_value_rj = col_comment - 3;
|
|
35
|
+
|
|
36
|
+var mexpr = [];
|
|
37
|
+for (let m of mpatt) mexpr.push(new RegExp('^' + m + '$'));
|
|
38
|
+
|
|
39
|
+const argv = process.argv.slice(2), argc = argv.length;
|
|
40
|
+
|
|
41
|
+var src_file = 0, src_name = 'STDIN', dst_file;
|
|
42
|
+if (argc > 0) {
|
|
43
|
+ src_file = src_name = argv[0];
|
|
44
|
+ dst_file = argv[argc > 1 ? 1 : 0];
|
|
45
|
+}
|
|
46
|
+
|
|
47
|
+// Read from file or STDIN until it terminates
|
|
48
|
+const filtered = process_text(fs.readFileSync(src_file).toString());
|
|
49
|
+if (dst_file)
|
|
50
|
+ fs.writeFileSync(dst_file, filtered);
|
|
51
|
+else
|
|
52
|
+ console.log(filtered);
|
|
53
|
+
|
|
54
|
+// Find the pin pattern so non-pin defines can be skipped
|
|
55
|
+function get_pin_pattern(txt) {
|
|
56
|
+ var r, m = 0, match_count = [ 0, 0, 0 ];
|
|
57
|
+ definePatt.lastIndex = 0;
|
|
58
|
+ while ((r = definePatt.exec(txt)) !== null) {
|
|
59
|
+ let ind = -1;
|
|
60
|
+ if (mexpr.some((p) => {
|
|
61
|
+ ind++;
|
|
62
|
+ const didmatch = r[2].match(p);
|
|
63
|
+ return r[2].match(p);
|
|
64
|
+ }) ) {
|
|
65
|
+ const m = ++match_count[ind];
|
|
66
|
+ if (m >= 10) {
|
|
67
|
+ return { match: mpatt[ind], pad:ppad[ind] };
|
|
68
|
+ }
|
|
69
|
+ }
|
|
70
|
+ }
|
|
71
|
+ return null;
|
|
72
|
+}
|
|
73
|
+
|
|
74
|
+function process_text(txt) {
|
|
75
|
+ if (!txt.length) return '(no text)';
|
|
76
|
+ const patt = get_pin_pattern(txt);
|
|
77
|
+ if (!patt) return txt;
|
|
78
|
+ const pindefPatt = new RegExp(`^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+(${patt.match})\\s*(//.*)?$`),
|
|
79
|
+ noPinPatt = new RegExp(`^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+(-1)\\s*(//.*)?$`),
|
|
80
|
+ skipPatt = new RegExp('^(\\s*(//)?#define)\\s+(AT90USB|USBCON|BOARD_.+|.+_MACHINE_NAME|.+_SERIAL)\\s+(.+)\\s*(//.*)?$'),
|
|
81
|
+ aliasPatt = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+([A-Z_][A-Z0-9_()]+)\\s*(//.*)?$'),
|
|
82
|
+ switchPatt = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s*(//.*)?$'),
|
|
83
|
+ undefPatt = new RegExp('^(\\s*(//)?#undef)\\s+([A-Z_][A-Z0-9_]+)\\s*(//.*)?$'),
|
|
84
|
+ defPatt = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+(\\w+)\\s*(//.*)?$'),
|
|
85
|
+ condPatt = new RegExp('^(\\s*(//)?#(if|ifn?def|else|elif)(\\s+\\S+)*)\\s+(//.*)$'),
|
|
86
|
+ commPatt = new RegExp('^\\s{20,}(//.*)?$');
|
|
87
|
+ const col_value_lj = col_comment - patt.pad - 2;
|
|
88
|
+ var r, out = '', check_comment_next = false;
|
|
89
|
+ txt.split('\n').forEach((line) => {
|
|
90
|
+ if (check_comment_next)
|
|
91
|
+ check_comment_next = ((r = commPatt.exec(line)) !== null);
|
|
92
|
+
|
|
93
|
+ if (check_comment_next)
|
|
94
|
+ // Comments in column 45
|
|
95
|
+ line = ''.rpad(col_comment) + r[1];
|
|
96
|
+
|
|
97
|
+ else if ((r = pindefPatt.exec(line)) !== null) {
|
|
98
|
+ //
|
|
99
|
+ // #define MY_PIN [pin]
|
|
100
|
+ //
|
|
101
|
+ const pinnum = r[4].charAt(0) == 'P' ? r[4] : r[4].lpad(patt.pad);
|
|
102
|
+ line = r[1] + ' ' + r[3];
|
|
103
|
+ line = line.rpad(col_value_lj) + pinnum;
|
|
104
|
+ if (r[5]) line = line.rpad(col_comment) + r[5];
|
|
105
|
+ }
|
|
106
|
+ else if ((r = noPinPatt.exec(line)) !== null) {
|
|
107
|
+ //
|
|
108
|
+ // #define MY_PIN -1
|
|
109
|
+ //
|
|
110
|
+ line = r[1] + ' ' + r[3];
|
|
111
|
+ line = line.rpad(col_value_lj) + '-1';
|
|
112
|
+ if (r[5]) line = line.rpad(col_comment) + r[5];
|
|
113
|
+ }
|
|
114
|
+ else if ((r = skipPatt.exec(line)) !== null) {
|
|
115
|
+ }
|
|
116
|
+ else if ((r = aliasPatt.exec(line)) !== null) {
|
|
117
|
+ line = r[1] + ' ' + r[3];
|
|
118
|
+ line += r[4].lpad(col_value_rj + 1 - line.length);
|
|
119
|
+ if (r[5]) line = line.rpad(col_comment) + r[5];
|
|
120
|
+ }
|
|
121
|
+ else if ((r = switchPatt.exec(line)) !== null) {
|
|
122
|
+ line = r[1] + ' ' + r[3];
|
|
123
|
+ if (r[4]) line = line.rpad(col_comment) + r[4];
|
|
124
|
+ check_comment_next = true;
|
|
125
|
+ }
|
|
126
|
+ else if ((r = defPatt.exec(line)) !== null) {
|
|
127
|
+ line = r[1] + ' ' + r[3] + ' ' + r[4];
|
|
128
|
+ if (r[5]) line = line.rpad(col_comment) + r[5];
|
|
129
|
+ }
|
|
130
|
+ else if ((r = undefPatt.exec(line)) !== null) {
|
|
131
|
+ line = r[1] + ' ' + r[3];
|
|
132
|
+ if (r[4]) line = line.rpad(col_comment) + r[4];
|
|
133
|
+ }
|
|
134
|
+ else if ((r = condPatt.exec(line)) !== null) {
|
|
135
|
+ line = r[1].rpad(col_comment) + r[5];
|
|
136
|
+ check_comment_next = true;
|
|
137
|
+ }
|
|
138
|
+ out += line + '\n';
|
|
139
|
+ });
|
|
140
|
+ return out.replace(/\n\n+/g, '\n\n').replace(/\n\n$/g, '\n');
|
|
141
|
+}
|