diff --git a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CaseChangingCharStream.cs b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CaseChangingCharStream.cs new file mode 100644 index 000000000..060052727 --- /dev/null +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CaseChangingCharStream.cs @@ -0,0 +1,47 @@ +using System; +using Antlr4.Runtime.Misc; + +namespace Antlr4.Runtime +{ + public class CaseChangingCharStream : ICharStream + { + private ICharStream stream; + private bool upper; + + public CaseChangingCharStream(ICharStream stream, bool upper) + { + this.stream = stream; + this.upper = upper; + } + + public int Index => stream.Index; + + public int Size => stream.Size; + + public string SourceName => stream.SourceName; + + public void Consume() => stream.Consume(); + + [return: NotNull] + public string GetText(Interval interval) => stream.GetText(interval); + + public int LA(int i) + { + int c = stream.LA(i); + + if (c <= 0) return c; + + var o = Convert.ToChar(c); + + if (upper) return Convert.ToInt32(char.ToUpper(o)); + + return Convert.ToInt32(char.ToLower(o)); + } + + public int Mark() => stream.Mark(); + + public void Release(int marker) => stream.Release(marker); + + public void Seek(int index) => stream.Seek(index); + } +} diff --git a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs index c5e20b153..9a29ed58d 100644 --- a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs @@ -89,5 +89,15 @@ namespace Antlr4.Runtime { return new CodePointCharStream(s); } + + public static ICharStream toUpper(ICharStream inStream) + { + return new CaseChangingCharStream(inStream, true); + } + + public static ICharStream toLower(ICharStream inStream) + { + return new CaseChangingCharStream(inStream, false); + } } }