Skip to content

Commit f8e65ff

Browse files
authored
Merge pull request #3 from dxworks/victorc/update_languages
Victorc/update languages
2 parents 9b24242 + 2ae0b79 commit f8e65ff

File tree

359 files changed

+9745
-86900
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

359 files changed

+9745
-86900
lines changed

.DS_Store

-8 KB
Binary file not shown.

.appveyor.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
alwaysApply: true
3+
---
4+
# The fundamental development guideline for making any changes
5+
6+
# Running command in terminal
7+
8+
This project use nix to manage the development envioroment. So we need to run `nix develop` once first in every new terminal window to start the nix enviroment. For AI agent, add `nix develop -c` before any terminal command:
9+
10+
```
11+
nix develop -c <command>
12+
```
13+
14+
15+
## Run Test
16+
17+
Always run all tests instead of just one test file
18+
19+
```bash
20+
nix develop -c python -m pytest # Run all tests
21+
```
22+
23+
## Run All Test and Format checking
24+
25+
26+
```bash
27+
nix develop -c make
28+
```
29+
30+
31+
## Automated test practices
32+
33+
* All code and logic need to be covered by unit test
34+
* Try to test end to end, external observable pre and post state change, as much as possible
35+
* Don't mock any code unless it's about file system

.cursor/rules/issue.mdc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
description:
3+
globs:
4+
alwaysApply: true
5+
---
6+
7+
# When fixing an issue (e.g. from Github issues) please:
8+
9+
1. find where is the right place to add a new unit test to reproduce the issue
10+
2. add a new unit test
11+
3. run all test to see if the test really fails
12+
4. fix the issue until all test pass
13+
5. clean up
14+
6. run all unit test at last to confirm things are still working.
15+
16+
follow [basic-development.mdc](mdc:.cursor/rules/basic-development.mdc)
17+

.cursor/rules/lizard-rule.mdc

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
description:
3+
globs:
4+
alwaysApply: true
5+
---
6+
7+
# Adding Language Support to Lizard Code Analyzer
8+
9+
This guide explains how to add or modify support for programming languages in the Lizard code complexity analyzer.
10+
11+
## Overview
12+
13+
Lizard uses a state machine-based approach to parse code. Each language implementation consists of:
14+
15+
1. A language reader class that inherits from `CodeReader`
16+
2. Token generation and processing logic
17+
3. Language-specific state handling
18+
19+
## Step-by-Step Guide
20+
21+
### 1. Create Language Reader Class
22+
23+
Create a new file in `lizard_languages/` named after your language (e.g., `mylang.py`). The basic structure should be:
24+
25+
```python
26+
from .code_reader import CodeReader
27+
from .clike import CCppCommentsMixin # If language has C-style comments
28+
29+
class MyLanguageReader(CodeReader, CCppCommentsMixin):
30+
# File extensions for your language
31+
ext = ['mylang']
32+
33+
# Language names (used in command line args)
34+
language_names = ['mylanguage', 'mylang']
35+
36+
def __init__(self, context):
37+
super(MyLanguageReader, self).__init__(context)
38+
# Initialize language-specific state machine
39+
```
40+
41+
### 2. Implement Token Generation
42+
43+
Define how your language's code should be tokenized:
44+
45+
```python
46+
@staticmethod
47+
def generate_tokens(source_code, addition='', token_class=None):
48+
# Add language-specific token patterns
49+
addition = addition + r"|pattern1|pattern2"
50+
51+
# Use existing token generator or create custom one
52+
return CodeReader.generate_tokens(source_code, addition, token_class)
53+
```
54+
55+
### 3. State Machine Implementation
56+
57+
Choose one of these approaches:
58+
59+
a. For C-like languages:
60+
- Inherit from `CLikeReader` or extend `CLikeStates`
61+
- Customize token handling as needed
62+
63+
b. For custom syntax:
64+
- Create a custom state machine class
65+
- Implement state transitions for your language
66+
67+
### 4. Add Test Cases
68+
69+
Create test file in `test/test_languages/testMyLang.py`:
70+
71+
```python
72+
import unittest
73+
from lizard import analyze_file, FileAnalyzer, get_extensions
74+
from lizard_languages import MyLanguageReader
75+
76+
class TestMyLanguage(unittest.TestCase):
77+
def setUp(self):
78+
self.analyzer = FileAnalyzer(get_extensions([MyLanguageReader]))
79+
80+
def test_basic_parsing(self):
81+
result = analyze_file.analyze_source_code("test.mylang", code)
82+
self.assertEqual(expected, result.function_list[0].cyclomatic_complexity)
83+
```
84+
85+
## Key Concepts
86+
87+
### State Machine
88+
- Use `CodeStateMachine` for handling language states
89+
- Implement state transitions for:
90+
- Function declarations
91+
- Nested scopes
92+
- Control structures
93+
94+
### Token Processing
95+
- Handle special tokens (strings, comments, etc.)
96+
- Track nesting levels and scope
97+
- Count cyclomatic complexity
98+
99+
### Metrics Collection
100+
- Function/method identification
101+
- Complexity calculation
102+
- NLOC (Non-empty Lines of Code)
103+
- CCN (Cyclomatic Complexity Number)
104+
105+
## Best Practices
106+
107+
1. **Inheritance**:
108+
- Use existing base classes when possible
109+
- Share common functionality through mixins
110+
111+
2. **Testing**:
112+
- Cover basic syntax
113+
- Test edge cases
114+
- Include complex real-world examples
115+
116+
3. **Performance**:
117+
- Optimize token generation
118+
- Minimize state transitions
119+
- Use efficient regex patterns
120+
121+
4. **Maintenance**:
122+
- Document language-specific behaviors
123+
- Keep state machine logic simple
124+
- Follow existing patterns in the codebase
125+
126+
## Examples
127+
128+
Reference existing implementations:
129+
- `javascript.py` for dynamic languages
130+
- `clike.py` for C-style languages
131+
- `python.py` for indentation-based languages
132+
133+
## Integration
134+
135+
1. Add your reader to `lizard_languages/__init__.py`
136+
2. Update documentation if needed
137+
3. Add test cases
138+
4. Verify with real code samples
139+
140+
## Testing Your Implementation
141+
142+
```bash
143+
python -m pytest # Run all tests
144+
python -m pytest test/test_languages/testMyLang.py # Test specific language
145+
```
146+
147+
Remember to handle:
148+
- Comments (single-line, multi-line)
149+
- String literals
150+
- Language-specific keywords
151+
- Special syntax constructs

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ dist/
4040
.idea
4141
.pytest_cache/
4242
.local/
43+
44+
.DS_Store
45+
.venv/

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
language: python
22

33
python:
4-
- 2.7
5-
- 3.4
6-
- 3.5
74
- 3.8
5+
- 3.10
86
- pypy
97
- pypy3
108

CHANGELOG.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,108 @@
11
# Change Log
22

3+
## 1.18.0
4+
5+
### New Features
6+
- Add R language support with comprehensive function parsing
7+
- Add Structured Text (St) language support
8+
- Add C++ lambda expression support for improved parsing
9+
- Add Nesting Depth feature for better code analysis
10+
11+
### Improvements
12+
- Enhanced R function parsing for multiple assignments
13+
- Improved string interpolation handling in Go
14+
- Better function nesting detection
15+
- Updated documentation to include new supported languages
16+
17+
### Bug Fixes
18+
- Fix nesting depth calculation issues
19+
- Fix function nesting problems
20+
- Various parsing improvements across multiple languages
21+
22+
## 1.17.30
23+
24+
Fix TypeScript function detection issue.
25+
26+
## 1.17.29
27+
28+
Fix modern PHP issues.
29+
30+
## 1.17.28
31+
32+
Fix Java function count issue.
33+
34+
## 1.17.27
35+
36+
Fix Java function count issue.
37+
38+
## 1.17.26
39+
40+
A blank release to fix the installation issue.
41+
42+
## 1.17.25
43+
44+
Bug fixes. "lizard forgive" for python
45+
46+
## 1.17.24
47+
48+
Added support for `#lizard forgive global` to suppress warnings for all code outside of functions, for languages like Python, Perl, etc, that allow global code.
49+
50+
`#lizard forgive` at the global level will suppress warnings for all code after it.
51+
52+
## 1.17.23
53+
54+
Bug fixes.
55+
56+
## 1.17.22
57+
58+
Add Complete Perl support.
59+
60+
## 1.17.21
61+
62+
Add Perl support.
63+
64+
## 1.17.19
65+
66+
Bug fixes.
67+
68+
## 1.17.18
69+
70+
Bug fixes.
71+
72+
## 1.17.17
73+
74+
Bug fixes.
75+
76+
## 1.17.16
77+
78+
Add support for gitignore files.
79+
A few bug fixes.
80+
81+
## 1.17.15
82+
83+
Add VueJS support.
84+
85+
## 1.17.14
86+
87+
Add TSX support.
88+
89+
## 1.17.13
90+
91+
Bug fix:
92+
Replace re.NOFLAG with 0 to make it compatible with Python 3.10+
93+
94+
## 1.17.12
95+
96+
Bug fix:
97+
Fortran support for ELSEIF
98+
99+
## 1.17.9
100+
101+
Bug fix:
102+
Java generic like List<? extends String>
103+
JSX file extension
104+
105+
3106
## 1.17.8
4107

5108
Add Fortran to the language supported

0 commit comments

Comments
 (0)