百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

parlot

> 编程语言
开源

快速、轻量级的解析器创建工具

558 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

快速、轻量级的解析器创建工具

Parlot

Parlot is a fast, lightweight and simple to use .NET parser combinator.

Parlot provides a fluent API based on parser combinators that provide a more readable grammar definition.

Branches and NuGet feeds

Branch Purpose Package feed
release/1.x Maintenance of stable Parlot 1.x releases.
main Development of Parlot 2.0, including source-generated parsers.

Stable packages

Tagged releases are published to NuGet.org, using the feed https://api.nuget.org/v3/index.json. To install the latest stable version:

bash
dotnet add package Parlot

Preview packages

After a successful Ubuntu build triggered by a push to main, the workflow publishes preview packages to the Parlot feed on feedz.io. Versions follow 2.0.0-preview-, using the GitHub Actions build run number. These packages contain the latest development changes and are intended for testing before release.

Add the preview feed alongside NuGet.org, then install the latest prerelease version:

bash
dotnet nuget add source https://f.feedz.io/sebastienros/parlot/nuget/index.json --name parlot-preview
dotnet add package Parlot --prerelease

Keep NuGet.org enabled so dependencies can be restored. If your NuGet.config uses package source mapping, also map Parlot to the parlot-preview source.

Fluent API

The Fluent API provides simple parser combinators that are assembled to express more complex expressions. The main goal of this API is to provide an easy-to-read grammar. Another advantage is that grammars are built at runtime, and they can be extended dynamically.

Getting Started

To use the Fluent API, you need to import the static Parsers class which provides access to Terms, Literals, and other parser combinators:

csharp
using Parlot.Fluent;
using static Parlot.Fluent.Parsers;

Important: The using static Parlot.Fluent.Parsers; statement is required to access Terms, Literals, ZeroOrOne, Between, and other parser combinators used in the examples below.

Alternatively, if your project has ImplicitUsings (also known as Global Usings) enabled, this import is included automatically.

The following example is a complete parser that creates a mathematical expression tree (AST). The source is available here.

…

Documentation

  • Existing parsers and usage examples
  • Best practices for custom parsers
  • Source generation guide
  • Security guidance

Source-generated parsers

Parlot's analyzer generates direct parsing methods during the normal build. Generated code and shared internal support sources compile into your assembly, with no Parlot runtime dependency.

How it works

  • Reference the Parlot.SourceGenerator package with PrivateAssets="all".
  • Declare a static partial bool TryParse(string text, out T value) method in a normal .cs file.
  • Define its factory in a build-only .parlot.cs file with [GenerateParser(nameof(TryParse))].
  • The analyzer executes the factory at build time and emits the method implementation and support code.
  • For variants, pass configuration arguments between the input and output parameters and capture them in If, Select, or other supported parse-time callbacks in the matching factory.

Application code, in MyGrammar.cs:

csharp
public static partial class MyGrammar
{
    public static partial bool TryParse(string text, out string value);
}

// Usage: MyGrammar.TryParse("hello", out var value)

Build-only grammar, in MyGrammar.parlot.cs:

csharp
using Parlot.SourceGenerator;
using Parlot.Fluent;
using static Parlot.Fluent.Parsers;

public static partial class MyGrammar
{
    [GenerateParser(nameof(TryParse))]
    private static Parser Build() => Terms.Text("hello").Eof();
}

Requirements

  • Use a compiler host with Roslyn 5.9 or later and C# 12 or later. Generated code supports net472, netstandard2.0, net8.0, and net10.0; older targets use compatibility packages such as System.Memory, not Parlot.
  • Add an extra CancellationToken before the out result to enable cooperative cancellation, without adding it to the grammar factory.
  • No interceptors configuration is needed.
  • Methods must be static and non-generic. By-value parameters may only be read in supported parse-time callbacks, not during graph construction.
  • The containing class must be partial.

Advanced Configuration

Additional attributes can be combined with [GenerateParser]:

  • [IncludeFiles("*.cs")] – Include source files (supports globs) for types used by your parser.
  • [IncludeUsings("Namespace")] – Add extra using directives to generated code.
  • [IncludeGenerators("AssemblyName")] – Run other source generators before parser generation.

For detailed documentation, see Source Generation Guide.

Why use source generation?

  • Inlined parsing without combinator dispatch
  • Faster startup (no runtime parser graph construction)
  • AOT-friendly, deterministic parser code
  • No Parlot assembly or package dependency for downstream consumers

Performance

Parlot is faster and allocates less memory than all other known parser combinators for .NET.

It was originally created to provide a more efficient alternative to projects like:

  • Superpower
  • Sprache
  • Irony

Finally, even though Pidgin showed some very good performance, Parlot is still faster.

The tables below were measured on September 7, 2026 using the current dependency-free generated parsers. All 41 cases use BenchmarkDotNet's out-of-process ShortRun job, with three warmup and three measurement iterations. These are short-run estimates; consider the reported error bounds when comparing timings.

To reproduce:

bash
dotnet build -c Release
dotnet run --project test/Parlot.Benchmarks/Parlot.Benchmarks.csproj -c Release --no-build -- --filter "*ExprBench*" "*JsonBench*" "*RegexBenchmarks*"

Expression Benchmarks

This benchmark creates an expression tree (AST) representing mathematical expressions with operator precedence and grouping. It exercises three expressions:

  • Small: 3 - 1 / 2 + 1
  • Big: 1 - ( 3 + 2.5 ) * 4 - 1 / 2 + 1 - ( 3 + 2.5 ) * 4 - 1 / 2 + 1 - ( 3 + 2.5 ) * 4 - 1 / 2
  • Unary: -(3 + 2) * -4 + --6

The benchmark compares Raw, Fluent, and source-generated Parlot parsers with Pidgin. It parses the expressions into the same AST without evaluating them.

In these results, Parlot Fluent is about 12-13 times faster than Pidgin and Parlot Raw is faster still. The source-generated parser takes about 20-26% less time than Fluent and allocates 144 fewer bytes per parse in all three expressions. Generated helpers use normal JIT inlining heuristics to avoid expanding large parser chains into oversized native methods.

…

JSON Benchmarks

This benchmark was taken from the Pidgin repository and demonstrates how to perform simple JSON document parsing. It exercises the parsers with different kinds of documents. Pidgin, Sprache, Superpower and Parlot are compared. The programming models are all based on parser combinators. For reference, Newtonsoft.Json is also added to show the differences with a dedicated parser. The benchmark compares Fluent and source-generated Parlot parsers with Pidgin, Sprache, Superpower, Newtonsoft.Json, and System.Text.Json. For most documents, the best JSON parser is System.Text.Json; don't build your own!

…

Regular Expressions

Regular expressions can also be replaced by more formal parser definitions. The following benchmarks show how Parlot compares to them when checking if a string matches an email with the pattern [\w\.+-]+@[\w-]+\.[\w\.-]+. Note that in the case of pattern matching Parlot can use the pattern matching mode and do fewer allocations. The benchmark compares regular, compiled, and source-generated .NET regular expressions with Fluent and source-generated Parlot parsers.

…

Versions

The benchmarks were executed with the following versions:

  • Parlot (current source)
  • Pidgin 3.5.1
  • Sprache 3.0.0-develop-00049
  • Superpower 3.2.2-dev-00214
  • Newtonsoft.Json 13.0.5-beta1

Operator Syntax

Parlot supports intuitive operators for parser composition:

  • + operator: Combines parsers in sequence (alternative to .And())
  • | operator: Creates choice between parsers (alternative to .Or())
csharp
// Using operators
var parser = Literals.Char('a') + Literals.Char('b') + Literals.Char('c');
var choice = Literals.Char('x') | Literals.Char('y') | Literals.Char('z');

// Equivalent to
var parser = Literals.Char('a').And(Literals.Char('b')).And(Literals.Char('c'));
var choice = Literals.Char('x').Or(Literals.Char('y')).Or(Literals.Char('z'));

Usages

Parlot is already used in these projects:

  • Shortcodes
  • Fluid
  • OrchardCore
  • YesSql
  • NCalc
  • [hyperbee.xs] https://github.com/Stillpoint-Software/hyperbee.xs

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言