Atypical.Antlr4Library.Templates
1.0.3
dotnet new install Atypical.Antlr4Library.Templates::1.0.3
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
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:
dotnet new install Atypical.Antlr4Library.Templates
Usage
Create a new project using the template:
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 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 theCSVService
.
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:
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 file for details.
About
Developed and maintained by Philippe Matray, founder of Atypical Consulting SRL.
What is Antlr4?
Created by Terence Parr, Antlr4 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
Install the Template
dotnet new install Atypical.Antlr4Library.Templates
Create a New Solution
mkdir AntlrDemo cd AntlrDemo dotnet new sln -n MyAntlrSolution
Create a New Antlr4 Project
dotnet new antlr4 -n MyAntlrLibrary dotnet sln add MyAntlrLibrary
Create a Console Application with the demo code
dotnet new console -n MyAntlrApp dotnet add MyAntlrApp reference MyAntlrLibrary dotnet sln add MyAntlrApp
Replace the contents of
Program.cs
with the following code: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:
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 or open an issue in the repository.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.