diff --git a/contributors.txt b/contributors.txt index f77a4c1d9a..2eedf6400a 100644 --- a/contributors.txt +++ b/contributors.txt @@ -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 \ No newline at end of file diff --git a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.vs2013.csproj b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.vs2013.csproj index b9117f3673..d114415034 100644 --- a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.vs2013.csproj +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.vs2013.csproj @@ -54,6 +54,7 @@ + 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 0000000000..c45ed688e9 --- /dev/null +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CaseChangingCharStream.cs @@ -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 +{ + /// + /// This class supports case-insensitive lexing by wrapping an existing + /// 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'. + /// + public class CaseChangingCharStream : ICharStream + { + private ICharStream stream; + private bool upper; + + /// + /// Constructs a new CaseChangingCharStream wrapping the given forcing + /// all characters to upper case or lower case. + /// + /// The stream to wrap. + /// If true force each symbol to upper case, otherwise force to lower. + 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); + } + } +} diff --git a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs index c5e20b153c..24a228ac79 100644 --- a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs @@ -89,5 +89,27 @@ public static ICharStream fromstring(string s) { return new CodePointCharStream(s); } + + /// + /// Takes the stream and forces all symbols to uppercase for lexing purposes + /// but leaves the original text as-is. + /// + /// + /// + public static ICharStream toUpper(ICharStream inStream) + { + return new CaseChangingCharStream(inStream, true); + } + + /// + /// Takes the stream and forces all symbols to lowercase for lexing purposes + /// but leaves the original text as-is. + /// + /// + /// + public static ICharStream toLower(ICharStream inStream) + { + return new CaseChangingCharStream(inStream, false); + } } }