Skip to content

Commit 3b6f1bf

Browse files
committed
πŸ§‘β€πŸ’» Update pinsformat
1 parent 1d61571 commit 3b6f1bf

File tree

2 files changed

+48
-35
lines changed

2 files changed

+48
-35
lines changed

β€Žbuildroot/share/scripts/pinsformat.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
const fs = require("fs");
1212

13+
var do_log = false
14+
function logmsg(msg, line='') {
15+
if (do_log) console.log(msg, line);
16+
}
17+
1318
// String lpad / rpad
1419
String.prototype.lpad = function(len, chr) {
1520
if (!len) return this;
@@ -37,7 +42,7 @@ String.prototype.concat_with_space = function(str) {
3742
};
3843

3944
const mpatt = [ '-?\\d{1,3}', 'P[A-I]\\d+', 'P\\d_\\d+', 'Pin[A-Z]\\d\\b' ],
40-
definePatt = new RegExp(`^\\s*(//)?#define\\s+[A-Z_][A-Z0-9_]+\\s+(${mpatt[0]}|${mpatt[1]}|${mpatt[2]}|${mpatt[3]})\\s*(//.*)?$`, 'gm'),
45+
definePatt = new RegExp(`^\\s*(//)?#define\\s+[A-Z_][A-Z0-9_]+\\s+(${'|'.join(mpatt)})\\s*(//.*)?$`, 'gm'),
4146
ppad = [ 3, 4, 5, 5 ],
4247
col_comment = 50,
4348
col_value_rj = col_comment - 3;
@@ -47,11 +52,11 @@ for (let m of mpatt) mexpr.push(new RegExp('^' + m + '$'));
4752

4853
const argv = process.argv.slice(2), argc = argv.length;
4954

50-
var src_file = 0, src_name = 'STDIN', dst_file, do_log = false;
55+
var src_file = 0, dst_file;
5156
if (argc > 0) {
5257
let ind = 0;
5358
if (argv[0] == '-v') { do_log = true; ind++; }
54-
dst_file = src_file = src_name = argv[ind++];
59+
dst_file = src_file = argv[ind++];
5560
if (ind < argc) dst_file = argv[ind];
5661
}
5762

@@ -115,13 +120,13 @@ function process_text(txt) {
115120
//
116121
// #define SKIP_ME
117122
//
118-
if (do_log) console.log("skip:", line);
123+
logmsg("skip:", line);
119124
}
120125
else if ((r = pindefPatt.exec(line)) !== null) {
121126
//
122127
// #define MY_PIN [pin]
123128
//
124-
if (do_log) console.log("pin:", line);
129+
logmsg("pin:", line);
125130
const pinnum = r[4].charAt(0) == 'P' ? r[4] : r[4].lpad(patt.pad);
126131
line = r[1] + ' ' + r[3];
127132
line = line.rpad(col_value_lj).concat_with_space(pinnum);
@@ -131,22 +136,23 @@ function process_text(txt) {
131136
//
132137
// #define MY_PIN -1
133138
//
134-
if (do_log) console.log("pin -1:", line);
139+
logmsg("pin -1:", line);
135140
line = r[1] + ' ' + r[3];
136141
line = line.rpad(col_value_lj).concat_with_space('-1');
137142
if (r[5]) line = line.rpad(col_comment).concat_with_space(r[5]);
138143
}
139144
else if (skipPatt2.exec(line) !== null || skipPatt3.exec(line) !== null) {
140145
//
141146
// #define SKIP_ME
147+
// #else, #endif
142148
//
143-
if (do_log) console.log("skip:", line);
149+
logmsg("skip:", line);
144150
}
145151
else if ((r = aliasPatt.exec(line)) !== null) {
146152
//
147153
// #define ALIAS OTHER
148154
//
149-
if (do_log) console.log("alias:", line);
155+
logmsg("alias:", line);
150156
line = r[1] + ' ' + r[3];
151157
line = line.concat_with_space(r[4].lpad(col_value_rj + 1 - line.length));
152158
if (r[5]) line = line.rpad(col_comment).concat_with_space(r[5]);
@@ -155,7 +161,7 @@ function process_text(txt) {
155161
//
156162
// #define SWITCH
157163
//
158-
if (do_log) console.log("switch:", line);
164+
logmsg("switch:", line);
159165
line = r[1] + ' ' + r[3];
160166
if (r[4]) line = line.rpad(col_comment).concat_with_space(r[4]);
161167
check_comment_next = true;
@@ -164,7 +170,7 @@ function process_text(txt) {
164170
//
165171
// #define ...
166172
//
167-
if (do_log) console.log("def:", line);
173+
logmsg("def:", line);
168174
line = r[1] + ' ' + r[3] + ' ';
169175
line = line.concat_with_space(r[4].lpad(col_value_rj + 1 - line.length));
170176
if (r[5]) line = line.rpad(col_comment - 1) + ' ' + r[5];
@@ -173,15 +179,15 @@ function process_text(txt) {
173179
//
174180
// #undef ...
175181
//
176-
if (do_log) console.log("undef:", line);
182+
logmsg("undef:", line);
177183
line = r[1] + ' ' + r[3];
178184
if (r[4]) line = line.rpad(col_comment).concat_with_space(r[4]);
179185
}
180186
else if ((r = condPatt.exec(line)) !== null) {
181187
//
182188
// #if, #ifdef, #ifndef, #elif ...
183189
//
184-
if (do_log) console.log("cond:", line);
190+
logmsg("cond:", line);
185191
line = r[1].rpad(col_comment).concat_with_space(r[5]);
186192
check_comment_next = true;
187193
}

β€Žbuildroot/share/scripts/pinsformat.py

+30-23
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ def logmsg(msg, line):
1717
col_comment = 50
1818

1919
# String lpad / rpad
20-
def lpad(astr, fill, c=None):
20+
def lpad(astr, fill, c=' '):
2121
if not fill: return astr
22-
if c == None: c = ' '
2322
need = fill - len(astr)
2423
return astr if need <= 0 else (need * c) + astr
2524

26-
def rpad(astr, fill, c=None):
25+
def rpad(astr, fill, c=' '):
2726
if not fill: return astr
28-
if c == None: c = ' '
2927
need = fill - len(astr)
3028
return astr if need <= 0 else astr + (need * c)
3129

@@ -64,9 +62,8 @@ def format_pins(argv):
6462
# If no source file specified read from STDIN
6563
file_text = sys.stdin.read()
6664
else:
67-
# Open the file src_file
68-
with open(src_file, 'r') as rf:
69-
file_text = rf.read()
65+
# Open and read the file src_file
66+
with open(src_file, 'r') as rf: file_text = rf.read()
7067

7168
if len(file_text) == 0:
7269
print('No text to process')
@@ -75,8 +72,7 @@ def format_pins(argv):
7572
# Read from file or STDIN until it terminates
7673
filtered = process_text(file_text)
7774
if dst_file:
78-
with open(dst_file, 'w') as wf:
79-
wf.write(filtered)
75+
with open(dst_file, 'w') as wf: wf.write(filtered)
8076
else:
8177
print(filtered)
8278

@@ -105,16 +101,18 @@ def process_text(txt):
105101
patt = get_pin_pattern(txt)
106102
if patt == None: return txt
107103

108-
pindefPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+(' + patt['match'] + r')\s*(//.*)?$')
109-
noPinPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+(-1)\s*(//.*)?$')
110-
skipPatt1 = re.compile(r'^(\s*(//)?#define)\s+(AT90USB|USBCON|(BOARD|DAC|FLASH|HAS|IS|USE)_.+|.+_(ADDRESS|AVAILABLE|BAUDRATE|CLOCK|CONNECTION|DEFAULT|FREQ|ITEM|MODULE|NAME|ONLY|PERIOD|RANGE|RATE|SERIAL|SIZE|SPI|STATE|STEP|TIMER))\s+(.+)\s*(//.*)?$')
111-
skipPatt2 = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+(0x[0-9A-Fa-f]+|\d+|.+[a-z].+)\s*(//.*)?$')
112-
aliasPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+([A-Z_][A-Z0-9_()]+)\s*(//.*)?$')
104+
pmatch = patt['match']
105+
pindefPatt = re.compile(rf'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+({pmatch})\s*(//.*)?$')
106+
noPinPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+(-1)\s*(//.*)?$')
107+
skipPatt1 = re.compile(r'^(\s*(//)?#define)\s+(AT90USB|USBCON|(BOARD|DAC|FLASH|HAS|IS|USE)_.+|.+_(ADDRESS|AVAILABLE|BAUDRATE|CLOCK|CONNECTION|DEFAULT|ERROR|EXTRUDERS|FREQ|ITEM|MKS_BASE_VERSION|MODULE|NAME|ONLY|ORIENTATION|PERIOD|RANGE|RATE|READ_RETRIES|SERIAL|SIZE|SPI|STATE|STEP|TIMER|VERSION))\s+(.+)\s*(//.*)?$')
108+
skipPatt2 = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+(0x[0-9A-Fa-f]+|\d+|.+[a-z].+)\s*(//.*)?$')
109+
skipPatt3 = re.compile(r'^\s*#e(lse|ndif)\b.*$')
110+
aliasPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+([A-Z_][A-Z0-9_()]+)\s*(//.*)?$')
113111
switchPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s*(//.*)?$')
114-
undefPatt = re.compile(r'^(\s*(//)?#undef)\s+([A-Z_][A-Z0-9_]+)\s*(//.*)?$')
115-
defPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+([-_\w]+)\s*(//.*)?$')
116-
condPatt = re.compile(r'^(\s*(//)?#(if|ifn?def|else|elif)(\s+\S+)*)\s+(//.*)$')
117-
commPatt = re.compile(r'^\s{20,}(//.*)?$')
112+
undefPatt = re.compile(r'^(\s*(//)?#undef)\s+([A-Z_][A-Z0-9_]+)\s*(//.*)?$')
113+
defPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+([-_\w]+)\s*(//.*)?$')
114+
condPatt = re.compile(r'^(\s*(//)?#(if|ifn?def|elif)(\s+\S+)*)\s+(//.*)$')
115+
commPatt = re.compile(r'^\s{20,}(//.*)?$')
118116

119117
col_value_lj = col_comment - patt['pad'] - 2
120118
col_value_rj = col_comment - 3
@@ -136,7 +134,7 @@ def tryPindef(d):
136134
if r == None: return False
137135
logmsg("pin:", line)
138136
pinnum = r[4] if r[4][0] == 'P' else lpad(r[4], patt['pad'])
139-
line = r[1] + ' ' + r[3]
137+
line = f'{r[1]} {r[3]}'
140138
line = rpad(line, col_value_lj) + pinnum
141139
if r[5]: line = rpad(line, col_comment) + r[5]
142140
d['line'] = line
@@ -150,7 +148,7 @@ def tryNoPin(d):
150148
r = noPinPatt.match(line)
151149
if r == None: return False
152150
logmsg("pin -1:", line)
153-
line = r[1] + ' ' + r[3]
151+
line = f'{r[1]} {r[3]}'
154152
line = rpad(line, col_value_lj) + '-1'
155153
if r[5]: line = rpad(line, col_comment) + r[5]
156154
d['line'] = line
@@ -164,6 +162,14 @@ def trySkip2(d):
164162
logmsg("skip:", d['line'])
165163
return True
166164

165+
#
166+
# #else|endif
167+
#
168+
def trySkip3(d):
169+
if skipPatt3.match( d['line']) == None: return False
170+
logmsg("skip:", d['line'])
171+
return True
172+
167173
#
168174
# #define ALIAS OTHER
169175
#
@@ -220,7 +226,7 @@ def tryUndef(d):
220226
return True
221227

222228
#
223-
# #if ...
229+
# #if|ifdef|ifndef|elif ...
224230
#
225231
def tryCond(d):
226232
line = d['line']
@@ -243,18 +249,19 @@ def tryCond(d):
243249
wDict['check_comment_next'] = (r != None)
244250

245251
if wDict['check_comment_next']:
246-
# Comments in column 45
252+
# Comments in column 50
247253
line = rpad('', col_comment) + r[1]
248254

249255
elif trySkip1(wDict): pass #define SKIP_ME
250256
elif tryPindef(wDict): pass #define MY_PIN [pin]
251257
elif tryNoPin(wDict): pass #define MY_PIN -1
252258
elif trySkip2(wDict): pass #define SKIP_ME_TOO
259+
elif trySkip3(wDict): pass #else|endif
253260
elif tryAlias(wDict): pass #define ALIAS OTHER
254261
elif trySwitch(wDict): pass #define SWITCH
255262
elif tryDef(wDict): pass #define ...
256263
elif tryUndef(wDict): pass #undef ...
257-
elif tryCond(wDict): pass #if ...
264+
elif tryCond(wDict): pass #if|ifdef|ifndef|elif ...
258265

259266
out += wDict['line'] + '\n'
260267

0 commit comments

Comments
Β (0)