Skip to content

Commit f80b51d

Browse files
authored
Update PG parser
update antlr4 parser for pg
2 parents dd62df8 + e97f84e commit f80b51d

File tree

9 files changed

+30765
-36500
lines changed

9 files changed

+30765
-36500
lines changed

backend/preprocessor/antlr_parser/pg_parser/PostgreSQLLexer.interp

Lines changed: 128 additions & 365 deletions
Large diffs are not rendered by default.

backend/preprocessor/antlr_parser/pg_parser/PostgreSQLLexer.py

Lines changed: 3279 additions & 3717 deletions
Large diffs are not rendered by default.

backend/preprocessor/antlr_parser/pg_parser/PostgreSQLLexer.tokens

Lines changed: 1096 additions & 1258 deletions
Large diffs are not rendered by default.
Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,95 @@
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.
219

20+
from typing import TextIO
21+
from antlr4 import *
22+
from antlr4.Token import CommonToken
23+
import sys
24+
from typing import TextIO
325

426
class PostgreSQLLexerBase(Lexer):
527

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):
752
super().__init__(input, output)
853
self.tags = []
954

10-
def pushTag(self):
11-
self.tags.append(super().text)
55+
def PushTag(self):
56+
self.tags.append(self.text)
1257

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
1560

16-
def popTag(self):
61+
def PopTag(self):
1762
if self.tags:
1863
self.tags.pop()
1964

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('*')
2273

23-
def charIsLetter(self):
24-
return super().inputStream.LA(-1).isalpha()
74+
def CharIsLetter(self):
75+
return chr(self._input.LA(-1)).isalpha()
2576

2677
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
2980

3081
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
4086

4187
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

Comments
 (0)