https://github.com/alschmut/objectivecparser
An Antlr4 Parser for Objective-C, Java and Kotlin code, calculating the lines of code per method
https://github.com/alschmut/objectivecparser
antlr4 grammar java kotlin lines-of-code objc objective-c parser static-analysis
Last synced: 6 months ago
JSON representation
An Antlr4 Parser for Objective-C, Java and Kotlin code, calculating the lines of code per method
- Host: GitHub
- URL: https://github.com/alschmut/objectivecparser
- Owner: alschmut
- License: mit
- Created: 2024-03-06T08:22:45.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-08T16:31:41.000Z (over 2 years ago)
- Last Synced: 2025-01-20T21:50:02.942Z (over 1 year ago)
- Topics: antlr4, grammar, java, kotlin, lines-of-code, objc, objective-c, parser, static-analysis
- Language: Python
- Homepage:
- Size: 1.03 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ObjectiveCParser
ObjectiveCParser performs a static analysis on Objective-C code to calculate the metric *lines of code per method*. The result is written to a *.csv file. ObjectiveCParser is a python script, which uses a generated [antlr4](https://wwwpyt.antlr.org) parser for the given Objective-C language grammar from [antlr-grammars-v4](https://github.com/antlr/grammars-v4).
> Please star this repository, if you found the project helpful :)
## Installation Requirements
Make sure python3 (including pip3) is installed. Install [antlr4](https://wwwpyt.antlr.org), which enables parsing source code.
```sh
pip3 install antlr4-python3-runtime
pip3 install antlr4-tools
```
## How to analyse your Objective-C code?
```sh
git clone https://github.com/alschmut/ObjectiveCParser.git
python3 src/main/ProjectParser.py /path/to/MyObjectiveCProject
```
This generates a `*.csv` file with the below structure:
|path |method_loc
|--- |---
|/path/to/MyFile.m/someMethod |1
|/path/to/MyFile.m/anotherMethod |5
## How to visualise the metrics?
I can reccommend using the Open-Source tool [CodeCharta](https://github.com/MaibornWolff/codecharta), which visualises metrics with a 3D city. Just follow the below steps:
1. `npm i -g codecharta-analysis`
1. `ccsh csvimport -d=";" -o MyObjectiveCProject.cc.json MyObjectiveCProject.csv`
1. Open the [CodeCharta Web Demo](https://maibornwolff.github.io/codecharta/visualization/app/index.html?file=codecharta.cc.json.gz&file=codecharta_analysis.cc.json.gz) and import the generated `MyObjectiveCProject.cc.json` file
## How to add a new programming language to be analysed with antlr4
### Generate Python classes for a new grammar
- Find the grammar on [antlr-grammars-v4](https://github.com/antlr/grammars-v4)
- Create a new folder `` inside `src/main/antlrParser/`
- Copy paste all `.g4` files like Parser, Lexer or UnicodeClasses into the folder
- Execute `antlr4 -Dlanguage=Python3 *.g4` inside your folder. This generates some Python3 classes and other files
### Override generated listener methods
- Create a new file `ListenerExtended.py` inside `src/main/antlrParser/ExtendedListener/`
- Create a new class which just looks similar to the other existing classes like `ObjcParserListenerExtended` and extend the BaseListener class
- Override the language specific `enter...()` functions. To find out which functions to override, have a look at the hierarchical grammar definition inside `Parser.g4`. Then store the obtained values inside the predefined BaseListener class.
### Walk through the new grammar
Create another function inside the `src/main/antlrParser/LanguageParser.py` like below. Replace the placeholder with your generated/created classes.
```python
def parse__file(self, input_stream: InputStream):
lexer = (input_stream)
stream = CommonTokenStream(lexer)
parser = (stream)
tree = parser.()
listener = ()
return self.walk(listener, tree)
```
### Add your supported language extension
Inside `src/main/antlrParser/Lanague.py` an enum with all supported programming languages is stored. Add your language name with its file-extension
### Add new option to use your parse__file function
The `LanguageParser.parse_file()` function calls the appropriate parsing function for each language. Add yours with another if-statement checking the file extension