Eyapp/Options.pm

filter: Statementstmt 87.5% 42 / 48 Branchbran 53.3% 16 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 3 totaltotal 74.1% 66 / 89 CC 16 SCAR 15.4
CC
16
Cov
74%
_SetOption
7.5
__LoadFile
5.7
Option
4.5
CRAP 4.7
« Eyapp/TokenGen.pm ↑ Summary Eyapp/YATW.pm »
1 #
2 # Module Parse::Eyapp::Options
3 #
4 # Copyright © 2006, 2007, 2008, 2009, 2010, 2011, 2012 Casiano Rodriguez-Leon.
5 # Copyright © 2017 William N. Braswell, Jr.
6 # All Rights Reserved.
7 #
8 # Based on Parse::Yapp.
9 #
10 # Parse::Yapp is Copyright © 1998, 1999, 2000, 2001, Francois Desarmenien.
11 # Parse::Yapp is Copyright © 2017 William N. Braswell, Jr.
12 # All Rights Reserved.
13  
14 package Parse::Eyapp::Options;
15  
16 61 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 61 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 ############################################################################
20 #Definitions of options
21 #
22 # %known_options allowed options
23 #
24 # %default_options default
25 #
26 # %actions sub refs to execute if option is set with ($self,$value)
27 # as parameters
28 ############################################################################
29 #
30 #A value of '' means any value can do
31 #
32 61 my(%known_options)= (
33 language => {
34 perl => "Ouput parser for Perl language",
35 # for future use...
36 # 'c++' => "Output parser for C++ language",
37 # c => "Output parser for C language"
38 },
39 linenumbers => {
40 0 => "Don't embbed line numbers in parser",
41 1 => "Embbed source line numbers in parser"
42 },
43 firstline => {
44 '' => "Line number where the input grammar starts"
45 },
46 inputfile => {
47 '' => "Input file name: will automagically fills input"
48 },
49 prefix => {
50 '' => "Accept if a prefix of the input belongs to the language"
51 },
52 classname => {
53 '' => "Class name of parser object (Perl and C++)"
54 },
55 standalone => {
56 0 => "Don't create a standalone parser (Perl and C++)",
57 1 => "Create a standalone parser"
58 },
59 buildingtree => {
60 0 => "Not building AST (for lists)",
61 1 => "Building AST (for lists)"
62 },
63 input => {
64 '' => "Input text of grammar"
65 },
66 template => {
67 '' => "Template text for generating grammar file"
68 },
69 prefixname => {
70 '' => "Prefix for the Tree Classes"
71 },
72 modulino => {
73 '' => "Produce modulino code at the end of the generated module"
74 },
75 start => {
76 '' => "Specify start symbol"
77 },
78 tree => {
79 0 => "don't build AST",
80 1 => "build AST"
81 },
82 nocompact => {
83 0 => "Do not compact action tables. No DEFAULT field for 'STATES'",
84 1 => "Compact action tables"
85 },
86 lexerisdefined => {
87 0 => "Built a lexer",
88 1 => "don't build a lexer"
89 },
90 );
91  
92 61 my(%default_options)= (
93 language => 'perl',
94 firstline => 1,
95 linenumbers => 1,
96 inputfile => undef,
97 classname => 'Parser',
98 standalone => 0,
99 buildingtree => 1,
100 input => undef,
101 template => undef,
102 shebang => undef,
103 prefixname => '',
104 modulino => undef,
105 tree => undef,
106 nocompact => 0,
107 lexerisdefined => 0,
108 );
109  
110 61 my(%actions)= (
111 inputfile => \&__LoadFile
112 );
113  
114 #############################################################################
115 #
116 # Actions
117 #
118 # These are NOT a method, although they look like...
119 #
120 # They are super-private routines (that's why I prepend __ to their names)
121 #
122 #############################################################################
123 54 ▶sub __LoadFile {
Subroutine __LoadFile: called CC=3 SCAR=17.3
124 54 my($self,$filename)=@_;
125  
126 54 ▶ return if defined($self->{OPTIONS}{input});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"OPTIONS"}{"input"} 54 0 54
127  
128 0 ▶ open(IN,"<$filename")
Branch: 1 branch
BranchTrueFalseTotal
open IN, "<$filename" or croak("Cannot open input file '${filename}' for reading") 0 0 0
129 or croak "Cannot open input file '$filename' for reading";
130 0 $self->{OPTIONS}{input}=join('',<IN>);
131 0 close(IN);
132 }
133  
134 #############################################################################
135 #
136 # Private methods
137 #
138 #############################################################################
139  
140 270 ▶sub _SetOption {
Subroutine _SetOption: called CC=7 SCAR=20.2
141 270 my($self)=shift;
142 270 my($key,$value)=@_;
143  
144 270 $key=lc($key);
145  
146 270 ▶ @_ == 2
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or croak("Invalid number of arguments") 270 0 270
147 or croak "Invalid number of arguments";
148  
149 270 ▶ exists($known_options{$key})
Branch: 1 branch
BranchTrueFalseTotal
exists $known_options{$key} or croak("Unknown option: '${key}'") 270 0 270
150 or croak "Unknown option: '$key'";
151  
152 270 ▶ if(exists($known_options{$key}{lc($value)})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $known_options{$key}{lc $value}) { } 54 216 270
elsif (not exists $known_options{$key}{''}) { } 0 216 216
153 54 ▶ $value=lc($value) if defined($value);
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 54 0 54
154 }
155 elsif(not exists($known_options{$key}{''})) {
156 0 croak "Invalid value '$value' for option '$key'";
157 }
158  
159 exists($actions{$key})
160 270 ▶ and &{$actions{$key}}($self,$value);
Branch: 1 branch
BranchTrueFalseTotal
exists $actions{$key} and &{$actions{$key};}($self, $value) 54 216 270
161  
162 270 $self->{OPTIONS}{$key}=$value;
163 }
164  
165 1192 ▶sub _GetOption {
Subroutine _GetOption: called CC=3 SCAR=11.2
166 1192 my($self)=shift;
167 1192 my($key)=map { lc($_) } @_;
168  
169 1192 ▶ @_ == 1
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or croak("Invalid number of arguments") 1192 0 1192
170 or croak "Invalid number of arguments";
171  
172 1192 ▶ exists($known_options{$key})
Branch: 1 branch
BranchTrueFalseTotal
exists $known_options{$key} or croak("Unknown option: '${key}'") 1192 0 1192
173 or croak "Unknown option: '$key'";
174  
175 1192 $self->{OPTIONS}{$key};
176 }
177  
178 #############################################################################
179 #
180 # Public methods
181 #
182 #############################################################################
183  
184 #
185 # Constructor
186 #
187 54 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
188 54 my($class)=shift;
189 54 my($self)={ OPTIONS => { %default_options } };
190  
191 54 ▶ ref($class)
Branch: 1 branch
BranchTrueFalseTotal
ref $class and $class = ref $class 0 54 54
192 and $class=ref($class);
193
194 54 bless($self,$class);
195  
196 54 $self->Options(@_);
197  
198 54 $self;
199 }
200  
201 #
202 # Specify one or more options to set
203 #
204 108 ▶sub Options {
Subroutine Options: called CC=3 SCAR=11.0
Pod undocumented
205 108 my($self)=shift;
206 108 my($key,$value);
207  
208 108 ▶ @_ % 2 == 0
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 or croak("Invalid number of arguments") 108 0 108
209 or croak "Invalid number of arguments";
210  
211 108 ▶ while(($key,$value)=splice(@_,0,2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 270 108 378
212 270 $self->_SetOption($key,$value);
213 }
214 }
215  
216 #
217 # Set (2 parameters) or Get (1 parameter) values for one option
218 #
219 1192 ▶sub Option {
Subroutine Option: called CC=3 SCAR=15.1
Pod undocumented
220 1192 my($self)=shift;
221 1192 my($key,$value)=@_;
222  
223 1192 ▶ @_ == 1
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 and return $self->_GetOption($key) 1192 0 1192
224 and return $self->_GetOption($key);
225  
226 0 ▶ @_ == 2
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 and return $self->_SetOption($key, $value) 0 0 0
227 and return $self->_SetOption($key,$value);
228  
229 0 croak "Invalid number of arguments";
230  
231 }
232  
233 61 1;
234  
235 __END__
« Eyapp/TokenGen.pm ↑ Summary Eyapp/YATW.pm »