https://github.com/phmatray/antlr4library
A .NET template for creating Antlr4 projects in C#, including a sample CSV parser. This template provides a starting point for building language parsers or interpreters using Antlr4 in a C# environment.
https://github.com/phmatray/antlr4library
antlr dotnet nuke parser template
Last synced: 6 months ago
JSON representation
A .NET template for creating Antlr4 projects in C#, including a sample CSV parser. This template provides a starting point for building language parsers or interpreters using Antlr4 in a C# environment.
- Host: GitHub
- URL: https://github.com/phmatray/antlr4library
- Owner: phmatray
- License: mit
- Created: 2024-10-20T11:57:37.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-11-30T11:35:13.000Z (10 months ago)
- Last Synced: 2025-02-01T07:27:55.399Z (8 months ago)
- Topics: antlr, dotnet, nuke, parser, template
- Language: C#
- Homepage:
- Size: 335 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Atypical Antlr4 Library Template
A .NET template for creating Antlr4 projects in C#, including a sample CSV parser. This template provides a starting point for building language parsers or interpreters using Antlr4 in a C# environment.
## Table of Contents
- [Overview](#overview)
- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [Project Structure](#project-structure)
- [Customization](#customization)
- [Contributing](#contributing)
- [License](#license)
- [About](#about)## Overview
This template includes:
- An Antlr4 grammar file (`CSV.g4`) for parsing CSV files.
- Generated lexer and parser classes.
- A service class demonstrating how to use the parser.
- A visitor class implementing the logic to build a CSV model.
- A model class representing the parsed CSV data.
- Pre-configured project files for building and packaging.## Features
- **Antlr4 Integration**: Seamless integration with Antlr4 for parsing custom languages or data formats.
- **Sample Grammar**: Includes a sample CSV grammar to get you started.
- **Visitor Pattern**: Implements the visitor pattern for traversing the parse tree.
- **Easy Customization**: Modify the grammar and visitor classes to suit your needs.
- **.NET 8 Support**: Built targeting .NET 8.0 for the latest features and performance improvements.## Installation
Install the template using the `dotnet new` command:
```bash
dotnet new install Atypical.Antlr4Library.Templates
```## Usage
Create a new project using the template:
```bash
dotnet new antlr4 -n YourProjectName
```This command creates a new directory `YourProjectName` with the template contents.
### With JetBrains Rider or Visual Studio

You can create a new project using JetBrains Rider or Visual Studio by selecting the `Antlr4 Library` template from the project creation dialog.
For color syntax highlighting and code completion, install the [ANTLR v4](https://plugins.jetbrains.com/plugin/7358-antlr-v4) plugin by Terence Parr.
## Project Structure
- `Grammar/CSV.g4`: The Antlr4 grammar file for CSV parsing.
- `Models/CSV.cs`: The model class representing the CSV data.
- `Services/CSVService.cs`: Service class to parse CSV data using the generated parser.
- `Services/CSVVisitor.cs`: Visitor class to build the CSV model from the parse tree.
- `Antlr4Library.csproj`: Project file with Antlr4 build tasks and runtime dependencies.
- `Program.cs`: Sample program demonstrating how to use the `CSVService`.## Customization
### Modifying the Grammar
Edit the `Grammar/CSV.g4` file to change the grammar rules or create a new grammar for your specific language or data format.
After modifying the grammar, rebuild the project to regenerate the parser and lexer classes:
```bash
dotnet build
```### Updating the Visitor
Modify `CSVVisitor.cs` to implement custom logic for traversing the parse tree generated by your grammar.
### Parsing Custom Data
Use `CSVService.cs` as a reference to create your own service classes for parsing different types of data.
## Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request to the repository.
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
## About
Developed and maintained by [Philippe Matray](https://www.linkedin.com/in/phmatray), founder of [Atypical Consulting SRL](https://atypical.consulting).
---
## What is Antlr4?
Created by Terence Parr, [Antlr4](https://www.antlr.org/) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It's widely used to build language parsers, interpreters, and compilers for various programming languages and data formats.
It has been around for many years and has a large community of users and contributors. Antlr4 is available for multiple programming languages, including Java, C#, Python, and JavaScript.
## Quick Start Guide
1. **Install the Template**
```bash
dotnet new install Atypical.Antlr4Library.Templates
```2. **Create a New Solution**
```bash
mkdir AntlrDemo
cd AntlrDemo
dotnet new sln -n MyAntlrSolution
```3. **Create a New Antlr4 Project**
```bash
dotnet new antlr4 -n MyAntlrLibrary
dotnet sln add MyAntlrLibrary
```4. **Create a Console Application with the demo code**
```bash
dotnet new console -n MyAntlrApp
dotnet add MyAntlrApp reference MyAntlrLibrary
dotnet sln add MyAntlrApp
```
Replace the contents of `Program.cs` with the following code:
```csharp
using MyAntlrLibrary.Services;const string csvInput =
""""
Details,Month,Amount
Mid Bonus,June,"$2,000"
,January,"""zippo"""
Total Bonuses,"","$5,000"
"""";
var service = new CSVService();
var csv = service.Parse(csvInput);
Console.WriteLine(string.Join(", ", csv.Header));
Console.WriteLine("---------------------");
foreach (var row in csv.Rows)
{
Console.WriteLine(string.Join(", ", row));
}
```
Then build and run the application:
```bash
dotnet run --project MyAntlrApp
```
You should see the parsed CSV output in the console.---
## Contact
For support or inquiries, please contact Philippe Matray at [philippe@atypical.consulting](mailto:philippe@atypical.consulting) or open an issue in the repository.