case-insensitive-lexing: add implementation for Python2/Python3

This commit is contained in:
Kazuki Sawada 2019-08-01 23:01:33 +09:00
parent 013dca5d7d
commit c097636a9b
No known key found for this signature in database
GPG Key ID: 105FFD70FF15272E
2 changed files with 14 additions and 0 deletions

View File

@ -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)

View File

@ -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())