Project Description

LParse is a monadic parser combinator library, similar to Haskell’s Parsec. It allows you create parsers on C# language. All parsers are first-class language objects, they can be easily combined with each other and mixed with custom logic to implement complex parsing scenarios.

The project built under .NET Framework 4 Client Profile.

Let’s take a brief look at some LParse features.

var decimalDigit = from ch in TextParsers.Range('0', '9') select ch - '0'

This is the definition of a decimal digit parser. It makes use of LINQ syntax, in the “from” part we have specified a parser that read any character from “0” to “9”, in the “select” part we have placed custom logic to make some computation on parsed value (here converting character to corresponding integer value). We can define parser which parses hexadecimal letter in the same way:

var hexLetter = from ch in TextParsers.Range('a', 'f') select ch - 'a' + 10

This parser is able to read any character from ‘a’ to ‘f’ and convert it to corresponding integer value. Now, let’s combine our parsers to create more complex one:

var hexDigit = decimalDigit.Or(hexLetter);

Resulting parser is able to parse any character from ‘0’ to ‘9’ and from ‘a’ to ‘f’ and convert it to integer value. Let’s check it out:

var a = hexDigit.Parse("3"); // a is 3
var b = hexDigit.Parse("d"); // b is 13

And what will happen if we'll try to parse something other than hexadecimal digit?

var d = res.Parse("k"); // ParseException: Unexcepted `k' at 0, excepting `0',
  `1', `2', `3', `4', `5', `6', `7', `8', `9', `a', `b', `c', `d', `e' or `f'.

LParse informs us about invalid input and gives suggestions about excepted values.

LParse library provides large set of parser combinators and predefined elementary parsers, it allows to parse both strings and custom token streams, allows low-level access to library internals to perform optimizations if necessary, and shipped with several sample parsers in separate assemblies.

Last edited May 28, 2010 at 9:17 AM by schernichkin, version 3