Changeset 549

Show
Ignore:
Timestamp:
06/10/08 10:13:24 (6 months ago)
Author:
athomas
Message:

Preliminary support for merging grammars. Requested in #58.

Files:

Legend:

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

    r548 r549  
    135135        for this node. See __init__ for more information on attributes. 
    136136 
    137         As a special case, if a positional argument is a `Grammar` object, its 
    138         *children* will be merged. 
    139  
    140137        >>> top = Node(name='top') 
    141138        >>> top(subnode=Node()) 
     
    145142        """ 
    146143        for node in anonymous: 
    147             if isinstance(node, Grammar): 
    148                 children = dict([(n.name, n) for n in node]) 
    149                 self(**children) 
    150                 continue 
    151144            if not isinstance(node, Node): 
    152145                raise InvalidAnonymousNode('"%r" must be a Node object' % node) 
     
    159152        for k, v in options.iteritems(): 
    160153            if isinstance(v, Node): 
    161                 if k.endswith('_'): 
    162                     k = k[:-1] 
     154                k = k.rstrip('_') 
    163155                v.name = k 
    164156                v.parent = self 
     
    399391 
    400392    anonymous = property(_get_anonymous, doc=_get_anonymous.__doc__) 
     393 
     394    def update(self, node): 
     395        """Merge another node into this one. 
     396 
     397        If a merging node collides with an existing one, the existing node 
     398        will be preserved and the merging nodes children merged. 
     399 
     400        :param node: Node to merge into this. 
     401        """ 
     402        self.__anonymous_children += node.__anonymous_children 
     403        for key, child in node._children.iteritems(): 
     404            if key not in self: 
     405                self[key] = child 
     406            else: 
     407                self[key].update(child) 
    401408 
    402409    def __repr__(self):