From c941919d3d6c8e0e650ff1402c149f5b6c90d109 Mon Sep 17 00:00:00 2001 From: Sebastian Lang Date: Fri, 1 Dec 2017 15:09:14 +0100 Subject: [PATCH 1/4] Add CaseChangingCharStream that converts symbols to upper or lower case --- .../Antlr4.Runtime/CaseChangingCharStream.cs | 47 +++++++++++++++++++ .../CSharp/Antlr4.Runtime/CharStreams.cs | 10 ++++ 2 files changed, 57 insertions(+) create mode 100644 runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CaseChangingCharStream.cs 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..060052727d --- /dev/null +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CaseChangingCharStream.cs @@ -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); + } +} diff --git a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs index c5e20b153c..9a29ed58da 100644 --- a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs @@ -89,5 +89,15 @@ public static ICharStream fromstring(string 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); + } } } From e011f6f7c39b2874441012156a06e7dfdda17dab Mon Sep 17 00:00:00 2001 From: David Morais Ferreira Date: Fri, 1 Dec 2017 23:55:16 +0100 Subject: [PATCH 2/4] Use older CSharp syntax, added comments and added file to csproj --- .../Antlr4.Runtime.vs2013.csproj | 1 + .../Antlr4.Runtime/CaseChangingCharStream.cs | 70 ++++++++++++++++--- .../CSharp/Antlr4.Runtime/CharStreams.cs | 12 ++++ 3 files changed, 74 insertions(+), 9 deletions(-) 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 index 060052727d..1132c1d523 100644 --- a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CaseChangingCharStream.cs +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CaseChangingCharStream.cs @@ -1,29 +1,72 @@ -using System; +/* 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 => stream.Index; + public int Index + { + get + { + return stream.Index; + } + } - public int Size => stream.Size; + public int Size + { + get + { + return stream.Size; + } + } - public string SourceName => stream.SourceName; + public string SourceName + { + get + { + return stream.SourceName; + } + } - public void Consume() => stream.Consume(); + public void Consume() + { + stream.Consume(); + } [return: NotNull] - public string GetText(Interval interval) => stream.GetText(interval); + public string GetText(Interval interval) + { + return stream.GetText(interval); + } public int LA(int i) { @@ -38,10 +81,19 @@ public int LA(int i) return Convert.ToInt32(char.ToLower(o)); } - public int Mark() => stream.Mark(); + public int Mark() + { + return stream.Mark(); + } - public void Release(int marker) => stream.Release(marker); + public void Release(int marker) + { + stream.Release(marker); + } - public void Seek(int index) => stream.Seek(index); + 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 9a29ed58da..24a228ac79 100644 --- a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs @@ -90,11 +90,23 @@ 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); From 334df5dd940ca9914e4432b52cc4bdd67e37b1a6 Mon Sep 17 00:00:00 2001 From: David Morais Ferreira Date: Fri, 1 Dec 2017 23:55:34 +0100 Subject: [PATCH 3/4] Signed contributors.txt --- contributors.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/contributors.txt b/contributors.txt index 5ee90f1922..a96377ec88 100644 --- a/contributors.txt +++ b/contributors.txt @@ -173,4 +173,6 @@ YYYY/MM/DD, github id, Full name, email 2017/10/29, gendalph, Максим Прохоренко, Maxim\dotProhorenko@gm@il.com 2017/11/02, jasonmoo, Jason Mooberry, jason.mooberry@gmail.com 2017/11/05, ajaypanyala, Ajay Panyala, ajay.panyala@gmail.com -2017/11/24, zqlu.cn, Zhiqiang Lu, zqlu.cn@gmail.com \ No newline at end of file +2017/11/24, zqlu.cn, Zhiqiang Lu, zqlu.cn@gmail.com +2017/12/01, DavidMoraisFerreira, David Morais Ferreira, david.moraisferreira@gmail.com +2017/12/01, SebastianLng, Sebastian Lang, sebastian.lang@outlook.com \ No newline at end of file From d067c397d4cfdbb22cd784694afa56cbdf3b79a6 Mon Sep 17 00:00:00 2001 From: David Morais Ferreira Date: Sat, 2 Dec 2017 11:53:26 +0100 Subject: [PATCH 4/4] Implement proposed changes --- .../Antlr4.Runtime/CaseChangingCharStream.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CaseChangingCharStream.cs b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CaseChangingCharStream.cs index 1132c1d523..c45ed688e9 100644 --- a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CaseChangingCharStream.cs +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CaseChangingCharStream.cs @@ -72,13 +72,19 @@ public int LA(int i) { int c = stream.LA(i); - if (c <= 0) return c; + if (c <= 0) + { + return c; + } - var o = Convert.ToChar(c); + char o = (char)c; - if (upper) return Convert.ToInt32(char.ToUpper(o)); + if (upper) + { + return (int)char.ToUpperInvariant(o); + } - return Convert.ToInt32(char.ToLower(o)); + return (int)char.ToLowerInvariant(o); } public int Mark()