Click here to Skip to main content
Click here to Skip to main content

An Expression Parser for CodeDom

, 22 Apr 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Tired of writing long, complicated CodeDom expressions? This parser's for you!


If you've ever tried to work at any length with CodeDom, you know that it's verbose and sometimes confusing to work with. For me, the hardest part of working with CodeDom is writing expressions. I find myself trying to limit what happens in my expressions so that I can make the CodeDom easier to write.

After seeing Pascal Ganaye's article (The expression evaluator revisited (Eval function in 100% managed .NET)[^]), I figured that I could use that code and create a CodeDom expression parser. This article presents my results.

Putting It To Use

I've put this parser to work and made an embeddable rules library in .NET 3.0. It stores CodeDom statements in the application configuration. Check it out here: DmRules - A helper library for running rules in .NET 3.0[^].


The parser uses a C#-like syntax and can read all of the following:

  • Local variables
  • Primitives: string, double, int, long, etc.
  • Operators: +, -, *, /, %, >, >=, <, <=, ==, !=, !, &, &&, |, ||, unary -
  • Fields
  • Properties
  • Methods
  • Indexes
  • Casting with (Type)
  • typeof()
  • Enums

Using the Code

Let's say I have to write a statement in CodeDom like this:

xr.HasAttributes && xr.MoveToAttribute("xmi.value")

It's a simple enough thing to want to put in an if condition. Writing the CodeDom would look something like this:

CodeExpression ce = new CodeBinaryOperatorExpression(
   new CodePropertyReferenceExpression(
   new CodeVariableReferenceExpression("xr"), "HasAttributes"),
   CodeBinaryOperatorType.BooleanAnd, new CodeMethodInvokeExpression(
   new CodeMethodReferenceExpression(
   new CodeVariableReferenceExpression("xr"), "MoveToAttribute"), 
   new CodeExpression[] { new CodePrimitiveExpression("xmi.value") }));

With this library, you can write this instead:

Parser p = new Parser();
CodeExpression ce = p.ParseExpression(
   "xr.HasAttributes && xr.MoveToAttribute(\"xmi.value\")");

Now, I also have other requirements. For instance, I need the parser to know what a field is and to recognize when I do a cast. So, I might want to have the following expression:

(double)Convert.ChangeType(xr.Value, typeof(double))

Here's how it would look in CodeDom:

CodeExpression ce1 = new CodeCastExpression(
   new CodeTypeReference(typeof(double)), 
   new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(
   new CodeTypeReferenceExpression(typeof(Convert)), "ChangeType"), 
   new CodeExpression[] { new CodePropertyReferenceExpression(
   new CodeVariableReferenceExpression("xr"), "Value"), 
   new CodeTypeOfExpression(new CodeTypeReference(typeof(double))) }));

And here's how to write it with the parser:

Parser p = new Parser();
CodeExpression ce = p.ParseExpression(
   "(double)Convert.ChangeType(xr.Value, typeof(double))");

The reason that the ParseExpression method is done as an instance method instead of a static method is because I want the Parser object to collect some context. There are two properties exposed on Parser: Fields and RecognizedTypes. By default, anything after a this reference is considered a property. If the name matches one of the entries in the Fields collection, then it is treated as a field instead. Also, the parser will check the RecognizedTypes to see if an identifier should be treated as a variable or as a type. It also helps when using a cast or a typeof.

In the most recent update of this code, I have included the ability to use enums. Each of the enumerations is treated as a field reference of a type, so the parser needs to handle them differently. Just add an enum type to the Enums property of the parser.

Parser p = new Parser();
p.Enums.Add("MyEnum", new CodeTypeReference("MyEnum"));
CodeExpression ce = p.ParseExpression("this.Foo == MyEnum.Bar");

Some Other Examples

All of the following are correctly parsed:

-5                  // This one actually parses as (0 - 5)
!stream.EndOfFile   // Parses as (stream.EndOfFile == false)
!stream.EndOfFile && this.Count > 5
foo[0][0] > 2
s == ""

What's Inside

Here's a listing of the files inside:

  • CodeDomExpParser
    • Enums.cs
    • Parser.cs
    • Token.cs
    • Tokenizer.cs
  • CodeDomExpParser.Test - This is a test harness for the parser library.
    • DogFood.cs - This contains some of the more complicated CodeDom statements I had to do in my XMI CodeDom library and also some examples gathered from the comments. Hence the expression, "eating my own dog food."
    • TestParser.cs - Throws a few tests at the parser. Look at this file for examples of what can be parsed.
    • TestTokenizer.cs - Runs some tests against the tokenizer.


I used a lot of the ideas that Pascal was using in his article for the tokenizer and parser. With the basic stuff in place, I pretty much went my own way. It was a cool project to work on. Hopefully, some of you will find it useful.


  • 0.1 : 2006-06-08 : Initial version
    • Handles most of the expressions that I need it to do
    • More thorough testing would definitely be nice
    • One of the to-do items is to allow the input of an existing CodeDom graph for finding types, fields, and the like
  • 0.2 : 2006-06-21
    • Added modulus operator and created a .NET 2.0 version
  • 0.3 : 2006-07-19
    • Fixed a problem where empty strings were not being parsed
    • Added the capability to parse enums
  • 0.4 : 2009-04-20
    • Fixed two bugs mentioned in the comments
    • Converted to Visual Studio 2008 solution
    • Uses Visual Studio Test Tools instead of NUnit
    • DogFood test cases now use a CompareExpression method that automates testing


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Dustin Metzgar
Software Developer
United States United States
No Biography provided
Follow on   Twitter

Comments and Discussions

QuestionMy vote of 5 PinmemberFilip D'haene8-Sep-11 7:48 
GeneralBroken Link PinmemberByteGhost14-Jul-10 19:11 
GeneralDecimal Point Pinmemberzarpas3-Jun-09 6:13 
GeneralRe: Decimal Point PinmemberDustin Metzgar4-Jun-09 22:28 
GeneralRe: Decimal Point Pinmemberzarpas5-Jun-09 1:47 
GeneralRe: Decimal Point PinmemberDustin Metzgar7-Jun-09 17:29 
GeneralRe: Decimal Point Pinmemberzarpas9-Jun-09 0:04 
GeneralRe: Decimal Point PinmemberDustin Metzgar9-Jun-09 0:09 
GeneralRe: Decimal Point Pinmemberzarpas10-Jun-09 6:06 
QuestionIs this a bug? Pinmemberzarpas28-Apr-09 6:16 
I have created this test:
public void Parser_Test()
Parser p = new Parser();
CodeExpression ce = p.ParseExpression("10 - 5 - 1");
The result I expected was 10 - 5 - 1 = 4
but what i get is (10 - ( 5 - 1)) = 6
It is an error or the analyzer works that way?
AnswerRe: Is this a bug? PinmemberDustin Metzgar28-Apr-09 21:59 
GeneralRe: Is this a bug? Pinmemberzarpas28-Apr-09 23:02 
GeneralParser has a big bug PinmemberMember 345703616-Apr-09 6:29 
GeneralRe: Parser has a big bug PinmemberDustin Metzgar19-Apr-09 21:55 
GeneralTwo things I would like to say ;-) Pinmembermichael.kruisz14-Oct-08 1:18 
GeneralRe: Two things I would like to say ;-) PinmemberDustin Metzgar14-Oct-08 9:24 
GeneralRe: Two things I would like to say ;-) PinmemberMichael Kruisz14-Oct-08 22:28 
GeneralRe: Two things I would like to say ;-) PinmemberDustin Metzgar20-Oct-08 8:23 
GeneralRe: Two things I would like to say ;-) PinmemberMichael Kruisz21-Oct-08 22:20 
Questiondoes it evaluate the expression ? PinmemberParesh Gheewala7-Aug-08 21:59 
AnswerRe: does it evaluate the expression ? PinmemberDustin Metzgar8-Aug-08 3:08 
GeneralA little fix PinmemberEmmanuel Liossis14-Mar-08 7:31 
GeneralRe: A little fix PinmemberDustin Metzgar14-Mar-08 16:32 
QuestionWhat a nice piece of work PinmemberPascal Ganaye31-Jul-07 6:11 
Questionencounter an execption Pinmemberbeergetit12-Jun-07 18:04 
AnswerRe: encounter an execption PinmemberDustin Metzgar14-Oct-08 9:49 
GeneralDateTime Support Pinmemberbfizel27-Feb-07 3:03 
GeneralRe: DateTime Support PinmvpDustin Metzgar27-Feb-07 10:44 
GeneralTime Saver PinmemberZapper.Net24-Jul-06 6:48 
GeneralRe: Time Saver PinmemberDustin Metzgar24-Jul-06 18:24 
GeneralA minor change ... PinmemberSébastien Lorion26-Jun-06 5:25 
GeneralMan... PinmemberBoneSoft13-Jun-06 11:48 
GeneralRe: Man... PinmemberDustin Metzgar13-Jun-06 12:29 
GeneralRe: Man... PinmemberBoneSoft14-Jun-06 17:33 
GeneralRe: Man... PinmemberDustin Metzgar14-Jun-06 17:41 
GeneralMy name in the body ! PinmemberPascal Ganaye9-Jun-06 8:56 
GeneralGoto is bad PinmemberSteve Hansen8-Jun-06 4:54 
QuestionRe: Goto is bad PinmemberDustin Metzgar8-Jun-06 4:59 
AnswerRe: Goto is bad PinmemberSteve Hansen8-Jun-06 5:11 
GeneralRe: Goto is bad PinmemberDustin Metzgar8-Jun-06 5:14 
GeneralHmm... PinmemberDustin Metzgar8-Jun-06 3:58 
GeneralRe: Hmm... PinprotectorMarc Clifton8-Jun-06 6:20 
GeneralRe: Hmm... PinmemberDustin Metzgar9-Jun-06 2:21 
GeneralRe: Hmm... PinmemberIgor Sukhov8-Jun-06 15:16 
GeneralRe: Hmm... PinmemberDustin Metzgar8-Jun-06 17:04 
GeneralRe: Hmm... Pinmemberbodzebod13-Jun-06 4:55 
GeneralRe: Hmm... PinmemberDustin Metzgar13-Jun-06 5:22 
GeneralRe: Hmm... PinmemberENewton25-Jul-06 9:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 22 Apr 2009
Article Copyright 2006 by Dustin Metzgar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid