Changeset 429
- Timestamp:
- 05/21/07 02:29:18 (2 years ago)
- Files:
-
- cly/trunk/cly/extra.py (modified) (3 diffs)
- cly/trunk/cly/__init__.py (modified) (7 diffs)
- cly/trunk/cly/types.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
cly/trunk/cly/extra.py
r428 r429 44 44 45 45 46 def integrate(root, *functions):46 def integrate(root, functions): 47 47 """Use heuristics to integrate functions into a node. 48 48 … … 51 51 each argument with a default is converted into a non-optional Variable. 52 52 53 >>> from cly import Grammar , Parser53 >>> from cly import Grammar 54 54 >>> def foo_bar(one, two, three="Baz", four="Waz"): 55 55 ... print one, two, three, four 56 56 >>> grammar = Grammar() 57 >>> integrate(grammar, foo_bar)57 >>> integrate(grammar, [foo_bar]) 58 58 >>> for i in grammar.walk(): print i 59 59 <Grammar:/> … … 107 107 108 108 109 def quickstart( *grammar_or_callables, **interact_args):109 def quickstart(grammar_or_callables, *interact_args, **interact_kwargs): 110 110 """Start an interactive session from a grammar, or from inspecting a set of 111 111 callables.""" 112 if len(grammar_or_callables) == 1 and isinstance(grammar_or_callables[0],113 (Grammar, Parser)):114 interact = Interact(grammar_or_callables[0], **interact_args)112 if isinstance(grammar_or_callables, (Grammar, Parser)): 113 interact = Interact(grammar_or_callables, *interact_args, 114 **interact_kwargs) 115 115 else: 116 116 grammar = Grammar() 117 integrate(grammar, *grammar_or_callables)118 interact = Interact(grammar, * *interact_args)117 integrate(grammar, grammar_or_callables) 118 interact = Interact(grammar, *interact_args, **interact_kwargs) 119 119 interact.interact_loop() 120 120 cly/trunk/cly/__init__.py
r428 r429 321 321 322 322 def children(self, context, follow=False): 323 """Iterate over child nodes, optionally follow()ing branches.""" 323 """Iterate over child nodes, optionally follow()ing branches. 324 >>> tree = Node('One')(two=Node('Two', three=Node('Three'), 325 ... four=Node('Four')), five=Alias('../two/*')) 326 >>> context = Context(None, None) 327 >>> for i in tree.children(context): print i 328 <Alias:/five for /two/*> 329 <Node:/two> 330 >>> for i in tree.children(context, follow=True): print i 331 <Node:/two/four> 332 <Node:/two/three> 333 <Node:/two> 334 """ 324 335 for child in self: 325 336 if child.valid(context): … … 345 356 346 357 def next(self, context): 347 """Return an iterable o f the next potentialnodes."""358 """Return an iterable over the set of next candidate nodes.""" 348 359 for child in self.children(context, follow=True): 349 360 yield child … … 652 663 653 664 def selected(self, context, match): 654 """Convert the match to a value with self.parse _value(), then add665 """Convert the match to a value with self.parse(), then add 655 666 the result to the context "vars" member. 656 667 … … 664 675 """ 665 676 try: 666 value = self.parse( match)677 value = self.parse(context, match) 667 678 except ValidationError, e: 668 679 raise ValidationError(context, token=match.group(), … … 674 685 return Node.selected(self, context, match) 675 686 676 def parse(self, match):687 def parse(self, context, match): 677 688 """Parse the match and return a value. Value can be of any type: tuple, 678 689 list, object, etc. … … 682 693 683 694 >>> v = Variable('Test') 684 >>> v.parse( re.match(r'\w+', 'test'))695 >>> v.parse(None, re.match(r'\w+', 'test')) 685 696 'test' 686 697 """ … … 985 996 986 997 987 def quickstart( *grammar_or_callables, **interact_args):998 def quickstart(grammar_or_callables, *interact_args, **interact_kwargs): 988 999 """Start an interactive session from a grammar, or from inspecting a set of 989 1000 callables.""" 990 1001 from cly.extra import quickstart 991 quickstart( *grammar_or_callables, **interact_args)1002 quickstart(grammar_or_callables, *interact_args, **interact_kwargs) 992 1003 993 1004 cly/trunk/cly/types.py
r425 r429 45 45 pattern = r"""(\w+)|"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'""" 46 46 47 def parse(self, match):47 def parse(self, context, match): 48 48 return match.group(match.lastindex).decode('string_escape') 49 49 … … 65 65 self.allow_fragments = allow_fragments 66 66 67 #def parse(self, match):67 #def parse(self, context, match): 68 68 #import urlparse 69 69 #return urlparse.urlparse(match.string[match.start():match.end()], self.scheme, self.allow_fragments) … … 93 93 pattern = r'\d+' 94 94 95 def parse(self, match):95 def parse(self, context, match): 96 96 return int(match.group()) 97 97 … … 107 107 1234500000000.0 108 108 """ 109 pattern = r'[-+]?[0-9]*\.?[0-9]+( [eE][-+]?[0-9]+)?'110 111 def parse(self, match):109 pattern = r'[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?' 110 111 def parse(self, context, match): 112 112 return float(match.group()) 113 113 … … 125 125 pattern = r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' 126 126 127 def parse(self, match):127 def parse(self, context, match): 128 128 return tuple(map(int, match.groups())) 129 129 … … 142 142 pattern = r'(?i)([A-Z0-9][A-Z0-9_-]*)(?:\.([A-Z0-9][A-Z0-9_-]*))*' 143 143 144 def parse(self, match):144 def parse(self, context, match): 145 145 return tuple(match.group().split('.')) 146 146 … … 161 161 pattern = r'(?i)(%s)|(%s)' % (IP.pattern, Hostname.pattern) 162 162 163 def parse(self, match):163 def parse(self, context, match): 164 164 components = match.string[match.start():match.end()].split('.') 165 165 if match.lastindex == 1:
