Changeset 181

Show
Ignore:
Timestamp:
11/10/04 21:51:29 (4 years ago)
Author:
svn
Message:

Added support for wildcard grammar sub-trees. This means that a wildcard will
match down to a terminating ACTION, then start parsing from the wildcard node
again. Very handy.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • manage/trunk

    • Property svn:ignore set to
      .todo.*
  • manage/trunk/CLI.pm

    r180 r181  
    110110# node, or undef if not found. 
    111111# 
    112 # parse_node(\@tokens [, $node, \@args, $trackall]) 
    113 # parse_node($ctx [, $trackall]) 
     112# parse_node(\@tokens [, $node, \@args, $checkpoint]) 
     113# parse_node($ctx [, $checkpoint]) 
    114114sub parse_node { 
    115115my $self = shift; 
    116116my $ctx = shift; 
    117117my $cmd = $ctx->current_grammar(); 
    118 my $trackallparent = @_ ? shift() : 0
     118my $checkpoint = @_ ? shift() : undef
    119119my $token = $ctx->next_token(); 
    120120my ($width, $height) = GetTerminalSize(*STDIN); 
     
    167167                } 
    168168 
     169                # If we have a checkpoint, bring in the help 
     170                $process_help->($checkpoint->{HELP}, \%commands) 
     171                        if defined($checkpoint->{HELP}) and defined($cmd->{ACTION}) and defined($checkpoint); 
     172 
    169173                # If this node can be a terminating node, check for an ACTIONHELP 
    170174                # key. If it exists, use this as the help line for <eol>, otherwise try 
     
    181185                        } 
    182186                } 
    183                 $commands{'<eol>'} = 'Execute command.' if $trackallparent; 
     187 
     188                $commands{'<eol>'} = 'Execute command.' if $checkpoint and not defined($commands{'<eol>'}) and defined($cmd->{ACTION}); 
    184189 
    185190        my $keywidth = 0; 
     
    244249                my $key = $rx; 
    245250                my $trackvar = 0; 
    246                 my $trackall = 0; 
     251                my $newcheckpoint = 0; 
    247252 
    248253                        # Skip if help 
    249254                        next if $rx =~ /^HELP/; 
    250  
    251                         # Find out if this candidate is a unique minimum match 
    252 #               my $unique = 0; 
    253255 
    254256                        # Remove ordering token 
     
    261263                                # Remove wildcard 
    262264                                if ($rx =~ /^\*/) { 
    263                                         $trackall = 1
     265                                        $newcheckpoint = $cmd->{$key}
    264266                                        $rx =~ s/^\*//; 
    265267                                } 
    266268                        } 
    267  
    268 #                       if (defined($token)) { 
    269 #                       my @candidates = grep(/^(\d+\))?(\$(\*)?)?\q$token\e/, keys(%$cmd)); 
    270 # 
    271 #                               $unique = 1 if @candidates == 1 and $candidates[0] eq $key; 
    272 #                       } 
    273269 
    274270                        if ((!defined($token) and $rx eq 'ACTION') or (defined($token) and $token =~ /^(?:$rx)$/is)) { 
     
    309305 
    310306                                # Recurse if we are tracking 
    311                                 return $self->parse_node($ctx->next_grammar($cmd), $trackall
    312                                         if $ctx->more_tokens() and $trackall
     307                                return $self->parse_node($ctx->next_grammar($cmd->{$key}), $newcheckpoint
     308                                        if $ctx->more_tokens() and $newcheckpoint
    313309 
    314310                                if ($type eq 'HASH') { 
    315311                                        $ctx->inject_token($token) if $rx eq 'ACTION'; 
    316                                         return $self->parse_node($ctx->next_grammar($cmd->{$key})); 
     312                                        return $self->parse_node($ctx->next_grammar($cmd->{$key}), $checkpoint); 
    317313                                } elsif ($type eq 'CODE') { 
    318314                                        # Tokens are still remaining 
     
    336332                return ('ERROR', $error, $ctx); 
    337333        } else { 
    338 #               if (defined($token)) { 
    339 #               my @rawcandidates = grep(/^(\d+\))?(\$(\*)?)?\Q$token\E/, keys(%$cmd)); 
    340 
    341 #                       if (@rawcandidates > 1) { 
    342 #                       my @candidates = (); 
    343 
    344 #                               for my $c (@rawcandidates) { 
    345 #                               my $cp = $c; 
    346 
    347 #                                       $cp =~ s/^(\d+\))?(\$(\*)?)?//; 
    348 #                                       push(@candidates, $cp); 
    349 #                               } 
    350 #                               return ('ERROR', "No unique candidates for '$token', candidates are '@candidates'", $ctx) 
    351 #                       } 
    352 #               } 
     334                return $self->parse_node($ctx->next_grammar($checkpoint), $checkpoint) 
     335                        if $checkpoint; 
    353336                return ('ERROR', "Unrecognized command '$token'.", $ctx); 
    354337        } 
  • manage/trunk/plugins/nodelist2.pm

    r174 r181  
    33{ 
    44        test => { 
    5                 '1)lalala' => sub { print "FOO\n"; }, 
    6                 '2)$.*' => { 
    7                         VALIDATE => 1, 
    8                         HELP => sub { 
    9                                 return ( 
    10                                         foo => 1, 
    11                                         bar => 2, 
    12                                         ); 
     5                '$*foo|bar' => { 
     6                        HELP => { 
     7                                foo => "Foo", 
     8                                bar => "Bar", 
    139                        }, 
    14                         ACTION => { 
    15                                 ACTION => sub { 
    16                                 my $c = shift; 
    17  
    18                                         print("@_\n"); 
    19                                 }, 
    20                                 HELP => "Do it.", 
    21                         }, 
    22                 }, 
    23         }, 
    24         user => { 
    25                 HELP => 'NL2 user management.', 
    26                 '1)add' => { 
    27                         HELP => 'Add a new user.', 
    28                         '$[\w-]+' => { 
    29                                 HELP => 'Username to add.', 
    30                                 '$.*' => { 
    31                                         HELP => 'User description.', 
     10                        '$.+' => { 
     11                                HELP => [ "<value>", "Some value" ], 
     12                                ACTION => { 
     13                                        HELP => "Test foo or bar", 
    3214                                        ACTION => sub { 
    3315                                        }, 
     
    3517                        }, 
    3618                }, 
    37                 '2)show' => { 
    38                         ACTION => sub { 
    39                         }, 
    40                         HELP => 'List users.' 
    41                 }, 
    42                 '3)$[\w -]+' => { 
    43                         HELP => [ '<username>', 'User to manage.' ], 
    44                         remove => { 
    45                                 HELP => 'Remove user.', 
    46                                 ACTION => sub { 
    47                                 }, 
    48                         }, 
    49                         lock => { 
    50                                 HELP => 'Lock user.', 
    51                                 ACTION => sub { 
    52                                 }, 
    53                         }, 
    54                         show => { 
    55                                 HELP => 'Show user details, including configuration values.', 
    56                                 ACTION => sub { 
    57                                 }, 
    58                         }, 
    59                         set => { 
    60                                 HELP => 'Set user configuration values.', 
    61                                 '$[\w-]+' => { 
    62                                         HELP => ['<key>', 'User configuration key to set.' ], 
    63                                         '$.*' => { 
    64                                                 HELP => ['<value>', 'Value to set configuration key to.' ], 
    65                                                 ACTION => sub { 
    66                                                 }, 
    67                                         }, 
    68                                 }, 
    69                         }, 
    70                         unset => { 
    71                                 HELP => 'Remove a user configuration key.', 
    72                                 '$[\w-]+' => { 
    73                                         HELP => 'Configuration key to remove.', 
    74                                         ACTION => sub { 
    75                                         }, 
    76                                 }, 
    77                         }, 
    78                         '$\d+' => { 
    79                                 HELP => sub { 
    80                                         return ( 
    81                                                 '<digit>' => 'Fooobbrr' 
    82                                                 ); 
    83                                 }, 
    84                         }, 
    85                 }, 
    8619        }, 
    8720}; 
  • manage/trunk/.todo

    r170 r181  
    1 <!-- Automagically generated by the ToDo program on Thu Jul  1 17:54:52 2004 --> 
    2 <todo version="0.1.17"> 
     1<!-- Automagically generated by the ToDo program on 11/11/04, 13:31 --> 
     2<todo version="0.1.18"> 
    33    <title> 
    44        Manage - command line interface intfrastructure 
    55    </title> 
    6     <note priority="high" time="1088668492"
     6    <note priority="high" time="1088668492" done="1100140286"
    77        Because shortest match is allowed on grammar nodes, we need to expand the matches before execution. eg. The command "a u barry" might match the grammar "add $user barry", but the arg $user is passed as "u" to the final ACTION. Not good. 
     8        <comment> 
     9            No longer supporting short match, too hard. 
     10        </comment> 
    811    </note> 
    912</todo>