Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Update pinsformat
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jan 1, 2024
1 parent 1d61571 commit 3b6f1bf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
30 changes: 18 additions & 12 deletions buildroot/share/scripts/pinsformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

const fs = require("fs");

var do_log = false
function logmsg(msg, line='') {
if (do_log) console.log(msg, line);
}

// String lpad / rpad
String.prototype.lpad = function(len, chr) {
if (!len) return this;
Expand Down Expand Up @@ -37,7 +42,7 @@ String.prototype.concat_with_space = function(str) {
};

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

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

var src_file = 0, src_name = 'STDIN', dst_file, do_log = false;
var src_file = 0, dst_file;
if (argc > 0) {
let ind = 0;
if (argv[0] == '-v') { do_log = true; ind++; }
dst_file = src_file = src_name = argv[ind++];
dst_file = src_file = argv[ind++];
if (ind < argc) dst_file = argv[ind];
}

Expand Down Expand Up @@ -115,13 +120,13 @@ function process_text(txt) {
//
// #define SKIP_ME
//
if (do_log) console.log("skip:", line);
logmsg("skip:", line);
}
else if ((r = pindefPatt.exec(line)) !== null) {
//
// #define MY_PIN [pin]
//
if (do_log) console.log("pin:", line);
logmsg("pin:", line);
const pinnum = r[4].charAt(0) == 'P' ? r[4] : r[4].lpad(patt.pad);
line = r[1] + ' ' + r[3];
line = line.rpad(col_value_lj).concat_with_space(pinnum);
Expand All @@ -131,22 +136,23 @@ function process_text(txt) {
//
// #define MY_PIN -1
//
if (do_log) console.log("pin -1:", line);
logmsg("pin -1:", line);
line = r[1] + ' ' + r[3];
line = line.rpad(col_value_lj).concat_with_space('-1');
if (r[5]) line = line.rpad(col_comment).concat_with_space(r[5]);
}
else if (skipPatt2.exec(line) !== null || skipPatt3.exec(line) !== null) {
//
// #define SKIP_ME
// #else, #endif
//
if (do_log) console.log("skip:", line);
logmsg("skip:", line);
}
else if ((r = aliasPatt.exec(line)) !== null) {
//
// #define ALIAS OTHER
//
if (do_log) console.log("alias:", line);
logmsg("alias:", line);
line = r[1] + ' ' + r[3];
line = line.concat_with_space(r[4].lpad(col_value_rj + 1 - line.length));
if (r[5]) line = line.rpad(col_comment).concat_with_space(r[5]);
Expand All @@ -155,7 +161,7 @@ function process_text(txt) {
//
// #define SWITCH
//
if (do_log) console.log("switch:", line);
logmsg("switch:", line);
line = r[1] + ' ' + r[3];
if (r[4]) line = line.rpad(col_comment).concat_with_space(r[4]);
check_comment_next = true;
Expand All @@ -164,7 +170,7 @@ function process_text(txt) {
//
// #define ...
//
if (do_log) console.log("def:", line);
logmsg("def:", line);
line = r[1] + ' ' + r[3] + ' ';
line = line.concat_with_space(r[4].lpad(col_value_rj + 1 - line.length));
if (r[5]) line = line.rpad(col_comment - 1) + ' ' + r[5];
Expand All @@ -173,15 +179,15 @@ function process_text(txt) {
//
// #undef ...
//
if (do_log) console.log("undef:", line);
logmsg("undef:", line);
line = r[1] + ' ' + r[3];
if (r[4]) line = line.rpad(col_comment).concat_with_space(r[4]);
}
else if ((r = condPatt.exec(line)) !== null) {
//
// #if, #ifdef, #ifndef, #elif ...
//
if (do_log) console.log("cond:", line);
logmsg("cond:", line);
line = r[1].rpad(col_comment).concat_with_space(r[5]);
check_comment_next = true;
}
Expand Down
53 changes: 30 additions & 23 deletions buildroot/share/scripts/pinsformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ def logmsg(msg, line):
col_comment = 50

# String lpad / rpad
def lpad(astr, fill, c=None):
def lpad(astr, fill, c=' '):
if not fill: return astr
if c == None: c = ' '
need = fill - len(astr)
return astr if need <= 0 else (need * c) + astr

def rpad(astr, fill, c=None):
def rpad(astr, fill, c=' '):
if not fill: return astr
if c == None: c = ' '
need = fill - len(astr)
return astr if need <= 0 else astr + (need * c)

Expand Down Expand Up @@ -64,9 +62,8 @@ def format_pins(argv):
# If no source file specified read from STDIN
file_text = sys.stdin.read()
else:
# Open the file src_file
with open(src_file, 'r') as rf:
file_text = rf.read()
# Open and read the file src_file
with open(src_file, 'r') as rf: file_text = rf.read()

if len(file_text) == 0:
print('No text to process')
Expand All @@ -75,8 +72,7 @@ def format_pins(argv):
# Read from file or STDIN until it terminates
filtered = process_text(file_text)
if dst_file:
with open(dst_file, 'w') as wf:
wf.write(filtered)
with open(dst_file, 'w') as wf: wf.write(filtered)
else:
print(filtered)

Expand Down Expand Up @@ -105,16 +101,18 @@ def process_text(txt):
patt = get_pin_pattern(txt)
if patt == None: return txt

pindefPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+(' + patt['match'] + r')\s*(//.*)?$')
noPinPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+(-1)\s*(//.*)?$')
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*(//.*)?$')
skipPatt2 = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+(0x[0-9A-Fa-f]+|\d+|.+[a-z].+)\s*(//.*)?$')
aliasPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+([A-Z_][A-Z0-9_()]+)\s*(//.*)?$')
pmatch = patt['match']
pindefPatt = re.compile(rf'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+({pmatch})\s*(//.*)?$')
noPinPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+(-1)\s*(//.*)?$')
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*(//.*)?$')
skipPatt2 = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+(0x[0-9A-Fa-f]+|\d+|.+[a-z].+)\s*(//.*)?$')
skipPatt3 = re.compile(r'^\s*#e(lse|ndif)\b.*$')
aliasPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+([A-Z_][A-Z0-9_()]+)\s*(//.*)?$')
switchPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s*(//.*)?$')
undefPatt = re.compile(r'^(\s*(//)?#undef)\s+([A-Z_][A-Z0-9_]+)\s*(//.*)?$')
defPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+([-_\w]+)\s*(//.*)?$')
condPatt = re.compile(r'^(\s*(//)?#(if|ifn?def|else|elif)(\s+\S+)*)\s+(//.*)$')
commPatt = re.compile(r'^\s{20,}(//.*)?$')
undefPatt = re.compile(r'^(\s*(//)?#undef)\s+([A-Z_][A-Z0-9_]+)\s*(//.*)?$')
defPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+([-_\w]+)\s*(//.*)?$')
condPatt = re.compile(r'^(\s*(//)?#(if|ifn?def|elif)(\s+\S+)*)\s+(//.*)$')
commPatt = re.compile(r'^\s{20,}(//.*)?$')

col_value_lj = col_comment - patt['pad'] - 2
col_value_rj = col_comment - 3
Expand All @@ -136,7 +134,7 @@ def tryPindef(d):
if r == None: return False
logmsg("pin:", line)
pinnum = r[4] if r[4][0] == 'P' else lpad(r[4], patt['pad'])
line = r[1] + ' ' + r[3]
line = f'{r[1]} {r[3]}'
line = rpad(line, col_value_lj) + pinnum
if r[5]: line = rpad(line, col_comment) + r[5]
d['line'] = line
Expand All @@ -150,7 +148,7 @@ def tryNoPin(d):
r = noPinPatt.match(line)
if r == None: return False
logmsg("pin -1:", line)
line = r[1] + ' ' + r[3]
line = f'{r[1]} {r[3]}'
line = rpad(line, col_value_lj) + '-1'
if r[5]: line = rpad(line, col_comment) + r[5]
d['line'] = line
Expand All @@ -164,6 +162,14 @@ def trySkip2(d):
logmsg("skip:", d['line'])
return True

#
# #else|endif
#
def trySkip3(d):
if skipPatt3.match( d['line']) == None: return False
logmsg("skip:", d['line'])
return True

#
# #define ALIAS OTHER
#
Expand Down Expand Up @@ -220,7 +226,7 @@ def tryUndef(d):
return True

#
# #if ...
# #if|ifdef|ifndef|elif ...
#
def tryCond(d):
line = d['line']
Expand All @@ -243,18 +249,19 @@ def tryCond(d):
wDict['check_comment_next'] = (r != None)

if wDict['check_comment_next']:
# Comments in column 45
# Comments in column 50
line = rpad('', col_comment) + r[1]

elif trySkip1(wDict): pass #define SKIP_ME
elif tryPindef(wDict): pass #define MY_PIN [pin]
elif tryNoPin(wDict): pass #define MY_PIN -1
elif trySkip2(wDict): pass #define SKIP_ME_TOO
elif trySkip3(wDict): pass #else|endif
elif tryAlias(wDict): pass #define ALIAS OTHER
elif trySwitch(wDict): pass #define SWITCH
elif tryDef(wDict): pass #define ...
elif tryUndef(wDict): pass #undef ...
elif tryCond(wDict): pass #if ...
elif tryCond(wDict): pass #if|ifdef|ifndef|elif ...

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

Expand Down

0 comments on commit 3b6f1bf

Please sign in to comment.