Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,6 @@ YYYY/MM/DD, github id, Full name, email
2017/11/05, ajaypanyala, Ajay Panyala, ajay.panyala@gmail.com
2017/11/24, zqlu.cn, Zhiqiang Lu, zqlu.cn@gmail.com
2017/11/28, niccroad, Nicolas Croad, nic.croad@gmail.com
2017/12/03, oranoran, Oran Epelbaum, oran / epelbaum me
2017/12/01, DavidMoraisFerreira, David Morais Ferreira, david.moraisferreira@gmail.com
2017/12/01, SebastianLng, Sebastian Lang, sebastian.lang@outlook.com
2017/12/03, oranoran, Oran Epelbaum, oran / epelbaum me
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<Compile Include="Atn\ATNDeserializationOptions.cs" />
<Compile Include="Atn\ATNDeserializer.cs" />
<Compile Include="Atn\ConflictInfo.cs" />
<Compile Include="CaseChangingCharStream.cs" />
<Compile Include="CharStreams.cs" />
<Compile Include="Dfa\AbstractEdgeMap.cs" />
<Compile Include="Dfa\AcceptStateInfo.cs" />
Expand Down
105 changes: 105 additions & 0 deletions runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CaseChangingCharStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* 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
{
/// <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;

/// <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;
}

public int Index
{
get
{
return stream.Index;
}
}

public int Size
{
get
{
return stream.Size;
}
}

public string SourceName
{
get
{
return stream.SourceName;
}
}

public void Consume()
{
stream.Consume();
}

[return: NotNull]
public string GetText(Interval interval)
{
return stream.GetText(interval);
}

public int LA(int i)
{
int c = stream.LA(i);

if (c <= 0)
{
return c;
}

char o = (char)c;

if (upper)
{
return (int)char.ToUpperInvariant(o);
}

return (int)char.ToLowerInvariant(o);
}

public int Mark()
{
return stream.Mark();
}

public void Release(int marker)
{
stream.Release(marker);
}

public void Seek(int index)
{
stream.Seek(index);
}
}
}
22 changes: 22 additions & 0 deletions runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,27 @@ public static ICharStream fromstring(string s)
{
return new CodePointCharStream(s);
}

/// <summary>
/// Takes the stream and forces all symbols to uppercase for lexing purposes
/// but leaves the original text as-is.
/// </summary>
/// <param name="inStream"></param>
/// <returns></returns>
public static ICharStream toUpper(ICharStream inStream)
{
return new CaseChangingCharStream(inStream, true);
}

/// <summary>
/// Takes the stream and forces all symbols to lowercase for lexing purposes
/// but leaves the original text as-is.
/// </summary>
/// <param name="inStream"></param>
/// <returns></returns>
public static ICharStream toLower(ICharStream inStream)
{
return new CaseChangingCharStream(inStream, false);
}
}
}