LeMP Home Page

the Lexical Macro Processor for C#

Introduction

LeMP is an open-source LISP-style macro processor for C#, comparable to sweet.js for Javascript. Install it in Visual Studio to help you write boilerplate.

public static partial class ExtensionMethods
{
  define GenerateInRangeMethods($Num)
  {
    // Returns true if num in range lo..hi
    public static bool IsInRange
      (this $Num num, $Num lo, $Num hi)
      => num >= lo && num <= hi;
    public static $Num PutInRange
      (this $Num n, $Num min, $Num max)
    {
      if (n < min)
        return min;
      if (n > max)
        return max;
      return n;
    }
  }

  GenerateInRangeMethods(int);
  GenerateInRangeMethods(long);
  GenerateInRangeMethods(double);
}
// Generated from Untitled.ecs by LeMP 2.6.4.0.
public static partial class ExtensionMethods
{
  // Returns true if num in range lo..hi
  public static bool IsInRange
    (this int num, int lo, int hi) => 
    num >= lo && num <= hi;
  public static int PutInRange
    (this int n, int min, int max)
  {
    if (n < min)
      return min;
    if (n > max)
      return max;
    return n;
  }
  // Returns true if num in range lo..hi
  public static bool IsInRange
    (this long num, long lo, long hi) => 
    num >= lo && num <= hi;
  public static long PutInRange
    (this long n, long min, long max)
  {
    if (n < min)
      return min;
    if (n > max)
      return max;
    return n;
  }
  // Returns true if num in range lo..hi
  public static bool IsInRange
    (this double num, double lo, double hi) => 
    num >= lo && num <= hi;
  public static double PutInRange
    (this double n, double min, double max)
  {
    if (n < min)
      return min;
    if (n > max)
      return max;
    return n;
  }
}

LeMP helps you solve the repetition-of-boilerplate problem, and it allows you to transform code at compile-time in arbitrary ways. For example, the biggest macro that comes packaged with LeMP is a parser generator called LLLPG. This example defines EmailAddress.Parse(), which parses an email address into UserName and Domain parts:

#importMacros(Loyc.LLPG);

struct EmailAddress
{
   public EmailAddress(public UString UserName, public UString Domain) {}
   public override string ToString() { return UserName + "@" + Domain; }

   LLLPG (lexer(inputSource: src, inputClass: LexerSource)) {
      // LexerSource provides the runtime APIs that LLLPG uses. This is
      // static to avoid reallocating the helper object for each address.
      [ThreadStatic] static LexerSource<UString> src;
   
      public static rule EmailAddress Parse(UString email) @{
         {
            if (src == null)
               src = new LexerSource<UString>(email, "", 0, false);
            else
               src.Reset(email, "", 0, false);
         }
         UsernameChars ('.' UsernameChars)*
         { int at = src.InputPosition; }
         '@' DomainCharSeq ('.' DomainCharSeq)* EOF
         {
            UString userName = email.Substring(0, at);
            UString domain = email.Substring(at + 1);
            return new EmailAddress(userName, domain);
         }
      }
      static rule UsernameChars() @{
         ('a'..'z'|'A'..'Z'|'0'..'9'|'!'|'#'|'$'|'%'|'&'|'\''|
         '*'|'+'|'/'|'='|'?'|'^'|'_'|'`'|'{'|'|'|'}'|'~'|'-')+
      };
      static rule DomainCharSeq() @{
               ('a'..'z'|'A'..'Z'|'0'..'9')
         [ '-'? ('a'..'z'|'A'..'Z'|'0'..'9') ]*
      };
   }
}

Example: using

A really simple example is ‘using’ statements:

using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Loyc.Collections;
using Loyc.MiniTest;
using Loyc.Syntax;

Luckily, Visual Studio can add these for us. But wouldn’t it be nice if half the screen wasn’t ‘using’ statements every time you open a file? There is a LeMP macro that lets you collapse these onto a couple of lines:

using System(.Linq, .Text, .Collections(, .Generic), .IO, );
using Loyc(.Collections, .MiniTest, .Syntax);

The comma , before the closing ) adds an “empty” parameter to the list, which indicates that using System itself is one of the outputs you want to produce.

Example: Null checking

As long as there is no such thing as non-nullable reference types, we’ll be checking if our method parameters are null, and if we’re extra careful, we might check our return value, too. This can be done in the traditional way,

static string Twice(string s)
{
	if (s != null)
		throw new ArgumentNullException("s");
	return s + s;
}

Or in the new way, using Microsoft Code Contracts:

static string Twice(string s)
{
	Contract.Requires(s != null)
	return s + s;
}

But with LeMP, it’s a one-liner:

static string Twice(notnull string s) => s + s;

Your output file will say

static string Twice(string s)
{
	Contract.Assert(s != null, "Precondition failed: s != null")
	return s + s;
}

Note: This feature does not require the MS Code Contracts rewriter to be installed in Visual Studio, since LeMP has a built-in “rewriter” of its own, and it relies on Contract.Assert, one of the only methods of the Contracts class that does not require the rewriter. This behavior is customizable, e.g. LeMP can be told to use the standard methods instead, such as Contract.Requires and Contract.Ènsures.)

The notnull attribute can be applied to the return value, as well, to check at run-time that a method does not return null. However, notnull is not supported on ordinary variables. LeMP also includes other “code contract” attributes. For example, the notnull modifier actually equivalent to either [requires(# != null)] or [ensures(# != null)], depending on whether you use it on an argument or return value, respectively. The hash sign # represents the value of the current parameter, or return value, depending on where you have used the contract attribute.

Example: Small data types

I like to create a lot of small data types, rather than using a few huge ones. And when you’re making small data types, C# is annoying. A simple type isn’t hard:

public class Person {
	public string Name;
	public DateTime DateOfBirth;
	public List<Person> Children;
};

But this simplicity has a big price:

So, you probably need a constructor. But adding a constructor is a pain!

public class Person
{
	public string Name           { get; private set; }
	public DateTime DateOfBirth  { get; private set; }
	public List<Person> Children { get; private set; }
	public Person(string name, DateTime dateOfBirth, List<Person> children)
	{ 
		Name = name;
		DateOfBirth = dateOfBirth;
		Children = children;
		// TODO: Add validation code
	}
}

It’s too much repetition!

LeMP solves these problems with a combination of (1) a macro, and (2) a little syntactical “makeover” of C#. In LeMP you’d write this:

public class Person
{
	public this(
		public string Name           { get; private set; },
		public DateTime DateOfBirth  { get; private set; },
		public List<Person> Children { get; private set; })
	{
		// TODO: Add validation code
	}
}

Your output file will contain exactly the code listed above, and there is no repetition except for public .. { get; private set; } (but you might not want everything to be a public property anyway, and if you’re using C# 6.0 / VS2015 you can drop the private set part). Great!

What’s going on? Enhanced C# includes two syntax changes to support this, each with a supporting macro:

  1. To reduce repetition and ambiguity, Enhanced C# allows this as a constructor name (a feature borrowed from the D language). A macro changes this into Person so that plain C# understands it.
  2. Enhanced C# allows property definitions as method parameters (or wherever an expression is allowed). A macro is programmed to notice properties, and visibility attributes (like public) on variables. When it notices one of those, it responds by transferring it out to the class, and putting a normal argument in the constructor. Finally, it adds a statement at the beginning of the constructor, to assign the value of the argument to the property or field.

Learn more

Learn more about LeMP in these published articles:

Macro reference manual

Help wanted

Do you have time to make LeMP better?

Integration into Visual Studio is basic at the moment; help wanted if you have skill in writing extensions.