diff --git a/doc/case-insensitive-lexing.md b/doc/case-insensitive-lexing.md index 879372eed..4c4248438 100644 --- a/doc/case-insensitive-lexing.md +++ b/doc/case-insensitive-lexing.md @@ -76,3 +76,4 @@ Here are implementations of `CaseChangingCharStream` in various target languages * [Go](https://github.com/antlr/antlr4/blob/master/doc/resources/case_changing_stream.go) * [Java](https://github.com/antlr/antlr4/blob/master/doc/resources/CaseChangingCharStream.java) * [JavaScript](https://github.com/antlr/antlr4/blob/master/doc/resources/CaseChangingStream.js) +* [Python2/3](https://github.com/antlr/antlr4/blob/master/doc/resources/CaseChangingStream.py) diff --git a/doc/resources/CaseChangingStream.py b/doc/resources/CaseChangingStream.py new file mode 100644 index 000000000..6d2815de4 --- /dev/null +++ b/doc/resources/CaseChangingStream.py @@ -0,0 +1,13 @@ +class CaseChangingStream(): + def __init__(self, stream, upper): + self._stream = stream + self._upper = upper + + def __getattr__(self, name): + return self._stream.__getattribute__(name) + + def LA(self, offset): + c = self._stream.LA(offset) + if c <= 0: + return c + return ord(chr(c).upper() if self._upper else chr(c).lower())