lizard 1.17.30__py2.py3-none-any.whl → 1.18.0__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
lizard_languages/jsx.py DELETED
@@ -1,337 +0,0 @@
1
- '''
2
- Language parser for JSX
3
- '''
4
-
5
- from .javascript import JavaScriptReader
6
- from .typescript import JSTokenizer, Tokenizer, TypeScriptStates
7
- from .code_reader import CodeReader
8
- from .js_style_regex_expression import js_style_regex_expression
9
- from .js_style_language_states import JavaScriptStyleLanguageStates
10
-
11
-
12
- class JSXTypeScriptStates(TypeScriptStates):
13
- """State machine for JSX/TSX files extending TypeScriptStates"""
14
-
15
- def __init__(self, context):
16
- super().__init__(context)
17
- # Initialize attributes that might be accessed later
18
- self._parent_function_name = None
19
- self.in_variable_declaration = False
20
- self.last_variable_name = None
21
-
22
- def statemachine_before_return(self):
23
- # Ensure the main function is closed at the end
24
- if self.started_function:
25
- self._pop_function_from_stack()
26
- # After popping, if current_function is not *global*, pop again to add to function_list
27
- if self.context.current_function and self.context.current_function.name != "*global*":
28
- self.context.end_of_function()
29
-
30
- def _state_global(self, token):
31
- # Handle variable declarations
32
- if token in ('const', 'let', 'var'):
33
- self.in_variable_declaration = True
34
- super()._state_global(token)
35
- return
36
-
37
- if self.in_variable_declaration:
38
- if token == '=':
39
- # Save the variable name when we see the assignment
40
- self.last_variable_name = self.last_tokens.strip()
41
- super()._state_global(token)
42
- return
43
- elif token == '=>':
44
- # We're in an arrow function with a variable assignment
45
- if self.last_variable_name and not self.started_function:
46
- self.function_name = self.last_variable_name
47
- self._push_function_to_stack()
48
- self.in_variable_declaration = False
49
- # Switch to arrow function state to handle the body
50
- self._state = self._arrow_function
51
- return
52
- elif token == ';' or self.context.newline:
53
- self.in_variable_declaration = False
54
-
55
- # Handle arrow function in JSX/TSX prop context
56
- if token == '=>' and not self.in_variable_declaration:
57
- if not self.started_function:
58
- self.function_name = '(anonymous)'
59
- self._push_function_to_stack()
60
- return
61
-
62
- if not self.as_object:
63
- if token == ':':
64
- self._consume_type_annotation()
65
- return
66
-
67
- # Pop anonymous function after closing '}' in TSX/JSX prop
68
- if token == '}' and self.started_function and self.function_name == '(anonymous)':
69
- self._pop_function_from_stack()
70
-
71
- # Continue with regular TypeScript state handling
72
- super()._state_global(token)
73
-
74
- def _arrow_function(self, token):
75
- self.next(self._state_global, token)
76
-
77
-
78
- class TSXTokenizer(JSTokenizer):
79
- def __init__(self):
80
- super().__init__()
81
-
82
- def process_token(self, token):
83
- if token == "<":
84
- from .jsx import XMLTagWithAttrTokenizer # Import only when needed
85
- self.sub_tokenizer = XMLTagWithAttrTokenizer()
86
- return
87
-
88
- if token == "=>":
89
- # Special handling for arrow functions
90
- yield token
91
- return
92
-
93
- for tok in super().process_token(token):
94
- yield tok
95
-
96
-
97
- class JSXMixin:
98
- '''Base mixin class for JSX/TSX shared functionality'''
99
- @staticmethod
100
- @js_style_regex_expression
101
- def generate_tokens(source_code, addition='', token_class=None):
102
- addition = addition +\
103
- r"|(?:\$\w+)" + \
104
- r"|(?:\<\/\w+\>)" + \
105
- r"|(?:=>)" + \
106
- r"|`.*?`"
107
- js_tokenizer = TSXTokenizer()
108
- for token in CodeReader.generate_tokens(
109
- source_code, addition, token_class):
110
- for tok in js_tokenizer(token):
111
- yield tok
112
-
113
- def _expecting_func_opening_bracket(self, token):
114
- if token == '<':
115
- self.next(self._expecting_jsx)
116
- return
117
- if token == '=>':
118
- # Handle arrow functions in JSX attributes
119
- self._handle_arrow_function()
120
- return
121
- super()._expecting_func_opening_bracket(token)
122
-
123
- def _handle_arrow_function(self):
124
- # Process arrow function in JSX context
125
- self.context.add_to_long_function_name(" => ")
126
-
127
- # Store the current function
128
- current_function = self.context.current_function
129
-
130
- # Create a new anonymous function
131
- self.context.restart_new_function('(anonymous)')
132
-
133
- # Set up for the arrow function body
134
- def callback():
135
- # Return to the original function when done
136
- self.context.current_function = current_function
137
-
138
- self.sub_state(self.__class__(self.context), callback)
139
-
140
- def _expecting_arrow_function_body(self, token):
141
- if token == '{':
142
- # Arrow function with block body
143
- self.next(self._function_body)
144
- else:
145
- # Arrow function with expression body
146
- self.next(self._expecting_func_opening_bracket)
147
-
148
- def _function_body(self, token):
149
- if token == '}':
150
- # End of arrow function body
151
- self.context.end_of_function()
152
- self.next(self._expecting_func_opening_bracket)
153
-
154
- def _expecting_jsx(self, token):
155
- if token == '>':
156
- self.next(self._expecting_func_opening_bracket)
157
-
158
-
159
- class JSXJavaScriptStyleLanguageStates(JavaScriptStyleLanguageStates):
160
- def __init__(self, context):
161
- super(JSXJavaScriptStyleLanguageStates, self).__init__(context)
162
-
163
- def _state_global(self, token):
164
- # Handle variable declarations
165
- if token in ('const', 'let', 'var'):
166
- # Remember that we're in a variable declaration
167
- self.in_variable_declaration = True
168
- super()._state_global(token)
169
- return
170
-
171
- if hasattr(self, 'in_variable_declaration') and self.in_variable_declaration:
172
- if token == '=':
173
- # We're in a variable assignment
174
- self.function_name = self.last_tokens.strip()
175
- super()._state_global(token)
176
- return
177
- elif token == '=>':
178
- # We're in an arrow function with a variable assignment
179
- if not self.started_function and self.function_name:
180
- self._push_function_to_stack()
181
- self._state = self._arrow_function
182
- return
183
- elif token == ';' or self.context.newline:
184
- # End of variable declaration
185
- self.in_variable_declaration = False
186
-
187
- super()._state_global(token)
188
-
189
- def _expecting_func_opening_bracket(self, token):
190
- if token == ':':
191
- # Handle type annotations like TypeScript does
192
- self._consume_type_annotation()
193
- return
194
- super()._expecting_func_opening_bracket(token)
195
-
196
- def _consume_type_annotation(self):
197
- # Skip over type annotations (simplified version of TypeScript's behavior)
198
- def skip_until_terminator(token):
199
- if token in ['{', '=', ';', ')', '(', '=>']:
200
- self.next(self._state_global, token)
201
- return True
202
- return False
203
-
204
- self.next(skip_until_terminator)
205
-
206
-
207
- class JSXReader(JavaScriptReader, JSXMixin):
208
- # pylint: disable=R0903
209
-
210
- ext = ['jsx']
211
- language_names = ['jsx']
212
-
213
- @staticmethod
214
- @js_style_regex_expression
215
- def generate_tokens(source_code, addition='', token_class=None):
216
- # Add support for JSX syntax patterns
217
- addition = addition + \
218
- r"|(?:<[A-Za-z][A-Za-z0-9]*(?:\.[A-Za-z][A-Za-z0-9]*)*>)" + \
219
- r"|(?:<\/[A-Za-z][A-Za-z0-9]*(?:\.[A-Za-z][A-Za-z0-9]*)*>)"
220
- return JSXMixin.generate_tokens(source_code, addition, token_class)
221
-
222
- def __init__(self, context):
223
- super(JSXReader, self).__init__(context)
224
- # Use our JSXTypeScriptStates for better handling of JSX
225
- self.parallel_states = [JSXTypeScriptStates(context)]
226
-
227
-
228
- class XMLTagWithAttrTokenizer(Tokenizer):
229
- def __init__(self):
230
- super(XMLTagWithAttrTokenizer, self).__init__()
231
- self.tag = None
232
- self.state = self._global_state
233
- self.cache = ['<']
234
- self.brace_count = 0 # Track nested braces for complex expressions
235
- self.arrow_function_detected = False # Track if we've detected an arrow function
236
-
237
- def process_token(self, token):
238
- self.cache.append(token)
239
- if not token.isspace():
240
- result = self.state(token)
241
- if result is not None:
242
- if isinstance(result, list):
243
- for tok in result:
244
- yield tok
245
- else:
246
- return result
247
- return ()
248
-
249
- def abort(self):
250
- self.stop()
251
- return self.cache
252
-
253
- def flush(self):
254
- tmp, self.cache = self.cache, []
255
- return [''.join(tmp)]
256
-
257
- def _global_state(self, token):
258
- if not isidentifier(token):
259
- return self.abort()
260
- self.tag = token
261
- self.state = self._after_tag
262
-
263
- def _after_tag(self, token):
264
- if token == '>':
265
- self.state = self._body
266
- elif token == "/":
267
- self.state = self._expecting_self_closing
268
- elif isidentifier(token):
269
- self.state = self._expecting_equal_sign
270
- else:
271
- return self.abort()
272
-
273
- def _expecting_self_closing(self, token):
274
- if token == ">":
275
- self.stop()
276
- return self.flush()
277
- return self.abort()
278
-
279
- def _expecting_equal_sign(self, token):
280
- if token == '=':
281
- self.state = self._expecting_value
282
- else:
283
- return self.abort()
284
-
285
- def _expecting_value(self, token):
286
- if token[0] in "'\"":
287
- self.state = self._after_tag
288
- elif token == "{":
289
- self.brace_count = 1 # Start counting braces
290
- self.state = self._jsx_expression
291
- # Don't add the closing brace automatically
292
- # self.cache.append("}")
293
- self.sub_tokenizer = TSXTokenizer()
294
-
295
- def _jsx_expression(self, token):
296
- # Handle nested braces in expressions
297
- if token == "{":
298
- self.brace_count += 1
299
- elif token == "}":
300
- self.brace_count -= 1
301
- if self.brace_count == 0:
302
- # We've found the matching closing brace
303
- self.state = self._after_tag
304
- return
305
-
306
- # Handle arrow functions in JSX attributes
307
- if token == "=>":
308
- self.arrow_function_detected = True
309
- # Explicitly yield the arrow token to ensure it's processed
310
- return ["=>"]
311
-
312
- # Handle type annotations in JSX attributes
313
- if token == "<":
314
- # This might be a TypeScript generic type annotation
315
- # We'll continue in the current state and let the tokenizer handle it
316
- pass
317
-
318
- def _body(self, token):
319
- if token == "<":
320
- self.sub_tokenizer = XMLTagWithAttrTokenizer()
321
- self.cache.pop()
322
- return self.flush()
323
-
324
- if token.startswith("</"):
325
- self.stop()
326
- return self.flush()
327
-
328
- if token == '{':
329
- self.sub_tokenizer = TSXTokenizer()
330
- return self.flush()
331
-
332
-
333
- def isidentifier(token):
334
- try:
335
- return token.isidentifier()
336
- except AttributeError:
337
- return token.encode(encoding='UTF-8')[0].isalpha()