forked from jasder/antlr
Add CaseChangingCharStream that converts symbols to upper or lower case
This commit is contained in:
parent
87ea4c4862
commit
1af14fb287
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue