Parsley.Generator
4.0.0
See the version list below for details.
dotnet add package Parsley.Generator --version 4.0.0
NuGet\Install-Package Parsley.Generator -Version 4.0.0
<PackageReference Include="Parsley.Generator" Version="4.0.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="Parsley.Generator" Version="4.0.0" />
<PackageReference Include="Parsley.Generator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add Parsley.Generator --version 4.0.0
#r "nuget: Parsley.Generator, 4.0.0"
#:package Parsley.Generator@4.0.0
#addin nuget:?package=Parsley.Generator&version=4.0.0
#tool nuget:?package=Parsley.Generator&version=4.0.0
Parsley.Generator
An incremental source generator that synthesizes a Parsley parser, its lexer and, when the grammar has semantic
actions, its evaluator into a partial class. It's built from the same sources as parsley and parsley-luthor
(linked from ../Parsley and ../LexGen), so it produces the same parsers the tools do.
using Parsley;
[Parsley("Expr.xbnf")]
public partial class ExprCalc { }
var tree = ExprCalc.Parse("1+(2^4)*3");
Console.WriteLine(ExprCalc.Evaluate(tree)); // 49
The attribute
[Parsley(grammarFile, UseUnicode = ..., HasLexer = ..., HasLinePragmas = ..., Engine = ...)]
| Argument | |
|---|---|
grammarFile |
The .xbnf grammar, relative to the source file the attribute is in. If no file is there, the one AdditionalFile whose path ends with it is used. |
UseUnicode |
Use Unicode character classes in the lexer (parsley-luthor -u). Default false. |
HasLexer |
Generate the lexer. Default true. When false, no lexer, Tokenize, or text Parse overloads are generated: feed Parse/TryParse tokens from a lexer of your own. It doesn't apply to a grammar with islands or terminals from other lexers, which always gets all its lexers, its own included: false is ignored there, with a warning (PARSLEY009). |
HasLinePragmas |
Write #line directives that map the code copied from the grammar (semantic actions, type and state, @using) back to it, so compiler errors and the debugger go to the grammar (parsley -n turns them off). Default true. |
Engine |
ParserEngine.Auto (the lightest the grammar allows), LL1, LLk or AllStar (parsley -e). Default Auto. |
The attribute and ParserEngine are generated into the project (namespace Parsley, internal, and not kept in
metadata). The class, and any type it's nested in, must be partial.
What's generated
Into the partial class:
- The parser's symbol constants, public, named as the symbols are written (
Expr,number,add, ...,EndOfInput,ErrorSymbol,SymbolCount). See XBNF.md's Symbol constants. - Public static entry points:
Tokenize(string)/Tokenize(TextReader), with the lexer's optional position, line, column and tab width (unlessHasLexer = false)Parse(IEnumerable<Token>),Parse(string),Parse(TextReader)(the last two unlessHasLexer = false), andTryParse(IEnumerable<Token>, out ParseNode, out IReadOnlyList<ParseError>)Evaluate(ParseNode, state), when the grammar has semantic actions, returning the start production's typeTryParseXxx(IEnumerable<Token>, out ParseNode, out IReadOnlyList<ParseError>)for eachsharedproduction, andCreateLexerMode(TokenizeFunc), for other grammars to embed this one as islands. A grammar with shared productions and nostartgets noParseorTryParseof its own.
- Nested classes:
Parser,Evaluator(with semantic actions) andLexer(unlessHasLexer = false), and a lexer for eachlexer=alias. They're partial, with no access of their own, so they're private unless you give one an access in a part of your own, inside your part of the class:public partial class ExprParser { public partial class Parser { } public static partial class Lexer { } } - For a grammar with islands, for each
@externalgrammar: an internal nested static class named after its alias, so the rest of the project can read its symbol constants, holding that grammar'sParser,LexerandEvaluator, its symbol constants, and its own@externalgrammars' classes in turn. So[Parser("bamc.xbnf")]also gets json.xbnf's parser and lexer, asBamcParser.Json, andBamcParser.Json.Objectis the id of json.xbnf'sObject, for looking at an island's tree.Tokenizelexes through aLexerController, which switches to an island's lexer and back. See XBNF.md's Islands and other lexers. - A lexer for each
lexer=alias, named after it (RegexLexer). Awhenon such a terminal is a static partial method on the nested parser, as a contextual terminal's is an instance one.
The grammar's @class, @evaluator and @lexer directives are ignored, with a warning (PARSLEY005) on each, in the
grammar and in its @external grammars alike: the class decides the names and the namespace, and every lexer is
generated nested in it. @using still applies to semantic actions. A constant that would clash with a member of the class, with
the generated members, or with the class an @external grammar's constants are in, gets a number after it and a
warning, as the tools do with clashes inside the parser: json.xbnf's Json rule is BamcParser.Json.Json2, since
the class is named Json after its alias. See XBNF.md's Symbol constants.
Grammar hooks (when, matched, entering, parsed) are partial methods on the nested parser, so implement them
in another part of it:
partial class C99
{
partial class Parser
{
private partial bool IsTypedefName(Token token, Token previous) { ... }
}
}
The runtime
The runtime the parsers and lexers share is generated once per project, for only the engines its parsers use:
ParsleyRuntime, the LL1ParserBase, LLkParserBase or AllStarParserBase it needs (namespace
Parsley.Runtime, public) and, when at least one parser has a lexer, the lexer runtime, LuthorRuntime and the
types it uses (also in Parsley.Runtime, internal). Generated lexers call it there.
If the project references Parsley.Runtime.dll, none of it is generated, and the parsers and lexers use the dll's,
which has all of it, the lexer runtime public.
Reference the dll when more than one project in an app uses the generator. Each generated copy of
Parsley.Runtime is public, so a project that references two of them would see each type twice.
Grammar files
The generator reads grammars, and the files they @import or declare with @external, from the project's
AdditionalFiles, never from disk.
The package's build targets make every .xbnf in the project an AdditionalFile. Set
<ParsleyIncludeXbnf>false</ParsleyIncludeXbnf> to list them yourself.
From a project reference in this solution, rather than the package, reference it as an analyzer and import the targets:
<ItemGroup>
<ProjectReference Include="..\Parsley.Generator\Parsley.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<Import Project="..\Parsley.Generator\build\Parsley.Generator.targets" />
The lexer is always built from the grammar's terminals, as parsley -l writes it. A grammar that pairs with a
hand-written .lex, like the ES5 test's, gets the grammar-derived lexer instead, unless HasLexer = false, which
leaves you to supply the tokens.
The generated code needs C# 12, as the tools' output does.
Diagnostics
| Id | |
|---|---|
| PARSLEY001 | The grammar file isn't among the AdditionalFiles |
| PARSLEY002 | More than one AdditionalFile ends with the given path |
| PARSLEY003 | The class, or a type it's nested in, isn't partial |
| PARSLEY004 | An error in the grammar, reported at its place in the .xbnf |
| PARSLEY005 | A warning about the grammar |
| PARSLEY006 | A message about the grammar (info), such as left recursion that was rewritten |
| PARSLEY007 | Generation failed for another reason |
| PARSLEY008 | The attribute has no grammar file |
| PARSLEY009 | HasLexer = false was ignored, because the grammar has islands or terminals from other lexers |
Learn more about Target Frameworks and .NET Standard.
This package has 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.