Skip to content

Commit d6763dc

Browse files
committed
Change grouping from _left_right to _group
1 parent a653650 commit d6763dc

1 file changed

Lines changed: 71 additions & 18 deletions

File tree

sqlparse/engine/grouping.py

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,42 +91,95 @@ def _group_left_right(tlist, m, cls,
9191

9292

9393
def group_typecasts(tlist):
94-
_group_left_right(tlist, (T.Punctuation, '::'), sql.Identifier)
94+
def match(token):
95+
return token.match(T.Punctuation, '::')
96+
97+
def valid(token):
98+
return token is not None
99+
100+
def post(tlist, pidx, tidx, nidx):
101+
return pidx, nidx
102+
103+
valid_prev = valid_next = valid
104+
_group(tlist, sql.Identifier, match, valid_prev, valid_next, post)
95105

96106

97107
def group_period(tlist):
98-
lfunc = lambda tk: imt(tk, i=(sql.SquareBrackets, sql.Identifier),
99-
t=(T.Name, T.String.Symbol,))
108+
def match(token):
109+
return token.match(T.Punctuation, '.')
110+
111+
def valid_prev(token):
112+
sqlcls = sql.SquareBrackets, sql.Identifier
113+
ttypes = T.Name, T.String.Symbol
114+
return imt(token, i=sqlcls, t=ttypes)
115+
116+
def valid_next(token):
117+
sqlcls = sql.SquareBrackets, sql.Function
118+
ttypes = T.Name, T.String.Symbol, T.Wildcard
119+
return imt(token, i=sqlcls, t=ttypes)
100120

101-
rfunc = lambda tk: imt(tk, i=(sql.SquareBrackets, sql.Function),
102-
t=(T.Name, T.String.Symbol, T.Wildcard))
121+
def post(tlist, pidx, tidx, nidx):
122+
return pidx, nidx
103123

104-
_group_left_right(tlist, (T.Punctuation, '.'), sql.Identifier,
105-
valid_left=lfunc, valid_right=rfunc)
124+
_group(tlist, sql.Identifier, match, valid_prev, valid_next, post)
106125

107126

108127
def group_as(tlist):
109-
lfunc = lambda tk: not imt(tk, t=T.Keyword) or tk.normalized == 'NULL'
110-
rfunc = lambda tk: not imt(tk, t=(T.DML, T.DDL))
111-
_group_left_right(tlist, (T.Keyword, 'AS'), sql.Identifier,
112-
valid_left=lfunc, valid_right=rfunc)
128+
def match(token):
129+
return token.is_keyword and token.normalized == 'AS'
130+
131+
def valid_prev(token):
132+
return token.normalized == 'NULL' or not token.is_keyword
133+
134+
def valid_next(token):
135+
ttypes = T.DML, T.DDL
136+
return not imt(token, t=ttypes)
137+
138+
def post(tlist, pidx, tidx, nidx):
139+
return pidx, nidx
140+
141+
_group(tlist, sql.Identifier, match, valid_prev, valid_next, post)
113142

114143

115144
def group_assignment(tlist):
116-
_group_left_right(tlist, (T.Assignment, ':='), sql.Assignment,
117-
semicolon=True)
145+
def match(token):
146+
return token.match(T.Assignment, ':=')
147+
148+
def valid(token):
149+
return token is not None
150+
151+
def post(tlist, pidx, tidx, nidx):
152+
m_semicolon = T.Punctuation, ';'
153+
snidx, _ = tlist.token_next_by(m=m_semicolon, idx=nidx)
154+
nidx = snidx or nidx
155+
return pidx, nidx
156+
157+
valid_prev = valid_next = valid
158+
_group(tlist, sql.Assignment, match, valid_prev, valid_next, post)
118159

119160

120161
def group_comparison(tlist):
121162
sqlcls = (sql.Parenthesis, sql.Function, sql.Identifier,
122-
sql.Operation)
163+
sql.Operation)
123164
ttypes = T_NUMERICAL + T_STRING + T_NAME
124165

125-
func = lambda tk: (imt(tk, t=ttypes, i=sqlcls) or
126-
(tk and tk.is_keyword and tk.normalized == 'NULL'))
166+
def match(token):
167+
return token.ttype == T.Operator.Comparison
168+
169+
def valid(token):
170+
if imt(token, t=ttypes, i=sqlcls):
171+
return True
172+
elif token and token.is_keyword and token.normalized == 'NULL':
173+
return True
174+
else:
175+
return False
176+
177+
def post(tlist, pidx, tidx, nidx):
178+
return pidx, nidx
127179

128-
_group_left_right(tlist, (T.Operator.Comparison, None), sql.Comparison,
129-
valid_left=func, valid_right=func)
180+
valid_prev = valid_next = valid
181+
_group(tlist, sql.Comparison, match,
182+
valid_prev, valid_next, post, extend=False)
130183

131184

132185
@recurse(sql.Identifier)

0 commit comments

Comments
 (0)