0
# Language Support
1
2
Language parsing capabilities supporting 26+ programming languages through the lizard_languages package. This system provides extensible language detection and parsing for comprehensive multi-language code analysis.
3
4
## Capabilities
5
6
### Language Discovery
7
8
Functions for discovering and accessing available language parsers.
9
10
```python { .api }
11
def languages():
12
"""
13
Returns list of all available language reader classes.
14
15
Returns:
16
list: List of 26 language reader classes for supported languages
17
18
Example:
19
available_langs = languages()
20
print(f"Supported languages: {len(available_langs)}")
21
for lang_class in available_langs:
22
print(f" {lang_class.__name__}: {lang_class.ext}")
23
"""
24
25
def get_reader_for(filename):
26
"""
27
Returns appropriate language reader class for a filename.
28
Uses file extension matching to determine the correct parser.
29
30
Args:
31
filename (str): File path or name to match
32
33
Returns:
34
CodeReader subclass or None: Language reader class if match found, None otherwise
35
36
Example:
37
reader_class = get_reader_for('app.py')
38
if reader_class:
39
print(f"Python file detected: {reader_class.__name__}")
40
41
reader_class = get_reader_for('script.js')
42
# Returns JavaScriptReader class
43
"""
44
```
45
46
### Base Language Reader
47
48
Base class that all language parsers inherit from, providing common functionality.
49
50
```python { .api }
51
class CodeReader:
52
"""
53
Base class for all language parsers.
54
55
Class Attributes:
56
ext (list): File extensions supported by the reader
57
language_names (list): Language names (optional)
58
_conditions (set): Default condition keywords
59
"""
60
61
@classmethod
62
def match_filename(cls, filename):
63
"""
64
Checks if reader supports a filename based on extension.
65
66
Args:
67
filename (str): Filename to check
68
69
Returns:
70
bool: True if this reader can parse the file
71
72
Example:
73
if PythonReader.match_filename('app.py'):
74
print("Python file detected")
75
"""
76
77
@staticmethod
78
def generate_tokens(source_code, addition='', token_class=None):
79
"""
80
Tokenizes source code for parsing.
81
82
Args:
83
source_code (str): Source code to tokenize
84
addition (str): Additional regex patterns (optional)
85
token_class: Custom token class (optional)
86
87
Returns:
88
generator: Generator yielding tokens from source code
89
"""
90
91
def preprocess(self, tokens):
92
"""
93
Preprocesses tokens before parsing.
94
Override in subclasses for language-specific preprocessing.
95
96
Args:
97
tokens: Token generator
98
99
Returns:
100
generator: Generator yielding preprocessed tokens
101
"""
102
103
def __call__(self, tokens, reader):
104
"""
105
Main parsing method that processes tokens.
106
107
Args:
108
tokens: Token generator
109
reader: File reader context
110
111
Returns:
112
generator: Generator yielding processed tokens
113
"""
114
```
115
116
### Supported Languages
117
118
#### C-Like Languages
119
120
Languages with C-style syntax including C, C++, Java, C#, and Objective-C.
121
122
```python { .api }
123
class CLikeReader(CodeReader):
124
"""
125
C/C++/Objective-C language parser.
126
127
Extensions: ['c', 'cpp', 'cc', 'mm', 'cxx', 'h', 'hpp']
128
Language names: ['cpp', 'c']
129
130
Handles: macros, templates, operator overloading, preprocessor directives
131
"""
132
133
class JavaReader(CodeReader):
134
"""
135
Java language parser.
136
137
Extensions: ['java']
138
139
Handles: classes, records, annotations, method references, lambda expressions
140
"""
141
142
class CSharpReader(CLikeReader):
143
"""
144
C# language parser.
145
146
Extensions: ['cs']
147
148
Inherits C-like parsing with C#-specific constructs
149
"""
150
151
class ObjCReader(CLikeReader):
152
"""
153
Objective-C language parser.
154
155
Extensions: ['m', 'mm']
156
157
Handles: Objective-C method syntax, categories, protocols
158
"""
159
```
160
161
#### Script Languages
162
163
Dynamic scripting languages with flexible syntax.
164
165
```python { .api }
166
class PythonReader(CodeReader):
167
"""
168
Python language parser.
169
170
Extensions: ['py']
171
172
Handles: indentation-based nesting, decorators, docstrings, comprehensions
173
"""
174
175
class RubyReader(CodeReader):
176
"""
177
Ruby language parser.
178
179
Extensions: ['rb']
180
181
Handles: blocks, modules, embedded documentation, metaprogramming constructs
182
"""
183
184
class PHPReader(CodeReader):
185
"""
186
PHP language parser.
187
188
Extensions: ['php']
189
190
Handles: mixed HTML/PHP, variable syntax, namespaces
191
"""
192
193
class PerlReader(CodeReader):
194
"""
195
Perl language parser.
196
197
Extensions: ['pl', 'pm']
198
199
Handles: Perl's complex syntax, regular expressions, special variables
200
"""
201
```
202
203
#### Web Technologies
204
205
Languages and frameworks for web development.
206
207
```python { .api }
208
class JavaScriptReader(CodeReader):
209
"""
210
JavaScript language parser.
211
212
Extensions: ['js', 'cjs', 'mjs']
213
214
Handles: closures, arrow functions, prototypes, ES6+ features
215
"""
216
217
class TypeScriptReader(CodeReader):
218
"""
219
TypeScript language parser.
220
221
Extensions: ['ts']
222
223
Handles: type annotations, interfaces, generics, decorators
224
"""
225
226
class JSXReader(CodeReader):
227
"""
228
React JSX language parser.
229
230
Extensions: ['jsx']
231
232
Handles: JSX syntax, React components, embedded expressions
233
"""
234
235
class TSXReader(CodeReader):
236
"""
237
TypeScript JSX language parser.
238
239
Extensions: ['tsx']
240
241
Handles: TypeScript with JSX syntax
242
"""
243
244
class VueReader(CodeReader):
245
"""
246
Vue.js single file component parser.
247
248
Extensions: ['vue']
249
250
Handles: template, script, and style blocks in .vue files
251
"""
252
```
253
254
#### Systems Languages
255
256
Compiled systems programming languages.
257
258
```python { .api }
259
class RustReader(CodeReader):
260
"""
261
Rust language parser.
262
263
Extensions: ['rs']
264
265
Handles: ownership syntax, traits, macros, pattern matching
266
"""
267
268
class GoReader(CodeReader):
269
"""
270
Go language parser.
271
272
Extensions: ['go']
273
274
Handles: goroutines, interfaces, channels, packages
275
"""
276
277
class ZigReader(CodeReader):
278
"""
279
Zig language parser.
280
281
Extensions: ['zig']
282
283
Handles: Zig's systems programming constructs
284
"""
285
```
286
287
#### Mobile and Modern Languages
288
289
Languages for mobile and modern application development.
290
291
```python { .api }
292
class SwiftReader(CodeReader):
293
"""
294
Swift language parser.
295
296
Extensions: ['swift']
297
298
Handles: optionals, closures, protocols, extensions
299
"""
300
301
class KotlinReader(CodeReader):
302
"""
303
Kotlin language parser.
304
305
Extensions: ['kt', 'kts']
306
307
Handles: data classes, coroutines, extensions, null safety
308
"""
309
```
310
311
#### Other Supported Languages
312
313
Additional languages supported by Lizard.
314
315
```python { .api }
316
class FortranReader(CodeReader):
317
"""Fortran language parser."""
318
# Extensions: ['f70', 'f90', 'f95', 'f03', 'f08', 'f', 'for', 'ftn', 'fpp']
319
320
class ScalaReader(CodeReader):
321
"""Scala language parser."""
322
# Extensions: ['scala']
323
324
class ErlangReader(CodeReader):
325
"""Erlang language parser."""
326
# Extensions: ['erl', 'hrl', 'es', 'escript']
327
328
class LuaReader(CodeReader):
329
"""Lua scripting language parser."""
330
# Extensions: ['lua']
331
332
class SolidityReader(CodeReader):
333
"""Solidity smart contract language parser."""
334
# Extensions: ['sol']
335
336
class GDScriptReader(CodeReader):
337
"""Godot scripting language parser."""
338
# Extensions: ['gd']
339
340
class TTCNReader(CodeReader):
341
"""TTCN test language parser."""
342
# Extensions: ['ttcn', 'ttcnpp']
343
```
344
345
## Usage Examples
346
347
### Language Detection
348
349
```python
350
from lizard_languages import get_reader_for, languages
351
352
# Check what languages are supported
353
all_languages = languages()
354
print(f"Total supported languages: {len(all_languages)}")
355
356
# Detect language for specific files
357
files = ['app.py', 'script.js', 'main.cpp', 'service.go']
358
for filename in files:
359
reader_class = get_reader_for(filename)
360
if reader_class:
361
print(f"{filename}: {reader_class.__name__}")
362
print(f" Supported extensions: {reader_class.ext}")
363
else:
364
print(f"{filename}: No parser found")
365
```
366
367
### Language-Specific Analysis
368
369
```python
370
import lizard
371
372
# Analyze only Python files
373
results = lizard.analyze(['src/'], lans=['python'])
374
for file_info in results:
375
print(f"Python file: {file_info.filename}")
376
377
# Analyze multiple specific languages
378
results = lizard.analyze(['src/'], lans=['python', 'javascript', 'typescript'])
379
for file_info in results:
380
print(f"Analyzed: {file_info.filename}")
381
```
382
383
### Custom Language Filtering
384
385
```python
386
from lizard_languages import languages
387
import lizard
388
389
# Find all web-related languages
390
web_languages = []
391
for lang_class in languages():
392
if any(ext in ['js', 'ts', 'jsx', 'tsx', 'vue', 'php']
393
for ext in lang_class.ext):
394
web_languages.append(lang_class.__name__.replace('Reader', '').lower())
395
396
print(f"Web languages: {web_languages}")
397
398
# Analyze only web technology files
399
results = lizard.analyze(['src/'], lans=web_languages)
400
for file_info in results:
401
print(f"Web file: {file_info.filename}")
402
```
403
404
### Extension and Language Mapping
405
406
```python
407
from lizard_languages import languages
408
409
# Create extension to language mapping
410
ext_to_lang = {}
411
for lang_class in languages():
412
lang_name = lang_class.__name__.replace('Reader', '')
413
for ext in lang_class.ext:
414
ext_to_lang[ext] = lang_name
415
416
# Check what language handles each extension
417
test_extensions = ['py', 'js', 'ts', 'go', 'rs', 'cpp', 'java']
418
for ext in test_extensions:
419
lang = ext_to_lang.get(ext, 'Unknown')
420
print(f".{ext} files → {lang}")
421
```