antlr/doc/resources/CaseChangingCharStream.cs

106 lines
2.6 KiB
C#
Raw Normal View History

2017-12-07 04:23:43 +08:00
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
using System;
using Antlr4.Runtime.Misc;
namespace Antlr4.Runtime
{
2017-12-07 04:23:43 +08:00
/// <summary>
/// This class supports case-insensitive lexing by wrapping an existing
/// <see cref="ICharStream"/> and forcing the lexer to see either upper or
/// lowercase characters. Grammar literals should then be either upper or
/// lower case such as 'BEGIN' or 'begin'. The text of the character
/// stream is unaffected. Example: input 'BeGiN' would match lexer rule
/// 'BEGIN' if constructor parameter upper=true but getText() would return
/// 'BeGiN'.
/// </summary>
public class CaseChangingCharStream : ICharStream
{
private ICharStream stream;
private bool upper;
2017-12-07 04:23:43 +08:00
/// <summary>
/// Constructs a new CaseChangingCharStream wrapping the given <paramref name="stream"/> forcing
/// all characters to upper case or lower case.
/// </summary>
/// <param name="stream">The stream to wrap.</param>
/// <param name="upper">If true force each symbol to upper case, otherwise force to lower.</param>
public CaseChangingCharStream(ICharStream stream, bool upper)
{
this.stream = stream;
this.upper = upper;
}
2017-12-07 04:23:43 +08:00
public int Index
{
get
{
return stream.Index;
}
}
2017-12-07 04:23:43 +08:00
public int Size
{
get
{
return stream.Size;
}
}
2017-12-07 04:23:43 +08:00
public string SourceName
{
get
{
return stream.SourceName;
}
}
2017-12-07 04:23:43 +08:00
public void Consume()
{
stream.Consume();
}
[return: NotNull]
2017-12-07 04:23:43 +08:00
public string GetText(Interval interval)
{
return stream.GetText(interval);
}
public int LA(int i)
{
int c = stream.LA(i);
2017-12-07 04:23:43 +08:00
if (c <= 0)
{
return c;
}
2017-12-07 04:23:43 +08:00
char o = (char)c;
2017-12-07 04:23:43 +08:00
if (upper)
{
return (int)char.ToUpperInvariant(o);
}
2017-12-07 04:23:43 +08:00
return (int)char.ToLowerInvariant(o);
}
2017-12-07 04:23:43 +08:00
public int Mark()
{
return stream.Mark();
}
2017-12-07 04:23:43 +08:00
public void Release(int marker)
{
stream.Release(marker);
}
2017-12-07 04:23:43 +08:00
public void Seek(int index)
{
stream.Seek(index);
}
}
}