|
1 |
| -from antlr4 import * |
| 1 | +# PostgreSQL grammar. |
| 2 | +# The MIT License (MIT). |
| 3 | +# Copyright (c) 2021-2023, Oleksii Kovalov (Oleksii.Kovalov@outlook.com). |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +# of this software and associated documentation files (the "Software"), to deal |
| 6 | +# in the Software without restriction, including without limitation the rights |
| 7 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +# copies of the Software, and to permit persons to whom the Software is |
| 9 | +# furnished to do so, subject to the following conditions: |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 13 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 16 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 18 | +# THE SOFTWARE. |
2 | 19 |
|
| 20 | +from typing import TextIO |
| 21 | +from antlr4 import * |
| 22 | +from antlr4.Token import CommonToken |
| 23 | +import sys |
| 24 | +from typing import TextIO |
3 | 25 |
|
4 | 26 | class PostgreSQLLexerBase(Lexer):
|
5 | 27 |
|
6 |
| - def __init__(self, input, output): |
| 28 | + def IsColumnZero(self): |
| 29 | + return self.column == 0 |
| 30 | + |
| 31 | + def VerifyNotOperator(): |
| 32 | + c1 = this.InputStream.LA(1); |
| 33 | + if (c1 == 'a'): |
| 34 | + c2 = this.InputStream.LA(2); |
| 35 | + if (c2 == 'n'): |
| 36 | + c3 = this.InputStream.LA(3); |
| 37 | + if (c3 == 'd'): |
| 38 | + c4 = this.InputStream.LA(4); |
| 39 | + if (c4 == '.'): |
| 40 | + return false; |
| 41 | + elif (c1 == 'o'): |
| 42 | + c2 = this.InputStream.LA(2); |
| 43 | + if (c2 == 'r'): |
| 44 | + c3 = this.InputStream.LA(3); |
| 45 | + if (c3 == '.'): |
| 46 | + return false; |
| 47 | + return true; |
| 48 | + |
| 49 | + |
| 50 | +class PostgreSQLLexerBase(Lexer): |
| 51 | + def __init__(self, input: InputStream, output: TextIO = sys.stdout): |
7 | 52 | super().__init__(input, output)
|
8 | 53 | self.tags = []
|
9 | 54 |
|
10 |
| - def pushTag(self): |
11 |
| - self.tags.append(super().text) |
| 55 | + def PushTag(self): |
| 56 | + self.tags.append(self.text) |
12 | 57 |
|
13 |
| - def isTag(self): |
14 |
| - return super().text == self.tags[-1] if self.tags else False |
| 58 | + def IsTag(self): |
| 59 | + return self.text == self.tags[-1] if self.tags else False |
15 | 60 |
|
16 |
| - def popTag(self): |
| 61 | + def PopTag(self): |
17 | 62 | if self.tags:
|
18 | 63 | self.tags.pop()
|
19 | 64 |
|
20 |
| - def checkLA(self, c): |
21 |
| - return super().inputStream.LA(1) != c |
| 65 | + def UnterminatedBlockCommentDebugAssert(self): |
| 66 | + assert self._input.LA(1) == -1 # EOF |
| 67 | + |
| 68 | + def CheckLaMinus(self): |
| 69 | + return self._input.LA(1) != ord('-') |
| 70 | + |
| 71 | + def CheckLaStar(self): |
| 72 | + return self._input.LA(1) != ord('*') |
22 | 73 |
|
23 |
| - def charIsLetter(self): |
24 |
| - return super().inputStream.LA(-1).isalpha() |
| 74 | + def CharIsLetter(self): |
| 75 | + return chr(self._input.LA(-1)).isalpha() |
25 | 76 |
|
26 | 77 | def HandleNumericFail(self):
|
27 |
| - super().inputStream.seek(super().inputStream.index() - 2) |
28 |
| - super().type(658) |
| 78 | + self._input.seek(self._input.index - 2) |
| 79 | + self.type = PostgreSQLLexer.INTEGRAL |
29 | 80 |
|
30 | 81 | def HandleLessLessGreaterGreater(self):
|
31 |
| - text = super().text |
32 |
| - if text == "<<": |
33 |
| - super().type(18) |
34 |
| - elif text == ">>": |
35 |
| - super().type(19) |
36 |
| - |
37 |
| - def UnterminatedBlockCommentDebugAssert(self): |
38 |
| - # Debug.Assert(InputStream.LA(1) == -1 /*EOF*/) |
39 |
| - pass |
| 82 | + if self.text == "<<": |
| 83 | + self.type = PostgreSQLLexer.LESS_LESS |
| 84 | + elif self.text == ">>": |
| 85 | + self.type = PostgreSQLLexer.GREATER_GREATER |
40 | 86 |
|
41 | 87 | def CheckIfUtf32Letter(self):
|
42 |
| - code_point = super().inputStream.LA(-2) << 8 + super().inputStream.LA(-1) |
43 |
| - if code_point < 0x10000: |
44 |
| - c = [chr(code_point)] |
45 |
| - else: |
46 |
| - code_point -= 0x10000 |
47 |
| - c = [chr(code_point // 0x400 + 0xd800), |
48 |
| - chr(code_point % 0x400 + 0xdc00)] |
49 |
| - return c[0].isalpha() |
| 88 | + try: |
| 89 | + char = chr(int.from_bytes((chr(self._input.LA(-2)) + chr(self._input.LA(-1))).encode("utf-32"), 'little')) |
| 90 | + return char.isalpha() |
| 91 | + except ValueError: |
| 92 | + return False |
| 93 | + |
| 94 | + def IsSemiColon(self): |
| 95 | + return chr(self._input.LA(1)) == ';' |
0 commit comments