Debian Codegraph @

crazy_complete/zsh_when.py

raw
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 # Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]>
3
4 '''Module for generating conditional code in Zsh.'''
5
6 from . import when
7 from . import shell
8
9
10 def _generate(ctxt, query, tokens):
11 '''Turn tokens/objects into condition code.'''
12
13 r = []
14
15 for obj in tokens:
16 if isinstance(obj, when.OptionIs):
17 r.append(_generate_option_is(ctxt, query, obj))
18
19 elif isinstance(obj, when.OptionMatch):
20 r.append(_generate_option_match(ctxt, query, obj))
21
22 elif isinstance(obj, when.HasOption):
23 r.append(_generate_has_option(ctxt, query, obj))
24
25 elif isinstance(obj, when.PositionalCount):
26 r.append(_generate_positional_count(ctxt, query, obj))
27
28 elif isinstance(obj, when.PositionalContains):
29 r.append(_generate_positional_contains(ctxt, query, obj))
30
31 elif isinstance(obj, when.PositionalMatch):
32 r.append(_generate_positional_match(ctxt, query, obj))
33
34 elif obj in ('&&', '||', '!'):
35 r.append(obj)
36
37 elif obj == '(':
38 r.append('{')
39
40 elif obj == ')':
41 r.append(';}')
42
43 else:
44 raise AssertionError("Not reached")
45
46 if when.needs_braces(tokens):
47 return '{ %s; }' % ' '.join(r)
48
49 return ' '.join(r)
50
51
52 def _generate_option_is(ctxt, query, obj):
53 func = query.use('option_is')
54 args = ['--', *obj.options, '--', *obj.values]
55
56 if obj.any:
57 query.use('any')
58 args.insert(0, '-a')
59
60 if obj.ignore_case:
61 query.use('nocase')
62 args.insert(0, '-i')
63
64 return shell.join_quoted([func, 'option_is'] + args)
65
66
67 def _generate_option_match(ctxt, query, obj):
68 query.use() # option_match also needs query!
69 func = ctxt.helpers.use_function('option_match')
70 args = ['--', *obj.options, '--', obj.regex]
71
72 if obj.any:
73 ctxt.helpers.use_function('option_match', 'any')
74 args.insert(0, '-a')
75
76 if obj.ignore_case:
77 ctxt.helpers.use_function('option_match', 'nocase')
78 args.insert(0, '-i')
79
80 return shell.join_quoted([func] + args)
81
82
83 def _generate_has_option(_ctxt, query, obj):
84 func = query.use('has_option')
85 args = [func, 'has_option', *obj.options]
86 return shell.join_quoted(args)
87
88
89 def _generate_positional_count(_ctxt, _query, obj):
90 return f'(( ${{#POSITIONALS[@]}} {obj.operator} {obj.number} ))'
91
92
93 def _generate_positional_contains(ctxt, _query, obj):
94 func = ctxt.helpers.use_function('array_contains')
95 values = shell.join_quoted(obj.values)
96
97 if obj.ignore_case:
98 return f'{func} "${{(L)POSITIONALS[{obj.number}]}}" {values.lower()}'
99 else:
100 return f'{func} "${{POSITIONALS[{obj.number}]}}" {values}'
101
102
103 def _generate_positional_match(_ctxt, _query, obj):
104 index = obj.number
105 regex = shell.quote(obj.regex)
106
107 if obj.ignore_case:
108 return f'[[ "${{(L)POSITIONALS[{index}]}}" =~ {regex.lower()} ]]'
109 else:
110 return f'[[ "${{POSITIONALS[{index}]}}" =~ {regex} ]]'
111
112
113 def generate_when_conditions(ctxt, query, when_):
114 '''Generate when condition code.'''
115
116 tokens = when.parse_when(when_)
117 return _generate(ctxt, query, tokens)