@@ -1176,10 +1176,7 @@ def find_word_in_code_line(
1176
1176
return line_no , word_range
1177
1177
1178
1178
def preprocess (
1179
- self ,
1180
- pp_defs : dict = None ,
1181
- include_dirs : set = None ,
1182
- debug : bool = False ,
1179
+ self , pp_defs : dict = None , include_dirs : set = None , debug : bool = False
1183
1180
) -> tuple [list , list ]:
1184
1181
if pp_defs is None :
1185
1182
pp_defs = {}
@@ -1268,9 +1265,7 @@ def parse(
1268
1265
if self .preproc :
1269
1266
log .debug ("=== PreProc Pass ===\n " )
1270
1267
pp_skips , pp_defines = self .preprocess (
1271
- pp_defs = pp_defs ,
1272
- include_dirs = include_dirs ,
1273
- debug = debug ,
1268
+ pp_defs = pp_defs , include_dirs = include_dirs , debug = debug
1274
1269
)
1275
1270
for pp_reg in pp_skips :
1276
1271
file_ast .start_ppif (pp_reg [0 ])
@@ -2043,7 +2038,6 @@ def replace_ops(expr: str):
2043
2038
expr = expr .replace ("!=" , " <> " )
2044
2039
expr = expr .replace ("!" , " not " )
2045
2040
expr = expr .replace (" <> " , " != " )
2046
-
2047
2041
return expr
2048
2042
2049
2043
def replace_defined (line : str ):
@@ -2076,9 +2070,7 @@ def replace_vars(line: str):
2076
2070
2077
2071
if defs is None :
2078
2072
defs = {}
2079
-
2080
- out_line = text
2081
- out_line = replace_defined (out_line )
2073
+ out_line = replace_defined (text )
2082
2074
out_line = replace_vars (out_line )
2083
2075
try :
2084
2076
line_res = eval (replace_ops (out_line ))
@@ -2106,27 +2098,30 @@ def replace_vars(line: str):
2106
2098
if def_cont_name is not None :
2107
2099
output_file .append ("" )
2108
2100
if line .rstrip ()[- 1 ] != "\\ " :
2109
- append_multiline_macro (defs_tmp , def_cont_name , line .strip ())
2101
+ defs_tmp [def_cont_name ] = append_multiline_macro (
2102
+ defs_tmp [def_cont_name ], line .strip ()
2103
+ )
2110
2104
def_cont_name = None
2111
2105
else :
2112
- append_multiline_macro (defs_tmp , def_cont_name , line [0 :- 1 ].strip ())
2113
-
2106
+ defs_tmp [def_cont_name ] = append_multiline_macro (
2107
+ defs_tmp [def_cont_name ], line [0 :- 1 ].strip ()
2108
+ )
2114
2109
continue
2115
2110
# Handle conditional statements
2116
2111
match = FRegex .PP_REGEX .match (line )
2117
- if match and check_pp_prefix ( match . group ( 1 )) :
2112
+ if match :
2118
2113
output_file .append (line )
2119
2114
def_name = None
2120
2115
if_start = False
2121
2116
# Opening conditional statements
2122
- if match .group (2 ).lower () == "if " :
2123
- is_path = eval_pp_if (line [match .end (2 ) :], defs_tmp )
2117
+ if match .group (1 ).lower () == "if " :
2118
+ is_path = eval_pp_if (line [match .end (1 ) :], defs_tmp )
2124
2119
if_start = True
2125
- elif match .group (2 ).lower () == "ifdef" :
2120
+ elif match .group (1 ).lower () == "ifdef" :
2126
2121
if_start = True
2127
2122
def_name = line [match .end (0 ) :].strip ()
2128
2123
is_path = def_name in defs_tmp
2129
- elif match .group (2 ).lower () == "ifndef" :
2124
+ elif match .group (1 ).lower () == "ifndef" :
2130
2125
if_start = True
2131
2126
def_name = line [match .end (0 ) :].strip ()
2132
2127
is_path = not (def_name in defs_tmp )
@@ -2144,7 +2139,7 @@ def replace_vars(line: str):
2144
2139
inc_start = False
2145
2140
exc_start = False
2146
2141
exc_continue = False
2147
- if match .group (2 ).lower () == "elif" :
2142
+ if match .group (1 ).lower () == "elif" :
2148
2143
if (not pp_stack_group ) or (pp_stack_group [- 1 ][0 ] != len (pp_stack )):
2149
2144
# First elif statement for this elif group
2150
2145
if pp_stack [- 1 ][0 ] < 0 :
@@ -2156,15 +2151,15 @@ def replace_vars(line: str):
2156
2151
exc_continue = True
2157
2152
if pp_stack [- 1 ][0 ] < 0 :
2158
2153
pp_stack [- 1 ][0 ] = i + 1
2159
- elif eval_pp_if (line [match .end (2 ) :], defs_tmp ):
2154
+ elif eval_pp_if (line [match .end (1 ) :], defs_tmp ):
2160
2155
pp_stack [- 1 ][1 ] = i + 1
2161
2156
pp_skips .append (pp_stack .pop ())
2162
2157
pp_stack_group [- 1 ][1 ] = True
2163
2158
pp_stack .append ([- 1 , - 1 ])
2164
2159
inc_start = True
2165
2160
else :
2166
2161
exc_start = True
2167
- elif match .group (2 ).lower () == "else" :
2162
+ elif match .group (1 ).lower () == "else" :
2168
2163
if pp_stack [- 1 ][0 ] < 0 :
2169
2164
pp_stack [- 1 ][0 ] = i + 1
2170
2165
exc_start = True
@@ -2180,7 +2175,7 @@ def replace_vars(line: str):
2180
2175
pp_skips .append (pp_stack .pop ())
2181
2176
pp_stack .append ([- 1 , - 1 ])
2182
2177
inc_start = True
2183
- elif match .group (2 ).lower () == "endif" :
2178
+ elif match .group (1 ).lower () == "endif" :
2184
2179
if pp_stack_group and (pp_stack_group [- 1 ][0 ] == len (pp_stack )):
2185
2180
pp_stack_group .pop ()
2186
2181
if pp_stack [- 1 ][0 ] < 0 :
@@ -2201,12 +2196,10 @@ def replace_vars(line: str):
2201
2196
continue
2202
2197
# Handle variable/macro definitions files
2203
2198
match = FRegex .PP_DEF .match (line )
2204
- if (match is not None and check_pp_prefix (match .group (1 ))) and (
2205
- (len (pp_stack ) == 0 ) or (pp_stack [- 1 ][0 ] < 0 )
2206
- ):
2199
+ if (match is not None ) and ((len (pp_stack ) == 0 ) or (pp_stack [- 1 ][0 ] < 0 )):
2207
2200
output_file .append (line )
2208
2201
pp_defines .append (i + 1 )
2209
- def_name = match .group (3 )
2202
+ def_name = match .group (2 )
2210
2203
# If this is an argument list of a function add them to the name
2211
2204
# get_definition will only return the function name upon hover
2212
2205
# hence if the argument list is appended in the def_name then
@@ -2215,11 +2208,8 @@ def replace_vars(line: str):
2215
2208
# This also does not allow for multiline argument list definitions.
2216
2209
# if match.group(3):
2217
2210
# def_name += match.group(3)
2218
- if (match .group (2 ) == "define" ) and (def_name not in defs_tmp ):
2211
+ if (match .group (1 ) == "define" ) and (def_name not in defs_tmp ):
2219
2212
eq_ind = line [match .end (0 ) :].find (" " )
2220
- if eq_ind < 0 :
2221
- eq_ind = line [match .end (0 ) :].find ("\t " )
2222
-
2223
2213
if eq_ind >= 0 :
2224
2214
# Handle multiline macros
2225
2215
if line .rstrip ()[- 1 ] == "\\ " :
@@ -2231,11 +2221,11 @@ def replace_vars(line: str):
2231
2221
def_value = "True"
2232
2222
2233
2223
# are there arguments to parse?
2234
- if match .group (4 ):
2235
- def_value = (match .group (5 ), def_value )
2224
+ if match .group (3 ):
2225
+ def_value = (match .group (4 ), def_value )
2236
2226
2237
2227
defs_tmp [def_name ] = def_value
2238
- elif (match .group (2 ) == "undef" ) and (def_name in defs_tmp ):
2228
+ elif (match .group (1 ) == "undef" ) and (def_name in defs_tmp ):
2239
2229
defs_tmp .pop (def_name , None )
2240
2230
log .debug (f"{ line .strip ()} !!! Define statement({ i + 1 } )" )
2241
2231
continue
@@ -2286,10 +2276,9 @@ def replace_vars(line: str):
2286
2276
def_regex = def_regexes .get (def_tmp )
2287
2277
if def_regex is None :
2288
2278
if isinstance (value , tuple ):
2289
- def_regex = expand_def_func_macro (def_tmp , value )
2279
+ def_regex = expand_func_macro (def_tmp , value )
2290
2280
else :
2291
2281
def_regex = re .compile (rf"\b{ def_tmp } \b" )
2292
-
2293
2282
def_regexes [def_tmp ] = def_regex
2294
2283
2295
2284
if isinstance (def_regex , tuple ):
@@ -2305,7 +2294,7 @@ def replace_vars(line: str):
2305
2294
return output_file , pp_skips , pp_defines , defs_tmp
2306
2295
2307
2296
2308
- def expand_def_func_macro (def_name : str , def_value : tuple [str , str ]):
2297
+ def expand_func_macro (def_name : str , def_value : tuple [str , str ]):
2309
2298
def_args , sub = def_value
2310
2299
def_args = def_args .split ("," )
2311
2300
regex = re .compile (rf"\b{ def_name } \s*\({ ',' .join (['(.*)' ]* len (def_args ))} \)" )
@@ -2317,19 +2306,9 @@ def expand_def_func_macro(def_name: str, def_value: tuple[str, str]):
2317
2306
return regex , sub
2318
2307
2319
2308
2320
- def append_multiline_macro (pp_defs : dict , def_name : str , line : str ):
2321
- def_value = pp_defs [def_name ]
2322
- def_args = None
2309
+ def append_multiline_macro (def_value : str | tuple , line : str ):
2323
2310
if isinstance (def_value , tuple ):
2324
2311
def_args , def_value = def_value
2325
-
2326
- def_value += line
2327
-
2328
- if def_args is not None :
2329
- def_value = (def_args , def_value )
2330
-
2331
- pp_defs [def_name ] = def_value
2332
-
2333
-
2334
- def check_pp_prefix (prefix : str ):
2335
- return prefix == "#"
2312
+ def_value += line
2313
+ return (def_args , def_value )
2314
+ return def_value + line
0 commit comments