| 1 |
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. |
| 2 |
* Use of this file is governed by the BSD 3-clause license that |
| 3 |
* can be found in the LICENSE.txt file in the project root. |
| 4 |
*/ |
| 5 |
using System; |
| 6 |
using Antlr4.Runtime.Misc; |
| 7 |
|
| 8 |
namespace Antlr4.Runtime |
| 9 |
{ |
| 10 |
/// <summary> |
| 11 |
/// This class supports case-insensitive lexing by wrapping an existing |
| 12 |
/// <see cref="ICharStream"/> and forcing the lexer to see either upper or |
| 13 |
/// lowercase characters. Grammar literals should then be either upper or |
| 14 |
/// lower case such as 'BEGIN' or 'begin'. The text of the character |
| 15 |
/// stream is unaffected. Example: input 'BeGiN' would match lexer rule |
| 16 |
/// 'BEGIN' if constructor parameter upper=true but getText() would return |
| 17 |
/// 'BeGiN'. |
| 18 |
/// </summary> |
| 19 |
public class CaseChangingCharStream : ICharStream |
| 20 |
{ |
| 21 |
private ICharStream stream; |
| 22 |
private bool upper; |
| 23 |
|
| 24 |
/// <summary> |
| 25 |
/// Constructs a new CaseChangingCharStream wrapping the given <paramref name="stream"/> forcing |
| 26 |
/// all characters to upper case or lower case. |
| 27 |
/// </summary> |
| 28 |
/// <param name="stream">The stream to wrap.</param> |
| 29 |
/// <param name="upper">If true force each symbol to upper case, otherwise force to lower.</param> |
| 30 |
public CaseChangingCharStream(ICharStream stream, bool upper) |
| 31 |
{ |
| 32 |
this.stream = stream; |
| 33 |
this.upper = upper; |
| 34 |
} |
| 35 |
|
| 36 |
public int Index |
| 37 |
{ |
| 38 |
get |
| 39 |
{ |
| 40 |
return stream.Index; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
public int Size |
| 45 |
{ |
| 46 |
get |
| 47 |
{ |
| 48 |
return stream.Size; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
public string SourceName |
| 53 |
{ |
| 54 |
get |
| 55 |
{ |
| 56 |
return stream.SourceName; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public void Consume() |
| 61 |
{ |
| 62 |
stream.Consume(); |
| 63 |
} |
| 64 |
|
| 65 |
[return: NotNull] |
| 66 |
public string GetText(Interval interval) |
| 67 |
{ |
| 68 |
return stream.GetText(interval); |
| 69 |
} |
| 70 |
|
| 71 |
public int LA(int i) |
| 72 |
{ |
| 73 |
int c = stream.LA(i); |
| 74 |
|
| 75 |
if (c <= 0) |
| 76 |
{ |
| 77 |
return c; |
| 78 |
} |
| 79 |
|
| 80 |
char o = (char)c; |
| 81 |
|
| 82 |
if (upper) |
| 83 |
{ |
| 84 |
return (int)char.ToUpperInvariant(o); |
| 85 |
} |
| 86 |
|
| 87 |
return (int)char.ToLowerInvariant(o); |
| 88 |
} |
| 89 |
|
| 90 |
public int Mark() |
| 91 |
{ |
| 92 |
return stream.Mark(); |
| 93 |
} |
| 94 |
|
| 95 |
public void Release(int marker) |
| 96 |
{ |
| 97 |
stream.Release(marker); |
| 98 |
} |
| 99 |
|
| 100 |
public void Seek(int index) |
| 101 |
{ |
| 102 |
stream.Seek(index); |
| 103 |
} |
| 104 |
} |
| 105 |
} |