Changeset 552
- Timestamp:
- 06/15/08 05:17:05 (2 months ago)
- Files:
-
- cly/trunk/cly/builder.py (modified) (10 diffs)
- cly/trunk/cly/interactive.py (modified) (3 diffs)
- cly/trunk/cly/parser.py (modified) (7 diffs)
- cly/trunk/cly/test.py (modified) (6 diffs)
- cly/trunk/.todo (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
cly/trunk/cly/builder.py
r551 r552 235 235 predicate = lambda node: True 236 236 237 def walk(root):237 def _walk(root): 238 238 if not predicate(root): 239 239 return 240 240 yield root 241 241 for node in root._children.itervalues(): 242 for subnode in walk(node):242 for subnode in _walk(node): 243 243 yield subnode 244 244 245 for node in walk(self):245 for node in _walk(self): 246 246 yield node 247 247 … … 608 608 if not self.condition(context): 609 609 return [] 610 return Node.children(self, context )610 return Node.children(self, context, follow=True) 611 611 612 612 … … 615 615 callable. 616 616 617 :param callback: Callback to execute when the action is chosen. 617 618 :attr with_context: If True, passes the current parse Context as the first 618 619 argument. … … 632 633 pattern = '$' 633 634 group = 9999 634 with_user_context = None635 635 with_context = None 636 636 … … 643 643 644 644 def terminal(self, context): 645 if self.with_user_context or \ 646 (self.with_user_context is None and 647 context.parser.with_user_context): 648 warnings.warn('with_user_context has been deprecated. Use ' 649 'with_context and access the user_context attribute.', 650 DeprecationWarning, stacklevel=2) 651 return self.callback(context.user_context, **context.vars) 652 elif self.with_context or \ 653 (self.with_context is None and 654 context.parser.with_context): 645 if self.with_context: 655 646 return self.callback(context, **context.vars) 656 647 else: … … 876 867 if 'context' in locals: 877 868 context = locals.pop('context') 878 user_context = context.user_context879 if isinstance( user_context, dict):880 locals.update( user_context)869 data = context.data 870 if isinstance(data, dict): 871 locals.update(data) 881 872 vars = context.vars 882 873 else: 883 user_context= {}874 data = {} 884 875 vars = {} 885 876 locals.update(vars) … … 887 878 locals['a'] = args 888 879 locals['kw'] = kwargs 889 locals[' c'] = user_context880 locals['d'] = data 890 881 locals['defined'] = lambda v: v in locals 891 882 try: … … 933 924 934 925 Attributes that are methods on the Node will be evaluated as expressions. 935 All variables from the parse Context, user_contextdictionary, and keyword926 All variables from the parse Context, "data" dictionary, and keyword 936 927 arguments (in that order) are available as locals to the evaluated 937 928 expression, as well as some additional variables: … … 939 930 v 940 931 All variables from the parse Context. 941 c942 The user_contextdictionary.932 d 933 The "data" dictionary. 943 934 a 944 935 Any positional arguments. … … 982 973 983 974 g = XMLGrammar('example.xml') 984 interact(g, user_context={'echo': echo})975 interact(g, data={'echo': echo}) 985 976 986 977 """ cly/trunk/cly/interactive.py
r547 r552 190 190 The prompt. 191 191 192 :param user_context:192 :param data: 193 193 A user-specified object to pass to the parser. The parser builds each 194 194 parse ``Context`` with this object, which in turn will deliver this … … 213 213 214 214 def __init__(self, grammar_or_parser, application='cly', prompt=None, 215 user_context=None, with_user_context=None, with_context=None, 216 history_file=None, history_length=500, 215 data=None, history_file=None, history_length=500, 217 216 inhibit_exceptions=False, with_backtrace=False): 218 217 if prompt is None: … … 221 220 history_file = os.path.expanduser('~/.%s_history' % application) 222 221 if isinstance(grammar_or_parser, Grammar): 223 parser = Parser(grammar_or_parser, 224 user_context=user_context, 225 with_user_context=with_user_context, 226 with_context=with_context) 222 parser = Parser(grammar_or_parser, data=data) 227 223 else: 228 224 parser = grammar_or_parser 229 225 230 if user_context is not None:231 parser.user_context = user_context232 226 self.parser = parser 233 self. user_context = user_context227 self.data = data 234 228 235 229 self.input_driver = InputDriver( cly/trunk/cly/parser.py
r550 r552 104 104 105 105 """ 106 def __init__(self, parser, command, user_context=None):106 def __init__(self, parser, command, data=None): 107 107 self.parser = parser 108 108 self.command = command 109 109 self.cursor = 0 110 self. user_context = user_context110 self.data = data 111 111 self.vars = {} 112 112 self._traversed = {} … … 243 243 class Parser(object): 244 244 """Parse and execute CLY grammars. 245 246 A grammar is simply a data structure. To actually utilise it one needs to bind 247 it to a ``Parser`` object and parse some input with it. The parser takes care 248 of creating a ``Context`` for each parse run, parsing the input, and executing 249 any callbacks. 250 251 .. note:: 252 *parser* in this context refers to parsing user input, *not* parsing CLY 253 grammars. 245 246 A Parser object parses user input using a CLY grammar. For each parse, the 247 parser creates a ``Context`` containing the state for the run, parses the 248 input, and executes any callbacks. 249 250 :param grammar: Grammar to parse with. 251 :param data: User data to attach to Context. 254 252 """ 255 def __init__(self, grammar, user_context=None, with_user_context=False, 256 with_context=False): 257 """Construct a new Parser. 258 259 :param grammar: Grammar object. 260 :param user_context: A user-defined object. 261 :param with_context: If True, pass current Context as the first 262 argument to all action callbacks. 263 """ 253 def __init__(self, grammar, data=None): 254 """Construct a new Parser.""" 264 255 self.grammar = grammar 265 self.user_context = user_context 266 self.with_user_context = with_user_context 267 self.with_context = with_context 256 self.data = data 268 257 269 258 def _set_grammar(self, grammar): … … 290 279 yield node 291 280 292 def parse(self, command, user_context=None):281 def parse(self, command, data=None): 293 282 """Parse command using the current grammar. 294 283 … … 296 285 of the parser. 297 286 298 If a user_context is provided it will be passed on to any ``Action`` 299 node callbacks that have set ``with_context=True``. 287 :param command: String to parse. 288 :param data: The Context object has this as a data member, available to 289 any ``Action`` node callbacks that have set 290 ``with_context=True``. 300 291 301 292 >>> from cly.builder import Grammar, Node, Action … … 310 301 <Context command:'two four' remaining:'four'> 311 302 """ 312 context = Context(self, command, user_context or self.user_context) 303 if data is None: 304 data = self.data 305 context = Context(self, command, data) 313 306 314 307 def parse(node, match): … … 330 323 return context 331 324 332 def execute(self, command, user_context=None):325 def execute(self, command, data=None): 333 326 """Parse and execute the given command. 334 327 … … 339 332 'foo bar' 340 333 """ 341 return self.parse(command, user_context).execute()334 return self.parse(command, data).execute() 342 335 343 336 def find(self, path): cly/trunk/cly/test.py
r548 r552 34 34 35 35 grammar = XMLGrammar(xml) 36 parser = Parser(grammar, user_context={'echo': self._echo})36 parser = Parser(grammar, data={'echo': self._echo}) 37 37 parser.execute('echo magic') 38 38 self.assertEqual(self._output, (('magic',), {})) … … 70 70 71 71 grammar = XMLGrammar(xml) 72 parser = Parser(grammar, user_context={'echo': self._echo})72 parser = Parser(grammar, data={'echo': self._echo}) 73 73 parser.execute('echo magic monkey') 74 74 self.assertEqual(self._output, ((['magic', 'monkey'],), {})) … … 89 89 90 90 grammar = XMLGrammar(xml) 91 parser = Parser(grammar, user_context={'echo': self._echo})91 parser = Parser(grammar, data={'echo': self._echo}) 92 92 context = parser.parse('echo ') 93 93 self.assertEqual(list(context.candidates()), ['monkey', 'muppet']) … … 112 112 113 113 grammar = XMLGrammar(xml, extra_nodes=[ABC]) 114 parser = Parser(grammar, user_context={'echo': self._echo})114 parser = Parser(grammar, data={'echo': self._echo}) 115 115 parser.execute('echo abaabbccc') 116 116 self.assertEqual(self._output, (('abaabbccc',), {})) … … 134 134 135 135 grammar = XMLGrammar(xml) 136 parser = Parser(grammar, user_context={136 parser = Parser(grammar, data={ 137 137 'echo': self._echo, 138 138 'lazy': lazy, … … 155 155 156 156 grammar = XMLGrammar(xml) 157 parser = Parser(grammar, user_context={157 parser = Parser(grammar, data={ 158 158 'echo': self._echo, 159 159 'echo_allowed': True, cly/trunk/.todo
r551 r552 19 19 Figure out why rlext.py dies when the parser is used... 20 20 </note> 21 <note priority="veryhigh" time="1213113286" >21 <note priority="veryhigh" time="1213113286" done="1213465157"> 22 22 Clarify/clean-up how with_context/with_user_context/user_context is used. 23 23 </note>
