Skip to content

Commit a2d13d2

Browse files
committed
refactor: Separate condition categories in language readers for improved structure
This commit refactors the condition handling in the CodeReader and its subclasses by introducing distinct sets for control flow keywords, logical operators, case keywords, and ternary operators. The changes enhance clarity and maintain backward compatibility by allowing subclasses to define their own conditions while utilizing a new structured approach. Additionally, the LizardExtension is updated to remove logical operators from conditions using the new semantic structure.
1 parent 0fffbfb commit a2d13d2

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

lizard_ext/lizardnonstrict.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ class LizardExtension(object): # pylint: disable=R0903
99

1010
# pylint: disable=W0221
1111
def __call__(self, tokens, reader):
12-
reader.conditions -= set(['&&', '||', 'and', 'or'])
12+
# Remove logical operators from conditions (non-strict mode)
13+
# Uses semantic logical_operators instead of hardcoded list
14+
reader.conditions -= reader.logical_operators
1315
return tokens

lizard_languages/code_reader.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,45 @@ class CodeReader:
9494
ext = []
9595
languages = None
9696
extra_subclasses = set()
97-
_conditions = {'if', 'for', 'while', '&&', '||', '?', 'catch', 'case'}
97+
98+
# Separated condition categories (new structure)
99+
_control_flow_keywords = {'if', 'for', 'while', 'catch'}
100+
_logical_operators = {'&&', '||'}
101+
_case_keywords = {'case'}
102+
_ternary_operators = {'?'}
103+
104+
# Backward compatibility: old combined set
105+
# If a subclass defines only _conditions, it will be used
106+
_conditions = None
107+
108+
@classmethod
109+
def _build_conditions(cls):
110+
"""Build combined conditions set from separated categories.
111+
112+
Returns combined set of all condition types for backward compatibility
113+
and default behavior.
114+
"""
115+
return (cls._control_flow_keywords |
116+
cls._logical_operators |
117+
cls._case_keywords |
118+
cls._ternary_operators)
98119

99120
def __init__(self, context):
100121
self.parallel_states = []
101122
self.context = context
102-
self.conditions = copy(self._conditions)
123+
124+
# Backward compatibility: if subclass defines _conditions, use it
125+
if self.__class__._conditions is not None:
126+
self.conditions = copy(self.__class__._conditions)
127+
else:
128+
# Use new separated structure
129+
self.conditions = copy(self.__class__._build_conditions())
130+
131+
# Expose individual sets for extensions to use
132+
self.control_flow_keywords = copy(self.__class__._control_flow_keywords)
133+
self.logical_operators = copy(self.__class__._logical_operators)
134+
self.case_keywords = copy(self.__class__._case_keywords)
135+
self.ternary_operators = copy(self.__class__._ternary_operators)
103136

104137
@classmethod
105138
def match_filename(cls, filename):

lizard_languages/csharp.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ class CSharpReader(CLikeReader):
1111
ext = ['cs']
1212
language_names = ['csharp']
1313

14-
_conditions = set(['if', 'for', 'while', '&&', '||', '?', 'catch',
15-
'case', '??'])
14+
# Separated condition categories
15+
_control_flow_keywords = {'if', 'for', 'while', 'catch'}
16+
_logical_operators = {'&&', '||'}
17+
_case_keywords = {'case'}
18+
_ternary_operators = {'?', '??'} # C# has both ?: and ?? (null-coalescing)
1619

1720
def __init__(self, context):
1821
super(CSharpReader, self).__init__(context)

lizard_languages/erlang.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ class ErlangReader(CodeReader):
1313

1414
ext = ['erl', 'hrl', 'es', 'escript']
1515
language_names = ['erlang']
16-
_conditions = {'and', 'case', 'catch', 'if', 'not', 'or', '?', 'when'}
16+
17+
# Separated condition categories
18+
_control_flow_keywords = {'if', 'catch', 'when'} # when is used in guards
19+
_logical_operators = {'and', 'or', 'not'}
20+
_case_keywords = {'case'}
21+
# Note: '?' in Erlang is a macro operator, not ternary, but adds to CCN
22+
_ternary_operators = {'?'}
1723

1824
def __init__(self, context):
1925
super(ErlangReader, self).__init__(context)

0 commit comments

Comments
 (0)