MCPcopy Create free account
hub / github.com/LHRLAB/ChatKBQA / _consume_a_node

Function _consume_a_node

components/expr_parser.py:189–230  ·  view source on GitHub ↗
(tokens, cursor, data_type)

Source from the content-addressed store, hash-verified

187 return self.val
188
189def _consume_a_node(tokens, cursor, data_type):
190 is_root = cursor == 0
191 cur_tok = tokens[cursor]
192 cursor += 1
193 if cur_tok == '(':
194 node, cursor = _consume_a_node(tokens, cursor, data_type)
195 assert tokens[cursor] == ')'
196 cursor += 1
197 elif cur_tok == 'AND':
198 # left, right, all unary
199 left, cursor = _consume_a_node(tokens, cursor, ASTNode.UNARY)
200 right, cursor = _consume_a_node(tokens, cursor, ASTNode.UNARY)
201 node = ASTNode.build(cur_tok, data_type, [left, right])
202 elif cur_tok == 'JOIN':
203 # if cur is unary, right unary, else right binary
204 left, cursor = _consume_a_node(tokens, cursor, ASTNode.BINARY)
205 right, cursor = _consume_a_node(tokens, cursor, data_type)
206 node = ASTNode.build(cur_tok, data_type, [left, right])
207 elif cur_tok == 'ARGMIN' or cur_tok == 'ARGMAX':
208 left, cursor = _consume_a_node(tokens, cursor, ASTNode.UNARY)
209 right, cursor = _consume_a_node(tokens, cursor, ASTNode.BINARY)
210 node = ASTNode.build(cur_tok, data_type, [left, right])
211 elif cur_tok == 'le' or cur_tok == 'lt' or cur_tok == 'ge' or cur_tok == 'gt':
212 left, cursor = _consume_a_node(tokens, cursor, ASTNode.UNARY)
213 right, cursor = _consume_a_node(tokens, cursor, ASTNode.UNARY)
214 node = ASTNode.build(cur_tok, data_type, [left, right])
215 elif cur_tok == 'R':
216 child, cursor = _consume_a_node(tokens, cursor, ASTNode.BINARY)
217 node = ASTNode.build(cur_tok, data_type, [child])
218 elif cur_tok == 'COUNT':
219 child, cursor = _consume_a_node(tokens, cursor, ASTNode.UNARY)
220 node = ASTNode.build(cur_tok, data_type, [child])
221 else:
222 # symbol
223 # class relation
224 # value
225 # entity
226 node = ASTNode.build(cur_tok, ASTNode.UNARY, [])
227 if is_root:
228 node.assign_depth_and_level()
229
230 return node, cursor
231
232# top lvel: and, arg, count, cant be JOIN, LE, R
233def parse_s_expr(expr):

Callers 1

parse_s_exprFunction · 0.85

Calls 2

buildMethod · 0.80

Tested by

no test coverage detected