Search Wiki:

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Rx Parsers has been migrated into Rxx on CodePlex.

The latest documentation for parsers is here.

The Rx Parsers project is now archived and will no longer be updated here.

Rx Parsers vNext and all future versions will now be delivered in Rxx starting

from Rxx version 1.1.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Overview

Rx Parsers is a .NET class library that makes it easy to define Context-Free Grammar (CFG) and Context-Sensitive Grammar (CSG) parser rules using LINQ for performing pattern matching on observable sequences, ultimately generating new observable sequences.

Essentially, Rx Parsers provides a Parser Combinator F-coalgebra implementation on top of System.IObservable<T> and Reactive Extensions (Rx) for the .NET Framework 4.0. This allows authors to define grammars similar to Backus–Naur Form (BNF) for producing an Abstract Syntax Tree (AST) from an input sequence. The core design is based largely on this blog post by Luke Hoban. See the Resources section below for additional sources.

The Rx Parsers library defines an ObservableParse delegate and a base reactive parser class that each depend upon the IObservable<T> interface for input and output. The library also provides some useful combinators in the form of extension methods, such as Select, SelectMany, Where, Or, And, NoneOrMore, OneOrMore, IgnoreBefore, Aggregate, and others.

The optional Push and Peek combinators can be used to explicitly partition an observable sequence based on subsections of rules, and then push matched values to observers through the parser's output channel before parsing has completed.

In addition, the library also includes an interactive version of Luke Hoban's parser design that explicitly amplifies the input type with IEnumerable<T>. This allows a base cursor combinator to be defined (called Next) so that stepping through the input sequence is no longer something that concrete parser implementations must define. The reactive version of the parser class is based on this principal as well, but works on the IObservable<T> interface instead. The reactive implementation is currently the primary use case for this library.

Example

The following code snippet defines an in-line parser grammar for an observable sequence of stock tick values. The parser's grammar matches particular reversal trends and converts them into an observable sequence of alerts. The full code is included in the sample labs console application available on the Downloads tab.

static void Main()
{
    var random = new Random();
 
    IObservable<int> values = Observable
        .Interval(TimeSpan.FromSeconds(1))
        .Select(_ => random.Next(1, 50));
 
    IConnectableObservable<StockTick> ticks = values
        .Scan(StockTick.Empty, (acc, cur) => new StockTick(value: cur, change: cur - acc.Value))
        .Publish();
 
    IObservable<StockAlert> alerts = ticks.Parse(parser => 
        from next in parser
        let ups = next.Where(tick => tick.Change > 0)
        let downs = next.Where(tick => tick.Change < 0)
        select (from up in ups.AtLeast(2)
                from down in downs
                where down.Change <= -11
                select new StockAlert(up.ToEnumerable(), down))
               .Or
               (from down in downs.AtLeast(2)
                from up in ups
                where up.Change >= 21
                select new StockAlert(down.ToEnumerable(), up)));
 
    Console.WriteLine("Press any key to stop...");
    Console.WriteLine();
 
    using (ticks.Subscribe(WriteTick))
    using (alerts.Subscribe(WriteAlert))
    using (ticks.Connect())
    {
        Console.ReadKey();
        Console.ResetColor();
    }
}

Getting Started

  • Download Reactive Extensions (Rx) for the .NET Framework 4.0. See the Requirements section below for more information.
  • Download the latest version of Rx Parsers from the Downloads tab.
  • Download the samples application (Visual Studio 2010 RC, C# Console Application), which provides several interactive and reactive parser samples. The full source code for every lab is available on the Downloads tab along with the full source code for the Rx Parsers library.

Requirements

Currently, the library is written in C# 4.0 for Visual Studio 2010 Release Candidate (RC) and targets the .NET Framework 4.0 RC Client Profile. Although, I suspect it should work fine on the release version of the .NET Framework 4.0 as well.

Reactive Extensions (Rx) for the .NET Framework 4.0 is a required dependency. You must install Rx before you can use this library.

Required Rx Version: 1.0.2521.0

Discussions, Bug Reports and Contributions

If you have questions or comments then start a discussion on the Discussions tab.

You can help to improve this library. Please report bugs on the Issue Tracker tab, or start a discussion if you're not sure whether you've found a bug.

If you'd like to donate bug fixes or additional functionality, please let me know via my web site contact form.

Background

This code was developed in response to a post in the Rx forums:

Reactive Parser Combinators
http://social.msdn.microsoft.com/Forums/en-US/rx/thread/0f72e5c0-1476-4969-92da-633000346d0d

Thanks, Steve, for the inspiration. I hope the library helps :)

And lots of thanks to the Rx team for making such a great product. Even if this one sux, Rx rocks.

Resources

Last edited Aug 23 2011 at 11:20 PM  by davedev, version 42
Updating...
Page view tracker