| 1 | package ABNF::Validator; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | ABNF::Validator - class to verify strings based on ABNF-grammars | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | =head1 METHODS | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | use 5.014; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use re 'eval'; | ||
| 20 | |||
| 21 | use Carp; | ||
| 22 | use Readonly; | ||
| 23 | use Method::Signatures; | ||
| 24 | use Data::Dumper; | ||
| 25 | |||
| 26 | use Parse::ABNF; | ||
| 27 | |||
| 28 | use ABNF::Grammar qw(splitRule $BASIC_RULES); | ||
| 29 | |||
| 30 | use base qw(Exporter); | ||
| 31 | |||
| 32 | our @EXPORT_OK = qw(Validator); | ||
| 33 | |||
| 34 | Readonly my $ARGUMENTS_RULES => "generic_arguments_rule_for_"; | ||
| 35 | |||
| 36 | Readonly my $CLASS_MAP => { | ||
| 37 | Choice => \&_choice, | ||
| 38 | Group => \&_group, | ||
| 39 | Range => \&_range, | ||
| 40 | Reference => \&_reference, | ||
| 41 | Repetition => \&_repetition, | ||
| 42 | Rule => \&_rule, | ||
| 43 | String => \&_string, | ||
| 44 | Literal => \&_literal, | ||
| 45 | ProseValue => \&_proseValue | ||
| 46 | }; | ||
| 47 | |||
| 48 | =pod | ||
| 49 | |||
| 50 | =head1 ABNF::Validator->C<new>($grammar) | ||
| 51 | |||
| 52 | Creates a new B<ABNF::Validator> object. | ||
| 53 | |||
| 54 | $grammar isa B<ABNF::Grammar>. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | method new(ABNF::Grammar $grammar) { | ||
| 59 | |||
| 60 | my $class = ref($self) || $self; | ||
| 61 | |||
| 62 | $self = { _grammar => $grammar }; | ||
| 63 | |||
| 64 | bless($self, $class); | ||
| 65 | |||
| 66 | $self->_init(); | ||
| 67 | |||
| 68 | return $self; | ||
| 69 | } | ||
| 70 | |||
| 71 | method _init() { | ||
| 72 | my $commands = $self->{_grammar}->commands(); | ||
| 73 | $self->{_commandsPattern} = do { | ||
| 74 | my $pattern = join(" | ", @$commands); | ||
| 75 | qr/\A (?: $pattern ) \Z/ix; | ||
| 76 | }; | ||
| 77 | |||
| 78 | $self->{_rules} = _value([ | ||
| 79 | values($self->{_grammar}->rules()), | ||
| 80 | values($BASIC_RULES) | ||
| 81 | ]); | ||
| 82 | |||
| 83 | $self->{_regexps} = do { | ||
| 84 | use Regexp::Grammars; | ||
| 85 | |||
| 86 | my %res = (); | ||
| 87 | foreach my $token ( @$commands ) { | ||
| 88 | # command | ||
| 89 | my $str = " | ||
| 90 | #<logfile: /dev/null> | ||
| 91 | |||
| 92 | ^ <" . _fixRulename($token) . "> \$ | ||
| 93 | |||
| 94 | $self->{_rules} | ||
| 95 | "; | ||
| 96 | $res{$token} = qr{$str }ixs; | ||
| 97 | |||
| 98 | # arguments | ||
| 99 | my $value = $self->{_grammar}->rule($token); | ||
| 100 | my $name = _fixRulename($ARGUMENTS_RULES . $token); | ||
| 101 | my $rule = {class => "Rule", name => $name}; | ||
| 102 | my $val = (splitRule($value))[-1]; | ||
| 103 | |||
| 104 | if ( $value->{value} != $val ) { | ||
| 105 | $rule->{value} = $val; | ||
| 106 | my $converted = _value($rule); | ||
| 107 | $res{$name} = qr{ | ||
| 108 | ^ <$name> $ | ||
| 109 | |||
| 110 | $converted | ||
| 111 | |||
| 112 | $self->{_rules} | ||
| 113 | }xis; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | \%res; | ||
| 118 | }; | ||
| 119 | } | ||
| 120 | |||
| 121 | func _value($val, $dent = 0) { | ||
| 122 | |||
| 123 | if ( UNIVERSAL::isa($val, 'ARRAY') ) { | ||
| 124 | return join('', map { _value($_ , $dent) } @$val); | ||
| 125 | } elsif ( UNIVERSAL::isa($val, 'HASH') && exists($CLASS_MAP->{ $val->{class} }) ) { | ||
| 126 | return $CLASS_MAP->{ $val->{class} }->($val, $dent); | ||
| 127 | } else { | ||
| 128 | croak "Unknown substance " . Dumper($val); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | func _choice($val, $dent) { | ||
| 134 | return "(?: " . join(' | ', map { _value($_ , $dent + 1) } @{$val->{value}}) . ")"; | ||
| 135 | } | ||
| 136 | |||
| 137 | func _group($val, $dent) { | ||
| 138 | return '(?: ' . _value($val->{value}, $dent + 1) . ' )'; | ||
| 139 | } | ||
| 140 | |||
| 141 | func _reference($val, $dent) { | ||
| 142 | return "<" . _fixRulename($val->{name}) . ">"; | ||
| 143 | } | ||
| 144 | |||
| 145 | func _repetition($val, $dent) { | ||
| 146 | |||
| 147 | no warnings 'uninitialized'; | ||
| 148 | my %maxMin = ( | ||
| 149 | # max min | ||
| 150 | "1 0" => '?', | ||
| 151 | " 0" => '*', | ||
| 152 | " 1" => '+', | ||
| 153 | ); | ||
| 154 | |||
| 155 | if ( my $mm = $maxMin{"$val->{max} $val->{min}"} ) { | ||
| 156 | return " (?: " . _value($val->{value}, $dent + 1) . " )$mm "; | ||
| 157 | } elsif( $val->{min} == $val->{max} ){ | ||
| 158 | return " (?: ". _value($val->{value}, $dent + 1) . " ){$val->{max}} "; | ||
| 159 | } else { | ||
| 160 | return " (?: " . _value($val->{value}, $dent+1) . " ){$val->{min}, $val->{max}} "; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | func _rule($val, $dent) { | ||
| 165 | my $ret = ""; | ||
| 166 | my $name = $val->{name}; | ||
| 167 | |||
| 168 | if ( 'ws' eq lc($name) ) { | ||
| 169 | warn "Changing rule ws to token to avoid 'infinitely recursive unpleasantness.'\n"; | ||
| 170 | $ret .= "<rule: ws>\n "; # may be token | ||
| 171 | } else { | ||
| 172 | $ret .= "<token: " . _fixRulename($val->{name}) . ">\n "; | ||
| 173 | } | ||
| 174 | $ret .= _value($val->{value}, $dent + 1); | ||
| 175 | $ret . "\n\n"; | ||
| 176 | } | ||
| 177 | |||
| 178 | #~ @{[_fixRulename($$v{name})]} | ||
| 179 | func _fixRulename($name) { | ||
| 180 | $name =~ s/[-\W]/_/g; | ||
| 181 | $name; | ||
| 182 | } | ||
| 183 | |||
| 184 | func _range($val, $dent) { | ||
| 185 | my $ret = ""; | ||
| 186 | $ret .= '['; | ||
| 187 | given ( $val->{type} ) { | ||
| 188 | when ( 'hex' ) { | ||
| 189 | $ret .= join('-', map { '\x{' . $_ . '}' } $val->{min}, $val->{max}); | ||
| 190 | } | ||
| 191 | when ( 'binary' ) { | ||
| 192 | $ret .= join('-', map { sprintf('\\%o', oct("0b$_")) } $val->{min}, $val->{max}); | ||
| 193 | } | ||
| 194 | when ( 'decimal' ) { | ||
| 195 | $ret .= join('-', map { sprintf('\\%o', $_) } $val->{min}, $val->{max}); | ||
| 196 | } | ||
| 197 | default { | ||
| 198 | croak "## Range type $val->{type} $val->{value} \n"; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | $ret .= "]"; | ||
| 202 | $ret; | ||
| 203 | } | ||
| 204 | |||
| 205 | func _string($val, $dent) { | ||
| 206 | my $ret = ""; | ||
| 207 | given ( $val->{type} ) { | ||
| 208 | when ( 'hex' ) { | ||
| 209 | $ret = join('', map { '\x' . $_ } @{$val->{value}}); | ||
| 210 | } | ||
| 211 | when ( 'binary' ) { | ||
| 212 | $ret .= join('', map { sprintf('\\%o', oct("0b$_")) } @{$val->{value}}); | ||
| 213 | } | ||
| 214 | when ( 'decimal' ) { | ||
| 215 | $ret .= join('', map { sprintf('\\%o', $_) } @{$val->{value}}); | ||
| 216 | } | ||
| 217 | default { | ||
| 218 | die "## String type $val->{type} $val->{value} \n"; | ||
| 219 | } | ||
| 220 | #~ warn "##", map({ "$_ ( $val->{$_} ) " } sort keys %$val ), "\n"; | ||
| 221 | } | ||
| 222 | #~ " $ret "; | ||
| 223 | $ret; | ||
| 224 | } | ||
| 225 | |||
| 226 | func _literal($val, $dent) { | ||
| 227 | return quotemeta($val->{value}); | ||
| 228 | } | ||
| 229 | |||
| 230 | func _proseValue($val, $dent) { | ||
| 231 | return "<" . _fixRulename($val->{value}) . ">"; | ||
| 232 | } | ||
| 233 | |||
| 234 | =pod | ||
| 235 | |||
| 236 | =head1 $validator->C<validate>($rule, $string) | ||
| 237 | |||
| 238 | Return 1 if $string matches $rule and 0 otherwise. | ||
| 239 | |||
| 240 | $rule is rulename. | ||
| 241 | |||
| 242 | $string is arguments string. | ||
| 243 | |||
| 244 | dies if there is no command like $rule. | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | method validate(Str $rule, Str $string) { | ||
| 249 | croak "Unexisted command $rule" unless exists($self->{_regexps}->{$rule}); | ||
| 250 | scalar($string =~ $self->{_regexps}->{$rule}); | ||
| 251 | } | ||
| 252 | |||
| 253 | =pod | ||
| 254 | |||
| 255 | =head1 $validator->C<validateArguments>($rule, $string) | ||
| 256 | |||
| 257 | Return 1 if $string matches arguments rules form $rule and 0 otherwise. | ||
| 258 | |||
| 259 | $rule is rulename. | ||
| 260 | |||
| 261 | $string is arguments string. | ||
| 262 | |||
| 263 | dies if there is no command like $rule. | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | |||
| 268 | method validateArguments($rule, $string) { | ||
| 269 | croak "Unexisted command $rule" unless exists($self->{_regexps}->{$rule}); | ||
| 270 | my $args = _fixRulename($ARGUMENTS_RULES . $rule); | ||
| 271 | scalar(exists($self->{_regexps}->{$args}) && ($string =~ $self->{_regexps}->{$args})); | ||
| 272 | } | ||
| 273 | |||
| 274 | =pod | ||
| 275 | |||
| 276 | =head1 $validator->C<validateCommand>($command) | ||
| 277 | |||
| 278 | Return 1 if there exists command like $command and 0 otherwise | ||
| 279 | |||
| 280 | =cut | ||
| 281 | |||
| 282 | method validateCommand($command) { | ||
| 283 | return $command =~ $self->{_commandsPattern}; | ||
| 284 | } | ||
| 285 | |||
| 286 | =pod | ||
| 287 | |||
| 288 | =head1 $validator->C<hasCommand>($command) | ||
| 289 | |||
| 290 | Return 1 if there exists command like $command and 0 otherwise | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | method hasCommand($command) { | ||
| 295 | return exists($self->{_regexps}->{$command}); | ||
| 296 | } | ||
| 297 | |||
| 298 | =pod | ||
| 299 | |||
| 300 | =head1 FUNCTIONS | ||
| 301 | |||
| 302 | =head1 C<Validator>() | ||
| 303 | |||
| 304 | Return __PACKAGE__ to reduce class name :3 | ||
| 305 | |||
| 306 | =cut | ||
| 307 | |||
| 308 | func Validator() { | ||
| 309 | return __PACKAGE__; | ||
| 310 | } | ||
| 311 | |||
| 312 | 1; | ||
| 313 | |||
| 314 | =pod | ||
| 315 | |||
| 316 | =head1 AUTHOR / COPYRIGHT / LICENSE | ||
| 317 | |||
| 318 | Copyright (c) 2013 Arseny Krasikov <nyaapa@cpan.org>. | ||
| 319 | |||
| 320 | This module is licensed under the same terms as Perl itself. | ||
| 321 | |||
| 322 | =cut |