• Stars
    star
    200
  • Rank 194,391 (Top 4 %)
  • Language
    C#
  • License
    MIT License
  • Created about 12 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A PEG parser generator for .NET that integrates with MSBuild and Visual Studio.

Pegasus

Pegasus is a PEG-style parser generator for C# that integrates with MSBuild and Visual Studio.

Join the Chat

MIT Licensed Get it on NuGet

Appveyor Build Test Coverage Pre-release packages available

Visual Studio Extension

Getting Started

The easiest way to get a copy of Pegasus is to install the Pegasus NuGet package in Visual Studio.

PM> Install-Package Pegasus

Due to a limitation in Visual Studio 2017 and earlier, you will need to reload your project for the 'PegGrammar' build action to be recognized.

Once you have the package installed, files in your project marked as 'PegGrammar' in the properties window will be compiled to their respective .peg.g.cs parser classes before every build. These parser classes will be automatically included in compilation.

For help with grammar syntax, see the syntax guide wiki entry

Example

Here is an example of a simple parser for mathematical expressions:

@namespace PegExamples
@classname ExpressionParser
@using System.Globalization

additive <double> -memoize
    = left:additive "+" right:multiplicative { left + right }
    / left:additive "-" right:multiplicative { left - right }
    / multiplicative

multiplicative <double> -memoize
    = left:multiplicative "*" right:power { left * right }
    / left:multiplicative "/" right:power { left / right }
    / power

power <double>
    = left:primary "^" right:power { Math.Pow(left, right) }
    / primary

primary <double> -memoize
    = decimal
    / "-" primary:primary { -primary }
    / "(" additive:additive ")" { additive }

decimal <double>
    = value:([0-9]+ ("." [0-9]+)?) { double.Parse(value, CultureInfo.InvariantCulture) }

This will take mathematical expressions as strings and evaluate them with the proper order of operations and associativity to produce a result as a decimal.

The above parser would be used like so:

var parser = new PegExamples.ExpressionParser();
var result = parser.Parse("5.1+2*3");
Console.WriteLine(result); // Outputs "11.1".

More Repositories

1

WebGitNet

WebGit .NET is an ASP.NET MVC app that provides access to your git repositories over HTTP. It supports browsing in a web browser AND push / pull over Git's "Smart HTTP protocol".
C#
133
star
2

markov

Generic Markov chain generation for C#
C#
54
star
3

Weave

Weave is a text templating engine for .NET that is all about attention to detail. Weave handles the tricky work of making your rendered text beautiful.
C#
21
star
4

LogImporter

A W3C log importer that pulls data from Log Parser's CSV output and Bulk Copies it into SQL Server
C#
10
star
5

TaggingLibrary

A library for providing tagging suggestions for documents, music, photos, etc.
C#
6
star
6

RandomAccessPerlinNoise

C#
5
star
7

tournaments

C#
4
star
8

SiUnits

C#
3
star
9

MurMurHashAlgorithm

Implements the MurMurHashAlgorithms for .NET
C#
3
star
10

MediaLibrary

A basic media library.
C#
3
star
11

Therefore

C#
2
star
12

BBCode.NET

A BBCode interpreter, focused on safe and clean output.
C#
2
star
13

NUnit.Extensions

Community extensions for NUnit
C#
2
star
14

PortableVCS

Portable VCS is a specification and reference implementation for a portable version control history transfer format.
2
star
15

winmm

C#
2
star
16

CardCatalog

A Magic The Gathering Deck Manager / Card Catalog.
JavaScript
2
star
17

ESO-LibLootStats

Better than Harvester of any kind.
Lua
1
star
18

Productivity

A personal productivity management solution.
C#
1
star
19

PrimeMath

C#
1
star
20

GameTheory

GameTheory toys
C#
1
star
21

FixMe

A utility to emit MSBuild warnings for FIXME-style comments.
C#
1
star
22

SharpBooks

C#
1
star
23

intervals

A library for working with intervals and sets. Includes a generic interface so that intervals of any type may be created.
C#
1
star
24

KnowledgeInterchangeFormat

An implementation of the KIF specification at http://www.ksl.stanford.edu/knowledge-sharing/kif/
C#
1
star
25

RandomImpls

Provides a few useful overrides of the System.Random class.
C#
1
star
26

ComplexViz

1
star
27

MyLife

C#
1
star
28

ADAutomator

Automator Scripts for Antimatter Dimensions
C#
1
star