Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Case-insensitive ANTLRStringStream for ANTLR 3
using Antlr.Runtime;
public class CaseInsensitiveStringStream : ANTLRStringStream
{
// the string used for lookahead (performance improvement by not having to call Char.ToLowerInvariant())
private readonly string _lastring;
public CaseInsensitiveStringStream(string input, string sourceName)
: base(input, sourceName)
{
_lastring = input.ToLowerInvariant();
}
public override int LA(int i)
{
if (i == 0)
return 0;
if (i < 0)
{
i++; // e.g., translate LA(-1) to use offset i=0; then data[p+0-1]
if ((p + i - 1) < 0)
{
return (int)CharStreamConstants.EndOfFile; // invalid; no char before first char
}
}
if ((p + i - 1) >= n)
{
return (int)CharStreamConstants.EndOfFile;
}
return _lastring[p + i - 1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment