Changeset 572

Show
Ignore:
Timestamp:
07/14/08 02:29:32 (2 months ago)
Author:
athomas
Message:

Stop being so tricky with exception handling, as it breaks.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • cly/trunk/cly/builder.py

    r571 r572  
    3838__docformat__ = 'restructuredtext en' 
    3939 
    40  
    4140class Node(object): 
    4241    """The base class for all grammar nodes. 
     
    121120    traversals = 1 
    122121    label = None 
     122    context = None 
    123123 
    124124    def __init__(self, *anonymous, **kwargs): 
     
    11251125            return eval(attr, locals) 
    11261126        except Exception, e: 
    1127             e.args = e.args + ('while parsing "%s"' % attr,) 
    1128             raise 
     1127            raise EvaluationError(error=e, expression=repr(attr)) 
    11291128    return attr_evaluator 
    11301129 
  • cly/trunk/cly/exceptions.py

    r481 r572  
    7979 
    8080 
     81class EvaluationError(Error): 
     82    """An error ocurred while evaluating a Python expression.""" 
     83    message = '$error occurred while parsing $expression' 
     84 
     85 
    8186if __name__ == '__main__': 
    8287    import doctest 
  • cly/trunk/cly/parser.py

    r567 r572  
    330330        context = self.context_factory(self, command, data) 
    331331 
    332         def parse(node, match): 
    333             context.trail.append((node, match)) 
    334             if match is not None: 
    335                 node.advance(context) 
    336             node.selected(context, match) 
    337  
    338             for subnode in node.next(context): 
    339                 if subnode.valid(context): 
    340                     submatch = subnode.match(context) 
    341                     if submatch is not None: 
    342                         return parse(subnode, submatch) 
    343             else: 
    344                 return 
    345             raise InvalidToken(context) 
    346  
    347         parse(self.grammar, None) 
    348         return context 
     332        # Set context on all nodes in the grammar 
     333        for node in self.grammar.walk(): 
     334            node.context = self 
     335 
     336        try: 
     337            def parse(node, match): 
     338                context.trail.append((node, match)) 
     339                if match is not None: 
     340                    node.advance(context) 
     341                node.selected(context, match) 
     342 
     343                for subnode in node.next(context): 
     344                    if subnode.valid(context): 
     345                        submatch = subnode.match(context) 
     346                        if submatch is not None: 
     347                            return parse(subnode, submatch) 
     348                else: 
     349                    return 
     350                raise InvalidToken(context) 
     351 
     352            parse(self.grammar, None) 
     353            return context 
     354        finally: 
     355            # Unset context on grammar 
     356            for node in self.grammar.walk(): 
     357                node.context = None 
    349358 
    350359    def merge(self, where, grammar):