Add CaseChangingCharStream that converts symbols to upper or lower case

This commit is contained in:
Sebastian Lang 2017-12-01 15:09:14 +01:00 committed by parrt
parent 87ea4c4862
commit 1af14fb287
2 changed files with 57 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -89,5 +89,15 @@ namespace Antlr4.Runtime
{ {
return new CodePointCharStream(s); 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);
}
} }
} }