Skip to content

Commit 532848f

Browse files
Support conditional dependencies.
Extend the "depends on" syntax to support conditional dependencies using "... if <B>". While functionally equivalent to "depends on !B || A", "depends on A if B" is much more readable. This change is implemented by converting the "if" syntax into the "!B" syntax during "depends on" token processing. Signed-off-by: Graham Roff <[email protected]>
1 parent 228d0b9 commit 532848f

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

kconfiglib.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3117,6 +3117,36 @@ def _parse_cond(self):
31173117

31183118
return expr
31193119

3120+
def _parse_depends(self):
3121+
# Parses conditional dependencies with syntax "depends on <expr> if <condition>"
3122+
# Returns the appropriate expression that represents the conditional dependency
3123+
3124+
# Parse the main dependency expression
3125+
main_expr = self._parse_expr(True)
3126+
3127+
# Check if there's an optional "if <condition>" clause
3128+
if self._check_token(_T_IF):
3129+
# Parse the condition expression
3130+
condition = self._parse_expr(True)
3131+
3132+
# Check for end of line
3133+
if self._tokens[self._tokens_i] is not None:
3134+
self._trailing_tokens_error()
3135+
3136+
# Create conditional dependency: "depends on A if B" becomes "!B || A"
3137+
# This means: if B is false, the dependency is satisfied (y)
3138+
# if B is true, the dependency becomes A
3139+
return self._make_or(
3140+
(NOT, condition),
3141+
main_expr
3142+
)
3143+
else:
3144+
# No conditional clause, just return the main expression
3145+
if self._tokens[self._tokens_i] is not None:
3146+
self._trailing_tokens_error()
3147+
3148+
return main_expr
3149+
31203150
def _parse_props(self, node):
31213151
# Parses and adds properties to the MenuNode 'node' (type, 'prompt',
31223152
# 'default's, etc.) Properties are later copied up to symbols and
@@ -3152,7 +3182,7 @@ def _parse_props(self, node):
31523182
self._parse_error("expected 'on' after 'depends'")
31533183

31543184
node.dep = self._make_and(node.dep,
3155-
self._expect_expr_and_eol())
3185+
self._parse_depends())
31563186

31573187
elif t0 is _T_HELP:
31583188
self._parse_help(node)

0 commit comments

Comments
 (0)