| File: | blib/lib/Parse/EBNF.pm |
| Coverage: | 37.3% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | package Parse::EBNF; | |||||
| 2 | ||||||
| 3 | 1 1 1 | 8 3 11 | use strict; | |||
| 4 | 1 1 1 | 12 4 15 | use Parse::EBNF::Rule; | |||
| 5 | ||||||
| 6 | our $VERSION = 1.03; | |||||
| 7 | ||||||
| 8 | sub new { | |||||
| 9 | 1 | 4 | my ($class) = @_; | |||
| 10 | 1 | 5 | my $self = bless {}, $class; | |||
| 11 | 1 | 3 | $self->{verbose} = 0; | |||
| 12 | 1 | 5 | $self->{rules} = {}; | |||
| 13 | 1 | 4 | return $self; | |||
| 14 | } | |||||
| 15 | ||||||
| 16 | sub parse_lines { | |||||
| 17 | 0 | my ($self, $lines) = @_; | ||||
| 18 | ||||||
| 19 | # | |||||
| 20 | # split into rules | |||||
| 21 | # | |||||
| 22 | ||||||
| 23 | 0 | my @rules; | ||||
| 24 | 0 | my $current; | ||||
| 25 | 0 0 | while(my $line = shift @{$lines}){ | ||||
| 26 | 0 | if ($line =~ /^\s*\[\d+\]/){ | ||||
| 27 | 0 | push @rules, $current; | ||||
| 28 | 0 | $current = ''; | ||||
| 29 | } | |||||
| 30 | 0 | $line =~ s![\r\n\t]! !g; | ||||
| 31 | 0 | $line =~ s!\s{2,}! !g; | ||||
| 32 | 0 | $current .= $line; | ||||
| 33 | } | |||||
| 34 | 0 | push @rules, $current; | ||||
| 35 | ||||||
| 36 | ||||||
| 37 | # | |||||
| 38 | # create rules | |||||
| 39 | # | |||||
| 40 | ||||||
| 41 | 0 | for my $rule(@rules){ | ||||
| 42 | 0 | if (defined($rule)){ | ||||
| 43 | 0 | my $rule_obj = Parse::EBNF::Rule->new($rule); | ||||
| 44 | 0 | $self->{rules}->{$rule_obj->{name}} = $rule_obj; | ||||
| 45 | } | |||||
| 46 | } | |||||
| 47 | } | |||||
| 48 | ||||||
| 49 | sub rules { | |||||
| 50 | 0 | my ($self) = @_; | ||||
| 51 | 0 | return $self->{rules}; | ||||
| 52 | } | |||||
| 53 | ||||||
| 54 | sub dump_rule { | |||||
| 55 | 0 | my ($self, $rule) = @_; | ||||
| 56 | ||||||
| 57 | 0 | unless ( defined $self->{rules}->{$rule} ){ | ||||
| 58 | ||||||
| 59 | 0 | print "rule $rule not found\n"; | ||||
| 60 | ||||||
| 61 | }else{ | |||||
| 62 | ||||||
| 63 | 1 1 1 | 13 3 16 | use Data::Dumper; | |||
| 64 | ||||||
| 65 | 0 | my $obj = $self->{rules}->{$rule}; | ||||
| 66 | ||||||
| 67 | 0 | my $d = Data::Dumper->new([$obj]); | ||||
| 68 | ||||||
| 69 | 0 | print $d->Dump(); | ||||
| 70 | } | |||||
| 71 | } | |||||
| 72 | ||||||
| 73 | sub dump_rules { | |||||
| 74 | 0 | my ($self) = @_; | ||||
| 75 | ||||||
| 76 | 1 1 1 | 12 6 11 | use Data::Dumper; | |||
| 77 | ||||||
| 78 | 0 | my $d = Data::Dumper->new([$self->{rules}]); | ||||
| 79 | ||||||
| 80 | 0 | print $d->Dump(); | ||||
| 81 | } | |||||
| 82 | ||||||
| 83 | 1; | |||||
| 84 | ||||||