deprecated/buildtools/buildsystemtools/lib/Parse/Yapp/Options.pm
author kelvzhu
Wed, 27 Oct 2010 16:03:51 +0800
changeset 662 60be34e1b006
parent 655 3f65fd25dfd4
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     1
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     2
# Module Parse::Yapp::Options
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     3
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     4
# (c) Copyright 1999-2001 Francois Desarmenien, all rights reserved.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     5
# (see the pod text in Parse::Yapp module for use and distribution rights)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     6
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     7
package Parse::Yapp::Options;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     8
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     9
use strict;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    10
use Carp;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    11
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    12
############################################################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    13
#Definitions of options
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    14
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    15
# %known_options    allowed options
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    16
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    17
# %default_options  default
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    18
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    19
# %actions          sub refs to execute if option is set with ($self,$value)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    20
#                   as parameters
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    21
############################################################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    22
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    23
#A value of '' means any value can do
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    24
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    25
my(%known_options)= (
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    26
    language    =>  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    27
        perl    => "Ouput parser for Perl language",
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    28
# for future use...
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    29
#       'c++'   =>  "Output parser for C++ language",
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    30
#       c       =>  "Output parser for C language"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    31
    },
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    32
    linenumbers =>  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    33
        0       =>  "Don't embbed line numbers in parser",
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    34
        1       =>  "Embbed source line numbers in parser"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    35
    },
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    36
    inputfile   =>  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    37
        ''      =>  "Input file name: will automagically fills input"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    38
    },
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    39
    classname   =>  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    40
        ''      =>  "Class name of parser object (Perl and C++)"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    41
    },
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    42
    standalone  =>  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    43
        0       =>  "Don't create a standalone parser (Perl and C++)",
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    44
        1       =>  "Create a standalone parser"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    45
    },
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    46
    input       =>  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    47
        ''      =>  "Input text of grammar"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    48
    },
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    49
    template    => {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    50
        ''      =>  "Template text for generating grammar file"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    51
    },
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    52
);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    53
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    54
my(%default_options)= (
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    55
    language => 'perl',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    56
    linenumbers => 1,
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    57
    inputfile => undef,
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    58
    classname   => 'Parser',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    59
    standalone => 0,
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    60
    input => undef,
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    61
    template => undef,
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    62
    shebang => undef,
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    63
);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    64
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    65
my(%actions)= (
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    66
    inputfile => \&__LoadFile
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    67
);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    68
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    69
#############################################################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    70
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    71
# Actions
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    72
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    73
# These are NOT a method, although they look like...
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    74
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    75
# They are super-private routines (that's why I prepend __ to their names)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    76
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    77
#############################################################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    78
sub __LoadFile {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    79
    my($self,$filename)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    80
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    81
        open(IN,"<$filename")
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    82
    or  croak "Cannot open input file '$filename' for reading";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    83
    $self->{OPTIONS}{input}=join('',<IN>);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    84
    close(IN);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    85
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    86
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    87
#############################################################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    88
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    89
# Private methods
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    90
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    91
#############################################################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    92
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    93
sub _SetOption {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    94
    my($self)=shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    95
    my($key,$value)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    96
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    97
    $key=lc($key);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    98
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    99
        @_ == 2
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   100
    or  croak "Invalid number of arguments";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   101
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   102
        exists($known_options{$key})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   103
    or  croak "Unknown option: '$key'";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   104
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   105
    if(exists($known_options{$key}{lc($value)})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   106
        $value=lc($value);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   107
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   108
    elsif(not exists($known_options{$key}{''})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   109
        croak "Invalid value '$value' for option '$key'";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   110
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   111
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   112
        exists($actions{$key})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   113
    and &{$actions{$key}}($self,$value);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   114
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   115
    $self->{OPTIONS}{$key}=$value;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   116
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   117
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   118
sub _GetOption {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   119
    my($self)=shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   120
    my($key)=map { lc($_) } @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   121
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   122
        @_ == 1
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   123
    or  croak "Invalid number of arguments";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   124
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   125
        exists($known_options{$key})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   126
    or  croak "Unknown option: '$key'";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   127
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   128
    $self->{OPTIONS}{$key};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   129
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   130
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   131
#############################################################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   132
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   133
# Public methods
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   134
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   135
#############################################################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   136
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   137
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   138
# Constructor
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   139
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   140
sub new {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   141
    my($class)=shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   142
    my($self)={ OPTIONS => { %default_options } };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   143
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   144
        ref($class)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   145
    and $class=ref($class);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   146
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   147
    bless($self,$class);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   148
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   149
    $self->Options(@_);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   150
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   151
    $self;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   152
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   153
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   154
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   155
# Specify one or more options to set
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   156
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   157
sub Options {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   158
    my($self)=shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   159
    my($key,$value);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   160
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   161
        @_ % 2 == 0
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   162
    or  croak "Invalid number of arguments";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   163
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   164
    while(($key,$value)=splice(@_,0,2)) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   165
        $self->_SetOption($key,$value);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   166
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   167
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   168
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   169
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   170
# Set (2 parameters) or Get (1 parameter) values for one option
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   171
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   172
sub Option {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   173
    my($self)=shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   174
    my($key,$value)=@_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   175
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   176
        @_ == 1
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   177
    and return $self->_GetOption($key);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   178
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   179
        @_ == 2
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   180
    and return $self->_SetOption($key,$value);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   181
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   182
    croak "Invalid number of arguments";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   183
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   184
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   185
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   186
1;