Created
February 19, 2014 05:10
-
-
Save sharwell/9086386 to your computer and use it in GitHub Desktop.
Case-insensitive ANTLRStringStream for ANTLR 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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