@@ -17,15 +17,13 @@ def logmsg(msg, line):
17
17
col_comment = 50
18
18
19
19
# String lpad / rpad
20
- def lpad (astr , fill , c = None ):
20
+ def lpad (astr , fill , c = ' ' ):
21
21
if not fill : return astr
22
- if c == None : c = ' '
23
22
need = fill - len (astr )
24
23
return astr if need <= 0 else (need * c ) + astr
25
24
26
- def rpad (astr , fill , c = None ):
25
+ def rpad (astr , fill , c = ' ' ):
27
26
if not fill : return astr
28
- if c == None : c = ' '
29
27
need = fill - len (astr )
30
28
return astr if need <= 0 else astr + (need * c )
31
29
@@ -64,9 +62,8 @@ def format_pins(argv):
64
62
# If no source file specified read from STDIN
65
63
file_text = sys .stdin .read ()
66
64
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 ()
70
67
71
68
if len (file_text ) == 0 :
72
69
print ('No text to process' )
@@ -75,8 +72,7 @@ def format_pins(argv):
75
72
# Read from file or STDIN until it terminates
76
73
filtered = process_text (file_text )
77
74
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 )
80
76
else :
81
77
print (filtered )
82
78
@@ -105,16 +101,18 @@ def process_text(txt):
105
101
patt = get_pin_pattern (txt )
106
102
if patt == None : return txt
107
103
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*(//.*)?$' )
113
111
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,}(//.*)?$' )
118
116
119
117
col_value_lj = col_comment - patt ['pad' ] - 2
120
118
col_value_rj = col_comment - 3
@@ -136,7 +134,7 @@ def tryPindef(d):
136
134
if r == None : return False
137
135
logmsg ("pin:" , line )
138
136
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 ]} '
140
138
line = rpad (line , col_value_lj ) + pinnum
141
139
if r [5 ]: line = rpad (line , col_comment ) + r [5 ]
142
140
d ['line' ] = line
@@ -150,7 +148,7 @@ def tryNoPin(d):
150
148
r = noPinPatt .match (line )
151
149
if r == None : return False
152
150
logmsg ("pin -1:" , line )
153
- line = r [1 ] + ' ' + r [3 ]
151
+ line = f' { r [1 ]} { r [3 ]} '
154
152
line = rpad (line , col_value_lj ) + '-1'
155
153
if r [5 ]: line = rpad (line , col_comment ) + r [5 ]
156
154
d ['line' ] = line
@@ -164,6 +162,14 @@ def trySkip2(d):
164
162
logmsg ("skip:" , d ['line' ])
165
163
return True
166
164
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
+
167
173
#
168
174
# #define ALIAS OTHER
169
175
#
@@ -220,7 +226,7 @@ def tryUndef(d):
220
226
return True
221
227
222
228
#
223
- # #if ...
229
+ # #if|ifdef|ifndef|elif ...
224
230
#
225
231
def tryCond (d ):
226
232
line = d ['line' ]
@@ -243,18 +249,19 @@ def tryCond(d):
243
249
wDict ['check_comment_next' ] = (r != None )
244
250
245
251
if wDict ['check_comment_next' ]:
246
- # Comments in column 45
252
+ # Comments in column 50
247
253
line = rpad ('' , col_comment ) + r [1 ]
248
254
249
255
elif trySkip1 (wDict ): pass #define SKIP_ME
250
256
elif tryPindef (wDict ): pass #define MY_PIN [pin]
251
257
elif tryNoPin (wDict ): pass #define MY_PIN -1
252
258
elif trySkip2 (wDict ): pass #define SKIP_ME_TOO
259
+ elif trySkip3 (wDict ): pass #else|endif
253
260
elif tryAlias (wDict ): pass #define ALIAS OTHER
254
261
elif trySwitch (wDict ): pass #define SWITCH
255
262
elif tryDef (wDict ): pass #define ...
256
263
elif tryUndef (wDict ): pass #undef ...
257
- elif tryCond (wDict ): pass #if ...
264
+ elif tryCond (wDict ): pass #if|ifdef|ifndef|elif ...
258
265
259
266
out += wDict ['line' ] + '\n '
260
267
0 commit comments