🧑💻 Update pinsformat
This commit is contained in:
@@ -10,6 +10,11 @@
|
|||||||
|
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
|
||||||
|
var do_log = false
|
||||||
|
function logmsg(msg, line='') {
|
||||||
|
if (do_log) console.log(msg, line);
|
||||||
|
}
|
||||||
|
|
||||||
// String lpad / rpad
|
// String lpad / rpad
|
||||||
String.prototype.lpad = function(len, chr) {
|
String.prototype.lpad = function(len, chr) {
|
||||||
if (!len) return this;
|
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' ],
|
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 ],
|
ppad = [ 3, 4, 5, 5 ],
|
||||||
col_comment = 50,
|
col_comment = 50,
|
||||||
col_value_rj = col_comment - 3;
|
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;
|
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) {
|
if (argc > 0) {
|
||||||
let ind = 0;
|
let ind = 0;
|
||||||
if (argv[0] == '-v') { do_log = true; ind++; }
|
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];
|
if (ind < argc) dst_file = argv[ind];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,13 +120,13 @@ function process_text(txt) {
|
|||||||
//
|
//
|
||||||
// #define SKIP_ME
|
// #define SKIP_ME
|
||||||
//
|
//
|
||||||
if (do_log) console.log("skip:", line);
|
logmsg("skip:", line);
|
||||||
}
|
}
|
||||||
else if ((r = pindefPatt.exec(line)) !== null) {
|
else if ((r = pindefPatt.exec(line)) !== null) {
|
||||||
//
|
//
|
||||||
// #define MY_PIN [pin]
|
// #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);
|
const pinnum = r[4].charAt(0) == 'P' ? r[4] : r[4].lpad(patt.pad);
|
||||||
line = r[1] + ' ' + r[3];
|
line = r[1] + ' ' + r[3];
|
||||||
line = line.rpad(col_value_lj).concat_with_space(pinnum);
|
line = line.rpad(col_value_lj).concat_with_space(pinnum);
|
||||||
@@ -131,7 +136,7 @@ function process_text(txt) {
|
|||||||
//
|
//
|
||||||
// #define MY_PIN -1
|
// #define MY_PIN -1
|
||||||
//
|
//
|
||||||
if (do_log) console.log("pin -1:", line);
|
logmsg("pin -1:", line);
|
||||||
line = r[1] + ' ' + r[3];
|
line = r[1] + ' ' + r[3];
|
||||||
line = line.rpad(col_value_lj).concat_with_space('-1');
|
line = line.rpad(col_value_lj).concat_with_space('-1');
|
||||||
if (r[5]) line = line.rpad(col_comment).concat_with_space(r[5]);
|
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) {
|
else if (skipPatt2.exec(line) !== null || skipPatt3.exec(line) !== null) {
|
||||||
//
|
//
|
||||||
// #define SKIP_ME
|
// #define SKIP_ME
|
||||||
|
// #else, #endif
|
||||||
//
|
//
|
||||||
if (do_log) console.log("skip:", line);
|
logmsg("skip:", line);
|
||||||
}
|
}
|
||||||
else if ((r = aliasPatt.exec(line)) !== null) {
|
else if ((r = aliasPatt.exec(line)) !== null) {
|
||||||
//
|
//
|
||||||
// #define ALIAS OTHER
|
// #define ALIAS OTHER
|
||||||
//
|
//
|
||||||
if (do_log) console.log("alias:", line);
|
logmsg("alias:", line);
|
||||||
line = r[1] + ' ' + r[3];
|
line = r[1] + ' ' + r[3];
|
||||||
line = line.concat_with_space(r[4].lpad(col_value_rj + 1 - line.length));
|
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]);
|
if (r[5]) line = line.rpad(col_comment).concat_with_space(r[5]);
|
||||||
@@ -155,7 +161,7 @@ function process_text(txt) {
|
|||||||
//
|
//
|
||||||
// #define SWITCH
|
// #define SWITCH
|
||||||
//
|
//
|
||||||
if (do_log) console.log("switch:", line);
|
logmsg("switch:", line);
|
||||||
line = r[1] + ' ' + r[3];
|
line = r[1] + ' ' + r[3];
|
||||||
if (r[4]) line = line.rpad(col_comment).concat_with_space(r[4]);
|
if (r[4]) line = line.rpad(col_comment).concat_with_space(r[4]);
|
||||||
check_comment_next = true;
|
check_comment_next = true;
|
||||||
@@ -164,7 +170,7 @@ function process_text(txt) {
|
|||||||
//
|
//
|
||||||
// #define ...
|
// #define ...
|
||||||
//
|
//
|
||||||
if (do_log) console.log("def:", line);
|
logmsg("def:", line);
|
||||||
line = r[1] + ' ' + r[3] + ' ';
|
line = r[1] + ' ' + r[3] + ' ';
|
||||||
line = line.concat_with_space(r[4].lpad(col_value_rj + 1 - line.length));
|
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];
|
if (r[5]) line = line.rpad(col_comment - 1) + ' ' + r[5];
|
||||||
@@ -173,7 +179,7 @@ function process_text(txt) {
|
|||||||
//
|
//
|
||||||
// #undef ...
|
// #undef ...
|
||||||
//
|
//
|
||||||
if (do_log) console.log("undef:", line);
|
logmsg("undef:", line);
|
||||||
line = r[1] + ' ' + r[3];
|
line = r[1] + ' ' + r[3];
|
||||||
if (r[4]) line = line.rpad(col_comment).concat_with_space(r[4]);
|
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, #ifdef, #ifndef, #elif ...
|
||||||
//
|
//
|
||||||
if (do_log) console.log("cond:", line);
|
logmsg("cond:", line);
|
||||||
line = r[1].rpad(col_comment).concat_with_space(r[5]);
|
line = r[1].rpad(col_comment).concat_with_space(r[5]);
|
||||||
check_comment_next = true;
|
check_comment_next = true;
|
||||||
}
|
}
|
||||||
|
@@ -17,15 +17,13 @@ def logmsg(msg, line):
|
|||||||
col_comment = 50
|
col_comment = 50
|
||||||
|
|
||||||
# String lpad / rpad
|
# String lpad / rpad
|
||||||
def lpad(astr, fill, c=None):
|
def lpad(astr, fill, c=' '):
|
||||||
if not fill: return astr
|
if not fill: return astr
|
||||||
if c == None: c = ' '
|
|
||||||
need = fill - len(astr)
|
need = fill - len(astr)
|
||||||
return astr if need <= 0 else (need * c) + 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 not fill: return astr
|
||||||
if c == None: c = ' '
|
|
||||||
need = fill - len(astr)
|
need = fill - len(astr)
|
||||||
return astr if need <= 0 else astr + (need * c)
|
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
|
# If no source file specified read from STDIN
|
||||||
file_text = sys.stdin.read()
|
file_text = sys.stdin.read()
|
||||||
else:
|
else:
|
||||||
# Open the file src_file
|
# Open and read the file src_file
|
||||||
with open(src_file, 'r') as rf:
|
with open(src_file, 'r') as rf: file_text = rf.read()
|
||||||
file_text = rf.read()
|
|
||||||
|
|
||||||
if len(file_text) == 0:
|
if len(file_text) == 0:
|
||||||
print('No text to process')
|
print('No text to process')
|
||||||
@@ -75,8 +72,7 @@ def format_pins(argv):
|
|||||||
# Read from file or STDIN until it terminates
|
# Read from file or STDIN until it terminates
|
||||||
filtered = process_text(file_text)
|
filtered = process_text(file_text)
|
||||||
if dst_file:
|
if dst_file:
|
||||||
with open(dst_file, 'w') as wf:
|
with open(dst_file, 'w') as wf: wf.write(filtered)
|
||||||
wf.write(filtered)
|
|
||||||
else:
|
else:
|
||||||
print(filtered)
|
print(filtered)
|
||||||
|
|
||||||
@@ -105,16 +101,18 @@ def process_text(txt):
|
|||||||
patt = get_pin_pattern(txt)
|
patt = get_pin_pattern(txt)
|
||||||
if patt == None: return txt
|
if patt == None: return txt
|
||||||
|
|
||||||
pindefPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+(' + patt['match'] + r')\s*(//.*)?$')
|
pmatch = patt['match']
|
||||||
noPinPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+(-1)\s*(//.*)?$')
|
pindefPatt = re.compile(rf'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+({pmatch})\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*(//.*)?$')
|
noPinPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+(-1)\s*(//.*)?$')
|
||||||
skipPatt2 = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+(0x[0-9A-Fa-f]+|\d+|.+[a-z].+)\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*(//.*)?$')
|
||||||
aliasPatt = re.compile(r'^(\s*(//)?#define)\s+([A-Z_][A-Z0-9_]+)\s+([A-Z_][A-Z0-9_()]+)\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*(//.*)?$')
|
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*(//.*)?$')
|
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*(//.*)?$')
|
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+(//.*)$')
|
condPatt = re.compile(r'^(\s*(//)?#(if|ifn?def|elif)(\s+\S+)*)\s+(//.*)$')
|
||||||
commPatt = re.compile(r'^\s{20,}(//.*)?$')
|
commPatt = re.compile(r'^\s{20,}(//.*)?$')
|
||||||
|
|
||||||
col_value_lj = col_comment - patt['pad'] - 2
|
col_value_lj = col_comment - patt['pad'] - 2
|
||||||
col_value_rj = col_comment - 3
|
col_value_rj = col_comment - 3
|
||||||
@@ -136,7 +134,7 @@ def process_text(txt):
|
|||||||
if r == None: return False
|
if r == None: return False
|
||||||
logmsg("pin:", line)
|
logmsg("pin:", line)
|
||||||
pinnum = r[4] if r[4][0] == 'P' else lpad(r[4], patt['pad'])
|
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
|
line = rpad(line, col_value_lj) + pinnum
|
||||||
if r[5]: line = rpad(line, col_comment) + r[5]
|
if r[5]: line = rpad(line, col_comment) + r[5]
|
||||||
d['line'] = line
|
d['line'] = line
|
||||||
@@ -150,7 +148,7 @@ def process_text(txt):
|
|||||||
r = noPinPatt.match(line)
|
r = noPinPatt.match(line)
|
||||||
if r == None: return False
|
if r == None: return False
|
||||||
logmsg("pin -1:", line)
|
logmsg("pin -1:", line)
|
||||||
line = r[1] + ' ' + r[3]
|
line = f'{r[1]} {r[3]}'
|
||||||
line = rpad(line, col_value_lj) + '-1'
|
line = rpad(line, col_value_lj) + '-1'
|
||||||
if r[5]: line = rpad(line, col_comment) + r[5]
|
if r[5]: line = rpad(line, col_comment) + r[5]
|
||||||
d['line'] = line
|
d['line'] = line
|
||||||
@@ -164,6 +162,14 @@ def process_text(txt):
|
|||||||
logmsg("skip:", d['line'])
|
logmsg("skip:", d['line'])
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
#
|
||||||
|
# #else|endif
|
||||||
|
#
|
||||||
|
def trySkip3(d):
|
||||||
|
if skipPatt3.match( d['line']) == None: return False
|
||||||
|
logmsg("skip:", d['line'])
|
||||||
|
return True
|
||||||
|
|
||||||
#
|
#
|
||||||
# #define ALIAS OTHER
|
# #define ALIAS OTHER
|
||||||
#
|
#
|
||||||
@@ -220,7 +226,7 @@ def process_text(txt):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
#
|
#
|
||||||
# #if ...
|
# #if|ifdef|ifndef|elif ...
|
||||||
#
|
#
|
||||||
def tryCond(d):
|
def tryCond(d):
|
||||||
line = d['line']
|
line = d['line']
|
||||||
@@ -243,18 +249,19 @@ def process_text(txt):
|
|||||||
wDict['check_comment_next'] = (r != None)
|
wDict['check_comment_next'] = (r != None)
|
||||||
|
|
||||||
if wDict['check_comment_next']:
|
if wDict['check_comment_next']:
|
||||||
# Comments in column 45
|
# Comments in column 50
|
||||||
line = rpad('', col_comment) + r[1]
|
line = rpad('', col_comment) + r[1]
|
||||||
|
|
||||||
elif trySkip1(wDict): pass #define SKIP_ME
|
elif trySkip1(wDict): pass #define SKIP_ME
|
||||||
elif tryPindef(wDict): pass #define MY_PIN [pin]
|
elif tryPindef(wDict): pass #define MY_PIN [pin]
|
||||||
elif tryNoPin(wDict): pass #define MY_PIN -1
|
elif tryNoPin(wDict): pass #define MY_PIN -1
|
||||||
elif trySkip2(wDict): pass #define SKIP_ME_TOO
|
elif trySkip2(wDict): pass #define SKIP_ME_TOO
|
||||||
|
elif trySkip3(wDict): pass #else|endif
|
||||||
elif tryAlias(wDict): pass #define ALIAS OTHER
|
elif tryAlias(wDict): pass #define ALIAS OTHER
|
||||||
elif trySwitch(wDict): pass #define SWITCH
|
elif trySwitch(wDict): pass #define SWITCH
|
||||||
elif tryDef(wDict): pass #define ...
|
elif tryDef(wDict): pass #define ...
|
||||||
elif tryUndef(wDict): pass #undef ...
|
elif tryUndef(wDict): pass #undef ...
|
||||||
elif tryCond(wDict): pass #if ...
|
elif tryCond(wDict): pass #if|ifdef|ifndef|elif ...
|
||||||
|
|
||||||
out += wDict['line'] + '\n'
|
out += wDict['line'] + '\n'
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user