-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
operator_precedence.py
139 lines (128 loc) · 4.7 KB
/
operator_precedence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import re
from enum import IntEnum
from typing import Callable, List
from sqlfmt.node import Node
from sqlfmt.token import TokenType
class OperatorPrecedence(IntEnum):
DOUBLE_COLON = 0
AS = 1
SQUARE_BRACKETS = 2
OTHER_TIGHT = 3
EXPONENT = 4
MULTIPLICATION = 5
ADDITION = 6
OTHER = 7
MEMBERSHIP = 8
COMPARATORS = 9
PRESENCE = 10
BOOL_NOT = 11
BOOL_AND = 12
BOOL_OR = 13
ON = 14
@staticmethod
def tiers() -> List["OperatorPrecedence"]:
return [
OperatorPrecedence.OTHER_TIGHT,
OperatorPrecedence.EXPONENT,
OperatorPrecedence.MULTIPLICATION,
OperatorPrecedence.OTHER,
OperatorPrecedence.COMPARATORS,
OperatorPrecedence.BOOL_NOT,
OperatorPrecedence.ON,
]
@classmethod
def from_node(cls, node: Node) -> "OperatorPrecedence":
assert (
node.is_operator
), f"Internal error! {node} is not an operator. Please open an issue"
return cls._function_lookup(token_type=node.token.type)(node)
@classmethod
def _function_lookup(
cls,
token_type: TokenType,
) -> Callable[[Node], "OperatorPrecedence"]:
mapping = {
TokenType.DOUBLE_COLON: lambda x: OperatorPrecedence.DOUBLE_COLON,
TokenType.BRACKET_OPEN: lambda x: OperatorPrecedence.SQUARE_BRACKETS,
TokenType.ON: lambda x: OperatorPrecedence.ON,
TokenType.STAR: lambda x: OperatorPrecedence.MULTIPLICATION,
TokenType.BOOLEAN_OPERATOR: cls._from_boolean,
TokenType.OPERATOR: cls._from_operator,
TokenType.WORD_OPERATOR: cls._from_word_operator,
}
return mapping.get(token_type, lambda x: OperatorPrecedence.OTHER)
@staticmethod
def _from_operator(node: Node) -> "OperatorPrecedence":
value_mapping = {
"+": OperatorPrecedence.ADDITION,
"-": OperatorPrecedence.ADDITION,
"/": OperatorPrecedence.MULTIPLICATION,
"%": OperatorPrecedence.MULTIPLICATION,
"%%": OperatorPrecedence.MULTIPLICATION,
"^": OperatorPrecedence.EXPONENT,
"=": OperatorPrecedence.COMPARATORS,
"==": OperatorPrecedence.COMPARATORS,
"!=": OperatorPrecedence.COMPARATORS,
"<>": OperatorPrecedence.COMPARATORS,
"<=": OperatorPrecedence.COMPARATORS,
">=": OperatorPrecedence.COMPARATORS,
">": OperatorPrecedence.COMPARATORS,
"<": OperatorPrecedence.COMPARATORS,
"~": OperatorPrecedence.MEMBERSHIP,
"~*": OperatorPrecedence.MEMBERSHIP,
"!~": OperatorPrecedence.MEMBERSHIP,
"!~*": OperatorPrecedence.MEMBERSHIP,
"~~": OperatorPrecedence.MEMBERSHIP,
"~~*": OperatorPrecedence.MEMBERSHIP,
"!~~": OperatorPrecedence.MEMBERSHIP,
"!~~*": OperatorPrecedence.MEMBERSHIP,
}
return value_mapping.get(node.value, OperatorPrecedence.OTHER)
@staticmethod
def _from_word_operator(node: Node) -> "OperatorPrecedence":
mapping = [
(OperatorPrecedence.AS, [r"as"]),
(
OperatorPrecedence.OTHER_TIGHT,
[
r"exclude",
r"replace",
r"except",
r"over",
r"within\s+group",
r"filter",
r"using",
],
),
(
OperatorPrecedence.MEMBERSHIP,
[
r"(not\s+)?between",
r"(not\s+)?in",
r"(not\s+)?i?like(\s+any)?",
r"(not\s+)?similar\s+to",
r"(not\s+)?rlike",
r"(not\s+)?regexp",
],
),
(OperatorPrecedence.PRESENCE, [r"is(\s+not)?", r"isnull", r"notnull"]),
]
for precedence, pattern_list in mapping:
programs = [
re.compile(f"{pattern}$", flags=re.IGNORECASE)
for pattern in pattern_list
]
if any([prog.match(node.value) for prog in programs]):
return precedence
else:
return OperatorPrecedence.OTHER
@staticmethod
def _from_boolean(node: Node) -> "OperatorPrecedence":
if node.is_the_and_after_the_between_operator:
return OperatorPrecedence.OTHER_TIGHT
value_mapping = {
"and": OperatorPrecedence.BOOL_AND,
"or": OperatorPrecedence.BOOL_OR,
"not": OperatorPrecedence.BOOL_NOT,
}
return value_mapping.get(node.value, OperatorPrecedence.BOOL_AND)