Changeset 451
- Timestamp:
- 08/21/07 07:25:45 (11 months ago)
- Files:
-
- pyndexter/trunk/pyndexter/__init__.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
pyndexter/trunk/pyndexter/__init__.py
r450 r451 196 196 197 197 198 # TODO Separate lexer and parser identifiers 198 199 NULL = 0 199 200 TERM = 1 … … 201 202 AND = 3 202 203 OR = 4 204 ATTR = 5 205 BEGINSUB = 6 206 ENDSUB = 7 203 207 204 208 __slots__ = ('type', 'value', 'left', 'right') … … 211 215 212 216 def __repr__(self): 213 type_map = ('null', 'term', 'not', 'and', 'or') 217 type_map = {self.NULL: 'null', self.TERM: 'term', self.NOT: 'not', 218 self.AND: 'and', self.OR: 'or', self.ATTR: 'attr'} 214 219 def show(node, depth=0): 215 220 if node.type == QueryNode.TERM: 216 221 text = '%s("%s"' % (' ' * depth, node.value) 222 elif node.type == QueryNode.ATTR: 223 text = '%s(%s:"%s"' % (' ' * depth, node.value[0], node.value[1]) 217 224 else: 218 text = "%s(%s%s" % (' ' * depth, type_map[node.type], node.value and ' "%s"' % node.valueor "")225 text = "%s(%s%s" % (' ' * depth, type_map[node.type], node.value and ' "%s"' % (node.value,) or "") 219 226 if node.left or node.right: 220 227 text += "\n" … … 244 251 -<term> exclude documents containing this term 245 252 <term> or <term> return documents matching either term 253 <attr>:<term> return documents with term in the specified attribute 246 254 247 255 eg. … … 270 278 """ 271 279 272 _tokenise_re = re.compile(r"(?P<ex>-)|(?P<or>or)|\"(?P<dq>(?:\\.|[^\"])*)\"|'(?P<sq>(?:\\.|[^'])*)'|(?P< te>(?:\S)+)", re.I)280 _tokenise_re = re.compile(r"(?P<ex>-)|(?P<or>or)|\"(?P<dq>(?:\\.|[^\"])*)\"|'(?P<sq>(?:\\.|[^'])*)'|(?P<ss>\()|(?P<se>\))|(?P<at>\w+?:\w+)|(?P<te>\w+)", re.I) 273 281 _group_map = {'dq': QueryNode.TERM, 'sq': QueryNode.TERM, 'te': QueryNode.TERM, 274 'ex': QueryNode.NOT, 'or': QueryNode.OR} 282 'ex': QueryNode.NOT, 'or': QueryNode.OR, 'at': QueryNode.ATTR, 283 'ss': QueryNode.BEGINSUB, 'se': QueryNode.ENDSUB} 275 284 276 285 def __init__(self, phrase): … … 286 295 287 296 def parse(self, tokens): 288 # TODO: add support for sub-expressions eg. "(a b) or c"289 297 left = self.parse_unary(tokens) 290 298 if tokens: 299 if tokens[0][0] == QueryNode.ENDSUB: 300 return left 291 301 if tokens[0][0] == QueryNode.OR: 292 302 tokens.pop(0) … … 307 317 if not tokens: 308 318 return None 319 if tokens[0][0] == QueryNode.BEGINSUB: 320 tokens.pop(0) 321 node = self.parse(tokens) 322 if not tokens or tokens[0][0] != QueryNode.ENDSUB: 323 raise InvalidQuery('Expected ) at end of sub-expression') 324 tokens.pop(0) 325 return node 309 326 if tokens[0][0] == QueryNode.NOT: 310 327 tokens.pop(0) … … 322 339 if not tokens: 323 340 raise InvalidQuery('Unexpected end of string') 341 if tokens[0][0] == QueryNode.ATTR: 342 token = tokens.pop(0) 343 attr, value = token[1].split(':', 1) 344 return QueryNode(QueryNode.ATTR, (attr, value)) 324 345 if tokens[0][0] in (QueryNode.TERM, QueryNode.OR): 325 346 token = tokens.pop(0) … … 350 371 from compiler import ast, misc, pycodegen 351 372 373 # TODO Make Query.ATTR work 352 374 def _generate(node): 353 375 if node.type == node.TERM:
