Sign up ×

I am writing a little lookup app, where i have a console handy for quick queries against a cache for sanity checks etc..


get SomeField=Blue

this will than get all objects from cache matching that filter.

I can apply more filters

get SomeField=Blue && SomeOtherField < 5

this can get more complex if i decide to support ()'s

what is a good pattern to use here? or possibly a component that can take a string and tokenize it for me?

for example, i'd want to break down the following into subset of filters

get ((field1=x || field1=y) && field2>x)

the only way i can think of doing this, is regex, and than pass off substrings to different routines designed to create a specific filter. (i.e. AndEquals, OrEquals, AndGraterThan etc)

share|improve this question

3 Answers 3

up vote 2 down vote accepted

You shouldn't do this with a regex, you need a full-blown parser. Have a look at ANTLR.

share|improve this answer

Have a look at IronPython. It's easy to integrate into a c# app and already supports all standard procedural language constructs. I'm using it in a game engine to perform real-time tweaks to the scene state while debugging.

share|improve this answer
This is a good recommendation. Embedding IronPython or Ruby "domain specific languages" within a larger C# application is pretty powerful. –  RationalGeek Nov 12 '10 at 19:13
sigh.. sounds great but i really wasn't looking to learn a new language for this task :) is there any specific construct you have in mind that i can quickly leverage? –  Sonic Soul Nov 12 '10 at 22:00

You could use something like the Specification pattern here.

public interface ISpecification<T>
    bool IsSatisfiedBy(T instance);
    ISpecification<T> And(ISpecification<T> specification);
    ISpecification<T> Or(ISpecification<T> specification);
    ISpecification<T> Not(ISpecification<T> specification);

Full working example here

share|improve this answer
this is interesting but not sure how it applies.. i need to break out expressions out of a long input string.. this appears to be strongly typed predicate builder –  Sonic Soul Nov 12 '10 at 4:03
I'm afraid you'll have to be more precise with your original question then. As David alluded to, it seems you might want to look into some sort of dynamic language features via the DLR or "dynamic" in C# 4. –  Chris Martin Nov 12 '10 at 5:21
Or of course, you can go crazy with reflection and try to figure out what your user wants. ;) –  Chris Martin Nov 12 '10 at 5:29

Your Answer


