Csly is inspired by the Python lex yacc library (PLY) and aims to simplify generating lexer/parsers in C#.
If you'd like to get coding right away, read the quick getting-started guide, which will guide you through the implementation of a basic parser.
Complete documentation can be found in the wiki. For a list of more advanced samples check out the samples folder in the repo.
Csly is packed with special features that make it simpler to use, maintainable, and type-safe.
Csly has been designed to avoid extra build steps. Parser generators often need a build-time step to generate target language source code. That is not the case with csly. A simple Nuget command will configure csly for use in a 100% .NET implementation.
The csly lexer/parser is defined with only 2 types:
- a C#
enum
for the lexer, - a C#
class
for the parser.
Lexeme and parser production rules are defined using C# custom attributes making your code compact and readable. Although these features already exist with parser combinators (like Sprache or Eto.Parse), csly can use productions rules defined using either BNF or EBNF notation, which I think is more natural and easier to understand, assuring maintainability.
See Lexer for lexers definition and BNF or EBNF for parser definitions.
Csly is strictly typed, so every parser you define renders according to its input and output types. For additional details on parser typing, head to the parser definition section.
Many domain-specific languages need parsing expressions (boolean or numeric). A recursive-descent parser is hard to maintain when parsing expressions have multiple precedence levels. For that reason, csly offers a way to generate expression-parsing rules using only operator tokens and a simple-to-understand precedence scheme. Csly will then generate production rules to parse expressions, managing precedence and either left-or-right associativity.
Some languages use indentation to denote functional blocks, like Python or Yaml. Csly provides native support for indentation. Head to Indented Languages
Comments or whitespace are almost every time discardable. But sometimes it makes sens to preserve them :
- use comments to generate auto doc (think javadoc, c#'s xmldoc or python's docstring )
- introduce meta data without cluttering your grammar
So CSLY borrowed the (antlr channel concept)[https://datacadamia.com/antlr/channel]. Every lexeme can be redirected to a specific channel. By default comments go to channel 2.
This feature is already available on branch dev but not deployed in any nuget package. Documentation is also a work in progress event though you can have it a glance looking at (EBNF unit tests)[https://github.com/b3b00/csly/blob/dev/ParserTests/EBNFTests.cs] :
- TestIndentedParserNestedBlocks
- TestIndentedParserWithEolAwareness
- TestIndentedParserWithEolAwareness2
- TestIssue213WithChannels
Lexemes are often similar from one language to another. Csly introduces a generic lexer that defines common lexemes and which can be reused across languages. The built-in generic lexer has better performance than a regex-based lexer.
See Generic lexer for the generic lexer implementation and Lexer for a general presentation on rolling your own.
Csly is not a fully-featured parser generator like ANTLR. You should therefore not use it to define strong-typed languages like C# or Java.
Csly is perfect for small domain-specific languages (DSLs) that can be bundled in C# applications for end-users to interact with your application using natural language, for example.
Install from the NuGet gallery GUI or with the Package Manager Console using the following command:
Install-Package sly
or with dotnet core
dotnet add package sly
If you found CSLY usefull consider supporting with :