Allow special characters in Variable names - shoff/Flee GitHub Wiki

bobby751 Oct 2, 2013 at 5:31 PM Hello, My question is quite simple, but I don't know if it could be easy to modify in source code. I tried, but I prefer to ask first...

Is it possible be able to have some special characters to variables? Currently, I think it is limited to a-z, A-Z, 0-9 and "_". -> I would like to add several prefix, like $, @, or %. What should I modify in the source code to be able to do that?

Actually, I would like to do this in order to be able to cast the type of variable. I cannot know the type within the event "ruleContext.Variables.ResolveVariableType".

I would like to add these prefix to be able to make something like this: no prefix: double $xxx : double[] @xxx : etc. I think it could be in the files: RegExp.vb, or CharacterElement.vb, but I'm afraid to make a change that could break everything...

Thanks a lot if you have an idea. :) Best regards!


bobby751 Oct 2, 2013 at 5:33 PM Just to be more precise, the second part works. Currently I use the "_" character as a prefix, but I would like to use more...

Here is my ResolveVariableType event:

static void Variables_ResolveVariableType(object sender, ResolveVariableTypeEventArgs e)
    {

        if (e.VariableName.Substring(0, 1) == "_")
        {
           e.VariableType = typeof(double[]);
        }
        else
        {
            e.VariableType = typeof(double);
        }

    }

Nicolas Oct 9, 2013 at 3:33 AM Hi,

In ExpressionTokenizer.vb, try to change

pattern = New TokenPattern(CInt(ExpressionConstants.IDENTIFIER), "IDENTIFIER", TokenPattern.PatternType.REGEXP, "[a-z_]\w*")

by

pattern = New TokenPattern(CInt(ExpressionConstants.IDENTIFIER), "IDENTIFIER", TokenPattern.PatternType.REGEXP, "[a-z_@$]\w*")

I don't recommand using % as a prefix since it is also used for the modulo operation.


bobby751 Oct 23, 2013 at 3:34 PM Thanks a lot


TomLoder May 8, 2015 at 10:16 AM Edited May 8, 2015 at 10:19 AM Thank you for this.

I have used the "[" and "]" braces to define variables in my equations and so that they can contain spaces and other characters with it. I changed the Regex pattern as suggested and it seems to work fine.

pattern = New TokenPattern(CInt(ExpressionConstants.IDENTIFIER), "IDENTIFIER", TokenPattern.PatternType.REGEXP, "\[.+?\]")
AddPattern(pattern)