views:

32

answers:

0

I am using the .Net port of ANTLR StringTemplate, but have been struggling with doing case insensitive replacements in the template.

I know that StringTemplate doesn't support case-insensitivity out of the box (because of language variation difficulties), but was able to find the following that shows how to implement a CaseInsensitiveStringStream:

http://www.antlr.org/wiki/pages/viewpage.action?pageId=1782

which gives me this code:

public class CaseInsensitiveStringStream : ANTLRStringStream
{
    public CaseInsensitiveStringStream(char[] data, int numberOfActualCharsInArray) : base(data, numberOfActualCharsInArray) { }

    public CaseInsensitiveStringStream() { }

    public CaseInsensitiveStringStream(string input) : base(input) { }

    // Only the lookahead is converted to lowercase. The original case is preserved in the stream.
    public override int LA(int i)
    {
        if (i == 0)
        {
            return 0;
        }

        if (i < 0)
        {
            i++;
        }

        if (((p + i) - 1) >= n)
        {
            return (int)CharStreamConstants.EndOfFile;
        }
        return Char.ToLowerInvariant(data[(p + i) - 1]); // This is how "case insensitive" is defined, i.e., could also use a special culture...
    }
}

However, I cannot figure out how to swap this CaseInsensitveStreamString for my default steam when I do a merge like:

    var sTemplate = "this is my sample template name: $sample.templatename$".
    var query = new StringTemplate(sTemplate);
    foreach (var key in contextDictionary.Keys)
    {
        query.SetAttribute(key,contextDictionary[key]);
    }
    var myRenderer = query.GetAttributeRenderer(typeof (DateTime));
    query.RegisterRenderer(typeof(DateTime),new RendererDateTime());
    var results = query.ToString();

Note: contextDictionary is passed in

At what point in the second set of code would I convert the default charStream to my caseInsensitiveStringStream?

I can't find any property or method like:

query.CharStream = new CaseInsensitveStringStream();

or

query.SetCharStream(new CaseInsensitiveStringStream());

Maybe I have to reconfigure the runtime?

Any help would be greatly appreciated.

Best regards,

Hal