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