How do you define function variables in grammer?

Nov 30, 2012 at 3:05 PM

I created a function call in my grammer and it's working great!  However, I can't figure out how to define variables so they do not cause 'unknown symbol' errors.  The only way that I have been able to get around the error is to put the variable name in the Globals dictionary, i.e.

myEvaluator.Globals.Add( "Day", "Day" );

Any hints on what I'm missing? 

Here is an example of one function:

DateDiff(Day, 07/07/2011, 07/14/2011) - this should return "7"

Nov 30, 2012 at 5:30 PM
Edited Nov 30, 2012 at 5:31 PM

Found it!

ConstantTerminal with a typeof LiteralValueNode

Now if I could just figure out how to identify and pass a dateformat I would be set!

Any ideas on how to identify 'mm/dd/yyyy' as a date format and pass it as a string?

Coordinator
Dec 2, 2012 at 11:00 PM

don't quite understand your last question about date format - what exactly you're trying to do...

But back to your original problem, your solution by adding a function implementation to Globals was correct and in fact, the suggested way to extend expression evaluator with custom function. You can even create a static class, put all your custom functions there (as static methods), and then add all of them in one line. Look how expression evaluator sample does this with System.Math methods.

Roman

Dec 3, 2012 at 1:52 PM

I have a function called 'IsValidDate' which returns a boolean value, i.e. IsValidDate(01/01/2012) = True.  Or the 'DateDiff' function above.  I want the user to be able to pass in a date without requiring that it be escaped with quotes.

I was able to get it to work by defining another class based on ASTNode like the LiteralValueNode.  The init function parses the ChildNodes and builds a date string then it validates it.

Thank you for the response, I'll take a look at the expression evaluator sample again.