diff --git a/buildroot/share/scripts/pinsformat.js b/buildroot/share/scripts/pinsformat.js index 89e99b73bd..41bdfdab87 100755 --- a/buildroot/share/scripts/pinsformat.js +++ b/buildroot/share/scripts/pinsformat.js @@ -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; @@ -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; @@ -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]; } @@ -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); @@ -131,7 +136,7 @@ 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]); @@ -139,14 +144,15 @@ function process_text(txt) { 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]); @@ -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; @@ -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]; @@ -173,7 +179,7 @@ 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]); } @@ -181,7 +187,7 @@ function process_text(txt) { // // #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; } diff --git a/buildroot/share/scripts/pinsformat.py b/buildroot/share/scripts/pinsformat.py index 3a376ecc7e..b49ae4931d 100755 --- a/buildroot/share/scripts/pinsformat.py +++ b/buildroot/share/scripts/pinsformat.py @@ -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) @@ -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') @@ -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) @@ -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 @@ -136,7 +134,7 @@ def process_text(txt): 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 @@ -150,7 +148,7 @@ def process_text(txt): 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 @@ -164,6 +162,14 @@ def process_text(txt): 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 # @@ -220,7 +226,7 @@ def process_text(txt): return True # - # #if ... + # #if|ifdef|ifndef|elif ... # def tryCond(d): line = d['line'] @@ -243,18 +249,19 @@ def process_text(txt): 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'