Changeset 403

Show
Ignore:
Timestamp:
04/11/07 08:09:49 (2 years ago)
Author:
athomas
Message:

cly:

  • Fixed a bug in the C rlext module. Frankly, I'm surprised this didn't trigger earlier.
  • Node.chosen() is now Node.selected().
  • Fixed bug in variable accumulation, though there is still work to do.
Files:

Legend:

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

    r356 r403  
    191191        return [] 
    192192 
    193     def chosen(self, context, match): 
    194         """ This node was chosen by the parser. """ 
     193    def selected(self, context, match): 
     194        """ This node was selected by the parser. """ 
    195195        context.traverse(self) 
    196196 
     
    199199        for child in self.children(context, follow=True): 
    200200            yield child 
    201          
     201 
    202202    def match(self, context): 
    203203        """ Does this node match the current token? 
    204          
     204 
    205205        Must return a regex match object or None for no match. If 
    206206        `match_candidates` is true the token must also match one of the values 
    207207        returned by `candidates()`. 
    208          
     208 
    209209        Must include separator in determining whether a match was 
    210210        successful. """ 
     
    232232 
    233233    def terminal(self, context): 
    234         """ This node was chosen as a terminal. """ 
     234        """ This node was selected as a terminal. """ 
    235235        raise UnexpectedEOL(context) 
    236236 
    237237    def depth(self): 
    238238        """ The depth of this node in the grammar. 
    239          
     239 
    240240        >>> grammar = Grammar(one=Node('One'), two=Node('Two')) 
    241241        >>> grammar.depth() 
     
    344344    callable. If `with_context` is true, the context object will be passed as 
    345345    the first argument. 
    346      
     346 
    347347    >>> def write_text(): 
    348348    ...     print "some text" 
     
    383383        raise UnexpectedEOL(None)  
    384384 
    385     def chosen(self, context, match): 
     385    def selected(self, context, match): 
    386386        # We don't "traverse" Action nodes, because they are always terminal, 
    387387        # and if we do they get excluded from help. 
     
    390390 
    391391class Validator(Node): 
    392     """ Validate and record the users input in a variable in the context. The 
    393     Node name is used as the variable name. If `accumulate` is True, the 
    394     resultant variable will be a list. """ 
     392    """Validate and record the users input in the `vars` member of the context. 
     393    The Node name is used as the variable name. If `accumulate` is True, the 
     394    resultant variable will be a list.""" 
     395 
    395396    pattern = r'\w+' 
    396397    accumulate = False 
    397398 
    398399    def valid(self, context): 
    399         if not self.accumulate and self.name in context.vars: 
    400             return False 
    401         if self.accumulate and isinstance(self.accumulate, int) and \ 
    402                 len(context.vars.get(self.name, [])) >= self.accumulate: 
    403             return False 
    404         return Node.valid(self, context) 
    405          
    406     def chosen(self, context, match): 
     400        if self.accumulate is True or \ 
     401                (self.accumulate is False and self.name not in context.vars) or \ 
     402                len(context.vars.get(self.name, [])) < self.accumulate: 
     403            return Node.valid(self, context) 
     404        return False 
     405 
     406    def selected(self, context, match): 
    407407        try: 
    408408            value = self.validator(match) 
    409         except Exception, e: 
     409        except InvalidMatch, e: 
    410410            raise ValidationError(context, token=match.group(), 
    411411                                  exception=unicode(e)) 
     
    416416 
    417417    def validator(self, match): 
     418        """Validate the regex match object provided and return the matched 
     419        value. Must throw a ValidationError if the input is invalid. Alternate 
     420        validators should override this method.""" 
    418421        return match.group() 
    419422 
     
    628631            if match is not None: 
    629632                node.advance(context) 
    630             node.chosen(context, match) 
     633            node.selected(context, match) 
    631634 
    632635            for subnode in node.next(context): 
  • cly/trunk/cly/rlext.c

    r337 r403  
    9292        {"force_redisplay", force_redisplay, METH_NOARGS, doc_force_redisplay}, 
    9393        {"cursor", cursor, METH_VARARGS, doc_cursor}, 
     94        {NULL, NULL, 0, NULL}, 
    9495}; 
    9596