0

I need to generate an IQueryable LINQ expression from a string. First examine class Foo

public class Foo 
{
    public int Type { get; set; }
    public int Value { get; set; }
    public string Name { get; set; }
}

Method ToFoo would then take anything inside {} and return an IQueryable list.

So ToFoo("{1, >, 10}") would return an IQueryable list. I have this part working already. What I'm missing is the Parsing method to combine the various IQueryable objects.

IQueryable<Foo> context = GetAllFoos(....);
string myInput = "({e1} | ({e2} & {e3})) | {e4}";
var result = SomeParser(myInput, context);
foreach(var x in result)
   WriteLine(x.Bar);

The only allowable operations would be & (Intersect) and | Union.

So in the example above SomeParser would return something like:

result = ToFoo("{e1}").Union(
                    ToFoo("{e2}").Intersect(ToFoo("{e3}")
                    ).Union(ToFoo("{e4}");

I was looking at ANTLR, but I'm unsure if this would require Java, which is a non-starter for deployment reasons. But I'm not sure if ANTLR would allow me to define my grammar and would build my c# classes, thus not having to require Java installed on the target systems.

In any case, it looks like I need to build an expression tree where I can navigate through the structure and UNION/INTERSECT the results returned from ToFoo

WhiskerBiscuit
  • 4,795
  • 8
  • 62
  • 100
  • 1
    You can write a recursive descent parser, build your own AST and climb over to generate code. See http://stackoverflow.com/questions/2245962/is-there-an-alternative-for-flex-bison-that-is-usable-on-8-bit-embedded-systems/2336769#2336769 I can't tell from your question whether you need to resolve names to definitions to do your task; if you do, a parser will be of very little actual help, because he name resolution part is much harder. (See my essay on "Life After Parsing" at my bio for more details). – Ira Baxter Jul 25 '15 at 06:51
  • 1
    ANTLR requires java at build time only, it doesn't need it to be installed on target machines. – Lucas Trzesniewski Jul 26 '15 at 12:18
  • Echoing @IraBaxter's question: What does `{e1}` mean? Is that some representation of a Foo object? A list of Foo objects? – Shlomo Jul 27 '15 at 05:02

1 Answers1

0

Try to use PEG parser https://code.google.com/p/peg-sharp/ - it's quite simple and doesn't require bothering with all those "tokenizers" - it works directly with source.

Vincent
  • 172
  • 2
  • 3