From c097636a9b6f7f5519480b84a687f9d125e4266b Mon Sep 17 00:00:00 2001 From: Kazuki Sawada Date: Thu, 1 Aug 2019 23:01:33 +0900 Subject: [PATCH] case-insensitive-lexing: add implementation for Python2/Python3 --- doc/case-insensitive-lexing.md | 1 + doc/resources/CaseChangingStream.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 doc/resources/CaseChangingStream.py 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())