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

An Expression Parser for CodeDom

By , 22 Apr 2009
 

Introduction

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[^].

Features

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]
foo.bar()[0] > 2
(int)foo
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.

Summary

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.

History

  • 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

License

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
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionMy vote of 5memberFilip D'haene8 Sep '11 - 6:48 
Excellent article!
 
Thanks for sharing. Smile | :)
GeneralBroken LinkmemberByteGhost14 Jul '10 - 18:11 
The link in your article to your DMRules is broken.Should be "http://www.codeproject.com/KB/WF/DmRules.aspx"
Another ball breaking day has passed
Signing Off....
ByteGhost

GeneralDecimal Pointmemberzarpas3 Jun '09 - 5:13 
Hi Dustin,
 
I want to use a constant with a decimal point but what i get is the number without that point.
 
My string to parse is something like : "1 + 1.1"
 
What I expect is 1 + 1.1 but what I get is 1 + 11.
 
Is there an error in the parser?
 
Regards.
GeneralRe: Decimal PointmemberDustin Metzgar4 Jun '09 - 21:28 
I just tried making a test for this:
            CodeExpression ce1 = new CodeBinaryOperatorExpression(
                new CodePrimitiveExpression(1),
                CodeBinaryOperatorType.Add,
                new CodePrimitiveExpression(1.1D));
            Console.WriteLine("Expected:");
            WriteExpression(ce1);
 
            Parser p = new Parser();
            CodeExpression ce2 = p.ParseExpression("1 + 1.1");
            Console.WriteLine("Parsed:");
            WriteExpression(ce2);
 
            Assert.IsTrue(CompareExpressions(ce1, ce2));
The issue does not repro for me. Is this the original string you tried? If you can't send the original string, are you able to break it down to something that still produces the error?
GeneralRe: Decimal Pointmemberzarpas5 Jun '09 - 0:47 
Hi,
 
I have made this test and the test fails.
 
What I get is:
Expected:
(1 + 1.1)
Parsed:
(1 + 11)
 
The problem is my regional configuration. In it, I have an spanish configuration and the decimal separator is a comma (,)
 
If i change the separator by a point (.), the test is OK.
 
If I parse a string like 1 + 1,1, the parser throw an exception:
Failed Parser_Test CodeDomExpParser.Test Test method CodeDomExpParser.Test.TestParser.Parser_Test threw exception: System.Exception: Token not expected: ,.
 
I see a dificult solution to this. Any Idea?
 
Regards,
GeneralRe: Decimal PointmemberDustin Metzgar7 Jun '09 - 16:29 
I guess it depends on which solution more closely matches what you would write in code. If you (assuming your UI culture is set to Spanish) were writing this in a regular C# program would you write 1,1 or 1.1? My guess is 1.1 because passing parameters would look weird if you had Foo.Bar(1,1,1,1) - is that four 1's, two 1.1's, etc. But I think it's just a matter of finding all the Double.Parse methods in the Tokenizer class and passing in System.Globalization.NumberFormatInfo.InvariantInfo as the IFormatProvider. I tried this and it works just fine after setting the culture to es-ES.
GeneralRe: Decimal Pointmemberzarpas8 Jun '09 - 23:04 
Hi Dustin,
 
I have made this modification in code:
 
if (isDouble)
{
s =s.Replace(".",Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator);
return new Token(Double.Parse(s), TokenType.Primitive, TokenPriority.None);
}
 
I assume that the entry string is ever written with a '.' as decimal separator but this is not a problem.
 
I don't know if this is the best solution but it is one that works for me.
 
Thanks,
GeneralRe: Decimal PointmemberDustin Metzgar8 Jun '09 - 23:09 
Why not just Double.Parse(s, System.Globalization.NumberFormatInfo.InvariantInfo)?
GeneralRe: Decimal Pointmemberzarpas10 Jun '09 - 5:06 
Hi Dustin,
 
I knew that my solution was not the best. I have tested with your code and it goes fine.
 
Thanks.
QuestionIs this a bug?memberzarpas28 Apr '09 - 5:16 
Hi,
 
I have created this test:
 
[TestMethod]
public void Parser_Test()
{
Parser p = new Parser();
CodeExpression ce = p.ParseExpression("10 - 5 - 1");
WriteExpression(ce);
...
}
 
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?
 
Regards,

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

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